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