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
227 stars 121 forks source link

Emulating a momentary switch with Sinric Pro #377

Closed statticus19 closed 1 month ago

statticus19 commented 1 month ago

This is my first arduino project and I'm trying to fake a momentary switch with an esp32 board. I've gotten most of the way there by adding a few lines to the zero code provided by the switch template from Sinric Pro. My main problem is that after the switch is tripped, it turns on and then off, but the signal is not sent back to Sinric that the switch is now back to an off state. So it is stuck in an on state when looking at the Sinric app.

Here's my code so far:

bool onPowerState1(const String &deviceId, bool &state) {

//trying to make switch momentary

if (state == true) {
digitalWrite(RELAYPIN_1,HIGH);
delay(500);
digitalWrite(RELAYPIN_1,LOW);
}
Serial.printf("Device 1 turned %s", deviceId.c_str(), state?"on":"off");
return false;
}

//end attempt at momentary switch

// setup function for WiFi connection
void setupWiFi() {
  Serial.printf("\r\n[Wifi]: Connecting");

  #if defined(ESP8266)
    WiFi.setSleepMode(WIFI_NONE_SLEEP); 
    WiFi.setAutoReconnect(true);
  #elif defined(ESP32)
    WiFi.setSleep(false); 
    WiFi.setAutoReconnect(true);
  #endif

  WiFi.begin(WIFI_SSID, WIFI_PASS);

  while (WiFi.status() != WL_CONNECTED) {
    Serial.printf(".");
    delay(250);
  }

  Serial.printf("connected!\r\n[WiFi]: IP-Address is %s\r\n", WiFi.localIP().toString().c_str());
}

// setup function for SinricPro
void setupSinricPro() {
  // add devices and callbacks to SinricPro
  pinMode(RELAYPIN_1, OUTPUT);

  SinricProSwitch& mySwitch1 = SinricPro[SWITCH_ID_1];
  mySwitch1.onPowerState(onPowerState1);

  // 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);
}

// main setup function
void setup() {
  Serial.begin(BAUD_RATE); Serial.printf("\r\n\r\n");
  setupWiFi();
  setupSinricPro();
}

void loop() {
  SinricPro.handle();

}
sivar2311 commented 1 month ago

Hi @statticus19 !

You can return the current state in the state variable. That's the reason why state is provided by reference and not by value to the callback. The solution for your "fake momentary switch" will look like so:

bool onPowerState(const String &deviceId, bool &state) {
  Serial.printf("Device \"%s\" turned %s\r\n", deviceId.c_str(), state?"on":"off");

  if (state == true) {
    digitalWrite(RELAYPIN_1,HIGH);
    delay(500);
    digitalWrite(RELAYPIN_1,LOW);
    state = false; // set back to off-state
  }
  return true; 
}

Note: The callback must return true to let SinricPro know that the callback was executed correctly.

Your code uses delay(500); which blocks the entire microcontroller for half a second. This is okay for a few milliseconds, but may lead to problems with longer delay times.

Below I have written an example which works without delay() to switch off the relay after a timer has expired.

The code is written in a "human readable style". Please read onPowerState() first, then handleRelayTimer() and continue with the small helper functions.

#include <Arduino.h>
#include <SinricPro.h>
#include <SinricProSwitch.h>
#include <WiFi.h>

const int   RELAYPIN  = 13;
const char* WIFI_SSID = "YOUR_WIFI_SSID_HERE";
const char* WIFI_PASS = "YOUR_WIFI_PASS_HERE";

const char* APP_KEY    = "YOUR_APP_KEY_HERE";
const char* APP_SECRET = "YOUR_APP_SECRET_HERE";
const char* SWITCH_ID  = "YOUR_DEVICEID_HERE";

const int BAUD_RATE = 115200;

const unsigned long relayTimeoutDuration = 500;

unsigned long relayTimer  = 0;

void turnRelayOn() {
    digitalWrite(RELAYPIN, HIGH);
    Serial.println("Relay turned on");
}

void turnRelayOff() {
    digitalWrite(RELAYPIN, LOW);
    Serial.println("Relay turned off");
}

void startRelayTimer() {
    unsigned long now = millis();
    relayTimer        = now;

    Serial.print("Relay timer started: ");
    Serial.println(now);
}

void stopRelayTimer() {
    relayTimer = 0;

    Serial.print("Relay timer stopped: ");
    Serial.println(millis());
}

bool relayTimerIsExpired() {
    unsigned long now = millis();
    return (now - relayTimer >= relayTimeoutDuration);
}

bool relayTimerIsRunning() {
    return relayTimer > 0;
}

void handleRelayTimer() {
    if (relayTimerIsRunning() && relayTimerIsExpired()) {
        stopRelayTimer();
        turnRelayOff();
    }
}

bool onPowerState(const String& deviceId, bool& state) {
    Serial.printf("Device \"%s\" turned %s\r\n", deviceId.c_str(), state ? "on" : "off");
    bool relayShouldTurnOn = state == true;

    if (relayShouldTurnOn) {
        startRelayTimer();
        turnRelayOn();

        state = false;
    }

    return true;
}

void setupWiFi() {
    Serial.printf("\r\n[Wifi]: Connecting");

#if defined(ESP8266)
    WiFi.setSleepMode(WIFI_NONE_SLEEP);
    WiFi.setAutoReconnect(true);
#elif defined(ESP32)
    WiFi.setSleep(false);
    WiFi.setAutoReconnect(true);
#endif

    WiFi.begin(WIFI_SSID, WIFI_PASS);

    while (WiFi.status() != WL_CONNECTED) {
        Serial.printf(".");
        delay(250);
    }

    Serial.printf("connected!\r\n[WiFi]: IP-Address is %s\r\n", WiFi.localIP().toString().c_str());
}

void setupSinricPro() {
    pinMode(RELAYPIN, OUTPUT);

    SinricProSwitch& mySwitch1 = SinricPro[SWITCH_ID];
    mySwitch1.onPowerState(onPowerState);

    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() {
    Serial.begin(BAUD_RATE);
    Serial.printf("\r\n\r\n");
    setupWiFi();
    setupSinricPro();
}

void loop() {
    SinricPro.handle();
    handleRelayTimer();
}
kakopappa commented 1 month ago

Or you could use a Auto On/Off timer