If Statement not working in my ESP8266 project that uses Fauxmo library.

Hi guys!

I programmed an ESP8266 with the Example sketch from Fauxmo, which let's me control the ESP8266 from Alexa by using my voice (turns it into a WEMO).

The example works fine and I am able to turn off and on the onboard LED light by talking to Alexa.

The problem I am having is that I 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.

#include <Arduino.h>
#ifdef ESP32
    #include <WiFi.h>
#else
    #include <ESP8266WiFi.h>
    #include <ESP8266HTTPClient.h>
#endif
#include "fauxmoESP.h"

#define SERIAL_BAUDRATE                 115200
#define LED                             2

fauxmoESP fauxmo;

// -----------------------------------------------------------------------------
// Wifi
// -----------------------------------------------------------------------------

void wifiSetup() {

    // Set WIFI module to STA mode
    WiFi.mode(WIFI_STA);

    // Connect
    Serial.printf("[WIFI] Connecting to %s ", "Wifi");
    WiFi.begin("Badwifi", "secretpass");

    // Wait
    while (WiFi.status() != WL_CONNECTED) {
        Serial.print(".");
        delay(100);
    }
    Serial.println();

    // Connected!
    Serial.printf("[WIFI] STATION Mode, SSID: %s, IP address: %s\n", WiFi.SSID().c_str(), WiFi.localIP().toString().c_str());

}

void setup() {

    // Init serial port and clean garbage
    Serial.begin(SERIAL_BAUDRATE);
    Serial.println();
    Serial.println();

    // Wifi
    wifiSetup();

    // LED
    pinMode(LED, OUTPUT);
    digitalWrite(LED, HIGH);

    // You can enable or disable the library at any moment
    // Disabling it will prevent the devices from being discovered and switched
    fauxmo.enable(true);

    // Add virtual devices
    fauxmo.addDevice("Power One");
	//fauxmo.addDevice("switch two"); // You can add more devices
	//fauxmo.addDevice("switch three");

    // fauxmoESP 2.0.0 has changed the callback signature to add the device_id,
    // this way it's easier to match devices to action without having to compare strings.
    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(); 
           // mySwitch.switchOn("xxx", "xxx");
            } else{
            HTTPClient http;        
            http.begin("http://192.168.1.230/?2-off");
            http.GET();            
            http.end(); 
            //mySwitch.switchOff("xxx", "xxx");
              }
          }//end of if
        
    });

    // Callback to retrieve current state (for GetBinaryState queries)
    fauxmo.onGetState([](unsigned char device_id, const char * device_name) {
        return !digitalRead(LED);
    });

}

void loop() {

    // Since fauxmoESP 2.0 the library uses the "compatibility" mode by
    // default, this means that it uses WiFiUdp class instead of AsyncUDP.
    // The later requires the Arduino Core for ESP8266 staging version
    // whilst the former works fine with current stable 2.3.0 version.
    // But, since it's not "async" anymore we have to manually poll for UDP
    // packets
    fauxmo.handle();

    static unsigned long last = millis();
    if (millis() - last > 5000) {
        last = millis();
        Serial.printf("[MAIN] Free heap: %d bytes\n", ESP.getFreeHeap());
    }

}
  1. You've checked this a Web browser and it works ?
    "http://192.168.1.230/?2-on"

  2. Build some debug print messages in the "if" statement to see how far it gets.

  3. Look at this example of how you could test return codes from http.GET()
    Arduino/BasicHttpClient.ino at master · esp8266/Arduino · GitHub

Hi :wink:

Yes! The url works. I'll try to add debuging messages to see if it even does the if statement.

I'll also have a look at number 3, thanks :slight_smile: