EnigmaIOT  0.9.8
Secure sensor and gateway platform based on ESP8266 and ESP32
enigmaiot_node.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 #elif defined ESP32
19 #include <WiFi.h>
20 #include <Update.h>
21 #include <driver/adc.h>
22 #include "esp_wifi.h"
23 #include "soc/soc.h" // Disable brownout problems
24 #include "soc/rtc_cntl_reg.h" // Disable brownout problems
25 #endif
26 #include <ArduinoJson.h>
27 
28 #ifndef LED_BUILTIN
29 #define LED_BUILTIN 2 // ESP32 boards normally have a LED in GPIO2 or GPIO5
30 #endif // !LED_BUILTIN
31 
32 #define BLUE_LED LED_BUILTIN
33 constexpr auto RESET_PIN = -1;
34 
35 #ifdef ESP8266
36 ADC_MODE (ADC_VCC);
37 #endif
38 
40  Serial.println ("Registered");
41 }
42 
44  Serial.printf ("Unregistered. Reason: %d\n", reason);
45 }
46 
47 void processRxData (const uint8_t* mac, const uint8_t* buffer, uint8_t length, nodeMessageType_t command, nodePayloadEncoding_t payloadEncoding) {
48  char macstr[ENIGMAIOT_ADDR_LEN * 3];
49  String commandStr;
50  uint8_t tempBuffer[MAX_MESSAGE_LENGTH];
51 
52  mac2str (mac, macstr);
53  Serial.println ();
54  Serial.printf ("Data from %s\n", macstr);
56  commandStr = "GET";
57  else if (command == nodeMessageType_t::DOWNSTREAM_DATA_SET)
58  commandStr = "SET";
59  else
60  return;
61 
62  Serial.printf ("Command %s\n", commandStr.c_str ());
63  Serial.printf ("Data: %s\n", printHexBuffer (buffer, length));
64  Serial.printf ("Encoding: 0x%02X\n", payloadEncoding);
65 
66  CayenneLPP lpp (MAX_DATA_PAYLOAD_SIZE);
67  DynamicJsonDocument doc (1000);
68  JsonArray root;
69  memcpy (tempBuffer, buffer, length);
70 
71  switch (payloadEncoding) {
72  case CAYENNELPP:
73  root = doc.createNestedArray ();
74  lpp.decode (tempBuffer, length, root);
75  serializeJsonPretty (doc, Serial);
76  break;
77  case MSG_PACK:
78  deserializeMsgPack (doc, tempBuffer, length);
79  serializeJsonPretty (doc, Serial);
80  break;
81  default:
82  DEBUG_WARN ("Payload encoding %d is not (yet) supported", payloadEncoding);
83  }
84 }
85 
86 void setup () {
87 
88  Serial.begin (115200); Serial.println (); Serial.println ();
89  time_t start = millis ();
90 
91 #ifdef ESP32
92  // Turn-off the 'brownout detector' to avoid random restarts during wake up,
93  // normally due to bad quality regulator on board
94  WRITE_PERI_REG (RTC_CNTL_BROWN_OUT_REG, 0);
95 #endif
96 
97 
103 
105 
106 
107  uint8_t macAddress[ENIGMAIOT_ADDR_LEN];
108 #ifdef ESP8266
109  if (wifi_get_macaddr (STATION_IF, macAddress))
110 #elif defined ESP32
111  if ((esp_wifi_get_mac (WIFI_IF_STA, macAddress) == ESP_OK))
112 #endif
113  {
114  EnigmaIOTNode.setNodeAddress (macAddress);
115  //char macStr[ENIGMAIOT_ADDR_LEN * 3];
116  DEBUG_DBG ("Node address set to %s", mac2str (macAddress));
117  } else {
118  DEBUG_WARN ("Node address error");
119  }
120 
121  // Put here your code to read sensor and compose buffer
122  CayenneLPP msg (MAX_DATA_PAYLOAD_SIZE);
123 
124 #ifdef ESP8266
125  msg.addAnalogInput (0, (float)(ESP.getVcc ()) / 1000);
126 #elif defined ESP32
127  msg.addAnalogInput (0, (float)(analogRead (ADC1_CHANNEL_0_GPIO_NUM) * 3.6 / 4096));
128 #endif
129  msg.addTemperature (1, 20.34);
130  msg.addDigitalInput (2, 123);
131  msg.addBarometricPressure (3, 1007.25);
132  msg.addCurrent (4, 2.43);
133 
134 #ifdef ESP8266
135  Serial.printf ("Vcc: %f\n", (float)(ESP.getVcc ()) / 1000);
136 #elif defined ESP32
137  Serial.printf ("Vcc: %f\n", (float)(analogRead (ADC1_CHANNEL_0_GPIO_NUM) * 3.6 / 4096));
138 #endif
139  // End of user code
140 
141  Serial.printf ("Trying to send: %s\n", printHexBuffer (msg.getBuffer (), msg.getSize ()));
142 
143  // Send buffer data
144  if (!EnigmaIOTNode.sendData (msg.getBuffer (), msg.getSize ())) {
145  Serial.println ("---- Error sending data");
146  } else {
147  Serial.println ("---- Data sent");
148  }
149  Serial.printf ("Total time: %lu ms\n", millis () - start);
150 
151  // Go to sleep
152  EnigmaIOTNode.sleep ();
153 }
154 
155 void loop () {
156 
158 
159 }
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
disconnectEventHandler
void disconnectEventHandler(nodeInvalidateReason_t reason)
Definition: enigmaiot_node.cpp:43
EnigmaIOTNodeClass::handle
void handle()
This method should be called periodically for instance inside loop() function. It is used for interna...
Definition: EnigmaIOTNode.cpp:1003
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
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
loop
void loop()
Definition: enigmaiot_node.cpp:155
connectEventHandler
void connectEventHandler()
Definition: enigmaiot_node.cpp:39
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
BLUE_LED
#define BLUE_LED
Definition: enigmaiot_node.cpp:32
Espnow_hal
Espnow_halClass Espnow_hal
Singleton instance of ESP-NOW class.
Definition: espnow_hal.cpp:20
RESET_PIN
constexpr auto RESET_PIN
Definition: enigmaiot_node.cpp:33
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
CAYENNELPP
@ CAYENNELPP
Definition: EnigmaIOTGateway.h:63
processRxData
void processRxData(const uint8_t *mac, const uint8_t *buffer, uint8_t length, nodeMessageType_t command, nodePayloadEncoding_t payloadEncoding)
Definition: enigmaiot_node.cpp:47
espnow_hal.h
ESP-NOW communication system abstraction layer. To be used on ESP8266 or ESP32 platforms.
setup
void setup()
Definition: enigmaiot_node.cpp:86
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