 |
EnigmaIOT
0.9.3
Secure sensor and gateway platform based on ESP8266 and ESP32
|
Go to the documentation of this file.
11 #if !defined ESP8266 && !defined ESP32
12 #error Node only supports ESP8266 or ESP32 platform
18 #include <CayenneLPP.h>
21 #include <ESP8266WiFi.h>
22 #include <ESP8266HTTPClient.h>
23 #include <ESP8266httpUpdate.h>
24 #include <ESPAsyncTCP.h>
31 #include <driver/adc.h>
34 #include "soc/rtc_cntl_reg.h"
36 #include <ArduinoJson.h>
37 #include <Curve25519.h>
38 #include <ESPAsyncWebServer.h>
39 #include <ESPAsyncWiFiManager.h>
40 #include <DNSServer.h>
44 #define LED_BUILTIN 2 // ESP32 boards normally have a LED in GPIO3 or GPIO5
45 #endif // !LED_BUILTIN
47 #define BLUE_LED LED_BUILTIN
55 Serial.println (
"Connected");
59 Serial.printf (
"Unregistered. Reason: %d\n", reason);
69 Serial.printf (
"Data from %s --> %s\n", macstr,
printHexBuffer (buffer, length));
77 Serial.printf (
"Command %s\n", commandStr.c_str ());
79 Serial.printf (
"Encoding: 0x%02X\n", payloadEncoding);
82 DynamicJsonDocument doc (1000);
84 memcpy (tempBuffer, buffer, length);
86 switch (payloadEncoding) {
88 root = doc.createNestedArray ();
89 lpp.decode (tempBuffer, length, root);
90 serializeJsonPretty (doc, Serial);
93 deserializeMsgPack (doc, tempBuffer, length);
94 serializeJsonPretty (doc, Serial);
97 DEBUG_WARN (
"Payload encoding %d is not (yet) supported", payloadEncoding);
103 Serial.begin (115200); Serial.println (); Serial.println ();
108 WRITE_PERI_REG (RTC_CNTL_BROWN_OUT_REG, 0);
124 if (wifi_get_macaddr (STATION_IF, macAddress))
126 if ((esp_wifi_get_mac (WIFI_IF_STA, macAddress) == ESP_OK))
131 DEBUG_DBG (
"Node address set to %s",
mac2str (macAddress, macStr));
133 DEBUG_WARN (
"Node address error");
139 const char* TZINFO =
"CET-1CEST-2,M3.5.0/02:00:00,M10.5.0/03:00:00";
142 static time_t displayTime;
145 setenv (
"TZ", TZINFO, 1);
146 displayTime = millis ();
150 localtime_r (&local_time, &timeinfo);
153 Serial.printf (
"%02d/%02d/%04d %02d:%02d:%02d\n",
156 timeinfo.tm_year + 1900,
161 Serial.printf (
"Time not sync'ed\n");
172 static time_t lastSensorData;
173 static const time_t SENSOR_PERIOD = 10000;
174 if (millis () - lastSensorData > SENSOR_PERIOD) {
175 lastSensorData = millis ();
179 msg.addAnalogInput (0, (
float)(ESP.getVcc ()) / 1000);
180 Serial.printf (
"Vcc: %f\n", (
float)(ESP.getVcc ()) / 1000);
182 msg.addAnalogInput (0, (
float)(analogRead (ADC1_CHANNEL_0_GPIO_NUM) * 3.6 / 4096));
183 Serial.printf (
"Vcc: %f V\n", (
float)(analogRead (ADC1_CHANNEL_0_GPIO_NUM) * 3.6 / 4096));
185 msg.addTemperature (1, 20.34);
188 Serial.printf (
"Trying to send: %s\n",
printHexBuffer (msg.getBuffer (), msg.getSize ()));
191 Serial.println (
"---- Error sending data");
193 Serial.println (
"---- Data sent");
time_t unixtime()
Gets current time in seconds from 1970, if time is synchronized.
bool hasClockSync()
Checks if internal clock is synchronized to gateway.
void enableClockSync(bool clockSync=true)
Controls clock synchronization function.
bool sendData(const uint8_t *data, size_t len, bool controlMessage, bool encrypt=true, nodePayloadEncoding_t payloadEncoding=CAYENNELPP)
Initiades data transmission distinguissing if it is payload or control data.
nodeMessageType
Message code definition.
static const int MAX_DATA_PAYLOAD_SIZE
Maximun payload size for data packets.
void setResetPin(int pin)
Sets a pin to be used to reset configuration it it is connected to ground during startup.
void processRxData(const uint8_t *mac, const uint8_t *buffer, uint8_t length, nodeMessageType_t command, nodePayloadEncoding_t payloadEncoding)
void connectEventHandler()
static const size_t ENIGMAIOT_ADDR_LEN
Address size. Mac address = 6 bytes.
void handle()
This method should be called periodically for instance inside loop() function. It is used for interna...
char * printHexBuffer(const uint8_t *buffer, uint16_t len)
Debug helper function that generates a string that represent a buffer hexadecimal values.
EnigmaIOTNodeClass EnigmaIOTNode
char * mac2str(const uint8_t *mac, char *buffer)
Debug helper function that generates a string that represent a MAC address.
void onDisconnected(onDisconnected_t handler)
Defines a function callback that will be called everytime node is disconnected from gateway.
static const uint8_t MAX_MESSAGE_LENGTH
Maximum payload size on ESP-NOW.
Library to build a node for EnigmaIoT system.
void onConnected(onConnected_t handler)
Defines a function callback that will be called everytime node is registered on gateway.
void begin(Comms_halClass *comm, uint8_t *gateway=NULL, uint8_t *networkKey=NULL, bool useCounter=true, bool sleepy=true)
Initalizes communication basic data and starts node registration.
void setLed(uint8_t led, time_t onTime=FLASH_LED_TIME)
Sets a LED to be flashed every time a message is transmitted.
Espnow_halClass Espnow_hal
Singleton instance of ESP-NOW class.
bool setNodeAddress(uint8_t address[ENIGMAIOT_ADDR_LEN])
Set node address to be used in EnigmaIOT communication.
nodeInvalidateReason_t
Key invalidation reason definition.
int64_t clock()
Gets current clock counter. millis() + offset.
void disconnectEventHandler(nodeInvalidateReason_t reason)
ESP-NOW communication system abstraction layer. To be used on ESP8266 or ESP32 platforms.
void onDataRx(onNodeDataRx_t handler)
Defines a function callback that will be called on every downlink data message that is received from ...