Comments (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.
Or you could use a Auto On/Off timer
from esp8266-esp32-sdk.
Related Issues (20)
- Connection HOT 13
- sha1 conflict with rweather/Crypto@^0.4.0 Library in PIO. HOT 2
- Relay status at startup HOT 7
- Capacitive Soil Moisture Sensor data not Reaching Alexa HOT 7
- device turns off for on and on for off what changes should i make in the onPowerstate HOT 2
- Device shows as unresponsive in App and Dashboard after router IP rotation, but is actually responsive HOT 6
- Sin ric pro How do I "mirror" WS2812b led strips with ESP32 HOT 2
- Sinric pro.h HOT 3
- garage status HOT 40
- I'm stuck coding for multiple devices. (ESP8266) HOT 10
- missing capability on AC Unit HOT 3
- Manual Push switch not working when disconnected from internet. HOT 8
- Connection rest by peer HOT 12
- Compilation error: invalid user-defined conversion from 'SINRICPRO_3_0_1::SinricProClass::Proxy' to 'SinricProDoorbell&&' {aka 'SINRICPRO_3_0_1::SinricProDoorbell&&'} [-fpermissive] HOT 5
- Error compiling code - probably upon recent update to ArduinoJSON HOT 2
- sinric app somtimes unresponsive app and serial different values HOT 26
- Sinric Pro With ESP8266 and OLED SSD1306 Display HOT 5
- Enable and Disable A4988 stepper driver with ESP8266 digital pin HOT 2
- Issues with Sending Sensor Data to Sinric Pro from ESP32 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from esp8266-esp32-sdk.