vintlabs / fauxmoESP

Add voice control of your ESP32 and ESP8266 devices using Amazon Alexa
MIT License
383 stars 69 forks source link

How to get state of device #70

Closed pvint closed 3 years ago

pvint commented 5 years ago

Original report by Adam Byers (Bitbucket: adamslab, GitHub: adamslab).


I see that in 3.1.0 fauxmo.setState is available to set the sate of a device if it's manipulated outside Alexa. What I can't see to figure out though is how to get the device state from fauxmo.

Basically, I have a setup where I have a light connected to the ESP8266 and I want to have a manual push button to turn the light on/off. I have basic code in place that will accomplish this and update Alexa with the state but if I turn on/off the light with the button eventually it all gets out of sync so that I end up having to push the button twice to get the light to turn on or off.

What I'd like is to be able to get the state of the light (on or off) and then have the button toggle the state of the light. Push the button, if the light is on, turn it off, if it's off, turn it on.

pvint commented 5 years ago

Original comment by Xose Pérez (Bitbucket: [Xose Pérez](https://bitbucket.org/Xose Pérez), ).


The setState won't update Alexa of the status in real time, instead, it will wait for the next Alexa request for the status to update it. So, yes, there is a while where the status of the light and the status in the Alexa app won't be in sync. Since the API is only a pull API there is no way to push the status of the device to Alexa right way.

pvint commented 5 years ago

Original comment by Adam Byers (Bitbucket: adamslab, GitHub: adamslab).


I may have not asked my question clearly. In fact, when I use setState I see that Alexa gets the state update pretty quick... I'll include my code (apologies) to help explain what I'm after. This is running inside the loop on the ESP8266 that the fauxmo code is running on.

#!arduino

  SWreading = digitalRead(CompManualSW);

  if (SWreading == HIGH && SWprevious == LOW && millis() - SWtime > Debounce) {
    if (SWstate == HIGH)
      SWstate = LOW;
    else
      SWstate = HIGH;

    if (SWstate == HIGH) {
      fauxmo.setState(ID_LightRly, false, 0);
    }

    if (SWstate == LOW) {
      fauxmo.setState(ID_LightRly, true, 255);
    }
    SWtime = millis();
  }

  digitalWrite(LightRly, SWstate);

  SWprevious = SWreading;

  Serial.println(SWstate);
  delay(100);
}

OK, hopefully it's somewhat clear what I'm doing there. I have a device (a light) that I'm controlling via fauxmo... there is also a momentary switch that I'm using to toggle the light on/off. I can successfully use the switch to turn the light on/off and using setState update the state of the light to Alexa. What I'm seeing though is that eventually this gets out of sync. So the light is on but Alexa shows that it's off, etc. What I was thinking was if there was a way to query faxmo as to the current state of the light (or at least what it thinks the state is) and then use the same basic code I have above, but instead of setting the device based on the last switch state, set it based off the state fauxmo reports... So if the switch is flipped, and fauxmo reports that the light is on, turn it off (and update the state with setState). If the switch is flipped and fauxmo reports that the light is off, turn it off (and update the state with setState).

Or, are you saying that the sync issue I'm seeing will eventually correct itself it once Alexa queries fauxmo for an update (again, I'm seeing near real time updates when using setState, it's just that at some point the status that shows in Alexa is incorrect to the actual state of the light).

Thank you

pvint commented 5 years ago

Original comment by Xose Pérez (Bitbucket: [Xose Pérez](https://bitbucket.org/Xose Pérez), ).


Well, I guess you also have something like this in your code, right?

fauxmo.onSetState([](unsigned char device_id, const char * device_name, bool state, unsigned char value) {
    if (device_id == ID_LightRly) {
        SWstate = state;
    }
});

This is where you get the notification changes from Alexa. This way the communication is in both directions, you get the status changes from Alexa with onSetState and you report when it changes because the switch has been released with setState.

pvint commented 3 years ago

Closing stale issue.