sinricpro / esp8266-esp32-sdk

Library for https://sinric.pro - simple way to connect your device to Alexa, Google Home, SmartThings and cloud
https://sinric.pro
228 stars 121 forks source link

ESP32 + Sinric + manual button issue #271

Closed Jaykumar365 closed 2 years ago

Jaykumar365 commented 2 years ago

`//Library used: //ArduinoJson Library: https://github.com/bblanchon/ArduinoJson //SinricPro Library: https://sinricpro.github.io/esp8266-esp32-sdk/ //arduinoWebSockets Library: https://github.com/Links2004/arduinoWebSockets

include

include

include "SinricPro.h"

include "SinricProSwitch.h"

include

define ENABLE_DEBUG

ifdef ENABLE_DEBUG

   #define DEBUG_ESP_PORT Serial
   #define NODEBUG_WEBSOCKETS             
   #define NDEBUG

endif

define WIFI_SSID "Vivo" //Enter WiFi Name

define WIFI_PASS "Password" //Enter WiFi Password

define APP_KEY "673ce759-8717-43b7-8a59-xxxxxxxxxx" //Enter APP-KEY

define APP_SECRET "c3ce1baf-d4e2-480c-ac48-xxxxxxxx" //Enter APP-SECRET

//Enter the device IDs here

define device_ID_1 "62711306057c4d2f4a452xxx" //SWITCH ID for Fan

define device_ID_2 "6278c189a6872f36392xxxxx" //SWITCH ID for Dim Light

define device_ID_3 "627112d3b1e204dxxxxxxxxx" //SWITCH ID for light

// define the GPIO connected with Relays and switches

define RelayPin1 23 //D23

define RelayPin2 22 //D22

define RelayPin3 21 //D21

define SwitchPin1 13 //D13

define SwitchPin2 12 //D12

define SwitchPin3 14 //D14

define wifiLed 2 //D2

//uncomment the following line if you use Push Buttons to toggle Relays //#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;

std::map<String, deviceConfig_t> devices = { //{deviceId, {relayPIN, flipSwitchPIN}} {device_ID_1, { RelayPin1, SwitchPin1 }}, {device_ID_2, { RelayPin2, SwitchPin2 }}, {device_ID_3, { RelayPin3, SwitchPin3 }}, };

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 digitalWrite(relayPIN, HIGH); } }

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_PULLUP);                   // 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(wifiLed, 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);

pinMode(wifiLed, OUTPUT); digitalWrite(wifiLed, LOW);

setupRelays(); setupFlipSwitches(); setupWiFi(); setupSinricPro(); }

void loop() { SinricPro.handle(); handleFlipSwitches(); }

// I can control the first relay that is device ID 1 //but cannot control it manually. //What is wrong here

sivar2311 commented 2 years ago

Maybe you missed the lines 42 and 43?

stale[bot] commented 2 years ago

This issue has gone quiet. Spooky quiet. We currently close issues after 14 days of inactivity. It’s been at least 7 days since the last update here. If we missed this issue or if you want to keep it open, please reply here. As a friendly reminder, the best way to fix this or any other problem is to provide a detailed error description including a serial log. Thanks for being a part of the SinricPro community!

stale[bot] commented 2 years ago

Hey again! It’s been 14 days since anything happened on this issue, so our friendly robot (that’s me!) is going to close it. Please keep in mind that I’m only a robot, so if I’ve closed this issue in error, I’m HUMAN_EMOTION_SORRY. Please feel free to comment on this issue or create a new one if you need anything else. As a friendly reminder, the best way to fix this or any other problem is to provide a detailed error description including a serial log. Thanks again for being a part of the SinricPro community!