Giter VIP home page Giter VIP logo

Comments (2)

sivar2311 avatar sivar2311 commented on June 26, 2024 2

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

from esp8266-esp32-sdk.

kakopappa avatar kakopappa commented on June 26, 2024

Or you could use a Auto On/Off timer

from esp8266-esp32-sdk.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.