|
EnigmaIOT
0.9.8
Secure sensor and gateway platform based on ESP8266 and ESP32
|
Go to the documentation of this file.
20 #include "esp_system.h"
21 #include "esp_event.h"
24 #include "soc/rtc_cntl_reg.h"
25 #elif defined(ESP8266)
26 #include <ESP8266WiFi.h>
27 #include <ESPAsyncTCP.h>
33 #include <CayenneLPP.h>
40 #include <Curve25519.h>
41 #include <ChaChaPoly.h>
44 #include <ArduinoJson.h>
45 #include <DNSServer.h>
46 #include <ESPAsyncWebServer.h>
47 #include <ESPAsyncWiFiManager.h>
53 #define BLUE_LED BUILTIN_LED
54 #define RED_LED BUILTIN_LED
57 TimerHandle_t connectionLedTimer;
58 #elif defined(ESP8266)
59 ETSTimer connectionLedTimer;
74 connectionLedTimer = xTimerCreate (
"led_flash", pdMS_TO_TICKS (period), pdTRUE, (
void*)0,
flashConnectionLed);
75 xTimerStart (connectionLedTimer, 0);
77 #elif defined (ESP8266)
78 ets_timer_disarm (&connectionLedTimer);
81 ets_timer_arm_new (&connectionLedTimer, period,
true,
true);
90 xTimerStop (connectionLedTimer, 0);
91 xTimerDelete (connectionLedTimer, 0);
93 #elif defined(ESP8266)
96 ets_timer_disarm (&connectionLedTimer);
119 const int PAYLOAD_SIZE = 512;
120 char payload[PAYLOAD_SIZE];
132 const int capacity = JSON_ARRAY_SIZE (25) + 25 * JSON_OBJECT_SIZE (4);
133 DynamicJsonDocument jsonBuffer (capacity);
134 JsonArray root = jsonBuffer.createNestedArray ();
137 cayennelpp.decode ((uint8_t*)buffer, length, root);
138 uint8_t error = cayennelpp.getError ();
139 if (error != LPP_ERROR_OK) {
140 DEBUG_ERROR (
"Error decoding CayenneLPP data: %d", error);
143 pld_size = serializeJson (root, payload, PAYLOAD_SIZE);
144 }
else if (payload_type ==
MSG_PACK) {
145 const int capacity = JSON_ARRAY_SIZE (25) + 25 * JSON_OBJECT_SIZE (4);
146 DynamicJsonDocument jsonBuffer (capacity);
147 DeserializationError error = deserializeMsgPack (jsonBuffer, buffer, length);
148 if (error != DeserializationError::Ok) {
149 DEBUG_ERROR (
"Error decoding MSG Pack data: %s", error.c_str ());
152 pld_size = serializeJson (jsonBuffer, payload, PAYLOAD_SIZE);
153 }
else if (payload_type ==
RAW) {
154 if (length <= PAYLOAD_SIZE) {
155 memcpy (payload, buffer, length);
158 memcpy (payload, buffer, PAYLOAD_SIZE);
159 pld_size = PAYLOAD_SIZE;
164 DEBUG_INFO (
"Published data message from %s: %s", mac_str, payload);
165 if (lostMessages > 0) {
166 pld_size = snprintf (payload, PAYLOAD_SIZE,
"%u", lostMessages);
170 pld_size = snprintf (payload, PAYLOAD_SIZE,
"{\"per\":%e,\"lostmessages\":%u,\"totalmessages\":%u,\"packetshour\":%.2f}",
183 unsigned int bufferLen = len;
186 DEBUG_INFO (
"DL Message for %s. Type 0x%02X", nodeName, msgType);
188 DEBUG_INFO (
"DL Message for " MACSTR ". Type 0x%02X", MAC2STR (address), msgType);
190 DEBUG_DBG (
"Data: %.*s", len,
data);
193 sprintf (buffer,
"%.*s", len,
data);
197 DEBUG_ERROR (
"Error sending esp_now message to " MACSTR, MAC2STR (address));
199 DEBUG_DBG (
"Esp-now message sent or queued correctly");
208 DEBUG_WARN (
"Error sending new node %s", nodeName);
210 DEBUG_DBG (
"New node %s message sent", nodeName);
216 DEBUG_WARN (
"Error sending new node %s", macstr);
218 DEBUG_DBG (
"New node %s message sent", macstr);
229 DEBUG_WARN (
"Error sending node disconnected %s reason %d", macstr, reason);
231 DEBUG_DBG (
"Node %s disconnected message sent. Reason %d", macstr, reason);
250 TaskHandle_t xEnigmaIOTGateway_handle = NULL;
251 TaskHandle_t gwoutput_handle = NULL;
255 Serial.begin (115200); Serial.println (); Serial.println ();
260 WRITE_PERI_REG (RTC_CNTL_BROWN_OUT_REG, 0);
274 DEBUG_WARN (
"Error reading config file");
286 #if CONNECT_TO_WIFI_AP == 1
287 WiFi.mode (WIFI_AP_STA);
292 #endif // CONNECT_TO_WIFI_AP
298 DEBUG_INFO (
"STA MAC Address: %s", WiFi.macAddress ().c_str ());
299 DEBUG_INFO (
"AP MAC Address: %s", WiFi.softAPmacAddress ().c_str ());
300 DEBUG_INFO (
"BSSID Address: %s", WiFi.BSSIDstr ().c_str ());
302 DEBUG_INFO (
"IP address: %s", WiFi.localIP ().toString ().c_str ());
303 DEBUG_INFO (
"AP IP address: %s", WiFi.softAPIP ().toString ().c_str ());
304 DEBUG_INFO (
"WiFi Channel: %d", WiFi.channel ());
306 DEBUG_INFO (
"WiFi SSID: %s", WiFi.SSID ().c_str ());
void onWiFiManagerExit(onWiFiManagerExit_t handle)
Register callback to be called on wifi manager exit.
bool loadConfig()
Loads output module configuration.
static const size_t ENIGMAIOT_ADDR_LEN
Address size. Mac address = 6 bytes.
void onNodeDisconnected(onNodeDisconnected_t handler)
Defines a function callback that will be called every time a node is disconnected.
void configManagerStart(EnigmaIOTGatewayClass *enigmaIotGw)
Called when wifi manager starts config portal.
void onDataRx(onGwDataRx_t handler)
Defines a function callback that will be called on every downlink data message that is received from ...
Generic Gateway output module template.
static const int MAX_DATA_PAYLOAD_SIZE
Maximun payload size for data packets.
bool nodeDisconnectedSend(char *address, gwInvalidateReason_t reason)
Send node disconnection notification.
bool configWiFiManager()
Starts configuration AP and web server and gets settings from it.
char * getNetworkName()
Gets EnigmaIOT network name.
GatewayOutput_dummy GwOutput
bool begin()
Starts output module.
void wifiManagerExit(boolean status)
void nodeDisconnected(uint8_t *mac, gwInvalidateReason_t reason)
void setRxLed(uint8_t led, time_t onTime=FLASH_LED_TIME)
Sets a LED to be flashed every time a message is received.
void handle()
This method should be called periodically for instance inside loop() function. It is used for interna...
void setTxLed(uint8_t led, time_t onTime=FLASH_LED_TIME)
Sets a LED to be flashed every time a message is transmitted.
void wifiManagerStarted()
void loop()
Should be called often for module management.
boolean connectionLedFlashing
void onDownlinkData(uint8_t *address, char *nodeName, control_message_type_t msgType, char *data, unsigned int len)
uint32_t getTotalPackets(uint8_t *address)
Gets total packets sent by node that has a specific address.
void onNewNode(onNewNode_t handler)
Defines a function callback that will be called every time a node gets connected or reconnected.
EnigmaIOTGatewayClass EnigmaIOTGateway
bool newNodeSend(char *address, uint16_t node_id)
Send new node notification.
void processRxData(uint8_t *mac, uint8_t *buffer, uint8_t length, uint16_t lostMessages, bool control, gatewayPayloadEncoding_t payload_type, char *nodeName=NULL)
void onWiFiManagerStarted(simpleEventHandler_t handle)
Register callback to be called on wifi manager start.
bool outputControlSend(char *address, uint8_t *data, size_t length)
Send control data from nodes.
void configManagerExit(bool status)
Called when wifi manager exits config portal.
void newNodeConnected(uint8_t *mac, uint16_t node_id, char *nodeName=NULL)
Espnow_halClass Espnow_hal
Singleton instance of ESP-NOW class.
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.
uint32_t getErrorPackets(uint8_t *address)
Gets number of errored packets of node that has a specific address.
double getPacketsHour(uint8_t *address)
Gets packet rate sent by node that has a specific address, in packets per hour.
char * mac2str(const uint8_t *mac, char *extBuffer)
Debug helper function that generates a string that represent a MAC address.
Auxiliary function definition.
bool outputDataSend(char *address, char *data, size_t length, GwOutput_data_type_t type=data)
Send data from nodes.
Library to build a gateway for EnigmaIoT system.
gwInvalidateReason_t
Key invalidation reason definition.
Auxiliary functions for debugging over Serial.
enum control_message_type control_message_type_t
void flashConnectionLed(void *led)
double getPER(uint8_t *address)
Gets packet error rate of node that has a specific address.
void stopConnectionFlash()
char * getNetworkKey(bool plain=false)
Gets hashed EnigmaIOT network key.
void begin(Comms_halClass *comm, uint8_t *networkKey=NULL, bool useDataCounter=true)
Initalizes communication basic data and starts accepting node registration.
ESP-NOW communication system abstraction layer. To be used on ESP8266 or ESP32 platforms.
void processRxControlData(char *macStr, uint8_t *data, uint8_t length)
Dummy Gateway output module.
void startConnectionFlash(int period)
void setDlCallback(onDlData_t cb)
Set data processing function.