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
Other
236 stars 125 forks source link

sinric lock momentary switch #153

Closed Dom2955774 closed 3 years ago

Dom2955774 commented 3 years ago

hi, when i press unlock on sinric pro my relay switches for 3 seconds and then goes back to a locked state, the problem im having is that sinric pro stays on the unlocked state. when i press the unlock button on sinric pro i need sinric pro to go back to the locked state after. here is my code

define ENABLE_DEBUG

ifdef ENABLE_DEBUG

   #define DEBUG_ESP_PORT Serial
   #define NODEBUG_WEBSOCKETS
   #define NDEBUG

endif

include

ifdef ESP8266

   #include <ESP8266WiFi.h>

endif

ifdef ESP32

   #include <WiFi.h>

endif

include "SinricPro.h"

include "SinricProLock.h"

define WIFI_SSID ""

define WIFI_PASS ""

define APP_KEY ""

define APP_SECRET ""

define LOCK_ID ""

define BAUD_RATE 115200

define relay D7

bool onLockState(String deviceId, bool &lockState) { if (lockState == false) { digitalWrite(relay,HIGH); delay(3000); digitalWrite(relay,LOW); } Serial.printf("Device %s is %s\r\n", deviceId.c_str(), lockState?"locked":"unlocked"); return true; }

void setupWiFi() { Serial.printf("\r\n[Wifi]: Connecting"); WiFi.begin(WIFI_SSID, WIFI_PASS);

while (WiFi.status() != WL_CONNECTED) { Serial.printf("."); delay(250); } IPAddress localIP = WiFi.localIP(); Serial.printf("connected!\r\n[WiFi]: IP-Address is %d.%d.%d.%d\r\n", localIP[0], localIP[1], localIP[2], localIP[3]); }

void setupSinricPro() { SinricProLock &myLock = SinricPro[LOCK_ID]; myLock.onLockState(onLockState);

// setup SinricPro SinricPro.onConnected([](){ Serial.printf("Connected to SinricPro\r\n"); }); SinricPro.onDisconnected([](){ Serial.printf("Disconnected from SinricPro\r\n"); }); SinricPro.begin(APP_KEY, APP_SECRET); }

void setup() { pinMode(relay, OUTPUT); Serial.begin(BAUD_RATE); Serial.printf("\r\n\r\n"); setupWiFi(); setupSinricPro(); }

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

sivar2311 commented 3 years ago
bool onLockState(String deviceId, bool &lockState) {
  if (lockState == false) {
    digitalWrite(relay,HIGH);
    Serial.println("Lock is unlocked");
    delay(3000);
    digitalWrite(relay,LOW);
    Serial.println("Lock is locked again");
    lockState = true; // <-- let the server know that the lock is locked again
  }
  return true;
}
Dom2955774 commented 3 years ago

100% thank you so much Savir2311