EnigmaIOT  0.9.8
Secure sensor and gateway platform based on ESP8266 and ESP32
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
EnigmaIOTGatewayMQTT.cpp
Go to the documentation of this file.
1 
12 #include <Arduino.h>
13 
14 #include "GwOutput_mqtt.h"
15 
16 #ifdef SECURE_MQTT
17 #include <WiFiClientSecure.h>
18 #else
19 #include <WiFiClient.h>
20 #endif // SECURE_MQTT
21 
22 
23 #ifdef ESP32
24 #include <WiFi.h>
25 #include "soc/soc.h" // Disable brownout problems
26 #include "soc/rtc_cntl_reg.h" // Disable brownout problems
27 #elif defined ESP8266
28 #include <ESP8266WiFi.h>
29 #endif // ESP32
30 
31 #include <ArduinoOTA.h>
32 
33 #include <CayenneLPP.h>
34 
35 #include <EnigmaIOTGateway.h>
36 #include <helperFunctions.h>
37 #include <EnigmaIOTdebug.h>
38 #include <espnow_hal.h>
39 #include <ArduinoJson.h>
40 #include <ESPAsyncWiFiManager.h>
41 
42 //#define MEAS_TEMP // Temperature measurement for Gateway monitoring using DS18B20
43 
44 #ifdef MEAS_TEMP
45 #include <DallasTemperature.h>
46 #include <OneWire.h>
47 const time_t statusPeriod = 300 * 1000;
48 const int DS18B20_PIN = 16;
49 const int DS18B20_PREC = 12;
50 OneWire ow (DS18B20_PIN);
51 DallasTemperature ds18b20 (&ow);
52 DeviceAddress dsAddress;
53 float temperature;
54 #endif
55 
56 #ifndef LED_BUILTIN
57 #define LED_BUILTIN 5
58 #endif // BUILTIN_LED
59 
60 #define BLUE_LED LED_BUILTIN
61 #define RED_LED LED_BUILTIN
62 
63 #ifdef ESP32
64 TimerHandle_t connectionLedTimer;
65 #elif defined(ESP8266)
66 ETSTimer connectionLedTimer;
67 #endif // ESP32
68 
70 bool connectionLedFlashing = false;
71 
72 bool restartRequested = false;
74 
75 
76 void flashConnectionLed (void* led) {
77  //digitalWrite (*(int*)led, !digitalRead (*(int*)led));
78  digitalWrite (LED_BUILTIN, !digitalRead (LED_BUILTIN));
79 }
80 
81 void startConnectionFlash (int period) {
82 #ifdef ESP32
83  if (!connectionLedFlashing) {
84  connectionLedFlashing = true;
85  connectionLedTimer = xTimerCreate ("led_flash", pdMS_TO_TICKS (period), pdTRUE, (void*)0, flashConnectionLed);
86  xTimerStart (connectionLedTimer, 0);
87  }
88 #elif defined (ESP8266)
89  ets_timer_disarm (&connectionLedTimer);
90  if (!connectionLedFlashing) {
91  connectionLedFlashing = true;
92  ets_timer_arm_new (&connectionLedTimer, period, true, true);
93  }
94 #endif // ESP32
95 }
96 
98 #ifdef ESP32
100  connectionLedFlashing = false;
101  xTimerStop (connectionLedTimer, 0);
102  xTimerDelete (connectionLedTimer, 0);
103  }
104 #elif defined(ESP8266)
105  if (connectionLedFlashing) {
106  connectionLedFlashing = false;
107  ets_timer_disarm (&connectionLedTimer);
108  digitalWrite (connectionLed, LED_OFF);
109  }
110 #endif // ESP32
111 }
112 
114  // Port defaults to 3232
115  // ArduinoOTA.setPort(3232);
116 
117  // Hostname defaults to esp3232-[MAC]
118  ArduinoOTA.setHostname (EnigmaIOTGateway.getNetworkName ());
119 
120  // No authentication by default
121  ArduinoOTA.setPassword (EnigmaIOTGateway.getNetworkKey (true));
122 
123  // Password can be set with it's md5 value as well
124  // MD5(admin) = 21232f297a57a5a743894a0e4a801fc3
125  // ArduinoOTA.setPasswordHash("21232f297a57a5a743894a0e4a801fc3");
126 
127  ArduinoOTA.onStart ([] () {
128  if (ArduinoOTA.getCommand () == U_FLASH) {
129  DEBUG_WARN ("Start updating sketch");
130  } else {// U_SPIFFS
131  DEBUG_WARN ("Start updating filesystem");
132  // NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
133  }
134  });
135  ArduinoOTA.onEnd ([] () {
136  DEBUG_WARN ("OTA Finished");
137  });
138  ArduinoOTA.onProgress ([] (unsigned int progress, unsigned int total) {
139  static bool printed = false;
140  unsigned int percent = progress / (total / 100);
141  digitalWrite (BLUE_LED, !digitalRead (BLUE_LED));
142  // if (!(percent % 1)) {
143  // //Serial.print ('.');
144  // }
145  if (!(percent % 20) && !printed && percent != 0) {
146  DEBUG_WARN (" %d%%\n", percent);
147  printed = true;
148  } else if (percent % 20) {
149  printed = false;
150  }
151  if (progress == total) {
152  DEBUG_WARN ("OTA transfer finished");
153  }
154  });
155  ArduinoOTA.onError ([] (ota_error_t error) {
156  DEBUG_WARN ("OTA Error[%u]: ", error);
157  if (error == OTA_AUTH_ERROR) { DEBUG_WARN ("OTA Auth Failed"); } else if (error == OTA_BEGIN_ERROR) { DEBUG_WARN ("OTA Begin Failed"); } else if (error == OTA_CONNECT_ERROR) { DEBUG_WARN ("OTA Connect Failed"); } else if (error == OTA_RECEIVE_ERROR) { DEBUG_WARN ("OTA Receive Failed"); } else if (error == OTA_END_ERROR) { DEBUG_WARN ("OTA End Failed"); }
158  });
159 
160  ArduinoOTA.begin ();
161 }
162 
163 void wifiManagerExit (boolean status) {
165 }
166 
169 }
170 
171 void processRxControlData (char* macStr, uint8_t* data, uint8_t length) {
172  if (data) {
173  if (data[0] == VERSION_ANS && length >= 4) {
174  DEBUG_INFO ("Version message: %d.%d.%d", data[1], data[2], data[3]);
175  Node* node = EnigmaIOTGateway.getNodes ()->getNodeFromName (macStr);
176  if (node) {
177  node->setVersion (data[1], data[2], data[3]);
178  }
179  }
180  GwOutput.outputControlSend (macStr, data, length);
181  }
182 }
183 
184 void doRestart () {
185  DEBUG_WARN ("Restart requested");
186  const size_t capacity = JSON_OBJECT_SIZE (1);
187  size_t len;
188  char* payload;
189 
190  DynamicJsonDocument doc (capacity);
191 
192  doc["action"] = "restart";
193 
194  len = measureJson (doc) + 1;
195  payload = (char*)malloc (len);
196  serializeJson (doc, (char*)payload, len);
197  char addr[] = "gateway";
198  GwOutput.outputDataSend (addr, payload, len - 1);
199  free (payload);
200 
201  restartRequested = true;
202  restartRequestTime = millis ();
203 
204 }
205 
206 #if SUPPORT_HA_DISCOVERY
207 void processHADiscovery (const char* topic, char* message, size_t len) {
208  DEBUG_INFO ("About to process HA discovery. Len: %d - %s --> %.*s", len, topic, len, message);
209  GwOutput.rawMsgSend (topic, message, len, true);
210 }
211 #endif
212 
213 void processRxData (uint8_t* mac, uint8_t* buffer, uint8_t length, uint16_t lostMessages, bool control, gatewayPayloadEncoding_t payload_type, char* nodeName = NULL) {
214  uint8_t* addr = mac;
215  size_t pld_size = 0;
216  const int PAYLOAD_SIZE = 1024; // Max MQTT payload in PubSubClient library normal operation.
217 
218  char payload[PAYLOAD_SIZE];
219 
220  char mac_str[ENIGMAIOT_ADDR_LEN * 3];
221  mac2str (addr, mac_str);
222 
223  if (control) {
224  processRxControlData (nodeName ? nodeName : mac_str, buffer, length);
225  return;
226  }
227  //char* netName = EnigmaIOTGateway.getNetworkName ();
228  if (payload_type == CAYENNELPP) {
229  DEBUG_INFO ("CayenneLPP message");
230  const int capacity = JSON_ARRAY_SIZE (25) + 25 * JSON_OBJECT_SIZE (4);
231  DynamicJsonDocument jsonBuffer (capacity);
232  JsonArray root = jsonBuffer.createNestedArray ();
233  CayenneLPP cayennelpp (MAX_DATA_PAYLOAD_SIZE);
234 
235  cayennelpp.decode ((uint8_t*)buffer, length, root);
236  uint8_t error = cayennelpp.getError ();
237  if (error != LPP_ERROR_OK) {
238  DEBUG_ERROR ("Error decoding CayenneLPP data: %d", error);
239  return;
240  }
241  pld_size = serializeJson (root, payload, PAYLOAD_SIZE);
242  } else if (payload_type == MSG_PACK) {
243  DEBUG_INFO ("MsgPack message");
244  const int capacity = JSON_ARRAY_SIZE (25) + 25 * JSON_OBJECT_SIZE (4);
245  DynamicJsonDocument jsonBuffer (capacity);
246  DeserializationError error = deserializeMsgPack (jsonBuffer, buffer, length);
247  if (error != DeserializationError::Ok) {
248  DEBUG_ERROR ("Error decoding MSG Pack data: %s", error.c_str ());
249  return;
250  }
251  pld_size = serializeJson (jsonBuffer, payload, PAYLOAD_SIZE);
252  } else if (payload_type == RAW) {
253  DEBUG_INFO ("RAW message");
254  if (length <= PAYLOAD_SIZE) {
255  memcpy (payload, buffer, length);
256  pld_size = length;
257  } else { // This will not happen but may lead to errors in case of using another physical transport
258  memcpy (payload, buffer, PAYLOAD_SIZE);
259  pld_size = PAYLOAD_SIZE;
260  }
261  }
262 
263  GwOutput.outputDataSend (nodeName ? nodeName : mac_str, payload, pld_size);
264  DEBUG_INFO ("Published data message from %s, length %d: %s, Encoding 0x%02X", nodeName ? nodeName : mac_str, pld_size, payload, payload_type);
265  if (lostMessages > 0) {
266  pld_size = snprintf (payload, PAYLOAD_SIZE, "{\"lostMessages\":%u}", lostMessages);
267  GwOutput.outputDataSend (nodeName ? nodeName : mac_str, payload, pld_size, GwOutput_data_type::lostmessages);
268  DEBUG_INFO ("Published MQTT from %s: %s", nodeName ? nodeName : mac_str, payload);
269  }
270 #if ENABLE_STATUS_MESSAGES
271  pld_size = snprintf (payload, PAYLOAD_SIZE, "{\"per\":%e,\"lostmessages\":%u,\"totalmessages\":%u,\"packetshour\":%.2f}",
272  EnigmaIOTGateway.getPER ((uint8_t*)mac),
273  EnigmaIOTGateway.getErrorPackets ((uint8_t*)mac),
274  EnigmaIOTGateway.getTotalPackets ((uint8_t*)mac),
275  EnigmaIOTGateway.getPacketsHour ((uint8_t*)mac));
276  GwOutput.outputDataSend (nodeName ? nodeName : mac_str, payload, pld_size, GwOutput_data_type::status);
277  DEBUG_INFO ("Published MQTT from %s: %s", nodeName ? nodeName : mac_str, payload);
278 #endif
279 }
280 
281 void onDownlinkData (uint8_t* address, char* nodeName, control_message_type_t msgType, char* data, unsigned int len) {
282  uint8_t* buffer;
283  unsigned int bufferLen = len;
285 
286  if (nodeName) {
287  DEBUG_INFO ("DL Message for %s. Type 0x%02X", nodeName, msgType);
288  } else {
289  DEBUG_INFO ("DL Message for " MACSTR ". Type 0x%02X", MAC2STR (address), msgType);
290  }
291  DEBUG_DBG ("Data: %.*s Length: %d", len, data, len);
292 
293  if (msgType == USERDATA_GET || msgType == USERDATA_SET) {
294  const int capacity = JSON_ARRAY_SIZE (25) + 25 * JSON_OBJECT_SIZE (4);
295  DynamicJsonDocument json (capacity);
296  DeserializationError error = deserializeJson (json, data, len, DeserializationOption::NestingLimit (3));
297  if (error == DeserializationError::Ok) {
298  DEBUG_INFO ("JSON Message. Result %s", error.c_str ());
299  bufferLen = measureMsgPack (json) + 1; // Add place for \0
300  buffer = (uint8_t*)malloc (bufferLen);
301  bufferLen = serializeMsgPack (json, (char*)buffer, bufferLen);
302  encoding = MSG_PACK;
303  } else {
304  DEBUG_INFO ("Not JSON Message. Error %s", error.c_str ());
305  bufferLen++; // Add place for \0
306  buffer = (uint8_t*)malloc (bufferLen);
307  sprintf ((char*)buffer, "%.*s", len, data);
308  encoding = RAW;
309  }
310  } else {
311  bufferLen = len + 1;
312  buffer = (uint8_t*)calloc (sizeof (uint8_t), bufferLen);
313  memcpy (buffer, data, len);
314  }
315 
316 
317  if (!EnigmaIOTGateway.sendDownstream (address, buffer, bufferLen, msgType, encoding, nodeName)) {
318  if (nodeName) {
319  DEBUG_WARN ("Error sending esp_now message to %s", nodeName);
320  } else {
321  DEBUG_WARN ("Error sending esp_now message to " MACSTR, MAC2STR (address));
322  }
323  } else {
324  DEBUG_DBG ("Esp-now message sent or queued correctly");
325  }
326 
327  free (buffer);
328 }
329 
330 void newNodeConnected (uint8_t* mac, uint16_t node_id, char* nodeName = nullptr) {
331 
332  //Serial.printf ("New node connected: %s\n", macstr);
333 
334  if (nodeName) {
335  if (!GwOutput.newNodeSend (nodeName, node_id)) {
336  DEBUG_WARN ("Error sending new node %s", nodeName);
337  } else {
338  DEBUG_DBG ("New node %s message sent", nodeName);
339  }
340  } else {
341  char macstr[ENIGMAIOT_ADDR_LEN * 3];
342  mac2str (mac, macstr);
343  if (!GwOutput.newNodeSend (macstr, node_id)) {
344  DEBUG_WARN ("Error sending new node %s", macstr);
345  } else {
346  DEBUG_DBG ("New node %s message sent", macstr);
347  }
348  }
349 
350 }
351 
352 void nodeDisconnected (uint8_t* mac, gwInvalidateReason_t reason) {
353  char macstr[ENIGMAIOT_ADDR_LEN * 3];
354  mac2str (mac, macstr);
355  //Serial.printf ("Node %s disconnected. Reason %u\n", macstr, reason);
356  if (!GwOutput.nodeDisconnectedSend (macstr, reason)) {
357  DEBUG_WARN ("Error sending node disconnected %s reason %d", macstr, reason);
358  } else {
359  DEBUG_DBG ("Node %s disconnected message sent. Reason %d", macstr, reason);
360  }
361 }
362 
363 //#ifdef ESP32
364 //void EnigmaIOTGateway_handle (void * param) {
365 // for (;;) {
366 // EnigmaIOTGateway.handle ();
367 // vTaskDelay (0);
368 // }
369 //}
370 //
371 //void GwOutput_handle (void* param) {
372 // for (;;) {
373 // GwOutput.loop ();
374 // vTaskDelay (0);
375 // }
376 //}
377 //
378 //TaskHandle_t xEnigmaIOTGateway_handle = NULL;
379 //TaskHandle_t gwoutput_handle = NULL;
380 //#endif // ESP32
381 
382 void setup () {
383  Serial.begin (921600); Serial.println (); Serial.println ();
384 
385 #ifdef ESP32
386 // Turn-off the 'brownout detector' to avoid random restarts during wake up,
387 // normally due to bad quality regulator on board
388  WRITE_PERI_REG (RTC_CNTL_BROWN_OUT_REG, 0);
389 #endif
390 
391 #ifdef ESP8266
392  ets_timer_setfn (&connectionLedTimer, flashConnectionLed, (void*)&connectionLed);
393 #elif defined ESP32
394 
395 #endif
396  pinMode (LED_BUILTIN, OUTPUT);
397  digitalWrite (LED_BUILTIN, LED_ON);
398 
399 #ifdef MEAS_TEMP
400  ds18b20.begin ();
401 
402  DEBUG_WARN ("Found %u sensors", ds18b20.getDeviceCount ());
403 
404  if (ds18b20.getAddress (dsAddress, 0)) {
405  DEBUG_WARN ("DS18B20 address: %02X %02X %02X %02X %02X %02X %02X %02X",
406  dsAddress[0], dsAddress[1], dsAddress[2], dsAddress[3],
407  dsAddress[4], dsAddress[5], dsAddress[6], dsAddress[7]);
408  } else {
409  DEBUG_WARN ("No DS18B20 found");
410  }
411  ds18b20.setWaitForConversion (false);
412  ds18b20.setResolution (DS18B20_PREC);
413 #endif // MEAS_TEMP
414 
415  startConnectionFlash (100);
416 
417  if (!GwOutput.loadConfig ()) {
418  DEBUG_WARN ("Error reading config file");
419  }
420 
428 #if SUPPORT_HA_DISCOVERY
429  EnigmaIOTGateway.onHADiscovery (processHADiscovery);
430 #endif
432 
434 
435  WiFi.mode (WIFI_AP_STA);
436  WiFi.begin ();
437 
439 
442 
443  DEBUG_INFO ("STA MAC Address: %s", WiFi.macAddress ().c_str ());
444  DEBUG_INFO ("AP MAC Address: %s", WiFi.softAPmacAddress ().c_str ());
445  DEBUG_INFO ("BSSID Address: %s", WiFi.BSSIDstr ().c_str ());
446 
447  DEBUG_INFO ("IP address: %s", WiFi.localIP ().toString ().c_str ());
448  DEBUG_INFO ("WiFi Channel: %d", WiFi.channel ());
449  DEBUG_INFO ("WiFi SSID: %s", WiFi.SSID ().c_str ());
450  DEBUG_INFO ("Network Name: %s", EnigmaIOTGateway.getNetworkName ());
451 
453  GwOutput.begin ();
454 
456 
457 #ifdef ESP32
458  //xTaskCreate (EnigmaIOTGateway_handle, "handle", 10000, NULL, 1, &xEnigmaIOTGateway_handle);
459  //xTaskCreatePinnedToCore (EnigmaIOTGateway_handle, "handle", 4096, NULL, 0, &xEnigmaIOTGateway_handle, 1);
460  //xTaskCreatePinnedToCore (GwOutput_handle, "gwoutput", 10000, NULL, 2, &gwoutput_handle, 1);
461 #endif
462 }
463 
464 #ifdef MEAS_TEMP
465 void sendStatus (float temperature) {
466  const size_t capacity = JSON_OBJECT_SIZE (1) + JSON_OBJECT_SIZE (3) + 30;;
467  size_t len;
468  char* payload;
469 
470  DynamicJsonDocument doc (capacity);
471 
472  JsonObject status = doc.createNestedObject ("status");
473  if (temperature > -100) {
474  status["temp"] = temperature;
475  }
477  status["mem"] = ESP.getFreeHeap ();
478 
479  len = measureJson (doc) + 1;
480  payload = (char*)malloc (len);
481  serializeJson (doc, (char*)payload, len);
482  char addr[] = "gateway";
483  GwOutput.outputDataSend (addr, payload, len - 1);
484  free (payload);
485 }
486 #endif // MEAS_TEMP
487 
488 void loop () {
489  GwOutput.loop ();
491  ArduinoOTA.handle ();
492 
493 #ifdef MEAS_TEMP
494  static bool tempRequested = false;
495  static time_t lastTempTime = 0;
496 
497  if (ds18b20.validAddress (dsAddress)) {
498  if ((millis () - lastTempTime > statusPeriod && !tempRequested) || !lastTempTime) {
499  if (ds18b20.requestTemperaturesByIndex (0)) {
500  DEBUG_INFO ("Temperature requested");
501  lastTempTime = millis ();
502  tempRequested = true;
503  } else {
504  DEBUG_WARN ("Temperature request error");
505  }
506  }
507  if (tempRequested) {
508  if (ds18b20.isConversionComplete ()) {
509  temperature = ds18b20.getTempC (dsAddress);
510  sendStatus (temperature);
511  DEBUG_WARN ("Temperature: %f", temperature);
512  tempRequested = false;
513  }
514  }
515  }
516 #endif // MEAS_TEMP
517 
518  if (restartRequested) {
519  if (millis () - restartRequestTime > 100) {
520  ESP.restart ();
521  }
522  }
523 }
EnigmaIOTGatewayClass::onWiFiManagerExit
void onWiFiManagerExit(onWiFiManagerExit_t handle)
Register callback to be called on wifi manager exit.
Definition: EnigmaIOTGateway.h:378
EnigmaIOTGatewayClass::getActiveNodesNumber
int getActiveNodesNumber()
Gets number of active nodes.
Definition: EnigmaIOTGateway.h:592
VERSION_ANS
@ VERSION_ANS
Definition: NodeList.h:53
GatewayOutput_dummy::loadConfig
bool loadConfig()
Loads output module configuration.
Definition: GwOutput_dummy.cpp:39
NodeList::getNodeFromName
Node * getNodeFromName(const char *name)
Gets node that correspond with given node name.
Definition: NodeList.cpp:174
USERDATA_SET
@ USERDATA_SET
Definition: NodeList.h:71
ENIGMAIOT_ADDR_LEN
static const size_t ENIGMAIOT_ADDR_LEN
Address size. Mac address = 6 bytes.
Definition: EnigmaIoTconfigAdvanced.h:23
EnigmaIOTGatewayClass::onNodeDisconnected
void onNodeDisconnected(onNodeDisconnected_t handler)
Defines a function callback that will be called every time a node is disconnected.
Definition: EnigmaIOTGateway.h:557
GatewayOutput_dummy::configManagerStart
void configManagerStart(EnigmaIOTGatewayClass *enigmaIotGw)
Called when wifi manager starts config portal.
Definition: GwOutput_dummy.cpp:31
EnigmaIOTGatewayClass::onDataRx
void onDataRx(onGwDataRx_t handler)
Defines a function callback that will be called on every downlink data message that is received from ...
Definition: EnigmaIOTGateway.h:449
EnigmaIOTGatewayClass::onHADiscovery
void onHADiscovery(onHADiscovery_t handler)
Defines a function callback that will be called when a Home Assistant discovery message is received f...
Definition: EnigmaIOTGateway.h:458
MAX_DATA_PAYLOAD_SIZE
static const int MAX_DATA_PAYLOAD_SIZE
Maximun payload size for data packets.
Definition: EnigmaIoTconfigAdvanced.h:48
GatewayOutput_dummy::nodeDisconnectedSend
bool nodeDisconnectedSend(char *address, gwInvalidateReason_t reason)
Send node disconnection notification.
Definition: GwOutput_dummy.cpp:72
flashConnectionLed
void flashConnectionLed(void *led)
Definition: EnigmaIOTGatewayMQTT.cpp:76
EnigmaIOTGatewayClass::configWiFiManager
bool configWiFiManager()
Starts configuration AP and web server and gets settings from it.
Definition: EnigmaIOTGateway.cpp:494
stopConnectionFlash
void stopConnectionFlash()
Definition: EnigmaIOTGatewayMQTT.cpp:97
RAW
@ RAW
Definition: EnigmaIOTGateway.h:62
EnigmaIOTGatewayClass::getNetworkName
char * getNetworkName()
Gets EnigmaIOT network name.
Definition: EnigmaIOTGateway.h:349
newNodeConnected
void newNodeConnected(uint8_t *mac, uint16_t node_id, char *nodeName=nullptr)
Definition: EnigmaIOTGatewayMQTT.cpp:330
GwOutput
GatewayOutput_dummy GwOutput
Definition: GwOutput_dummy.cpp:29
connectionLed
const int connectionLed
Definition: EnigmaIOTGatewayMQTT.cpp:69
GatewayOutput_dummy::begin
bool begin()
Starts output module.
Definition: GwOutput_dummy.cpp:47
LED_OFF
#define LED_OFF
Definition: enigmaiot_led_flasher.cpp:40
nodeDisconnected
void nodeDisconnected(uint8_t *mac, gwInvalidateReason_t reason)
Definition: EnigmaIOTGatewayMQTT.cpp:352
ENIGMAIOT
@ ENIGMAIOT
Definition: EnigmaIOTGateway.h:69
wifiManagerStarted
void wifiManagerStarted()
Definition: EnigmaIOTGatewayMQTT.cpp:167
EnigmaIOTGatewayClass::setRxLed
void setRxLed(uint8_t led, time_t onTime=FLASH_LED_TIME)
Sets a LED to be flashed every time a message is received.
Definition: EnigmaIOTGateway.cpp:66
EnigmaIOTGatewayClass::handle
void handle()
This method should be called periodically for instance inside loop() function. It is used for interna...
Definition: EnigmaIOTGateway.cpp:867
Node::setVersion
void setVersion(uint8_t major, uint8_t minor, uint8_t incremental)
Sets node EnigmaIOT version.
Definition: NodeList.h:459
connectionLedFlashing
bool connectionLedFlashing
Definition: EnigmaIOTGatewayMQTT.cpp:70
EnigmaIOTGatewayClass::setTxLed
void setTxLed(uint8_t led, time_t onTime=FLASH_LED_TIME)
Sets a LED to be flashed every time a message is transmitted.
Definition: EnigmaIOTGateway.cpp:59
BLUE_LED
#define BLUE_LED
Definition: EnigmaIOTGatewayMQTT.cpp:60
restartRequestTime
time_t restartRequestTime
Definition: EnigmaIOTGatewayMQTT.cpp:73
GatewayOutput_dummy::loop
void loop()
Should be called often for module management.
Definition: GwOutput_dummy.cpp:53
doRestart
void doRestart()
Definition: EnigmaIOTGatewayMQTT.cpp:184
arduinoOTAConfigure
void arduinoOTAConfigure()
Definition: EnigmaIOTGatewayMQTT.cpp:113
USERDATA_GET
@ USERDATA_GET
Definition: NodeList.h:70
gatewayPayloadEncoding_t
gatewayPayloadEncoding_t
Definition: EnigmaIOTGateway.h:61
EnigmaIOTGatewayClass::getTotalPackets
uint32_t getTotalPackets(uint8_t *address)
Gets total packets sent by node that has a specific address.
Definition: EnigmaIOTGateway.cpp:1470
EnigmaIOTGatewayClass::onNewNode
void onNewNode(onNewNode_t handler)
Defines a function callback that will be called every time a node gets connected or reconnected.
Definition: EnigmaIOTGateway.h:528
processRxData
void processRxData(uint8_t *mac, uint8_t *buffer, uint8_t length, uint16_t lostMessages, bool control, gatewayPayloadEncoding_t payload_type, char *nodeName=NULL)
Definition: EnigmaIOTGatewayMQTT.cpp:213
EnigmaIOTGatewayClass::getNodes
NodeList * getNodes()
Gets nodes data structure.
Definition: EnigmaIOTGateway.h:600
EnigmaIOTGateway
EnigmaIOTGatewayClass EnigmaIOTGateway
Definition: EnigmaIOTGateway.cpp:2050
GatewayOutput_dummy::newNodeSend
bool newNodeSend(char *address, uint16_t node_id)
Send new node notification.
Definition: GwOutput_dummy.cpp:67
EnigmaIOTGatewayClass::onWiFiManagerStarted
void onWiFiManagerStarted(simpleEventHandler_t handle)
Register callback to be called on wifi manager start.
Definition: EnigmaIOTGateway.h:386
MSG_PACK
@ MSG_PACK
Definition: EnigmaIOTGateway.h:65
GwOutput_mqtt.h
MQTT Gateway output module.
processRxControlData
void processRxControlData(char *macStr, uint8_t *data, uint8_t length)
Definition: EnigmaIOTGatewayMQTT.cpp:171
MACSTR
#define MACSTR
Definition: helperFunctions.cpp:83
Node
Class definition for a single sensor Node.
Definition: NodeList.h:109
GatewayOutput_dummy::outputControlSend
bool outputControlSend(char *address, uint8_t *data, size_t length)
Send control data from nodes.
Definition: GwOutput_dummy.cpp:62
GatewayOutput_dummy::configManagerExit
void configManagerExit(bool status)
Called when wifi manager exits config portal.
Definition: GwOutput_dummy.cpp:43
setup
void setup()
Definition: EnigmaIOTGatewayMQTT.cpp:382
wifiManagerExit
void wifiManagerExit(boolean status)
Definition: EnigmaIOTGatewayMQTT.cpp:163
loop
void loop()
Definition: EnigmaIOTGatewayMQTT.cpp:488
data
@ data
Definition: GwOutput_generic.h:23
Espnow_hal
Espnow_halClass Espnow_hal
Singleton instance of ESP-NOW class.
Definition: espnow_hal.cpp:20
EnigmaIOTGatewayClass::sendDownstream
bool sendDownstream(uint8_t *mac, const uint8_t *data, size_t len, control_message_type_t controlData, gatewayPayloadEncoding_t payload_type=RAW, char *nodeName=NULL)
Starts a downstream data message transmission.
Definition: EnigmaIOTGateway.cpp:364
EnigmaIOTGatewayClass::getErrorPackets
uint32_t getErrorPackets(uint8_t *address)
Gets number of errored packets of node that has a specific address.
Definition: EnigmaIOTGateway.cpp:1476
lostmessages
@ lostmessages
Definition: GwOutput_generic.h:24
LED_BUILTIN
#define LED_BUILTIN
Definition: EnigmaIOTGatewayMQTT.cpp:57
onDownlinkData
void onDownlinkData(uint8_t *address, char *nodeName, control_message_type_t msgType, char *data, unsigned int len)
Definition: EnigmaIOTGatewayMQTT.cpp:281
EnigmaIOTGatewayClass::onGatewayRestartRequested
void onGatewayRestartRequested(simpleEventHandler_t handler)
Defines a function callback that will process a gateway restart request.
Definition: EnigmaIOTGateway.h:565
startConnectionFlash
void startConnectionFlash(int period)
Definition: EnigmaIOTGatewayMQTT.cpp:81
EnigmaIOTGatewayClass::getPacketsHour
double getPacketsHour(uint8_t *address)
Gets packet rate sent by node that has a specific address, in packets per hour.
Definition: EnigmaIOTGateway.cpp:1482
restartRequested
bool restartRequested
Definition: EnigmaIOTGatewayMQTT.cpp:72
mac2str
char * mac2str(const uint8_t *mac, char *extBuffer)
Debug helper function that generates a string that represent a MAC address.
Definition: helperFunctions.cpp:85
helperFunctions.h
Auxiliary function definition.
GatewayOutput_dummy::outputDataSend
bool outputDataSend(char *address, char *data, size_t length, GwOutput_data_type_t type=data)
Send data from nodes.
Definition: GwOutput_dummy.cpp:57
EnigmaIOTGateway.h
Library to build a gateway for EnigmaIoT system.
gwInvalidateReason_t
gwInvalidateReason_t
Key invalidation reason definition.
Definition: EnigmaIOTGateway.h:75
EnigmaIOTdebug.h
Auxiliary functions for debugging over Serial.
control_message_type_t
enum control_message_type control_message_type_t
LED_ON
#define LED_ON
Definition: enigmaiot_led_flasher.cpp:39
EnigmaIOTGatewayClass::getPER
double getPER(uint8_t *address)
Gets packet error rate of node that has a specific address.
Definition: EnigmaIOTGateway.cpp:1460
CAYENNELPP
@ CAYENNELPP
Definition: EnigmaIOTGateway.h:63
RED_LED
#define RED_LED
Definition: EnigmaIOTGatewayMQTT.cpp:61
EnigmaIOTGatewayClass::getNetworkKey
char * getNetworkKey(bool plain=false)
Gets hashed EnigmaIOT network key.
Definition: EnigmaIOTGateway.h:357
EnigmaIOTGatewayClass::begin
void begin(Comms_halClass *comm, uint8_t *networkKey=NULL, bool useDataCounter=true)
Initalizes communication basic data and starts accepting node registration.
Definition: EnigmaIOTGateway.cpp:736
espnow_hal.h
ESP-NOW communication system abstraction layer. To be used on ESP8266 or ESP32 platforms.
status
@ status
Definition: GwOutput_generic.h:25
GatewayOutput_dummy::setDlCallback
void setDlCallback(onDlData_t cb)
Set data processing function.
Definition: GwOutput_dummy.h:108