MySensors Library & Examples  2.3.2-62-ge298769
MySensors.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-2023 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 
35 #ifndef MySensors_h
36 #define MySensors_h
37 
38 #ifdef __cplusplus
39 #include <Arduino.h>
40 #endif
41 #include <stdint.h>
42 
43 #include "MyConfig.h"
44 #include "core/MyHelperFunctions.cpp"
45 
46 #include "core/MySplashScreen.h"
47 #include "core/MySensorsCore.h"
48 
49 // OTA Debug, has to be defined before HAL
50 #if defined(MY_OTA_LOG_SENDER_FEATURE) || defined(MY_OTA_LOG_RECEIVER_FEATURE)
51 #include "core/MyOTALogging.h"
52 #endif
53 
54 // HARDWARE
56 #include "hal/crypto/MyCryptoHAL.h"
57 #if defined(ARDUINO_ARCH_ESP8266)
58 #include "hal/architecture/ESP8266/MyHwESP8266.cpp"
59 #include "hal/crypto/generic/MyCryptoGeneric.cpp"
60 #elif defined(ARDUINO_ARCH_ESP32)
61 #include "hal/architecture/ESP32/MyHwESP32.cpp"
62 #include "hal/crypto/ESP32/MyCryptoESP32.cpp"
63 #elif defined(ARDUINO_ARCH_AVR)
64 #include "hal/architecture/AVR/MyHwAVR.cpp"
65 #include "hal/crypto/AVR/MyCryptoAVR.cpp"
66 #elif defined(ARDUINO_ARCH_MEGAAVR)
67 #include "hal/architecture/AVR/MyHwMegaAVR.cpp"
68 #include "hal/crypto/AVR/MyCryptoAVR.cpp"
69 #elif defined(ARDUINO_ARCH_SAMD)
70 #include "drivers/extEEPROM/extEEPROM.cpp"
71 #include "hal/architecture/SAMD/MyHwSAMD.cpp"
72 #include "hal/crypto/generic/MyCryptoGeneric.cpp"
73 #elif defined(ARDUINO_ARCH_STM32F1)
74 #include "hal/architecture/STM32F1/MyHwSTM32F1.cpp"
75 #include "hal/crypto/generic/MyCryptoGeneric.cpp"
76 #elif defined(ARDUINO_ARCH_NRF5) || defined(ARDUINO_ARCH_NRF52)
77 #include "hal/architecture/NRF5/MyHwNRF5.cpp"
78 #include "hal/crypto/generic/MyCryptoGeneric.cpp"
79 #elif defined(__arm__) && defined(TEENSYDUINO)
80 #include "hal/architecture/Teensy3/MyHwTeensy3.cpp"
81 #include "hal/crypto/generic/MyCryptoGeneric.cpp"
82 #elif defined(__ASR6501__) || defined (__ASR6502__)
83 #include "hal/architecture/ASR650x/MyHwASR650x.cpp"
84 #include "hal/crypto/generic/MyCryptoGeneric.cpp"
85 #elif defined(__linux__)
86 #include "hal/architecture/Linux/MyHwLinuxGeneric.cpp"
87 #include "hal/crypto/generic/MyCryptoGeneric.cpp"
88 #else
89 #error Hardware abstraction not defined (unsupported platform)
90 #endif
91 
92 #include "hal/architecture/MyHwHAL.cpp"
93 
94 // commonly used macros, sometimes missing in arch definitions
95 #if !defined(_BV)
96 #define _BV(x) (1<<(x))
97 #endif
98 
99 #if !defined(min) && !defined(__linux__)
100 #define min(a,b) ((a)<(b)?(a):(b))
101 #endif
102 
103 #if !defined(max) && !defined(__linux__)
104 #define max(a,b) ((a)>(b)?(a):(b))
105 #endif
106 
107 #if !defined(MIN)
108 #define MIN min
109 #endif
110 
111 #if !defined(MAX)
112 #define MAX max
113 #endif
114 
115 // OTA Debug second part, depends on HAL
116 #if defined(MY_OTA_LOG_SENDER_FEATURE) || defined(MY_OTA_LOG_RECEIVER_FEATURE)
117 #include "core/MyOTALogging.cpp"
118 #endif
119 
120 #if defined(MY_LEDS_BLINKING_FEATURE)
121 #error MY_LEDS_BLINKING_FEATURE is now removed from MySensors core,\
122 define MY_DEFAULT_ERR_LED_PIN, MY_DEFAULT_TX_LED_PIN or\
123 MY_DEFAULT_RX_LED_PIN in your sketch instead to enable LEDs
124 #endif
125 
126 #if defined(MY_DEFAULT_RX_LED_PIN) || defined(MY_DEFAULT_TX_LED_PIN) || defined(MY_DEFAULT_ERR_LED_PIN)
127 #include "core/MyLeds.cpp"
128 #else
129 #include "core/MyLeds.h"
130 #endif
131 
132 #include "core/MyIndication.cpp"
133 
134 
135 // INCLUSION MODE
136 #if defined(MY_INCLUSION_MODE_FEATURE)
137 #include "core/MyInclusionMode.cpp"
138 #endif
139 
140 
141 // SIGNING
142 #include "core/MySigning.cpp"
143 #if defined(MY_SIGNING_FEATURE)
144 // SIGNING COMMON FUNCTIONS
145 #if defined(MY_SIGNING_ATSHA204) && defined(MY_SIGNING_SOFT)
146 #error Only one signing engine can be activated
147 #endif
148 #if defined(MY_SIGNING_ATSHA204) && defined(__linux__)
149 #error No support for ATSHA204 on this platform
150 #endif
151 
152 #if defined(MY_SIGNING_ATSHA204)
153 #include "core/MySigningAtsha204.cpp"
154 #include "drivers/ATSHA204/ATSHA204.cpp"
155 #elif defined(MY_SIGNING_SOFT)
156 #include "core/MySigningAtsha204Soft.cpp"
157 #endif
158 #endif
159 
160 // FLASH
161 #if defined(MY_OTA_FIRMWARE_FEATURE)
162 #ifndef MCUBOOT_PRESENT
163 #if defined(MY_OTA_USE_I2C_EEPROM)
164 #include "drivers/I2CEeprom/I2CEeprom.cpp"
165 #else
166 #include "drivers/SPIFlash/SPIFlash.cpp"
167 #endif
168 #endif
169 #include "core/MyOTAFirmwareUpdate.cpp"
170 #endif
171 
172 // GATEWAY - TRANSPORT
173 #if defined(MY_CONTROLLER_IP_ADDRESS) || defined(MY_CONTROLLER_URL_ADDRESS)
174 #define MY_GATEWAY_CLIENT_MODE
175 #endif
176 
177 #if defined(MY_USE_UDP) && !defined(MY_GATEWAY_CLIENT_MODE)
178 #error You must specify MY_CONTROLLER_IP_ADDRESS or MY_CONTROLLER_URL_ADDRESS for UDP
179 #endif
180 
181 
182 
183 // Set MQTT defaults if not set
184 
185 #if !defined(MY_MQTT_PUBLISH_TOPIC_PREFIX)
186 #define MY_MQTT_PUBLISH_TOPIC_PREFIX "mygateway1-out"
187 #endif
188 
189 #if !defined(MY_MQTT_SUBSCRIBE_TOPIC_PREFIX)
190 #define MY_MQTT_SUBSCRIBE_TOPIC_PREFIX "mygateway1-in"
191 #endif
192 
193 #if !defined(MY_MQTT_CLIENT_ID)
194 #define MY_MQTT_CLIENT_ID "mysensors-1"
195 #endif
196 
197 #if defined(MY_GATEWAY_MQTT_CLIENT)
198 #if defined(MY_SENSOR_NETWORK)
199 // We assume that a gateway having a radio also should act as repeater
200 #define MY_REPEATER_FEATURE
201 #endif
202 
203 // GATEWAY - COMMON FUNCTIONS
204 // We support MQTT Client using W5100, ESP8266, GSM modems supported by TinyGSM library and Linux
205 #if !defined(MY_GATEWAY_CLIENT_MODE) && !defined(MY_GATEWAY_TINYGSM)
206 #error You must specify MY_CONTROLLER_IP_ADDRESS or MY_CONTROLLER_URL_ADDRESS
207 #endif
208 
209 #if defined(MY_GATEWAY_TINYGSM) && !defined(MY_GATEWAY_MQTT_CLIENT)
210 // TinyGSM currently only supports MQTTClient mode.
211 #error MY_GATEWAY_TINYGSM only works with MY_GATEWAY_MQTT_CLIENT
212 #endif
213 
214 #include "core/MyGatewayTransport.cpp"
215 #include "core/MyProtocol.cpp"
216 
217 #if defined(MY_GATEWAY_TINYGSM)
218 #include "drivers/TinyGSM/TinyGsmClient.h"
219 #endif
220 
221 #if defined(MY_GATEWAY_LINUX)
222 #include "hal/architecture/Linux/drivers/core/EthernetClient.h"
223 #include "hal/architecture/Linux/drivers/core/EthernetServer.h"
224 #include "hal/architecture/Linux/drivers/core/IPAddress.h"
225 #endif
226 #include "drivers/PubSubClient/PubSubClient.cpp"
227 #include "core/MyGatewayTransportMQTTClient.cpp"
228 #elif defined(MY_GATEWAY_FEATURE)
229 // GATEWAY - COMMON FUNCTIONS
230 #include "core/MyGatewayTransport.cpp"
231 #include "core/MyProtocol.cpp"
232 
233 // GATEWAY - CONFIGURATION
234 #if defined(MY_SENSOR_NETWORK)
235 // We assume that a gateway having a radio also should act as repeater
236 #define MY_REPEATER_FEATURE
237 #endif
238 
239 #if defined(MY_GATEWAY_ESP8266) || defined(MY_GATEWAY_ESP32)
240 // GATEWAY - ESP8266 / ESP32
241 #include "core/MyGatewayTransportEthernet.cpp"
242 #elif defined(MY_GATEWAY_LINUX)
243 // GATEWAY - Generic Linux
244 #if defined(MY_USE_UDP)
245 #error UDP mode is not available for Linux
246 #endif
247 #include "hal/architecture/Linux/drivers/core/EthernetClient.h"
248 #include "hal/architecture/Linux/drivers/core/EthernetServer.h"
249 #include "hal/architecture/Linux/drivers/core/IPAddress.h"
250 #include "core/MyGatewayTransportEthernet.cpp"
251 #elif defined(MY_GATEWAY_W5100)
252 // GATEWAY - W5100
253 #include "core/MyGatewayTransportEthernet.cpp"
254 #elif defined(MY_GATEWAY_ENC28J60)
255 // GATEWAY - ENC28J60
256 #if defined(MY_USE_UDP)
257 #error UDP mode is not available for ENC28J60
258 #endif
259 #include "core/MyGatewayTransportEthernet.cpp"
260 #elif defined(MY_GATEWAY_SERIAL)
261 // GATEWAY - SERIAL
262 #include "core/MyGatewayTransportSerial.cpp"
263 #endif
264 #endif
265 
266 // TRANSPORT
267 #ifndef DOXYGEN
268 // count enabled transports
269 #if defined(MY_RADIO_RF24)
270 #define __RF24CNT 1
271 #else
272 #define __RF24CNT 0
273 #endif
274 #if defined(MY_RADIO_NRF5_ESB)
275 #define __NRF5ESBCNT 1
276 #else
277 #define __NRF5ESBCNT 0
278 #endif
279 #if defined(MY_RADIO_RFM69)
280 #define __RFM69CNT 1
281 #else
282 #define __RFM69CNT 0
283 #endif
284 #if defined(MY_RADIO_RFM95)
285 #define __RFM95CNT 1
286 #else
287 #define __RFM95CNT 0
288 #endif
289 #if defined(MY_RS485)
290 #define __RS485CNT 1
291 #else
292 #define __RS485CNT 0
293 #endif
294 #if defined(MY_PJON)
295 #define _PJONCNT 1
296 #else
297 #define _PJONCNT 0
298 #endif
299 #if defined(MY_RADIO_SX126x)
300 #define __SX126xCNT 1
301 #else
302 #define __SX126xCNT 0
303 #endif
304 #if (__RF24CNT + __NRF5ESBCNT + __RFM69CNT + __RFM95CNT + __RS485CNT + _PJONCNT + __SX126xCNT > 1)
305 #error Only one forward link driver can be activated
306 #endif
307 #endif //DOXYGEN
308 
309 // SANITY CHECK
310 #if defined(MY_REPEATER_FEATURE) || defined(MY_GATEWAY_FEATURE)
311 #define MY_TRANSPORT_SANITY_CHECK
312 #endif
313 
314 // TRANSPORT INCLUDES
315 #if defined(MY_RADIO_RF24) || defined(MY_RADIO_NRF5_ESB) || defined(MY_RADIO_RFM69) || defined(MY_RADIO_RFM95) || defined(MY_RS485) || defined (MY_PJON) || defined(MY_RADIO_SX126x)
316 #include "hal/transport/MyTransportHAL.h"
317 #include "core/MyTransport.h"
318 
319 // PARENT CHECK
320 #if defined(MY_PARENT_NODE_IS_STATIC) && (MY_PARENT_NODE_ID == AUTO)
321 #error Parent is static but no parent ID defined, set MY_PARENT_NODE_ID.
322 #endif
323 
324 #if defined(MY_TRANSPORT_DONT_CARE_MODE)
325 #error MY_TRANSPORT_DONT_CARE_MODE is deprecated, set MY_TRANSPORT_WAIT_READY_MS instead!
326 #endif
327 
328 // RAM ROUTING TABLE
329 #ifdef DOXYGEN
330 
336 #define MY_RAM_ROUTING_TABLE_ENABLED
337 #elif defined(MY_RAM_ROUTING_TABLE_FEATURE) && defined(MY_REPEATER_FEATURE)
338 // activate feature based on architecture
339 #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_SAMD) || defined(ARDUINO_ARCH_NRF5) || defined(ARDUINO_ARCH_STM32F1) || defined(TEENSYDUINO) || defined(__linux__) || defined(__ASR6501__) || defined (__ASR6502__)
340 #define MY_RAM_ROUTING_TABLE_ENABLED
341 #elif defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_MEGAAVR)
342 #if defined(__avr_atmega1280__) || defined(__avr_atmega1284__) || defined(__avr_atmega2560__) || defined(__avr_attiny3224__) || defined(__avr_attiny3227__)
343 // >4kb, enable it
344 #define MY_RAM_ROUTING_TABLE_ENABLED
345 #else
346 // memory limited, enable with care
347 // #define MY_RAM_ROUTING_TABLE_ENABLED
348 #endif // __avr_atmega1280__, __avr_atmega1284__, __avr_atmega2560__
349 #endif // ARDUINO_ARCH_AVR
350 #endif // DOXYGEN
351 
352 // SOFTSERIAL
353 #if defined(MY_GSM_TX) != defined(MY_GSM_RX)
354 #error Both, MY_GSM_TX and MY_GSM_RX need to be defined when using SoftSerial
355 #endif
356 
357 #if defined(MY_GATEWAY_TINYGSM) && !defined(SerialAT) && (!defined(MY_GSM_TX) || !defined(MY_GSM_RX))
358 #error You need to define either SerialAT or MY_GSM_RX and MY_GSM_TX pins
359 #endif
360 
361 // POWER PIN
362 #ifndef DOXYGEN
363 #if defined(MY_RF24_POWER_PIN) || defined(MY_RFM69_POWER_PIN) || defined(MY_RFM95_POWER_PIN) || defined(MY_RADIO_NRF5_ESB) || defined(MY_RADIO_SX126x_POWER_PIN)
364 #define RADIO_CAN_POWER_OFF (true)
365 #else
366 #define RADIO_CAN_POWER_OFF (false)
367 #endif
368 #endif
369 
370 // Transport drivers
371 #if defined(MY_RADIO_RF24)
372 #include "hal/transport/RF24/driver/RF24.cpp"
373 #include "hal/transport/RF24/MyTransportRF24.cpp"
374 #elif defined(MY_RADIO_NRF5_ESB)
375 #if !defined(ARDUINO_ARCH_NRF5)
376 #error No support for nRF5 radio on this platform
377 #endif
378 #include "hal/transport/NRF5_ESB/driver/Radio.cpp"
379 #include "hal/transport/NRF5_ESB/driver/Radio_ESB.cpp"
380 #include "hal/transport/NRF5_ESB/MyTransportNRF5_ESB.cpp"
381 #elif defined(MY_RS485)
382 #if !defined(MY_RS485_HWSERIAL)
383 #if defined(__linux__)
384 #error You must specify MY_RS485_HWSERIAL for RS485 transport
385 #endif
386 #include "drivers/AltSoftSerial/AltSoftSerial.cpp"
387 #endif
388 #include "hal/transport/RS485/MyTransportRS485.cpp"
389 #elif defined(MY_RADIO_RFM69)
390 #if defined(MY_RFM69_NEW_DRIVER)
391 #include "hal/transport/RFM69/driver/new/RFM69_new.cpp"
392 #else
393 #include "hal/transport/RFM69/driver/old/RFM69_old.cpp"
394 #endif
395 #include "hal/transport/RFM69/MyTransportRFM69.cpp"
396 #elif defined(MY_RADIO_RFM95)
397 #include "hal/transport/RFM95/driver/RFM95.cpp"
398 #include "hal/transport/RFM95/MyTransportRFM95.cpp"
399 #elif defined(MY_PJON)
400 #include "hal/transport/PJON/driver/PJON.h"
401 #include "hal/transport/PJON/driver/PJONSoftwareBitBang.h"
402 #if (PJON_BROADCAST == 0)
403 #error "You must change PJON_BROADCAST to BROADCAST_ADDRESS (255u) and PJON_NOT_ASSIGNED to other one."
404 #endif
405 #include "hal/transport/PJON/MyTransportPJON.cpp"
406 #elif defined(MY_RADIO_SX126x)
407 #include "hal/transport/SX126x/driver/SX126x.cpp"
408 #include "hal/transport/SX126x/MyTransportSX126x.cpp"
409 #endif
410 
411 #if (defined(MY_RF24_ENABLE_ENCRYPTION) && defined(MY_RADIO_RF24)) || (defined(MY_NRF5_ESB_ENABLE_ENCRYPTION) && defined(MY_RADIO_NRF5_ESB)) || (defined(MY_RFM69_ENABLE_ENCRYPTION) && defined(MY_RADIO_RFM69)) || (defined(MY_RFM95_ENABLE_ENCRYPTION) && defined(MY_RADIO_RFM95))
412 #define MY_TRANSPORT_ENCRYPTION
413 #endif
414 
415 #include "hal/transport/MyTransportHAL.cpp"
416 
417 // PASSIVE MODE
418 #if defined(MY_PASSIVE_NODE) && !defined(DOXYGEN)
419 #define MY_TRANSPORT_UPLINK_CHECK_DISABLED
420 #define MY_PARENT_NODE_IS_STATIC
421 #undef MY_REGISTRATION_FEATURE
422 #undef MY_SIGNING_FEATURE
423 #undef MY_OTA_FIRMWARE_FEATURE
424 #if defined(MY_GATEWAY_FEATURE) || defined(MY_REPEATER_FEATURE)
425 #error This node is configured as GW/repeater, MY_PASSIVE_NODE cannot be set simultaneously
426 #endif
427 #if (MY_NODE_ID == AUTO)
428 #error MY_PASSIVE_NODE configuration requires setting MY_NODE_ID
429 #endif
430 #endif
431 
432 #include "core/MyTransport.cpp"
433 #endif
434 
435 // Make sure to disable child features when parent feature is disabled
436 #if !defined(MY_SENSOR_NETWORK)
437 #undef MY_OTA_FIRMWARE_FEATURE
438 #undef MY_REPEATER_FEATURE
439 #undef MY_SIGNING_NODE_WHITELISTING
440 #undef MY_SIGNING_FEATURE
441 #endif
442 
443 #if !defined(MY_GATEWAY_FEATURE)
444 #undef MY_INCLUSION_MODE_FEATURE
445 #undef MY_INCLUSION_BUTTON_FEATURE
446 #endif
447 
448 #if !defined(MY_CORE_ONLY)
449 #if !defined(MY_GATEWAY_FEATURE) && !defined(MY_SENSOR_NETWORK)
450 #error No forward link or gateway feature activated. This means nowhere to send messages! Pretty pointless.
451 #endif
452 #endif
453 
454 #include "core/MyCapabilities.h"
455 #include "core/MyMessage.cpp"
456 #include "core/MyMultiMessage.cpp"
457 #include "core/MySplashScreen.cpp"
458 #include "core/MySensorsCore.cpp"
459 
460 // HW mains
461 #if defined(ARDUINO_ARCH_AVR)
462 #include "hal/architecture/AVR/MyMainAVR.cpp"
463 #elif defined(ARDUINO_ARCH_MEGAAVR)
464 #include "hal/architecture/AVR/MyMainAVR.cpp"
465 #elif defined(ARDUINO_ARCH_SAMD)
466 #include "hal/architecture/SAMD/MyMainSAMD.cpp"
467 #elif defined(ARDUINO_ARCH_ESP8266)
468 #include "hal/architecture/ESP8266/MyMainESP8266.cpp"
469 #elif defined(ARDUINO_ARCH_NRF5)
470 #include "hal/architecture/NRF5/MyMainNRF5.cpp"
471 #elif defined(ARDUINO_ARCH_ESP32)
472 #include "hal/architecture/ESP32/MyMainESP32.cpp"
473 #elif defined(__linux__)
474 #include "hal/architecture/Linux/MyMainLinuxGeneric.cpp"
475 #elif defined(ARDUINO_ARCH_STM32F1)
476 #include "hal/architecture/STM32F1/MyMainSTM32F1.cpp"
477 #elif defined(__ASR6501__) || defined(__ASR6502__)
478 #include "hal/architecture/ASR650x/MyMainASR650x.cpp"
479 #elif defined(__arm__) && defined(TEENSYDUINO)
480 #include "hal/architecture/Teensy3/MyMainTeensy3.cpp"
481 #elif defined(ARDUINO_ARCH_CUBECELL)
482 #include "hal/architecture/ASR650x/MyMainASR650x.cpp"
483 #endif
484 
485 #endif
486 
MyConfig.h
MySensors specific configuration flags.Set these in your sketch before including MySensors....
MyHwHAL.h
MyTransport.h
MySensorsCore.h
MyOTALogging.h
API declaration for MyOTALogging.
MyCapabilities.h