 |
EnigmaIOT
0.9.3
Secure sensor and gateway platform based on ESP8266 and ESP32
|
Go to the documentation of this file.
14 #include <CayenneLPP.h>
17 #include <ESP8266WiFi.h>
18 #include <ESP8266HTTPClient.h>
19 #include <ESP8266httpUpdate.h>
20 #include <ESPAsyncTCP.h>
27 #include <driver/adc.h>
30 #include "soc/rtc_cntl_reg.h"
32 #include <ArduinoJson.h>
33 #include <Curve25519.h>
34 #include <ESPAsyncWebServer.h>
35 #include <ESPAsyncWiFiManager.h>
36 #include <DNSServer.h>
40 #define LED_BUILTIN 2 // ESP32 boards normally have a LED in GPIO2 or GPIO5
41 #endif // !LED_BUILTIN
43 #define BLUE_LED LED_BUILTIN
51 Serial.println (
"Registered");
55 Serial.printf (
"Unregistered. Reason: %d\n", reason);
65 Serial.printf (
"Data from %s\n", macstr);
73 Serial.printf (
"Command %s\n", commandStr.c_str ());
75 Serial.printf (
"Encoding: 0x%02X\n", encoding);
78 DynamicJsonDocument doc (1000);
80 memcpy (tempBuffer, buffer, length);
84 root = doc.createNestedArray ();
85 lpp.decode (tempBuffer, length, root);
86 serializeJsonPretty (doc, Serial);
89 deserializeMsgPack (doc, tempBuffer, length);
90 serializeJsonPretty (doc, Serial);
93 DEBUG_WARN(
"Non supported encoding; %d", encoding);
99 Serial.begin (115200); Serial.println (); Serial.println ();
100 time_t start = millis ();
105 WRITE_PERI_REG (RTC_CNTL_BROWN_OUT_REG, 0);
118 if (wifi_get_macaddr (STATION_IF, macAddress))
120 if ((esp_wifi_get_mac (WIFI_IF_STA, macAddress) == ESP_OK))
125 DEBUG_DBG (
"Node address set to %s",
mac2str (macAddress, macStr));
127 DEBUG_WARN (
"Node address error");
131 const size_t capacity = JSON_OBJECT_SIZE (5);
132 DynamicJsonDocument json (capacity);
135 json[
"V"] = (float)(ESP.getVcc ()) / 1000;
137 json[
"V"] = (float)(analogRead (ADC1_CHANNEL_0_GPIO_NUM) * 3.6 / 4096);
144 int len = measureMsgPack (json) + 1;
145 uint8_t* buffer = (uint8_t*)malloc (len);
146 len = serializeMsgPack (json, (
char*)buffer, len);
149 Serial.printf (
"Vcc: %f\n", (
float)(ESP.getVcc ()) / 1000);
151 Serial.printf (
"Vcc: %f\n", (
float)(analogRead (ADC1_CHANNEL_0_GPIO_NUM) * 3.6 / 4096));
153 Serial.printf (
"Message Len %d\n", len);
156 Serial.printf (
"Trying to send: %s\n",
printHexBuffer (buffer, len));
160 Serial.println (
"---- Error sending data");
162 Serial.println (
"---- Data sent");
164 Serial.printf (
"Total time: %lu ms\n", millis () - start);
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.
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
void sleep()
Requests transition to sleep mode (low energy state)
void disconnectEventHandler(nodeInvalidateReason_t reason)
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.
void processRxData(const uint8_t *mac, const uint8_t *buffer, uint8_t length, nodeMessageType_t command, nodePayloadEncoding_t encoding)
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.
void connectEventHandler()
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 ...