HOME-AUTOMATION USING GOOGLE HOME, NODEMCU, SINRIC PRO






Libraries to Add:

* ArduinoJSON
* WEBSOCKETS
* SinricPro_Generic


// **************************** CODE BEGIN ***********************************

#ifdef ENABLE_DEBUG
        #define DEBUG_ESP_PORT Serial
        #define NODEBUG_WEBSOCKETS
        #define NDEBUG
 #endif  
 
 #include <Arduino.h>
 #ifdef ESP8266  
        #include <ESP8266WiFi.h>
 #endif  
 #ifdef ESP32    
        #include <WiFi.h>
 #endif
 
 #include "SinricPro.h"
 #include "SinricProSwitch.h"
 
 #include <map>
 
 #define WIFI_SSID         "hello"     
 #define WIFI_PASS         "helloworld"
 #define APP_KEY           "6fa2c48b-b08b-438f-9d93-32064ae6fc94"       
 #define APP_SECRET        "850fa29f-a022-400f-b25b-52ab04621c41-d83afab4-ba1c-434e-8d66-a77244ad3c23"   
 // comment the following line if you use a toggle switches instead of tactile buttons
 //#define TACTILE_BUTTON 1
 
 #define BAUD_RATE   9600
 
 #define DEBOUNCE_TIME 250
 
 typedef struct {      // struct for the std::map below
   int relayPIN;
   int flipSwitchPIN;
 } deviceConfig_t;
 
 
 // this is the main configuration
 // please put in your deviceId, the PIN for Relay and PIN for flipSwitch
 // this can be up to N devices...depending on how much pin's available on your device ;)
 // right now we have 4 devicesIds going to 4 relays and 4 flip switches to switch the relay manually
 std::map<String, deviceConfig_t> devices = {
     //{deviceId, {relayPIN to be connected,  flipSwitchPIN}}
     {"62c0162ead95bfbcdf34503a", {  D1, D8 }},
     {"62c01650ad95bfbcdf345066", {  D2, D7 }},
     {"62c015cb0aec232058f3f638", {  D3, D6 }},
     {"SWITCH_ID_NO_4_HERE", {  D4, D5 }}       
 };
 
 typedef struct {      // struct for the std::map below
   String deviceId;
   bool lastFlipSwitchState;
   unsigned long lastFlipSwitchChange;
 } flipSwitchConfig_t;
 
 std::map<int, flipSwitchConfig_t> flipSwitches;    // this map is used to map flipSwitch PINs to deviceId and handling debounce and last flipSwitch state checks
                                                   // it will be setup in "setupFlipSwitches" function, using informations from devices map
 
 void setupRelays() {  
   for (auto &device : devices) {           // for each device (relay, flipSwitch combination)
     int relayPIN = device.second.relayPIN; // get the relay pin
     pinMode(relayPIN, OUTPUT);             // set relay pin to OUTPUT
   }
 }
 
 void setupFlipSwitches() {
   for (auto &device : devices)  {                     // for each device (relay / flipSwitch combination)
     flipSwitchConfig_t flipSwitchConfig;              // create a new flipSwitch configuration
 
     flipSwitchConfig.deviceId = device.first;         // set the deviceId
     flipSwitchConfig.lastFlipSwitchChange = 0;        // set debounce time
     flipSwitchConfig.lastFlipSwitchState = false;     // set lastFlipSwitchState to false (LOW)
 
     int flipSwitchPIN = device.second.flipSwitchPIN;  // get the flipSwitchPIN
 
     flipSwitches[flipSwitchPIN] = flipSwitchConfig;   // save the flipSwitch config to flipSwitches map
     pinMode(flipSwitchPIN, INPUT);                   // set the flipSwitch pin to INPUT
   }
 }
 
 bool onPowerState(String deviceId, bool &state)
 {
   Serial.printf("%s: %s\r\n", deviceId.c_str(), state ? "on" : "off");
   int relayPIN = devices[deviceId].relayPIN; // get the relay pin for corresponding device
   digitalWrite(relayPIN, !state);             // set the new relay state
   return true;
 }
 
 void handleFlipSwitches() {
   unsigned long actualMillis = millis();                                          // get actual millis
   for (auto &flipSwitch : flipSwitches) {                                         // for each flipSwitch in flipSwitches map
     unsigned long lastFlipSwitchChange = flipSwitch.second.lastFlipSwitchChange;  // get the timestamp when flipSwitch was pressed last time (used to debounce / limit events)
 
     if (actualMillis - lastFlipSwitchChange > DEBOUNCE_TIME) {                    // if time is > debounce time...
 
       int flipSwitchPIN = flipSwitch.first;                                       // get the flipSwitch pin from configuration
       bool lastFlipSwitchState = flipSwitch.second.lastFlipSwitchState;           // get the lastFlipSwitchState
       bool flipSwitchState = digitalRead(flipSwitchPIN);                          // read the current flipSwitch state
       if (flipSwitchState != lastFlipSwitchState) {                               // if the flipSwitchState has changed...
 #ifdef TACTILE_BUTTON
         if (flipSwitchState) {                                                    // if the tactile button is pressed  
 #endif       
           flipSwitch.second.lastFlipSwitchChange = actualMillis;                  // update lastFlipSwitchChange time
           String deviceId = flipSwitch.second.deviceId;                           // get the deviceId from config
           int relayPIN = devices[deviceId].relayPIN;                              // get the relayPIN from config
           bool newRelayState = !digitalRead(relayPIN);                            // set the new relay State
           digitalWrite(relayPIN, newRelayState);                                  // set the trelay to the new state
 
           SinricProSwitch &mySwitch = SinricPro[deviceId];                        // get Switch device from SinricPro
           mySwitch.sendPowerStateEvent(!newRelayState);                            // send the event
 #ifdef TACTILE_BUTTON
         }
 #endif       
         flipSwitch.second.lastFlipSwitchState = flipSwitchState;                  // update lastFlipSwitchState
       }
     }
   }
 }
 
 void setupWiFi()
 {
   Serial.printf("\r\n[Wifi]: Connecting");
   WiFi.begin(WIFI_SSID, WIFI_PASS);
 
   while (WiFi.status() != WL_CONNECTED)
   {
     Serial.printf(".");
     delay(250);
   }
   digitalWrite(LED_BUILTIN, HIGH);
   Serial.printf("connected!\r\n[WiFi]: IP-Address is %s\r\n", WiFi.localIP().toString().c_str());
 }
 
 void setupSinricPro()
 {
   for (auto &device : devices)
   {
     const char *deviceId = device.first.c_str();
     SinricProSwitch &mySwitch = SinricPro[deviceId];
     mySwitch.onPowerState(onPowerState);
   }
 
   SinricPro.begin(APP_KEY, APP_SECRET);
   SinricPro.restoreDeviceStates(true);
 }
 
 void setup()
 {
   Serial.begin(BAUD_RATE);
   setupRelays();
   setupFlipSwitches();
   setupWiFi();
   setupSinricPro();
 }
 
 void loop()
 {
   SinricPro.handle();
   handleFlipSwitches();
 }


// ************************ CODE END *********************

Comments

Popular posts from this blog

IDE SETUP FILES

NON-SERVER CONTROLLER

Print String on 16X2 LCD