MySensors Library & Examples  2.3.2
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-2019 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 
289 #if (__RF24CNT + __NRF5ESBCNT + __RFM69CNT + __RFM95CNT + __RS485CNT > 1)
290 #error Only one forward link driver can be activated
291 #endif
292 #endif //DOXYGEN
293 
294 // SANITY CHECK
295 #if defined(MY_REPEATER_FEATURE) || defined(MY_GATEWAY_FEATURE)
296 #define MY_TRANSPORT_SANITY_CHECK
297 #endif
298 
299 // TRANSPORT INCLUDES
300 #if defined(MY_RADIO_RF24) || defined(MY_RADIO_NRF5_ESB) || defined(MY_RADIO_RFM69) || defined(MY_RADIO_RFM95) || defined(MY_RS485)
301 #include "hal/transport/MyTransportHAL.h"
302 #include "core/MyTransport.h"
303 
304 // PARENT CHECK
305 #if defined(MY_PARENT_NODE_IS_STATIC) && (MY_PARENT_NODE_ID == AUTO)
306 #error Parent is static but no parent ID defined, set MY_PARENT_NODE_ID.
307 #endif
308 
309 #if defined(MY_TRANSPORT_DONT_CARE_MODE)
310 #error MY_TRANSPORT_DONT_CARE_MODE is deprecated, set MY_TRANSPORT_WAIT_READY_MS instead!
311 #endif
312 
313 // RAM ROUTING TABLE
314 #if defined(MY_RAM_ROUTING_TABLE_FEATURE) && defined(MY_REPEATER_FEATURE)
315 // activate feature based on architecture
316 #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__)
317 #define MY_RAM_ROUTING_TABLE_ENABLED
318 #elif defined(ARDUINO_ARCH_AVR)
319 #if defined(__avr_atmega1280__) || defined(__avr_atmega1284__) || defined(__avr_atmega2560__)
320 // >4kb, enable it
321 #define MY_RAM_ROUTING_TABLE_ENABLED
322 #else
323 // memory limited, enable with care
324 // #define MY_RAM_ROUTING_TABLE_ENABLED
325 #endif // __avr_atmega1280__, __avr_atmega1284__, __avr_atmega2560__
326 #endif // ARDUINO_ARCH_AVR
327 #endif
328 #ifdef DOXYGEN
329 
335 #define MY_RAM_ROUTING_TABLE_ENABLED
336 #endif
337 
338 // SOFTSERIAL
339 #if defined(MY_GSM_TX) != defined(MY_GSM_RX)
340 #error Both, MY_GSM_TX and MY_GSM_RX need to be defined when using SoftSerial
341 #endif
342 
343 #if defined(MY_GATEWAY_TINYGSM) && !defined(SerialAT) && (!defined(MY_GSM_TX) || !defined(MY_GSM_RX))
344 #error You need to define either SerialAT or MY_GSM_RX and MY_GSM_TX pins
345 #endif
346 
347 // POWER PIN
348 #ifndef DOXYGEN
349 #if defined(MY_RF24_POWER_PIN) || defined(MY_RFM69_POWER_PIN) || defined(MY_RFM95_POWER_PIN) || defined(MY_RADIO_NRF5_ESB)
350 #define RADIO_CAN_POWER_OFF (true)
351 #else
352 #define RADIO_CAN_POWER_OFF (false)
353 #endif
354 #endif
355 
356 // Transport drivers
357 #if defined(MY_RADIO_RF24)
358 #include "hal/transport/RF24/driver/RF24.cpp"
359 #include "hal/transport/RF24/MyTransportRF24.cpp"
360 #elif defined(MY_RADIO_NRF5_ESB)
361 #if !defined(ARDUINO_ARCH_NRF5)
362 #error No support for nRF5 radio on this platform
363 #endif
364 #include "hal/transport/NRF5_ESB/driver/Radio.cpp"
365 #include "hal/transport/NRF5_ESB/driver/Radio_ESB.cpp"
366 #include "hal/transport/NRF5_ESB/MyTransportNRF5_ESB.cpp"
367 #elif defined(MY_RS485)
368 #if !defined(MY_RS485_HWSERIAL)
369 #if defined(__linux__)
370 #error You must specify MY_RS485_HWSERIAL for RS485 transport
371 #endif
372 #include "drivers/AltSoftSerial/AltSoftSerial.cpp"
373 #endif
374 #include "hal/transport/RS485/MyTransportRS485.cpp"
375 #elif defined(MY_RADIO_RFM69)
376 #if defined(MY_RFM69_NEW_DRIVER)
377 #include "hal/transport/RFM69/driver/new/RFM69_new.cpp"
378 #else
379 #include "hal/transport/RFM69/driver/old/RFM69_old.cpp"
380 #endif
381 #include "hal/transport/RFM69/MyTransportRFM69.cpp"
382 #elif defined(MY_RADIO_RFM95)
383 #include "hal/transport/RFM95/driver/RFM95.cpp"
384 #include "hal/transport/RFM95/MyTransportRFM95.cpp"
385 #endif
386 
387 #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))
388 #define MY_TRANSPORT_ENCRYPTION
389 #endif
390 
391 #include "hal/transport/MyTransportHAL.cpp"
392 
393 // PASSIVE MODE
394 #if defined(MY_PASSIVE_NODE) && !defined(DOXYGEN)
395 #define MY_TRANSPORT_UPLINK_CHECK_DISABLED
396 #define MY_PARENT_NODE_IS_STATIC
397 #undef MY_REGISTRATION_FEATURE
398 #undef MY_SIGNING_FEATURE
399 #undef MY_OTA_FIRMWARE_FEATURE
400 #if defined(MY_GATEWAY_FEATURE) || defined(MY_REPEATER_FEATURE)
401 #error This node is configured as GW/repeater, MY_PASSIVE_NODE cannot be set simultaneously
402 #endif
403 #if (MY_NODE_ID == AUTO)
404 #error MY_PASSIVE_NODE configuration requires setting MY_NODE_ID
405 #endif
406 #endif
407 
408 #include "core/MyTransport.cpp"
409 #endif
410 
411 // Make sure to disable child features when parent feature is disabled
412 #if !defined(MY_SENSOR_NETWORK)
413 #undef MY_OTA_FIRMWARE_FEATURE
414 #undef MY_REPEATER_FEATURE
415 #undef MY_SIGNING_NODE_WHITELISTING
416 #undef MY_SIGNING_FEATURE
417 #endif
418 
419 #if !defined(MY_GATEWAY_FEATURE)
420 #undef MY_INCLUSION_MODE_FEATURE
421 #undef MY_INCLUSION_BUTTON_FEATURE
422 #endif
423 
424 #if !defined(MY_CORE_ONLY)
425 #if !defined(MY_GATEWAY_FEATURE) && !defined(MY_SENSOR_NETWORK)
426 #error No forward link or gateway feature activated. This means nowhere to send messages! Pretty pointless.
427 #endif
428 #endif
429 
430 #include "core/MyCapabilities.h"
431 #include "core/MyMessage.cpp"
432 #include "core/MySplashScreen.cpp"
433 #include "core/MySensorsCore.cpp"
434 
435 // HW mains
436 #if defined(ARDUINO_ARCH_AVR)
437 #include "hal/architecture/AVR/MyMainAVR.cpp"
438 #elif defined(ARDUINO_ARCH_SAMD)
439 #include "hal/architecture/SAMD/MyMainSAMD.cpp"
440 #elif defined(ARDUINO_ARCH_ESP8266)
441 #include "hal/architecture/ESP8266/MyMainESP8266.cpp"
442 #elif defined(ARDUINO_ARCH_NRF5)
443 #include "hal/architecture/NRF5/MyMainNRF5.cpp"
444 #elif defined(ARDUINO_ARCH_ESP32)
445 #include "hal/architecture/ESP32/MyMainESP32.cpp"
446 #elif defined(__linux__)
447 #include "hal/architecture/Linux/MyMainLinuxGeneric.cpp"
448 #elif defined(ARDUINO_ARCH_STM32F1)
449 #include "hal/architecture/STM32F1/MyMainSTM32F1.cpp"
450 #elif defined(__arm__) && defined(TEENSYDUINO)
451 #include "hal/architecture/Teensy3/MyMainTeensy3.cpp"
452 #endif
453 
454 #endif
455 
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