EnigmaIOT  0.9.3
Secure sensor and gateway platform based on ESP8266 and ESP32
enigmaiot_node_msgpack.ino
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 #define LED_BUILTIN 2 // ESP32 boards normally have a LED in GPIO2 or GPIO5
41 #endif // !LED_BUILTIN
42 
43 #define BLUE_LED LED_BUILTIN
44 constexpr auto RESET_PIN = -1;
45 
46 #ifdef ESP8266
47 ADC_MODE (ADC_VCC);
48 #endif
49 
51  Serial.println ("Registered");
52 }
53 
55  Serial.printf ("Unregistered. Reason: %d\n", reason);
56 }
57 
58 void processRxData (const uint8_t* mac, const uint8_t* buffer, uint8_t length, nodeMessageType_t command, nodePayloadEncoding_t encoding) {
59  char macstr[ENIGMAIOT_ADDR_LEN * 3];
60  String commandStr;
61  uint8_t tempBuffer[MAX_MESSAGE_LENGTH];
62 
63  mac2str (mac, macstr);
64  Serial.println ();
65  Serial.printf ("Data from %s\n", macstr);
67  commandStr = "GET";
68  else if (command == nodeMessageType_t::DOWNSTREAM_DATA_SET)
69  commandStr = "SET";
70  else
71  return;
72 
73  Serial.printf ("Command %s\n", commandStr.c_str ());
74  Serial.printf ("Data: %s\n", printHexBuffer (buffer, length));
75  Serial.printf ("Encoding: 0x%02X\n", encoding);
76 
77  CayenneLPP lpp (MAX_DATA_PAYLOAD_SIZE);
78  DynamicJsonDocument doc (1000);
79  JsonArray root;
80  memcpy (tempBuffer, buffer, length);
81 
82  switch (encoding) {
83  case CAYENNELPP:
84  root = doc.createNestedArray ();
85  lpp.decode (tempBuffer, length, root);
86  serializeJsonPretty (doc, Serial);
87  break;
88  case MSG_PACK:
89  deserializeMsgPack (doc, tempBuffer, length);
90  serializeJsonPretty (doc, Serial);
91  break;
92  default:
93  DEBUG_WARN("Non supported encoding; %d", encoding);
94  }
95 }
96 
97 void setup () {
98 
99  Serial.begin (115200); Serial.println (); Serial.println ();
100  time_t start = millis ();
101 
102 #ifdef ESP32
103  // Turn-off the 'brownout detector' to avoid random restarts during wake up,
104  // normally due to bad quality regulator on board
105  WRITE_PERI_REG (RTC_CNTL_BROWN_OUT_REG, 0);
106 #endif
107 
113 
115 
116  uint8_t macAddress[ENIGMAIOT_ADDR_LEN];
117 #ifdef ESP8266
118  if (wifi_get_macaddr (STATION_IF, macAddress))
119 #elif defined ESP32
120  if ((esp_wifi_get_mac (WIFI_IF_STA, macAddress) == ESP_OK))
121 #endif
122  {
123  EnigmaIOTNode.setNodeAddress (macAddress);
124  char macStr[ENIGMAIOT_ADDR_LEN * 3];
125  DEBUG_DBG ("Node address set to %s", mac2str (macAddress, macStr));
126  } else {
127  DEBUG_WARN ("Node address error");
128  }
129 
130  // Put here your code to read sensor and compose buffer
131  const size_t capacity = JSON_OBJECT_SIZE (5);
132  DynamicJsonDocument json (capacity);
133 
134 #ifdef ESP8266
135  json["V"] = (float)(ESP.getVcc ()) / 1000;
136 #elif defined ESP32
137  json["V"] = (float)(analogRead (ADC1_CHANNEL_0_GPIO_NUM) * 3.6 / 4096);
138 #endif
139  json["tem"] = 203;
140  json["din"] = 123;
141  json["pres"] = 1007;
142  json["curr"] = 2.43;
143 
144  int len = measureMsgPack (json) + 1;
145  uint8_t* buffer = (uint8_t*)malloc (len);
146  len = serializeMsgPack (json, (char*)buffer, len);
147 
148 #ifdef ESP8266
149  Serial.printf ("Vcc: %f\n", (float)(ESP.getVcc ()) / 1000);
150 #elif defined ESP32
151  Serial.printf ("Vcc: %f\n", (float)(analogRead (ADC1_CHANNEL_0_GPIO_NUM) * 3.6 / 4096));
152 #endif
153  Serial.printf ("Message Len %d\n", len);
154  // End of user code
155 
156  Serial.printf ("Trying to send: %s\n", printHexBuffer (buffer, len));
157 
158  // Send buffer data
159  if (!EnigmaIOTNode.sendData (buffer, len, MSG_PACK)) {
160  Serial.println ("---- Error sending data");
161  } else {
162  Serial.println ("---- Data sent");
163  }
164  Serial.printf ("Total time: %lu ms\n", millis () - start);
165 
166  free (buffer);
167 
168  // Go to sleep
169  EnigmaIOTNode.sleep ();
170 }
171 
172 void loop () {
173 
175 
176 }
EnigmaIOTNodeClass::sendData
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.
Definition: EnigmaIOTNode.cpp:1260
nodeMessageType
nodeMessageType
Message code definition.
Definition: EnigmaIOTNode.h:35
MAX_DATA_PAYLOAD_SIZE
static const int MAX_DATA_PAYLOAD_SIZE
Maximun payload size for data packets.
Definition: EnigmaIoTconfig.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:72
ENIGMAIOT_ADDR_LEN
static const size_t ENIGMAIOT_ADDR_LEN
Address size. Mac address = 6 bytes.
Definition: EnigmaIoTconfig.h:14
EnigmaIOTNodeClass::handle
void handle()
This method should be called periodically for instance inside loop() function. It is used for interna...
Definition: EnigmaIOTNode.cpp:801
DOWNSTREAM_DATA_GET
@ DOWNSTREAM_DATA_GET
Definition: EnigmaIOTGateway.h:38
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:17
DOWNSTREAM_DATA_SET
@ DOWNSTREAM_DATA_SET
Definition: EnigmaIOTGateway.h:37
RESET_PIN
constexpr auto RESET_PIN
Definition: enigmaiot_node_msgpack.ino:44
EnigmaIOTNode
EnigmaIOTNodeClass EnigmaIOTNode
Definition: EnigmaIOTNode.cpp:2229
EnigmaIOTNodeClass::sleep
void sleep()
Requests transition to sleep mode (low energy state)
Definition: EnigmaIOTNode.cpp:1287
disconnectEventHandler
void disconnectEventHandler(nodeInvalidateReason_t reason)
Definition: enigmaiot_node_msgpack.ino:54
mac2str
char * mac2str(const uint8_t *mac, char *buffer)
Debug helper function that generates a string that represent a MAC address.
Definition: helperFunctions.cpp:84
EnigmaIOTNodeClass::onDisconnected
void onDisconnected(onDisconnected_t handler)
Defines a function callback that will be called everytime node is disconnected from gateway.
Definition: EnigmaIOTNode.h:627
nodePayloadEncoding_t
nodePayloadEncoding_t
Definition: EnigmaIOTNode.h:51
processRxData
void processRxData(const uint8_t *mac, const uint8_t *buffer, uint8_t length, nodeMessageType_t command, nodePayloadEncoding_t encoding)
Definition: enigmaiot_node_msgpack.ino:58
MAX_MESSAGE_LENGTH
static const uint8_t MAX_MESSAGE_LENGTH
Maximum payload size on ESP-NOW.
Definition: EnigmaIoTconfig.h:13
EnigmaIOTNode.h
Library to build a node for EnigmaIoT system.
MSG_PACK
@ MSG_PACK
Definition: EnigmaIOTGateway.h:54
EnigmaIOTNodeClass::onConnected
void onConnected(onConnected_t handler)
Defines a function callback that will be called everytime node is registered on gateway.
Definition: EnigmaIOTNode.h:598
loop
void loop()
Definition: enigmaiot_node_msgpack.ino:172
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:510
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:67
Espnow_hal
Espnow_halClass Espnow_hal
Singleton instance of ESP-NOW class.
Definition: espnow_hal.cpp:20
setup
void setup()
Definition: enigmaiot_node_msgpack.ino:97
EnigmaIOTNodeClass::setNodeAddress
bool setNodeAddress(uint8_t address[ENIGMAIOT_ADDR_LEN])
Set node address to be used in EnigmaIOT communication.
Definition: EnigmaIOTNode.cpp:741
nodeInvalidateReason_t
nodeInvalidateReason_t
Key invalidation reason definition.
Definition: EnigmaIOTNode.h:65
BLUE_LED
#define BLUE_LED
Definition: enigmaiot_node_msgpack.ino:43
connectEventHandler
void connectEventHandler()
Definition: enigmaiot_node_msgpack.ino:50
CAYENNELPP
@ CAYENNELPP
Definition: EnigmaIOTGateway.h:52
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:571