EnigmaIOT  0.9.3
Secure sensor and gateway platform based on ESP8266 and ESP32
enigmaiot_led_flasher.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 #define LED_ON LOW
40 #define LED_OFF !LED_ON
41 
42 #ifndef LED_BUILTIN
43 #define LED_BUILTIN 2 // ESP32 boards normally have a LED in GPIO2 or GPIO5
44 #endif // !LED_BUILTIN
45 
46 #define BLUE_LED LED_BUILTIN
47 constexpr auto RESET_PIN = -1;
48 
49 #ifdef ESP8266
50 ADC_MODE (ADC_VCC);
51 #endif
52 
54  Serial.println ("Connected");
55 }
56 
58  Serial.printf ("Unregistered. Reason: %d\n", reason);
59 }
60 
61 void processRxData (const uint8_t* mac, const uint8_t* buffer, uint8_t length, nodeMessageType_t command, nodePayloadEncoding_t payloadEncoding) {
62  char macstr[ENIGMAIOT_ADDR_LEN * 3];
63  String commandStr;
64  uint8_t tempBuffer[MAX_MESSAGE_LENGTH];
65 
66  mac2str (mac, macstr);
67  Serial.println ();
68  Serial.printf ("Data from %s --> %s\n", macstr, printHexBuffer (buffer, length));
70  commandStr = "GET";
71  else if (command == nodeMessageType_t::DOWNSTREAM_DATA_SET)
72  commandStr = "SET";
73  else
74  return;
75 
76  Serial.printf ("Command %s\n", commandStr.c_str ());
77  Serial.printf ("Data: %s\n", printHexBuffer (buffer, length));
78  Serial.printf ("Encoding: 0x%02X\n", payloadEncoding);
79 
80  CayenneLPP lpp (MAX_DATA_PAYLOAD_SIZE);
81  DynamicJsonDocument doc (1000);
82  JsonArray root;
83  memcpy (tempBuffer, buffer, length);
84 
85  switch (payloadEncoding) {
86  case CAYENNELPP:
87  root = doc.createNestedArray ();
88  lpp.decode (tempBuffer, length, root);
89  serializeJsonPretty (doc, Serial);
90  break;
91  case MSG_PACK:
92  deserializeMsgPack (doc, tempBuffer, length);
93  serializeJsonPretty (doc, Serial);
94  break;
95  default:
96  DEBUG_WARN ("Non supported encoding; %d", payloadEncoding);
97  }
98 }
99 
100 void setup () {
101 
102  Serial.begin (115200); Serial.println (); Serial.println ();
103 
104 #ifdef ESP32
105  // Turn-off the 'brownout detector' to avoid random restarts during wake up,
106  // normally due to bad quality regulator on board
107  WRITE_PERI_REG (RTC_CNTL_BROWN_OUT_REG, 0);
108 #endif
109 
110  //EnigmaIOTNode.setLed (BLUE_LED);
111  pinMode (BLUE_LED, OUTPUT);
112  digitalWrite (BLUE_LED, LED_OFF); // Turn off LED
117 
118  EnigmaIOTNode.begin (&Espnow_hal, NULL, NULL, true, false);
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 }
135 
136 void loop () {
137 
139 
140  static const time_t PERIOD = 3000;
141  static const time_t FLASH_DURATION = 100;
142  static time_t clock;
143 
144  clock = EnigmaIOTNode.clock () % PERIOD;
145 
147  if (clock >= 0 && clock < FLASH_DURATION) {
148  digitalWrite (BLUE_LED, LED_ON); // Turn on LED
149  } else {
150  digitalWrite (BLUE_LED, LED_OFF); // Turn on LED
151  }
152  }
153 
154  CayenneLPP msg (MAX_DATA_PAYLOAD_SIZE);
155 
156  static time_t lastSensorData;
157  static const time_t SENSOR_PERIOD = 10000;
158  if (millis () - lastSensorData > SENSOR_PERIOD) {
159  lastSensorData = millis ();
160 
161  // Read sensor data
162 #ifdef ESP8266
163  msg.addAnalogInput (0, (float)(ESP.getVcc ()) / 1000);
164  Serial.printf ("Vcc: %f\n", (float)(ESP.getVcc ()) / 1000);
165 #elif defined ESP32
166  msg.addAnalogInput (0, (float)(analogRead (ADC1_CHANNEL_0_GPIO_NUM) * 3.6 / 4096));
167  Serial.printf ("Vcc: %f\n", (float)(analogRead (ADC1_CHANNEL_0_GPIO_NUM) * 3.6 / 4096));
168 #endif
169  msg.addTemperature (1, 20.34);
170  // Read sensor data
171 
172  Serial.printf ("Trying to send: %s\n", printHexBuffer (msg.getBuffer (), msg.getSize ()));
173 
174  if (!EnigmaIOTNode.sendData (msg.getBuffer (), msg.getSize ())) {
175  Serial.println ("---- Error sending data");
176  } else {
177  Serial.println ("---- Data sent");
178  }
179 
180  }
181 }
setup
void setup()
Definition: enigmaiot_led_flasher.ino:100
EnigmaIOTNodeClass::hasClockSync
bool hasClockSync()
Checks if internal clock is synchronized to gateway.
Definition: EnigmaIOTNode.cpp:1189
EnigmaIOTNodeClass::enableClockSync
void enableClockSync(bool clockSync=true)
Controls clock synchronization function.
Definition: EnigmaIOTNode.h:511
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
BLUE_LED
#define BLUE_LED
Definition: enigmaiot_led_flasher.ino:46
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
processRxData
void processRxData(const uint8_t *mac, const uint8_t *buffer, uint8_t length, nodeMessageType_t command, nodePayloadEncoding_t payloadEncoding)
Definition: enigmaiot_led_flasher.ino:61
LED_ON
#define LED_ON
Definition: enigmaiot_led_flasher.ino:39
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
EnigmaIOTNode
EnigmaIOTNodeClass EnigmaIOTNode
Definition: EnigmaIOTNode.cpp:2229
loop
void loop()
Definition: enigmaiot_led_flasher.ino:136
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
disconnectEventHandler
void disconnectEventHandler(nodeInvalidateReason_t reason)
Definition: enigmaiot_led_flasher.ino:57
EnigmaIOTNodeClass::isRegistered
bool isRegistered()
Checks if node is registered.
Definition: EnigmaIOTNode.h:686
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
LED_OFF
#define LED_OFF
Definition: enigmaiot_led_flasher.ino:40
Espnow_hal
Espnow_halClass Espnow_hal
Singleton instance of ESP-NOW class.
Definition: espnow_hal.cpp:20
connectEventHandler
void connectEventHandler()
Definition: enigmaiot_led_flasher.ino:53
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
EnigmaIOTNodeClass::clock
int64_t clock()
Gets current clock counter. millis() + offset.
Definition: EnigmaIOTNode.cpp:1172
CAYENNELPP
@ CAYENNELPP
Definition: EnigmaIOTGateway.h:52
espnow_hal.h
ESP-NOW communication system abstraction layer. To be used on ESP8266 or ESP32 platforms.
RESET_PIN
constexpr auto RESET_PIN
Definition: enigmaiot_led_flasher.ino:47
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