MySensors Library & Examples  2.3.2-36-g2e00bf6
MySensorsCore.h
Go to the documentation of this file.
1 /*
2  * The MySensors Arduino library handles the wireless radio link and protocol
3  * between your home built sensors/actuators and HA controller of choice.
4  * The sensors forms a self healing radio network with optional repeaters. Each
5  * repeater and gateway builds a routing tables in EEPROM which keeps track of the
6  * network topology allowing messages to be routed to nodes.
7  *
8  * Created by Henrik Ekblad <[email protected]>
9  * Copyright (C) 2013-2020 Sensnology AB
10  * Full contributor list: https://github.com/mysensors/MySensors/graphs/contributors
11  *
12  * Documentation: http://www.mysensors.org
13  * Support Forum: http://forum.mysensors.org
14  *
15  * This program is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU General Public License
17  * version 2 as published by the Free Software Foundation.
18  */
19 
67 #ifndef MySensorsCore_h
68 #define MySensorsCore_h
69 
70 #include "Version.h"
71 #include "MyConfig.h"
72 #include "MyEepromAddresses.h"
73 #include "MyMessage.h"
74 #include <stddef.h>
75 #include <stdarg.h>
76 
77 #define GATEWAY_ADDRESS ((uint8_t)0)
78 #define NODE_SENSOR_ID ((uint8_t)255)
79 #define MY_CORE_VERSION ((uint8_t)2)
80 #define MY_CORE_MIN_VERSION ((uint8_t)2)
81 
82 #define MY_WAKE_UP_BY_TIMER ((int8_t)-1)
83 #define MY_SLEEP_NOT_POSSIBLE ((int8_t)-2)
84 #define INTERRUPT_NOT_DEFINED ((uint8_t)255)
85 #define MODE_NOT_DEFINED ((uint8_t)255)
86 #define VALUE_NOT_DEFINED ((uint8_t)255)
87 #define FUNCTION_NOT_SUPPORTED ((uint16_t)0)
88 
89 
94 typedef struct {
95  uint8_t isMetric;
97 
101 typedef struct {
103  // 8 bit
104  bool nodeRegistered : 1;
105  bool presentationSent : 1;
106  uint8_t reserved : 6;
107 } coreConfig_t;
108 
109 
110 // **** public functions ********
111 
115 uint8_t getNodeId(void);
116 
120 uint8_t getParentNodeId(void);
121 
125 void presentNode(void);
126 
140 bool present(const uint8_t sensorId, const mysensors_sensor_t sensorType,
141  const char *description = "",
142  const bool requestEcho = false);
143 #if !defined(__linux__)
144 bool present(const uint8_t childSensorId, const mysensors_sensor_t sensorType,
145  const __FlashStringHelper *description,
146  const bool requestEcho = false);
147 #endif
148 
158 bool sendSketchInfo(const char *name, const char *version, const bool requestEcho = false);
159 #if !defined(__linux__)
160 bool sendSketchInfo(const __FlashStringHelper *name, const __FlashStringHelper *version,
161  const bool requestEcho = false);
162 #endif
163 
173 bool send(MyMessage &msg, const bool requestEcho = false);
174 
184 bool sendBatteryLevel(const uint8_t level, const bool requestEcho = false);
185 
195 bool sendHeartbeat(const bool requestEcho = false);
196 
206 bool sendSignalStrength(const int16_t level, const bool requestEcho = false);
207 
217 bool sendTXPowerLevel(const uint8_t level, const bool requestEcho = false);
218 
228 bool request(const uint8_t childSensorId, const uint8_t variableType,
229  const uint8_t destination = GATEWAY_ADDRESS);
230 
239 bool requestTime(const bool requestEcho = false);
240 
245 
256 void saveState(const uint8_t pos, const uint8_t value);
257 
264 uint8_t loadState(const uint8_t pos);
265 
273 void wait(const uint32_t waitingMS);
274 
284 bool wait(const uint32_t waitingMS, const mysensors_command_t cmd);
285 
296 bool wait(const uint32_t waitingMS, const mysensors_command_t cmd, const uint8_t msgtype);
297 
302 void doYield(void);
303 
309 void sleepHandler(bool sleep);
310 
317 int8_t sleep(const uint32_t sleepingMS, const bool smartSleep = false);
318 
329 int8_t sleep(const uint8_t interrupt, const uint8_t mode, const uint32_t sleepingMS = 0,
330  const bool smartSleep = false);
331 
344 int8_t sleep(const uint8_t interrupt1, const uint8_t mode1, const uint8_t interrupt2,
345  const uint8_t mode2, const uint32_t sleepingMS = 0, const bool smartSleep = false);
346 
354 int8_t smartSleep(const uint32_t sleepingMS);
355 
365 int8_t smartSleep(const uint8_t interrupt, const uint8_t mode, const uint32_t sleepingMS = 0);
366 
378 int8_t smartSleep(const uint8_t interrupt1, const uint8_t mode1, const uint8_t interrupt2,
379  const uint8_t mode2, const uint32_t sleepingMS = 0);
380 
393 int8_t _sleep(const uint32_t sleepingMS, const bool smartSleep = false,
394  const uint8_t interrupt1 = INTERRUPT_NOT_DEFINED, const uint8_t mode1 = MODE_NOT_DEFINED,
395  const uint8_t interrupt2 = INTERRUPT_NOT_DEFINED, const uint8_t mode2 = MODE_NOT_DEFINED);
396 
402 uint32_t getSleepRemaining(void);
403 
404 
405 // **** private functions ********
406 
422 void _nodeLock(const char *str);
423 
427 void _checkNodeLock(void); // Node lock group
429 
433 void _begin(void);
437 void _process(void);
442 bool _processInternalCoreMessage(void);
446 void _infiniteLoop(void);
450 void _registerNode(void);
456 bool _sendRoute(MyMessage &message);
460 void receive(const MyMessage&) __attribute__((weak));
464 void receiveTime(uint32_t) __attribute__((weak));
468 void presentation(void) __attribute__((weak));
472 void before(void) __attribute__((weak));
476 void preHwInit(void) __attribute__((weak));
480 void setup(void) __attribute__((weak));
484 void loop(void) __attribute__((weak));
485 
486 
487 // Inline function and macros
488 static inline MyMessage& build(MyMessage &msg, const uint8_t destination, const uint8_t sensor,
489  const mysensors_command_t command, const uint8_t type, const bool requestEcho = false)
490 {
491  msg.setSender(getNodeId());
493  msg.setSensor(sensor);
494  msg.setType(type);
495  msg.setCommand(command);
496  msg.setRequestEcho(requestEcho);
497  msg.setEcho(false);
498  return msg;
499 }
500 
501 static inline MyMessage& buildGw(MyMessage &msg, const uint8_t type)
502 {
506  msg.setType(type);
507  msg.setCommand(C_INTERNAL);
508  msg.setRequestEcho(false);
509  msg.setEcho(false);
510  return msg;
511 }
512 
513 #endif
514 
coreConfig_t::controllerConfig
controllerConfig_t controllerConfig
Controller config.
Definition: MySensorsCore.h:102
coreConfig_t::nodeRegistered
bool nodeRegistered
Flag node registered.
Definition: MySensorsCore.h:104
type
uint8_t type
8 bit - Type varies depending on command
Definition: MyMessage.h:353
MyConfig.h
MySensors specific configuration flags.Set these in your sketch before including MySensors....
MyMessage::setSensor
MyMessage & setSensor(const uint8_t sensorId)
Set which child sensor this message belongs to.
sleepHandler
void sleepHandler(bool sleep)
sendSketchInfo
bool sendSketchInfo(const char *name, const char *version, const bool requestEcho=false)
receiveTime
void receiveTime(uint32_t) __attribute__((weak))
Callback for incoming time messages.
Definition: MockMySensors.ino:745
_sendRoute
bool _sendRoute(MyMessage &message)
Sends message according to routing table.
NODE_SENSOR_ID
#define NODE_SENSOR_ID
Node child is always created/presented when a node is started.
Definition: MySensorsCore.h:78
_checkNodeLock
void _checkNodeLock(void)
Check node lock status and prevent node execution if locked.
sensor
uint8_t sensor
8 bit - Id of sensor that this message concerns.
Definition: MyMessage.h:354
sendTXPowerLevel
bool sendTXPowerLevel(const uint8_t level, const bool requestEcho=false)
receive
void receive(const MyMessage &) __attribute__((weak))
Callback for incoming messages.
Definition: DimmableLEDActuator.ino:87
getSleepRemaining
uint32_t getSleepRemaining(void)
C_INTERNAL
@ C_INTERNAL
Internal MySensors messages (also include common messages provided/generated by the library).
Definition: MyMessage.h:71
loop
void loop(void) __attribute__((weak))
Main loop.
Definition: Echo.ino:28
_sleep
int8_t _sleep(const uint32_t sleepingMS, const bool smartSleep=false, const uint8_t interrupt1=INTERRUPT_NOT_DEFINED, const uint8_t mode1=MODE_NOT_DEFINED, const uint8_t interrupt2=INTERRUPT_NOT_DEFINED, const uint8_t mode2=MODE_NOT_DEFINED)
MyMessage::setSender
MyMessage & setSender(const uint8_t senderId)
Set sender ID.
controllerConfig_t
Controller configuration.
Definition: MySensorsCore.h:94
coreConfig_t
Node core configuration.
Definition: MySensorsCore.h:101
coreConfig_t::presentationSent
bool presentationSent
Flag presentation sent.
Definition: MySensorsCore.h:105
_begin
void _begin(void)
Node initialisation.
presentNode
void presentNode(void)
sendHeartbeat
bool sendHeartbeat(const bool requestEcho=false)
loadState
uint8_t loadState(const uint8_t pos)
doYield
void doYield(void)
destination
uint8_t destination
8 bit - Id of destination node
Definition: MyMessage.h:336
mysensors_command_t
mysensors_command_t
The command field (message-type) defines the overall properties of a message.
Definition: MyMessage.h:67
Version.h
MyMessage::setType
MyMessage & setType(const uint8_t messageType)
Set message type.
presentation
void presentation(void) __attribute__((weak))
Node presentation.
Definition: AirQualitySensor.ino:94
getControllerConfig
controllerConfig_t getControllerConfig(void)
saveState
void saveState(const uint8_t pos, const uint8_t value)
__attribute__
struct @4::@5 __attribute__
Doxygen will complain without this comment.
Definition: DigitalPin.h:65
send
bool send(MyMessage &msg, const bool requestEcho=false)
before
void before(void) __attribute__((weak))
Called before node initialises.
Definition: RelayActuator.ino:51
request
bool request(const uint8_t childSensorId, const uint8_t variableType, const uint8_t destination=GATEWAY_ADDRESS)
_infiniteLoop
void _infiniteLoop(void)
Puts node to a infinite loop if unrecoverable situation detected.
present
bool present(const uint8_t sensorId, const mysensors_sensor_t sensorType, const char *description="", const bool requestEcho=false)
getNodeId
uint8_t getNodeId(void)
MyMessage::setEcho
MyMessage & setEcho(const bool echo)
Setter for echo-flag.
getParentNodeId
uint8_t getParentNodeId(void)
setup
void setup(void) __attribute__((weak))
Called after node initialises but before main loop.
Definition: Echo.ino:20
MyMessage::setDestination
MyMessage & setDestination(const uint8_t destinationId)
Set final destination node id for this message.
requestTime
bool requestTime(const bool requestEcho=false)
MyMessage.h
API and type declarations for MySensors messages.
wait
void wait(const uint32_t waitingMS)
sleep
int8_t sleep(const uint32_t sleepingMS, const bool smartSleep=false)
_registerNode
void _registerNode(void)
Handles registration request.
_nodeLock
void _nodeLock(const char *str)
Lock a node and transmit provided message with 30m intervals.
MyEepromAddresses.h
Eeprom addresses for MySensors library data.
MyMessage::setCommand
MyMessage & setCommand(const mysensors_command_t command)
Setter for command type.
sendBatteryLevel
bool sendBatteryLevel(const uint8_t level, const bool requestEcho=false)
_processInternalCoreMessage
bool _processInternalCoreMessage(void)
Processes internal core message.
MyMessage::setRequestEcho
MyMessage & setRequestEcho(const bool requestEcho)
Setter for echo request.
MODE_NOT_DEFINED
#define MODE_NOT_DEFINED
_sleep() param: no mode defined
Definition: MySensorsCore.h:85
sendSignalStrength
bool sendSignalStrength(const int16_t level, const bool requestEcho=false)
INTERRUPT_NOT_DEFINED
#define INTERRUPT_NOT_DEFINED
_sleep() param: no interrupt defined
Definition: MySensorsCore.h:84
smartSleep
int8_t smartSleep(const uint32_t sleepingMS)
MyMessage
MyMessage is used to create, manipulate, send and read MySensors messages.
Definition: MyMessage.h:289
GATEWAY_ADDRESS
#define GATEWAY_ADDRESS
Node ID for GW sketch.
Definition: MySensorsCore.h:77
controllerConfig_t::isMetric
uint8_t isMetric
Flag indicating if metric or imperial measurements are used.
Definition: MySensorsCore.h:95
coreConfig_t::reserved
uint8_t reserved
reserved
Definition: MySensorsCore.h:106
_process
void _process(void)
Main framework process.
preHwInit
void preHwInit(void) __attribute__((weak))
Called before any hardware initialisation is done.
Definition: SensebenderGatewaySerial.ino:114