vintlabs / fauxmoESP

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

IF Else Statement and Fauxmo #51

Closed pvint closed 5 years ago

pvint commented 6 years ago

Original report by Matthew Paul Cupper (Bitbucket: mpcpro, GitHub: mpcpro).


Hi.

The problem I am having is that I am using the Example sketch and added the ESP8266 HTTPClient Library, and an If, Else Statement which is not working correctly.

If I tell Alexa "Power One On", Alexa will then tell the ESP8266 to turn on the onboard LED light. I made an If/Else statement based on if Power One is ON or OFF. If it is on, I want the ESP8266 to call a webpage (HTTP.GET) to activate another smart device in my house, and if "Power One Off" I want the ESP8266 to call another webpage.

Everything seems to work except for the IF / Else statement, since it is not calling the webpage I listed in the code, and thus nothing other then the onboard LED is getting activated or deactivated.

Wondering if anyone could look over my IF ELse statement and see if there is anything wrong.

#!arduino

fauxmo.onSetState([](unsigned char device_id, const char * device_name, bool state) {
    Serial.printf("[MAIN] Device #%d (%s) state: %s\n", device_id, device_name, state ? "ON" : "OFF");
    digitalWrite(LED, !state);

    if( (strcmp(device_name, "Power One") == 0)){
      if(state){ 
        HTTPClient http;        
        http.begin("http://192.168.1.230/?2-on");
        http.GET();            
        http.end(); 
       // 
        } else{
        HTTPClient http;        
        http.begin("http://192.168.1.230/?2-off");
        http.GET();            
        http.end(); 
        //
          }
      }//end of if

});
pvint commented 6 years ago

Original comment by debdale (Bitbucket: Debdale, ).


hi Mathew,

i'm trying to do a similar thing. i'm tying to POST a API to my raspberry pi, I've noticed if you move your (GET snippet/block) of code and place it anywhere else in the same sketch it will work.

also have you replaced the GET with POST just to see what happens?

pvint commented 6 years ago

Original comment by debdale (Bitbucket: Debdale, ).


looking into this further i think its also a library issue in a sense that the fauxmoESP.h and the ESP8266HTTPClient.h do not play well together??

i think there has been issues before with other HTTP/server libraries conflicting.

pvint commented 6 years ago

Original comment by Michael Gates (Bitbucket: MadBanana, GitHub: MadBanana).


I seem to be experiencing similar. FAUXMO works perfectly alone and the example behaves as it should. ESP8266HTTPClient also works correctly, sending HTTP commands to my Sonoff plugs.

The two together just will not work - I don't get any errors, FAUXMO toggles the LED state and sends to serial the change of state details but no HTTP string gets sent.

Glad it isn't just me ...

pvint commented 6 years ago

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


Try not to add too much code inside the callback, rise a flag instead and handle it from the main loop.

pvint commented 6 years ago

Original comment by Michael Gates (Bitbucket: MadBanana, GitHub: MadBanana).


Yes, thanks - I'd been restructuring the code for a while (don't have much spare time!) and it seems to work ok. Only using two devices and occasionally it hangs but much better than before.