 |
EnigmaIOT
0.9.3
Secure sensor and gateway platform based on ESP8266 and ESP32
|
Go to the documentation of this file.
19 #include "esp_system.h"
20 #include "esp_event.h"
23 #include "soc/rtc_cntl_reg.h"
24 #elif defined(ESP8266)
25 #include <ESP8266WiFi.h>
32 #include <CayenneLPP.h>
39 #include <Curve25519.h>
40 #include <ChaChaPoly.h>
43 #include <ArduinoJson.h>
44 #include <DNSServer.h>
45 #include <ESPAsyncWebServer.h>
46 #include <ESPAsyncWiFiManager.h>
52 #define BLUE_LED BUILTIN_LED
53 #define RED_LED BUILTIN_LED
56 TimerHandle_t connectionLedTimer;
57 #elif defined(ESP8266)
58 ETSTimer connectionLedTimer;
73 connectionLedTimer = xTimerCreate (
"led_flash", pdMS_TO_TICKS (period), pdTRUE, (
void*)0,
flashConnectionLed);
74 xTimerStart (connectionLedTimer, 0);
76 #elif defined (ESP8266)
77 ets_timer_disarm (&connectionLedTimer);
80 ets_timer_arm_new (&connectionLedTimer, period,
true,
true);
89 xTimerStop (connectionLedTimer, 0);
90 xTimerDelete (connectionLedTimer, 0);
92 #elif defined(ESP8266)
95 ets_timer_disarm (&connectionLedTimer);
119 const int PAYLOAD_SIZE = 512;
121 payload = (
char*)malloc (PAYLOAD_SIZE);
131 const int capacity = JSON_ARRAY_SIZE (25) + 25 * JSON_OBJECT_SIZE (4);
132 DynamicJsonDocument jsonBuffer (capacity);
133 JsonArray root = jsonBuffer.createNestedArray ();
136 cayennelpp.decode ((uint8_t*)buffer, length, root);
137 uint8_t error = cayennelpp.getError ();
138 if (error != LPP_ERROR_OK) {
139 DEBUG_ERROR (
"Error decoding CayenneLPP data: %d", error);
142 pld_size = serializeJson (root, payload, PAYLOAD_SIZE);
143 }
else if (payload_type ==
MSG_PACK) {
144 const int capacity = JSON_ARRAY_SIZE (25) + 25 * JSON_OBJECT_SIZE (4);
145 DynamicJsonDocument jsonBuffer (capacity);
146 DeserializationError error = deserializeMsgPack (jsonBuffer, buffer, length);
147 if (error != DeserializationError::Ok) {
148 DEBUG_ERROR (
"Error decoding MSG Pack data: %s", error.c_str ());
151 pld_size = serializeJson (jsonBuffer, payload, PAYLOAD_SIZE);
152 }
else if (payload_type ==
RAW) {
153 if (length <= PAYLOAD_SIZE) {
154 memcpy (payload, buffer, length);
157 memcpy (payload, buffer, PAYLOAD_SIZE);
158 pld_size = PAYLOAD_SIZE;
163 DEBUG_INFO (
"Published data message from %s: %s", mac_str, payload);
164 if (lostMessages > 0) {
165 pld_size = snprintf (payload, PAYLOAD_SIZE,
"%u", lostMessages);
169 pld_size = snprintf (payload, PAYLOAD_SIZE,
"{\"per\":%e,\"lostmessages\":%u,\"totalmessages\":%u,\"packetshour\":%.2f}",
181 unsigned int bufferLen = len;
184 DEBUG_INFO (
"DL Message for %s. Type 0x%02X", nodeName, msgType);
186 DEBUG_INFO (
"DL Message for " MACSTR ". Type 0x%02X", MAC2STR (address), msgType);
188 DEBUG_DBG (
"Data: %.*s", len,
data);
190 buffer = (
char*)malloc (len + 1);
191 sprintf (buffer,
"%.*s", len,
data);
195 DEBUG_ERROR (
"Error sending esp_now message to " MACSTR, MAC2STR (address));
197 DEBUG_DBG (
"Esp-now message sent or queued correctly");
206 DEBUG_WARN (
"Error sending new node %s", nodeName);
208 DEBUG_DBG (
"New node %s message sent", nodeName);
214 DEBUG_WARN (
"Error sending new node %s", macstr);
216 DEBUG_DBG (
"New node %s message sent", macstr);
227 DEBUG_WARN (
"Error sending node disconnected %s reason %d", macstr, reason);
229 DEBUG_DBG (
"Node %s disconnected message sent. Reason %d", macstr, reason);
234 void EnigmaIOTGateway_handle (
void * param) {
241 void GwOutput_handle (
void* param) {
248 TaskHandle_t xEnigmaIOTGateway_handle = NULL;
249 TaskHandle_t gwoutput_handle = NULL;
253 Serial.begin (115200); Serial.println (); Serial.println ();
258 WRITE_PERI_REG (RTC_CNTL_BROWN_OUT_REG, 0);
272 DEBUG_WARN (
"Error reading config file");
284 #if CONNECT_TO_WIFI_AP == 1
285 WiFi.mode (WIFI_AP_STA);
290 #endif // CONNECT_TO_WIFI_AP
296 DEBUG_INFO (
"STA MAC Address: %s", WiFi.macAddress ().c_str());
297 DEBUG_INFO (
"AP MAC Address: %s", WiFi.softAPmacAddress().c_str ());
298 DEBUG_INFO (
"BSSID Address: %s", WiFi.BSSIDstr().c_str ());
300 DEBUG_INFO (
"IP address: %s", WiFi.localIP ().toString ().c_str ());
301 DEBUG_INFO (
"AP IP address: %s", WiFi.softAPIP ().toString ().c_str ());
302 DEBUG_INFO (
"WiFi Channel: %d", WiFi.channel ());
304 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.
void processRxControlData(char *macStr, uint8_t *data, uint8_t length)
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 ...
static const int MAX_DATA_PAYLOAD_SIZE
Maximun payload size for data packets.
Generic Gateway output module template.
void stopConnectionFlash()
bool nodeDisconnectedSend(char *address, gwInvalidateReason_t reason)
Send node disconnection notification.
bool configWiFiManager()
Starts configuration AP and web server and gets settings from it.
static const size_t ENIGMAIOT_ADDR_LEN
Address size. Mac address = 6 bytes.
char * getNetworkName()
Gets EnigmaIOT network name.
GatewayOutput_dummy GwOutput
boolean connectionLedFlashing
bool begin()
Starts output module.
void onDownlinkData(uint8_t *address, char *nodeName, control_message_type_t msgType, char *data, unsigned int len)
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 processRxData(uint8_t *mac, uint8_t *buffer, uint8_t length, uint16_t lostMessages, bool control, gatewayPayloadEncoding_t payload_type, char *nodeName=NULL)
void setTxLed(uint8_t led, time_t onTime=FLASH_LED_TIME)
Sets a LED to be flashed every time a message is transmitted.
void loop()
Should be called often for module management.
char * mac2str(const uint8_t *mac, char *buffer)
Debug helper function that generates a string that represent a MAC address.
void nodeDisconnected(uint8_t *mac, gwInvalidateReason_t reason)
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.
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 wifiManagerStarted()
void flashConnectionLed(void *led)
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.
Auxiliary function definition.
bool outputDataSend(char *address, char *data, size_t length, GwOutput_data_type_t type=data)
Send data from nodes.
void wifiManagerExit(boolean status)
Library to build a gateway for EnigmaIoT system.
gwInvalidateReason_t
Key invalidation reason definition.
enum control_message_type control_message_type_t
double getPER(uint8_t *address)
Gets packet error rate of node that has a specific address.
void startConnectionFlash(int period)
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.
void onWiFiManagerStarted(onWiFiManagerStarted_t handle)
Register callback to be called on wifi manager start.
ESP-NOW communication system abstraction layer. To be used on ESP8266 or ESP32 platforms.
Dummy Gateway output module.
Auxiliary functions for debugging over Serial.
void setDlCallback(onDlData_t cb)
Set data processing function.
void newNodeConnected(uint8_t *mac, uint16_t node_id, char *nodeName=NULL)