Closed ghost closed 3 years ago
Instead of the while loops inside the loop()
function you should use SinricPro.onConnected()
and SinricPro.onDisconnected()
to recognize a connect / disconnect.
See here for more details.
Your code will conflict with restoreDeviceStates(true)
You need to implement a logic which detects a) the ESP was rebooting (first boot, reboot after powerloss or someting like this) b) the the wifi / internet connection was lost
Depending to one of the cases above you need to priorize the local state (Flip switches) or the remote state (SinricPro)
One possibility could be to implement a simple counter which is incremented in the onConnected callback. This counter must be checked inside your onPowerState callback as well.
Addendum:
To avoid the conflict with restoreDeviceStates you should call SinricPro.restoreDeviceStates(false)
inside the onConnected callback to turn off this functionality. Doing this, only on the first boot (or after a powerloss) of the ESP restoreDeviceStates will be used.
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!
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!
Hello... want to control switch using both sinricpro and manual flip switch.When connectes to wifi it works fine.But when disconnected it works well with flip switches.After reconnected to wifi unable to synchronise state of flip switch and sinric pro...
Help me in sendpowerstateevent....
include
include
include
include
define WIFI_SSID "realme"
define WIFI_PASS "12345678"
define APP_KEY ""
define APP_SECRET ""
define DEVICE_ID1 ""
define DEVICE_ID2 ""
define RELAY_SWITCH1 D2
define RELAY_SWITCH2 D3
define RELAY_PIN1 D0
define RELAY_PIN2 D1
define WIFI_LED D4
bool onPowerState(const String &deviceId, bool &state) { if (deviceId == DEVICE_ID1) { digitalWrite(RELAY_PIN1, !state); return true; } else if (deviceId == DEVICE_ID2) {
} }
SinricProSwitch& mySwitch1 = SinricPro[DEVICE_ID1]; SinricProSwitch& mySwitch2 = SinricPro[DEVICE_ID2];
void setup() {
Serial.begin(115200);
pinMode(RELAY_PIN1, OUTPUT); pinMode(RELAY_PIN2, OUTPUT); pinMode(WIFI_LED, OUTPUT);
pinMode(RELAY_SWITCH1, INPUT_PULLUP); pinMode(RELAY_SWITCH2, INPUT_PULLUP); digitalWrite(RELAY_PIN1, HIGH); digitalWrite(RELAY_PIN2, HIGH);
digitalWrite(WIFI_LED, HIGH);
WiFi.begin(WIFI_SSID, WIFI_PASS);
int count=0; while (WiFi.status() != WL_CONNECTED) { delay(100); count++; if(count==20) break; }
mySwitch1.onPowerState(onPowerState); mySwitch2.onPowerState(onPowerState);
SinricPro.begin(APP_KEY, APP_SECRET); SinricPro.restoreDeviceStates(true); mySwitch1.sendPowerStateEvent(!digitalRead(RELAY_SWITCH1));
mySwitch2.sendPowerStateEvent(!digitalRead(RELAY_SWITCH2));
}
bool lastState1 = digitalRead(RELAY_SWITCH1); bool lastState2 = digitalRead(RELAY_SWITCH2); void handleFlipSwitches() {
bool newRelayState1 = digitalRead(RELAY_SWITCH1); bool newRelayState2 = digitalRead(RELAY_SWITCH2);
if (lastState1 != newRelayState1) { digitalWrite(RELAY_PIN1, newRelayState1); mySwitch1.sendPowerStateEvent(!newRelayState1); } if (lastState2 != newRelayState2) { digitalWrite(RELAY_PIN2, newRelayState2); mySwitch2.sendPowerStateEvent(!newRelayState2); }
lastState1 = newRelayState1; lastState2 = newRelayState2; }
void loop() { if (WiFi.status() == WL_CONNECTED) {
} else { digitalWrite(WIFI_LED, HIGH); digitalWrite(RELAY_PIN1, digitalRead(RELAY_SWITCH1)); digitalWrite(RELAY_PIN2, digitalRead(RELAY_SWITCH2)); } }