vintlabs / fauxmoESP

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

Alexa app gives the switch as on while is off. #73

Closed pvint closed 3 years ago

pvint commented 5 years ago

Original report by Last (Bitbucket: adelast, ).


Hello to everyone,

first of all thank you very much for fauxmoESP. It works great!

I have a problem when I try to use the Alexa app with the manual switch, once I open the switch it shows it as on while is off and I can't change the status of it because it gives an error. If I use the voice command instead everything works fine. Can you please assist?

#!#include <Arduino.h>
#ifdef ESP32
    #include <WiFi.h>
#else
    #include <ESP8266WiFi.h>
#endif
#include "fauxmoESP.h"
//#include "credentials.sample.h"
// #include <RCSwitch.h>

#define WIFI_SSID "virginmedia"
#define WIFI_PASS "xxxxxxx"

#define SERIAL_BAUDRATE                 115200
#define LED                             D7

//RCSwitch mySwitch = RCSwitch();

fauxmoESP fauxmo;

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

void wifiSetup() {

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

    // Connect
    Serial.printf("[WIFI] Connecting to %s ", WIFI_SSID);
    WiFi.begin(WIFI_SSID, WIFI_PASS);

    // 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, LOW);
    fauxmo.setState(LED, 0);

    //Switch

   // mySwitch.enableTransmit(4); //DigitalPIN 2 Transmitter

    // 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("Salotto");

    // 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");

        if( (strcmp(device_name, "Salotto") == 0)){
          if(state){
           // mySwitch.switchOn("xxx", "xxx");
            Serial.printf("Accendo");
            digitalWrite(LED, HIGH); 

            } else{
            //mySwitch.switchOff("xxx", "xxx");
             Serial.printf("Spengo");
             digitalWrite(LED, LOW); 

              }
          }//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());
    }

}
pvint commented 5 years ago

Original comment by Deiu (Bitbucket: d3iu, GitHub: d3iu).


Yes please, how invert the inverse logic? I need it too.. i give alexa command to turn on and it turns off and i have to say turn off for it to turn on

pvint commented 5 years ago

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


You are using an old version of the library. Please switch to 3.1.0. You won't have the onGetState callback and you will have to use setState whenever you change the state of the LED from the manual switch to notify Alexa of the change.

pvint commented 5 years ago

Original comment by Last (Bitbucket: adelast, ).


Hi Xose and thank you for your reply.

The reason why I am using an old version of the library and of the board is because Alexa app wasn't able to find the switch on the latest version. I found on this site a post that was suggesting to downgrade both board and library to fix the issue.

I am using fauxmoESP with an Echo Spot.

Thanks

pvint commented 5 years ago

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


Did you read the Troubleshooting section in the main README.md on how to use the latest version?

pvint commented 5 years ago

Original comment by Last (Bitbucket: adelast, ).


Hi,

with the latest version of the library and a new example script everything works fine but in the Alexa app there's no button anymore. The device is not seen as a switch

pvint commented 5 years ago

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


That's right, it's seen as a light bulb instead.