EnigmaIOT  0.9.8
Secure sensor and gateway platform based on ESP8266 and ESP32
enigmaiot_led_flasher.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 #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
118 
119  EnigmaIOTNode.begin (&Espnow_hal, NULL, NULL, true, false);
120 
121  uint8_t macAddress[ENIGMAIOT_ADDR_LEN];
122 #ifdef ESP8266
123  if (wifi_get_macaddr (STATION_IF, macAddress))
124 #elif defined ESP32
125  if ((esp_wifi_get_mac (WIFI_IF_STA, macAddress) == ESP_OK))
126 #endif
127  {
128  EnigmaIOTNode.setNodeAddress (macAddress);
129  //char macStr[ENIGMAIOT_ADDR_LEN * 3];
130  DEBUG_DBG ("Node address set to %s", mac2str (macAddress));
131  } else {
132  DEBUG_WARN ("Node address error");
133  }
134 
135 }
136 
137 void loop () {
138 
140 
141  static const time_t PERIOD = 3000;
142  static const time_t FLASH_DURATION = 50;
143  static time_t clock;
144 
145  clock = EnigmaIOTNode.clock () % (PERIOD);
146 
148  if (clock >= 0 && clock < FLASH_DURATION) {
149  digitalWrite (BLUE_LED, LED_ON); // Turn on LED
150  } else {
151  digitalWrite (BLUE_LED, LED_OFF); // Turn on LED
152  }
153  }
154 
155  CayenneLPP msg (MAX_DATA_PAYLOAD_SIZE);
156 
157  static time_t lastSensorData;
158  static const time_t SENSOR_PERIOD = 10000;
159  if (millis () - lastSensorData > SENSOR_PERIOD) {
160  lastSensorData = millis ();
161 
162  // Read sensor data
163 #ifdef ESP8266
164  msg.addAnalogInput (0, (float)(ESP.getVcc ()) / 1000);
165  Serial.printf ("Vcc: %f\n", (float)(ESP.getVcc ()) / 1000);
166 #elif defined ESP32
167  msg.addAnalogInput (0, (float)(analogRead (ADC1_CHANNEL_0_GPIO_NUM) * 3.6 / 4096));
168  Serial.printf ("Vcc: %f\n", (float)(analogRead (ADC1_CHANNEL_0_GPIO_NUM) * 3.6 / 4096));
169 #endif
170  msg.addTemperature (1, 20.34);
171  // Read sensor data
172 
173  Serial.printf ("Trying to send: %s\n", printHexBuffer (msg.getBuffer (), msg.getSize ()));
174 
175  if (!EnigmaIOTNode.sendData (msg.getBuffer (), msg.getSize ())) {
176  Serial.println ("---- Error sending data");
177  } else {
178  Serial.println ("---- Data sent");
179  }
180 
181  }
182 }
RESET_PIN
constexpr auto RESET_PIN
Definition: enigmaiot_led_flasher.cpp:47
connectEventHandler
void connectEventHandler()
Definition: enigmaiot_led_flasher.cpp:53
EnigmaIOTNodeClass::hasClockSync
bool hasClockSync()
Checks if internal clock is synchronized to gateway.
Definition: EnigmaIOTNode.cpp:1464
EnigmaIOTNodeClass::enableClockSync
void enableClockSync(bool clockSync=true)
Controls clock synchronization function.
Definition: EnigmaIOTNode.h:584
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
BLUE_LED
#define BLUE_LED
Definition: enigmaiot_led_flasher.cpp:46
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::handle
void handle()
This method should be called periodically for instance inside loop() function. It is used for interna...
Definition: EnigmaIOTNode.cpp:1003
disconnectEventHandler
void disconnectEventHandler(nodeInvalidateReason_t reason)
Definition: enigmaiot_led_flasher.cpp:57
LED_OFF
#define LED_OFF
Definition: enigmaiot_led_flasher.cpp:40
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::enableBroadcast
void enableBroadcast(bool broadcast=true)
Enables node broadcast mode. Node will request broadcast key to Gateway. When it is received node wil...
Definition: EnigmaIOTNode.h:566
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
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
EnigmaIOTNodeClass::isRegistered
bool isRegistered()
Checks if node is registered.
Definition: EnigmaIOTNode.h:770
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
Espnow_hal
Espnow_halClass Espnow_hal
Singleton instance of ESP-NOW class.
Definition: espnow_hal.cpp:20
processRxData
void processRxData(const uint8_t *mac, const uint8_t *buffer, uint8_t length, nodeMessageType_t command, nodePayloadEncoding_t payloadEncoding)
Definition: enigmaiot_led_flasher.cpp:61
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
EnigmaIOTNodeClass::clock
int64_t clock()
Gets current clock counter. millis() + offset.
Definition: EnigmaIOTNode.cpp:1455
loop
void loop()
Definition: enigmaiot_led_flasher.cpp:137
setup
void setup()
Definition: enigmaiot_led_flasher.cpp:100
LED_ON
#define LED_ON
Definition: enigmaiot_led_flasher.cpp:39
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