EnigmaIOT  0.9.8
Secure sensor and gateway platform based on ESP8266 and ESP32
EnigmaIOT-Button-Controller.cpp
Go to the documentation of this file.
1 
9 #if !defined ESP8266 && !defined ESP32
10 #error Node only supports ESP8266 or ESP32 platform
11 #endif
12 
13 #include <Arduino.h>
15 #include <FailSafe.h>
16 #include "ButtonController.h" // <-- Include here your controller class header
17 
18 #include <EnigmaIOTNode.h>
19 #include <espnow_hal.h>
20 #include <CayenneLPP.h>
21 #include <ArduinoJson.h>
22 
23 #ifdef ESP8266
24 #include <ESP8266WiFi.h>
25 #include <ESP8266HTTPClient.h>
26 #include <ESP8266httpUpdate.h>
27 #include <Hash.h>
28 #elif defined ESP32
29 #include <WiFi.h>
30 #include <Update.h>
31 #include <driver/adc.h>
32 #include "esp_wifi.h"
33 #endif
34 #include <ArduinoJson.h>
35 #include <Curve25519.h>
36 #include <ESPAsyncWebServer.h>
37 #include <ESPAsyncWiFiManager.h>
38 #include <DNSServer.h>
39 
40 #define SLEEPY 0 // Set it to 1 if your node should sleep after sending data
41 
42 #ifndef LED_BUILTIN
43 #define LED_BUILTIN 2 // ESP32 boards normally have a LED in GPIO3 or GPIO5
44 #endif // !LED_BUILTIN
45 
46 // If you do need serial for your project you must disable serial debug by commenting next line
47 #define USE_SERIAL // Don't forget to set DEBUG_LEVEL to NONE if serial is disabled
48 
49 #define BLUE_LED LED_BUILTIN // You can set a different LED pin here. -1 means disabled
50 
51 EnigmaIOTjsonController* controller; // Generic controller is refferenced here. You do not need to modify it
52 
53 #define RESET_PIN 5 // You can set a different configuration reset pin here. Check for conflicts with used pins.
54 
55 const time_t BOOT_FLAG_TIMEOUT = 10000; // Time in ms to reset flag
56 const int MAX_CONSECUTIVE_BOOT = 3; // Number of rapid boot cycles before enabling fail safe mode
57 const int LED = LED_BUILTIN; // Number of rapid boot cycles before enabling fail safe mode
58 const int FAILSAFE_RTC_ADDRESS = 0; // If you use RTC memory adjust offset to not overwrite other data
59 
60 // Called when node is connected to gateway. You don't need to do anything here usually
62  DEBUG_WARN ("Connected");
64 }
65 
66 // Called when node is unregistered from gateway. You don't need to do anything here usually
68  DEBUG_WARN ("Disconnected. Reason %d", reason);
69 }
70 
71 // Called to route messages to EnitmaIOTNode class. Do not modify
72 bool sendUplinkData (const uint8_t* data, size_t len, nodePayloadEncoding_t payloadEncoding, dataMessageType_t dataMsgType) {
73  if (dataMsgType == DATA_TYPE) {
74  return EnigmaIOTNode.sendData (data, len, payloadEncoding);
75  } else if (dataMsgType == HA_DISC_TYPE) {
77  } else {
78  return false;
79  }
80 }
81 
82 // Called to route incoming messages to your code. Do not modify
83 void processRxData (const uint8_t* mac, const uint8_t* buffer, uint8_t length, nodeMessageType_t command, nodePayloadEncoding_t payloadEncoding) {
84  if (controller->processRxCommand (mac, buffer, length, command, payloadEncoding)) {
85  DEBUG_INFO ("Command processed");
86  } else {
87  DEBUG_WARN ("Command error");
88  }
89 }
90 
91 // Do not modify
92 void wifiManagerExit (boolean status) {
94 }
95 
96 // Do not modify
99 }
100 
101 void setup () {
102 
103 #ifdef USE_SERIAL
104  Serial.begin (921600);
105  Serial.println ();
106 #endif
107 
108  FailSafe.checkBoot (MAX_CONSECUTIVE_BOOT, LED, FAILSAFE_RTC_ADDRESS); // Parameters are optional
109  if (FailSafe.isActive ()) { // Skip all user setup if fail safe mode is activated
110  return;
111  }
112 
113  controller = (EnigmaIOTjsonController*)new CONTROLLER_CLASS_NAME (); // Use your class name here
114 
115  EnigmaIOTNode.setLed (BLUE_LED); // Set communication LED
116  EnigmaIOTNode.setResetPin (RESET_PIN); // Set reset pin
117  EnigmaIOTNode.onConnected (connectEventHandler); // Configure registration handler
118  EnigmaIOTNode.onDisconnected (disconnectEventHandler); // Configure unregistration handler
119  EnigmaIOTNode.onDataRx (processRxData); // Configure incoming data handler
120  EnigmaIOTNode.enableClockSync (false); // Set to true if you need this node to get its clock syncronized with gateway
124 
125  if (!controller->loadConfig ()) { // Trigger custom configuration loading
126  DEBUG_WARN ("Error reading config file");
127  if (FILESYSTEM.format ())
128  DEBUG_WARN ("Filesystem Formatted");
129  }
130 
131  EnigmaIOTNode.begin (&Espnow_hal, NULL, NULL, true, SLEEPY == 1); // Start EnigmaIOT communication
132 
133  uint8_t macAddress[ENIGMAIOT_ADDR_LEN];
134  // Set Address using internal MAC Address. Do not modify
135 #ifdef ESP8266
136  if (wifi_get_macaddr (STATION_IF, macAddress))
137 #elif defined ESP32
138  if ((esp_wifi_get_mac (WIFI_IF_STA, macAddress) == ESP_OK))
139 #endif
140  {
141  EnigmaIOTNode.setNodeAddress (macAddress);
142  DEBUG_DBG ("Node address set to %s", mac2str (macAddress));
143  } else {
144  DEBUG_WARN ("Node address error");
145  }
146 
147  controller->sendDataCallback (sendUplinkData); // Listen for data from controller class
148  controller->setup (&EnigmaIOTNode); // Start controller class
149 
150 #if SLEEPY == 1
151  EnigmaIOTNode.sleep ();
152 #endif
153 
154  DEBUG_DBG ("END setup");
155 }
156 
157 void loop () {
158  FailSafe.loop (BOOT_FLAG_TIMEOUT); // Use always this line
159 
160  if (FailSafe.isActive ()) { // Skip all user loop code if Fail Safe mode is active
161  return;
162  }
163 
164  controller->loop (); // Loop controller class
165  controller->callHAdiscoveryCalls (); // Send HA registration messages
166  EnigmaIOTNode.handle (); // Mantain EnigmaIOT connection
167 }
EnigmaIOTjsonController::loop
virtual void loop()=0
This should be called periodically for module handling.
BLUE_LED
#define BLUE_LED
Definition: EnigmaIOT-Button-Controller.cpp:49
EnigmaIOTjsonController::configManagerStart
virtual void configManagerStart()=0
Called when wifi manager starts config portal.
EnigmaIOTjsonController::configManagerExit
virtual void configManagerExit(bool status)=0
Called when wifi manager exits config portal.
BOOT_FLAG_TIMEOUT
const time_t BOOT_FLAG_TIMEOUT
Definition: EnigmaIOT-Button-Controller.cpp:55
disconnectEventHandler
void disconnectEventHandler(nodeInvalidateReason_t reason)
Definition: EnigmaIOT-Button-Controller.cpp:67
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
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
controller
EnigmaIOTjsonController * controller
Definition: EnigmaIOT-Button-Controller.cpp:51
EnigmaIOTjsonController::connectInform
virtual void connectInform()
Used to notify controller that it is registered on EnigmaIOT network.
Definition: EnigmaIOTjsonController.h:81
FAILSAFE_RTC_ADDRESS
const int FAILSAFE_RTC_ADDRESS
Definition: EnigmaIOT-Button-Controller.cpp:58
connectEventHandler
void connectEventHandler()
Definition: EnigmaIOT-Button-Controller.cpp:61
EnigmaIOTNodeClass::handle
void handle()
This method should be called periodically for instance inside loop() function. It is used for interna...
Definition: EnigmaIOTNode.cpp:1003
EnigmaIOTjsonController::sendDataCallback
void sendDataCallback(sendData_cb cb)
Register send data callback to run when module needs to send a message.
Definition: EnigmaIOTjsonController.h:74
CONTROLLER_CLASS_NAME
Definition: ButtonController.h:27
dataMessageType_t
dataMessageType_t
Definition: EnigmaIOTNode.h:68
EnigmaIOTjsonController::loadConfig
virtual bool loadConfig()=0
Loads output module configuration.
DATA_TYPE
@ DATA_TYPE
Definition: EnigmaIOTNode.h:69
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::sleep
void sleep()
Requests transition to sleep mode (low energy state)
Definition: EnigmaIOTNode.cpp:1564
wifiManagerExit
void wifiManagerExit(boolean status)
Definition: EnigmaIOT-Button-Controller.cpp:92
EnigmaIOTNodeClass::onWiFiManagerExit
void onWiFiManagerExit(onWiFiManagerExit_t handle)
Register callback to be called on wifi manager exit.
Definition: EnigmaIOTNode.h:719
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
ButtonController.h
nodePayloadEncoding_t
nodePayloadEncoding_t
Definition: EnigmaIOTNode.h:58
loop
void loop()
Definition: EnigmaIOT-Button-Controller.cpp:157
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::onWiFiManagerStarted
void onWiFiManagerStarted(simpleEventHandler_t handle)
Register callback to be called on wifi manager start.
Definition: EnigmaIOTNode.h:727
setup
void setup()
Definition: EnigmaIOT-Button-Controller.cpp:101
EnigmaIOTjsonController::processRxCommand
virtual bool processRxCommand(const uint8_t *mac, const uint8_t *buffer, uint8_t length, nodeMessageType_t command, nodePayloadEncoding_t payloadEncoding)=0
Called to process a downlink command.
EnigmaIOTNode.h
Library to build a node for EnigmaIoT system.
EnigmaIOTNodeClass::sendHADiscoveryMessage
bool sendHADiscoveryMessage(const uint8_t *data, size_t len)
Builds, encrypts and sends a HomeAssistant discovery message.
Definition: EnigmaIOTNode.cpp:1764
SLEEPY
#define SLEEPY
Definition: EnigmaIOT-Button-Controller.cpp:40
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
EnigmaIOTjsonController::setup
virtual void setup(EnigmaIOTNodeClass *node, void *config=NULL)=0
Initialize data structures.
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
processRxData
void processRxData(const uint8_t *mac, const uint8_t *buffer, uint8_t length, nodeMessageType_t command, nodePayloadEncoding_t payloadEncoding)
Definition: EnigmaIOT-Button-Controller.cpp:83
data
@ data
Definition: GwOutput_generic.h:23
LED
const int LED
Definition: EnigmaIOT-Button-Controller.cpp:57
Espnow_hal
Espnow_halClass Espnow_hal
Singleton instance of ESP-NOW class.
Definition: espnow_hal.cpp:20
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
wifiManagerStarted
void wifiManagerStarted()
Definition: EnigmaIOT-Button-Controller.cpp:97
MAX_CONSECUTIVE_BOOT
const int MAX_CONSECUTIVE_BOOT
Definition: EnigmaIOT-Button-Controller.cpp:56
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
LED_BUILTIN
#define LED_BUILTIN
Definition: EnigmaIOT-Button-Controller.cpp:43
EnigmaIOTjsonController.h
Prototype for JSON/MSGPACK based controller node.
HA_DISC_TYPE
@ HA_DISC_TYPE
Definition: EnigmaIOTNode.h:71
sendUplinkData
bool sendUplinkData(const uint8_t *data, size_t len, nodePayloadEncoding_t payloadEncoding, dataMessageType_t dataMsgType)
Definition: EnigmaIOT-Button-Controller.cpp:72
RESET_PIN
#define RESET_PIN
Definition: EnigmaIOT-Button-Controller.cpp:53
espnow_hal.h
ESP-NOW communication system abstraction layer. To be used on ESP8266 or ESP32 platforms.
status
@ status
Definition: GwOutput_generic.h:25
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
EnigmaIOTjsonController
Definition: EnigmaIOTjsonController.h:34