EnigmaIOT  0.9.8
Secure sensor and gateway platform based on ESP8266 and ESP32
SmartSwitchController.h
Go to the documentation of this file.
1 // BasicController.h
2 
3 #ifndef _BASICCONTROLLER_h
4 #define _BASICCONTROLLER_h
5 
6 #if defined(ARDUINO) && ARDUINO >= 100
7 #include "Arduino.h"
8 #else
9 #include "WProgram.h"
10 #endif
11 
12 //#define DEBUG_SERIAL
13 
14 #ifdef ESP32
15 #include <SPIFFS.h>
16 #endif
17 
19 #define CONTROLLER_CLASS_NAME SmartSwitchController
20 static const char* CONTROLLER_NAME = "SamrtSwitch controller";
21 
22 #if SUPPORT_HA_DISCOVERY
23 #include <haTrigger.h>
24 #include <haSwitch.h>
25 #endif
26 
27 // --------------------------------------------------
28 // You may define data structures and constants here
29 // --------------------------------------------------
30 #define DEFAULT_BUTTON_PIN 4
31 #define DEFAULT_RELAY_PIN 14
32 #define ON HIGH
33 #define OFF !ON
34 
35 typedef enum {
36  RELAY_OFF = 0,
37  RELAY_ON = 1,
40 
42  int relayPin;
44  uint8_t buttonPin;
45  bool linked;
47  int ON_STATE;
48 };
49 
51 protected:
52  // --------------------------------------------------
53  // add all parameters that your project needs here
54  // --------------------------------------------------
55  bool pushTriggered = false;
56  bool pushReleased = true;
58  AsyncWiFiManagerParameter* buttonPinParam;
59  AsyncWiFiManagerParameter* relayPinParam;
60  AsyncWiFiManagerParameter* bootStatusParam;
61  AsyncWiFiManagerParameter* bootStatusListParam;
62 
63 public:
64  void setup (EnigmaIOTNodeClass* node, void* data = NULL);
65 
66  bool processRxCommand (const uint8_t* address, const uint8_t* buffer, uint8_t length, nodeMessageType_t command, nodePayloadEncoding_t payloadEncoding);
67 
68  void loop ();
69 
71 
77 
83 
88  bool loadConfig ();
89 
90  void connectInform ();
91 
92 protected:
97  bool saveConfig ();
98 
99  bool sendCommandResp (const char* command, bool result);
100 
102  // You can send a 'hello' message when your node starts. Useful to detect unexpected reboot
103  const size_t capacity = JSON_OBJECT_SIZE (10);
104  DynamicJsonDocument json (capacity);
105  json["status"] = "start";
106  json["device"] = CONTROLLER_NAME;
107  char version_buf[10];
108  snprintf (version_buf, 10, "%d.%d.%d",
110  json["version"] = String (version_buf);
111 
112  return sendJson (json);
113  }
114 
122 
123  // ------------------------------------------------------------
124  // You may add additional method definitions that you need here
125  // ------------------------------------------------------------
126  void defaultConfig ();
127 
128  void toggleRelay ();
129 
130  void setRelay (bool state);
131 
132  bool sendRelayStatus ();
133 
134  void setLinked (bool state);
135 
136  bool sendLinkStatus ();
137 
138  void setBoot (int state);
139 
140  bool sendBootStatus ();
141 };
142 
143 #endif
144 
smartSwitchControllerHw_t::bootStatus
bootRelayStatus_t bootStatus
Definition: SmartSwitchController.h:46
CONTROLLER_CLASS_NAME::setRelay
void setRelay(bool state)
Definition: SmartSwitchController.cpp:227
CONTROLLER_CLASS_NAME::configManagerStart
void configManagerStart()
Called when wifi manager starts config portal.
nodeMessageType
nodeMessageType
Message code definition.
Definition: EnigmaIOTNode.h:35
CONTROLLER_CLASS_NAME::setBoot
void setBoot(int state)
Definition: SmartSwitchController.cpp:250
SAVE_RELAY_STATUS
@ SAVE_RELAY_STATUS
Definition: SmartSwitchController.h:38
CONTROLLER_CLASS_NAME::loop
void loop()
This should be called periodically for module handling.
CONTROLLER_CLASS_NAME::config
smartSwitchControllerHw_t config
Definition: SmartSwitchController.h:57
smartSwitchControllerHw_t::buttonPin
uint8_t buttonPin
Definition: SmartSwitchController.h:44
ENIGMAIOT_PROT_VERS
static const uint8_t ENIGMAIOT_PROT_VERS[3]
EnitmaIoT Version.
Definition: EnigmaIoTconfig.h:16
CONTROLLER_CLASS_NAME::processRxCommand
bool processRxCommand(const uint8_t *address, const uint8_t *buffer, uint8_t length, nodeMessageType_t command, nodePayloadEncoding_t payloadEncoding)
Called to process a downlink command.
smartSwitchControllerHw_t::ON_STATE
int ON_STATE
Definition: SmartSwitchController.h:47
CONTROLLER_CLASS_NAME::saveConfig
bool saveConfig()
Saves output module configuration.
CONTROLLER_CLASS_NAME::buildHATriggerDiscovery
void buildHATriggerDiscovery()
CONTROLLER_CLASS_NAME::defaultConfig
void defaultConfig()
Definition: SmartSwitchController.cpp:382
haSwitch.h
Home Assistant switch integration.
CONTROLLER_CLASS_NAME
Definition: ButtonController.h:27
EnigmaIOTjsonController::sendJson
bool sendJson(DynamicJsonDocument &json)
Sends a JSON encoded message to lower layer.
Definition: EnigmaIOTjsonController.h:170
CONTROLLER_CLASS_NAME::pushTriggered
bool pushTriggered
Definition: ButtonController.h:32
smartSwitchControllerHw_t::relayStatus
bool relayStatus
Definition: SmartSwitchController.h:43
CONTROLLER_CLASS_NAME::pushReleased
bool pushReleased
Definition: ButtonController.h:33
CONTROLLER_CLASS_NAME::sendCommandResp
bool sendCommandResp(const char *command, bool result)
Sends command processing response acknowledge.
CONTROLLER_CLASS_NAME::~CONTROLLER_CLASS_NAME
~CONTROLLER_CLASS_NAME()
RELAY_ON
@ RELAY_ON
Definition: SmartSwitchController.h:37
CONTROLLER_CLASS_NAME::sendRelayStatus
bool sendRelayStatus()
Definition: SmartSwitchController.cpp:138
nodePayloadEncoding_t
nodePayloadEncoding_t
Definition: EnigmaIOTNode.h:58
CONTROLLER_CLASS_NAME::buttonPinParam
AsyncWiFiManagerParameter * buttonPinParam
Definition: SmartSwitchController.h:58
CONTROLLER_CLASS_NAME::buildHALinkDiscovery
void buildHALinkDiscovery()
RELAY_OFF
@ RELAY_OFF
Definition: SmartSwitchController.h:36
haTrigger.h
Home Assistant trigger integration.
bootRelayStatus_t
bootRelayStatus_t
Definition: SmartSwitchController.h:35
CONTROLLER_CLASS_NAME::connectInform
void connectInform()
Used to notify controller that it is registered on EnigmaIOT network.
smartSwitchControllerHw_t::linked
bool linked
Definition: SmartSwitchController.h:45
CONTROLLER_CLASS_NAME::configManagerExit
void configManagerExit(bool status)
Called when wifi manager exits config portal.
CONTROLLER_CLASS_NAME::bootStatusListParam
AsyncWiFiManagerParameter * bootStatusListParam
Definition: SmartSwitchController.h:61
CONTROLLER_NAME
static const char * CONTROLLER_NAME
Definition: SmartSwitchController.h:20
data
@ data
Definition: GwOutput_generic.h:23
smartSwitchControllerHw_t
Definition: SmartSwitchController.h:41
CONTROLLER_CLASS_NAME::sendBootStatus
bool sendBootStatus()
Definition: SmartSwitchController.cpp:160
CONTROLLER_CLASS_NAME::buildHASwitchDiscovery
void buildHASwitchDiscovery()
Sends a HA discovery message for a single entity. Add as many functions like this as number of entiti...
CONTROLLER_CLASS_NAME::bootStatusParam
AsyncWiFiManagerParameter * bootStatusParam
Definition: SmartSwitchController.h:60
EnigmaIOTNodeClass
Main node class. Manages communication with gateway and allows sending and receiving user data.
Definition: EnigmaIOTNode.h:134
smartSwitchControllerHw_t::relayPin
int relayPin
Definition: SmartSwitchController.h:42
CONTROLLER_CLASS_NAME::setup
void setup(EnigmaIOTNodeClass *node, void *data=NULL)
Initialize data structures.
CONTROLLER_CLASS_NAME::setLinked
void setLinked(bool state)
Definition: SmartSwitchController.cpp:240
CONTROLLER_CLASS_NAME::sendLinkStatus
bool sendLinkStatus()
Definition: SmartSwitchController.cpp:150
EnigmaIOTjsonController.h
Prototype for JSON/MSGPACK based controller node.
CONTROLLER_CLASS_NAME::sendStartAnouncement
bool sendStartAnouncement()
Send a message to notify node has started running.
Definition: SmartSwitchController.h:101
CONTROLLER_CLASS_NAME::relayPinParam
AsyncWiFiManagerParameter * relayPinParam
Definition: SmartSwitchController.h:59
CONTROLLER_CLASS_NAME::toggleRelay
void toggleRelay()
Definition: SmartSwitchController.cpp:213
status
@ status
Definition: GwOutput_generic.h:25
CONTROLLER_CLASS_NAME::loadConfig
bool loadConfig()
Loads output module configuration.
EnigmaIOTjsonController
Definition: EnigmaIOTjsonController.h:34