EnigmaIOT  0.9.3
Secure sensor and gateway platform based on ESP8266 and ESP32
enigmaiot_node.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 payloadEncoding) {
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", payloadEncoding);
76 
77  CayenneLPP lpp (MAX_DATA_PAYLOAD_SIZE);
78  DynamicJsonDocument doc (1000);
79  JsonArray root;
80  memcpy (tempBuffer, buffer, length);
81 
82  switch (payloadEncoding) {
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 ("Payload encoding %d is not (yet) supported", payloadEncoding);
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 
108 
114 
116 
117 
118  uint8_t macAddress[ENIGMAIOT_ADDR_LEN];
119 #ifdef ESP8266
120  if (wifi_get_macaddr (STATION_IF, macAddress))
121 #elif defined ESP32
122  if ((esp_wifi_get_mac (WIFI_IF_STA, macAddress) == ESP_OK))
123 #endif
124  {
125  EnigmaIOTNode.setNodeAddress (macAddress);
126  char macStr[ENIGMAIOT_ADDR_LEN * 3];
127  DEBUG_DBG ("Node address set to %s", mac2str (macAddress, macStr));
128  } else {
129  DEBUG_WARN ("Node address error");
130  }
131 
132  // Put here your code to read sensor and compose buffer
133  CayenneLPP msg (MAX_DATA_PAYLOAD_SIZE);
134 
135 #ifdef ESP8266
136  msg.addAnalogInput (0, (float)(ESP.getVcc ()) / 1000);
137 #elif defined ESP32
138  msg.addAnalogInput (0, (float)(analogRead (ADC1_CHANNEL_0_GPIO_NUM) * 3.6 / 4096));
139 #endif
140  msg.addTemperature (1, 20.34);
141  msg.addDigitalInput (2, 123);
142  msg.addBarometricPressure (3, 1007.25);
143  msg.addCurrent (4, 2.43);
144 
145 #ifdef ESP8266
146  Serial.printf ("Vcc: %f\n", (float)(ESP.getVcc ())/ 1000);
147 #elif defined ESP32
148  Serial.printf ("Vcc: %f\n", (float)(analogRead(ADC1_CHANNEL_0_GPIO_NUM) * 3.6 / 4096));
149 #endif
150  // End of user code
151 
152  Serial.printf ("Trying to send: %s\n", printHexBuffer (msg.getBuffer (), msg.getSize ()));
153 
154  // Send buffer data
155  if (!EnigmaIOTNode.sendData (msg.getBuffer (), msg.getSize ())) {
156  Serial.println ("---- Error sending data");
157  } else {
158  Serial.println ("---- Data sent");
159  }
160  Serial.printf ("Total time: %lu ms\n", millis () - start);
161 
162  // Go to sleep
163  EnigmaIOTNode.sleep ();
164 }
165 
166 void loop () {
167 
169 
170 }
setup
void setup()
Definition: enigmaiot_node.ino:97
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
RESET_PIN
constexpr auto RESET_PIN
Definition: enigmaiot_node.ino:44
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
connectEventHandler
void connectEventHandler()
Definition: enigmaiot_node.ino:50
BLUE_LED
#define BLUE_LED
Definition: enigmaiot_node.ino:43
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
loop
void loop()
Definition: enigmaiot_node.ino:166
EnigmaIOTNode
EnigmaIOTNodeClass EnigmaIOTNode
Definition: EnigmaIOTNode.cpp:2229
EnigmaIOTNodeClass::sleep
void sleep()
Requests transition to sleep mode (low energy state)
Definition: EnigmaIOTNode.cpp:1287
processRxData
void processRxData(const uint8_t *mac, const uint8_t *buffer, uint8_t length, nodeMessageType_t command, nodePayloadEncoding_t payloadEncoding)
Definition: enigmaiot_node.ino:58
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
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
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
disconnectEventHandler
void disconnectEventHandler(nodeInvalidateReason_t reason)
Definition: enigmaiot_node.ino:54
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
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