EnigmaIOT  0.9.8
Secure sensor and gateway platform based on ESP8266 and ESP32
enigmaiot_node_msgpack.cpp
Go to the documentation of this file.
1 
11 #include <Arduino.h>
12 #include <EnigmaIOTNode.h>
13 #include <espnow_hal.h>
14 #include <CayenneLPP.h>
15 
16 #ifdef ESP8266
17 #include <ESP8266WiFi.h>
18 #include <ESP8266HTTPClient.h>
19 #include <ESP8266httpUpdate.h>
20 #include <ESPAsyncTCP.h> // Comment to compile for ESP32
21 #include <Hash.h>
22 #elif defined ESP32
23 #include <WiFi.h>
24 #include <AsyncTCP.h> // Comment to compile for ESP8266
25 #include <SPIFFS.h>
26 #include <Update.h>
27 #include <driver/adc.h>
28 #include "esp_wifi.h"
29 #include "soc/soc.h" // Disable brownout problems
30 #include "soc/rtc_cntl_reg.h" // Disable brownout problems
31 #endif
32 #include <ArduinoJson.h>
33 #include <Curve25519.h>
34 #include <ESPAsyncWebServer.h>
35 #include <ESPAsyncWiFiManager.h>
36 #include <DNSServer.h>
37 #include <FS.h>
38 
39 #ifndef LED_BUILTIN
40 #ifdef ESP8266
41 #define LED_BUILTIN 2 // ESP8266 boards normally have a LED in GPIO2
42 #else
43 #define LED_BUILTIN 5 // ESP32 boards normally have a LED in GPIO2 or GPIO5
44 #endif
45 #endif // !LED_BUILTIN
46 
47 #define BLUE_LED LED_BUILTIN
48 constexpr auto RESET_PIN = -1;
49 
50 #ifdef ESP8266
51 ADC_MODE (ADC_VCC);
52 #endif
53 
55  Serial.println ("Registered");
56 }
57 
59  Serial.printf ("Unregistered. Reason: %d\n", reason);
60 }
61 
62 void processRxData (const uint8_t* mac, const uint8_t* buffer, uint8_t length, nodeMessageType_t command, nodePayloadEncoding_t encoding) {
63  char macstr[ENIGMAIOT_ADDR_LEN * 3];
64  String commandStr;
65  uint8_t tempBuffer[MAX_MESSAGE_LENGTH];
66 
67  mac2str (mac, macstr);
68  Serial.println ();
69  Serial.printf ("Data from %s\n", macstr);
71  commandStr = "GET";
72  else if (command == nodeMessageType_t::DOWNSTREAM_DATA_SET)
73  commandStr = "SET";
74  else
75  return;
76 
77  Serial.printf ("Command %s\n", commandStr.c_str ());
78  Serial.printf ("Data: %s\n", printHexBuffer (buffer, length));
79  Serial.printf ("Encoding: 0x%02X\n", encoding);
80 
81  CayenneLPP lpp (MAX_DATA_PAYLOAD_SIZE);
82  DynamicJsonDocument doc (1000);
83  JsonArray root;
84  memcpy (tempBuffer, buffer, length);
85 
86  switch (encoding) {
87  case CAYENNELPP:
88  root = doc.createNestedArray ();
89  lpp.decode (tempBuffer, length, root);
90  serializeJsonPretty (doc, Serial);
91  break;
92  case MSG_PACK:
93  deserializeMsgPack (doc, tempBuffer, length);
94  serializeJsonPretty (doc, Serial);
95  break;
96  default:
97  DEBUG_WARN ("Non supported encoding; %d", encoding);
98  }
99 }
100 
101 void setup () {
102 
103  Serial.begin (115200); Serial.println (); Serial.println ();
104  time_t start = millis ();
105 
106 #ifdef ESP32
107  // Turn-off the 'brownout detector' to avoid random restarts during wake up,
108  // normally due to bad quality regulator on board
109  WRITE_PERI_REG (RTC_CNTL_BROWN_OUT_REG, 0);
110 #endif
111 
117 
119 
120  uint8_t macAddress[ENIGMAIOT_ADDR_LEN];
121 #ifdef ESP8266
122  if (wifi_get_macaddr (STATION_IF, macAddress))
123 #elif defined ESP32
124  if ((esp_wifi_get_mac (WIFI_IF_STA, macAddress) == ESP_OK))
125 #endif
126  {
127  EnigmaIOTNode.setNodeAddress (macAddress);
128  char macStr[ENIGMAIOT_ADDR_LEN * 3];
129  DEBUG_DBG ("Node address set to %s", mac2str (macAddress, macStr));
130  } else {
131  DEBUG_WARN ("Node address error");
132  }
133 
134  // Put here your code to read sensor and compose buffer
135  const size_t capacity = JSON_OBJECT_SIZE (5);
136  DynamicJsonDocument json (capacity);
137 
138 #ifdef ESP8266
139  json["V"] = (float)(ESP.getVcc ()) / 1000;
140 #elif defined ESP32
141  json["V"] = (float)(analogRead (ADC1_CHANNEL_0_GPIO_NUM) * 3.6 / 4096);
142 #endif
143  json["tem"] = 203;
144  json["din"] = 123;
145  json["pres"] = 1007;
146  json["curr"] = 2.43;
147 
148  int len = measureMsgPack (json) + 1;
149  uint8_t* buffer = (uint8_t*)malloc (len);
150  len = serializeMsgPack (json, (char*)buffer, len);
151 
152 #ifdef ESP8266
153  Serial.printf ("Vcc: %f\n", (float)(ESP.getVcc ()) / 1000);
154 #elif defined ESP32
155  Serial.printf ("Vcc: %f\n", (float)(analogRead (ADC1_CHANNEL_0_GPIO_NUM) * 3.6 / 4096));
156 #endif
157  Serial.printf ("Message Len %d\n", len);
158  // End of user code
159 
160  Serial.printf ("Trying to send: %s\n", printHexBuffer (buffer, len));
161 
162  // Send buffer data
163  if (!EnigmaIOTNode.sendData (buffer, len, MSG_PACK)) {
164  Serial.println ("---- Error sending data");
165  } else {
166  Serial.println ("---- Data sent");
167  }
168  Serial.printf ("Total time: %lu ms\n", millis () - start);
169 
170  free (buffer);
171 
172  // Go to sleep
173  EnigmaIOTNode.sleep ();
174 }
175 
176 void loop () {
177 
179 
180 }
setup
void setup()
Definition: enigmaiot_node_msgpack.cpp:101
ENIGMAIOT_ADDR_LEN
static const size_t ENIGMAIOT_ADDR_LEN
Address size. Mac address = 6 bytes.
Definition: EnigmaIoTconfigAdvanced.h:23
nodeMessageType
nodeMessageType
Message code definition.
Definition: EnigmaIOTNode.h:35
MAX_MESSAGE_LENGTH
static const uint8_t MAX_MESSAGE_LENGTH
Maximum payload size on ESP-NOW.
Definition: EnigmaIoTconfigAdvanced.h:21
MAX_DATA_PAYLOAD_SIZE
static const int MAX_DATA_PAYLOAD_SIZE
Maximun payload size for data packets.
Definition: EnigmaIoTconfigAdvanced.h:48
EnigmaIOTNodeClass::setResetPin
void setResetPin(int pin)
Sets a pin to be used to reset configuration it it is connected to ground during startup.
Definition: EnigmaIOTNode.cpp:94
BLUE_LED
#define BLUE_LED
Definition: enigmaiot_node_msgpack.cpp:47
loop
void loop()
Definition: enigmaiot_node_msgpack.cpp:176
processRxData
void processRxData(const uint8_t *mac, const uint8_t *buffer, uint8_t length, nodeMessageType_t command, nodePayloadEncoding_t encoding)
Definition: enigmaiot_node_msgpack.cpp:62
EnigmaIOTNodeClass::handle
void handle()
This method should be called periodically for instance inside loop() function. It is used for interna...
Definition: EnigmaIOTNode.cpp:1003
connectEventHandler
void connectEventHandler()
Definition: enigmaiot_node_msgpack.cpp:54
DOWNSTREAM_DATA_GET
@ DOWNSTREAM_DATA_GET
Definition: EnigmaIOTGateway.h:44
printHexBuffer
char * printHexBuffer(const uint8_t *buffer, uint16_t len)
Debug helper function that generates a string that represent a buffer hexadecimal values.
Definition: helperFunctions.cpp:16
DOWNSTREAM_DATA_SET
@ DOWNSTREAM_DATA_SET
Definition: EnigmaIOTGateway.h:42
EnigmaIOTNode
EnigmaIOTNodeClass EnigmaIOTNode
Definition: EnigmaIOTNode.cpp:2719
EnigmaIOTNodeClass::sleep
void sleep()
Requests transition to sleep mode (low energy state)
Definition: EnigmaIOTNode.cpp:1564
EnigmaIOTNodeClass::onDisconnected
void onDisconnected(onDisconnected_t handler)
Defines a function callback that will be called everytime node is disconnected from gateway.
Definition: EnigmaIOTNode.h:711
RESET_PIN
constexpr auto RESET_PIN
Definition: enigmaiot_node_msgpack.cpp:48
nodePayloadEncoding_t
nodePayloadEncoding_t
Definition: EnigmaIOTNode.h:58
EnigmaIOTNodeClass::sendData
bool sendData(const uint8_t *data, size_t len, dataMessageType_t dataMsgType, bool encrypt=true, nodePayloadEncoding_t payloadEncoding=CAYENNELPP)
Initiades data transmission distinguissing if it is payload or control data.
Definition: EnigmaIOTNode.cpp:1535
EnigmaIOTNode.h
Library to build a node for EnigmaIoT system.
MSG_PACK
@ MSG_PACK
Definition: EnigmaIOTGateway.h:65
EnigmaIOTNodeClass::onConnected
void onConnected(onConnected_t handler)
Defines a function callback that will be called everytime node is registered on gateway.
Definition: EnigmaIOTNode.h:682
EnigmaIOTNodeClass::begin
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.
Definition: EnigmaIOTNode.cpp:696
EnigmaIOTNodeClass::setLed
void setLed(uint8_t led, time_t onTime=FLASH_LED_TIME)
Sets a LED to be flashed every time a message is transmitted.
Definition: EnigmaIOTNode.cpp:89
Espnow_hal
Espnow_halClass Espnow_hal
Singleton instance of ESP-NOW class.
Definition: espnow_hal.cpp:20
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
EnigmaIOTNodeClass::setNodeAddress
bool setNodeAddress(uint8_t address[ENIGMAIOT_ADDR_LEN])
Set node address to be used in EnigmaIOT communication.
Definition: EnigmaIOTNode.cpp:943
nodeInvalidateReason_t
nodeInvalidateReason_t
Key invalidation reason definition.
Definition: EnigmaIOTNode.h:78
disconnectEventHandler
void disconnectEventHandler(nodeInvalidateReason_t reason)
Definition: enigmaiot_node_msgpack.cpp:58
CAYENNELPP
@ CAYENNELPP
Definition: EnigmaIOTGateway.h:63
espnow_hal.h
ESP-NOW communication system abstraction layer. To be used on ESP8266 or ESP32 platforms.
EnigmaIOTNodeClass::onDataRx
void onDataRx(onNodeDataRx_t handler)
Defines a function callback that will be called on every downlink data message that is received from ...
Definition: EnigmaIOTNode.h:655