vintlabs / fauxmoESP

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

Example code doesn't work #31

Closed pvint closed 5 years ago

pvint commented 6 years ago

Original report by Rene Seigert (Bitbucket: reneberlin, GitHub: reneberlin).


Hi There,

just a small issue i know the Issue is more in front of the Keyboard than the Code itself...

I just took the new enamplecode for two devices but there the Lights will not light up. Code is:

#!arduino
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include "fauxmoESP.h"
#include "credentials.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_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, 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("Dose Eins");  // First device = 0
  fauxmo.addDevice("Dose Zwei"); // You can add more devices (device 1)
  //fauxmo.addDevice("switch three"); // device 2

    // fauxmoESP 2.0.0 has changed the callback signature to add the device_id, this WARRANTY
    // 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 (device_id == 0) digitalWrite(LED, state);
        if (device_id == 1) digitalWrite(2, state);
    });

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

}

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

}

so for my understanding if i call Alexa to switch on "Dose Eins" it should light up the LED, in the other way too because Led is on Pin 2.

Alex confirms me asking with OK, but the LED doesn't light up.

the serial monitor shows me, that the Device were discovered and switch on and off.

[MAIN] Device #0 (Dose Eins) state: ON [MAIN] Free heap: 46264 bytes [MAIN] Free heap: 46264 bytes [MAIN] Device #1 (Dose Zwei) state: OFF

so the question is, where is the error? Is there a bug in the example-code? The old examplecode just worked fine (with one device)

Best regards René

pvint commented 6 years ago

Original comment by Rene Seigert (Bitbucket: reneberlin, GitHub: reneberlin).


changed priorities to trivial

pvint commented 6 years ago

Original comment by mmoraes80 (Bitbucket: mmoraes80, GitHub: mmoraes80).


I had the same problem but, with a little digging, I found out the problem is related to the pinout definition. Add the following code to your project and it should work:

#!arduino

#define D0 16
#define D1 5 // I2C Bus SCL (clock)
#define D2 4 // I2C Bus SDA (data)
#define D3 0
#define D4 2 // Same as "LED_BUILTIN", but inverted logic
#define D5 14 // SPI Bus SCK (clock)
#define D6 12 // SPI Bus MISO 
#define D7 13 // SPI Bus MOSI
#define D8 15 // SPI Bus SS (CS)
#define D9 3 // RX0 (Serial console)
#define D10 1 // TX0 (Serial console)
pvint commented 6 years ago

Original comment by Rene Seigert (Bitbucket: reneberlin, GitHub: reneberlin).


Thank you for that tip, but it doesn't work too.

the code from the .cpp file does work:

#!arduino

#include <Arduino.h>
#include <ESP8266WiFi.h>
#include "fauxmoESP.h"
#include "credentials.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_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, 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("Dose Eins");
  //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 WARRANTY
    // 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);
    });

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

}

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

}

so the definition seems to be fine.

pvint commented 6 years ago

Original comment by mmoraes80 (Bitbucket: mmoraes80, GitHub: mmoraes80).


Yes it works, but the code above only uses 1 device. When you add the new pin out definition, you need to change pin names as well:

#!arduino

#include <Arduino.h>
#include <ESP8266WiFi.h>
#include "fauxmoESP.h"
#include "credentials.h"

#define SERIAL_BAUDRATE                 115200

#define D0 16
#define D1 5 
#define D2 4 
#define D3 0
#define D4 2
#define D5 14
#define D6 12
#define D7 13
#define D8 15
#define D9 3
#define D10 1

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(D4, OUTPUT);
    pinMode(D5, OUTPUT);

    // Turn LED on
    digitalWrite(D4, 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("Dose Eins");  // First device = 0
    fauxmo.addDevice("Dose Zwei"); // You can add more devices (device 1)
  //fauxmo.addDevice("switch three"); // device 2

    // fauxmoESP 2.0.0 has changed the callback signature to add the device_id, this WARRANTY
    // 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 (device_id == 0) digitalWrite(D4, state);
        if (device_id == 1) digitalWrite(D5, state);
    });

    // Callback to retrieve current state (for GetBinaryState queries)
    fauxmo.onGetState([](unsigned char device_id, const char * device_name) {
        if (device_id == 0) return digitalRead(D4);
        if (device_id == 1) return digitalRead(D5);
    });

}

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

}

I have been using the corrected code with no problems with my Alexa Echo Dot and AMICA ESP8266 NodeMCU.

pvint commented 6 years ago

Original comment by Rene Seigert (Bitbucket: reneberlin, GitHub: reneberlin).


Hm tried that but don't work for me. no plan were the problem is here... Hardware has no vendor labeled only ESP8266MOD is printed on it...

pvint commented 6 years ago

Original comment by Rene Seigert (Bitbucket: reneberlin, GitHub: reneberlin).


Hi anyone having the same problem? i can't get more than one Device to get to work.

It seems to bee the if-part here:

#!arduino
    // Add virtual devices
    fauxmo.addDevice("Dose Eins");  // First device = 0
    fauxmo.addDevice("Dose Zwei"); // You can add more devices (device 1)
  //fauxmo.addDevice("switch three"); // device 2

    // fauxmoESP 2.0.0 has changed the callback signature to add the device_id, this WARRANTY
    // 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 (device_id == 0) digitalWrite(D4, state);
        if (device_id == 1) digitalWrite(D5, state);
    });

    // Callback to retrieve current state (for GetBinaryState queries)
    fauxmo.onGetState([](unsigned char device_id, const char * device_name) {
        if (device_id == 0) return digitalRead(D4);
        if (device_id == 1) return digitalRead(D5);
    });

when i mange the part to the example Part it works just fine with one device discovered and i am able to switch it on/off.

pvint commented 6 years ago

Original comment by mmoraes80 (Bitbucket: mmoraes80, GitHub: mmoraes80).


Make sure the line below does not "//" in front of it. Repeat the same for additional devices.

#!arduino

fauxmo.addDevice("Dose Eins");  // First device = 0

Same thing for lines:

#!arduino
if (device_id == 0) digitalWrite(D4, state);
if (device_id == 0) return digitalRead(D4);

What ESP8266 model are you using? I am currently using an ESP8266 NodeMCU LUA CP2102 ESP-12E Internet WIFI Development Board (https://www.amazon.com/gp/product/B010O1G1ES/ref=oh_aui_detailpage_o05_s00?ie=UTF8&psc=1) and a Makerfocus D1 Mini NodeMcu 4M Bytes Lua ESP-12F WIFI Development Board (https://www.amazon.com/gp/product/B01N3P763C/ref=oh_aui_detailpage_o08_s00?ie=UTF8&psc=1).

In both cases. I can manage all pins. Keep in mind the LED is inverted logic, so when you set it up as HIGH, it will turn OFF.

pvint commented 6 years ago

Original comment by Rene Seigert (Bitbucket: reneberlin, GitHub: reneberlin).


I'm using this type of ESP8266: https://www.amazon.de/gp/product/B01N9RXGHY/ref=oh_aui_detailpage_o03_s00?ie=UTF8&psc=1

Of course the lines are not commented. This works because Alexa can discover the devices and when i ask her she inserts with "ok". In the serial i can see that "Dose Eins" is now turn on. But the LED does not Light up.

#!arduino
Device #0 (Dose Eins) state: ON

I tried it with an external LED on other Pins. Same behavior.

pvint commented 6 years ago

Original comment by mmoraes80 (Bitbucket: mmoraes80, GitHub: mmoraes80).


try to add a not (!) statement in the code:

#!arduino

if (device_id == 0) digitalWrite(D4, !state);
pvint commented 6 years ago

Original comment by Rene Seigert (Bitbucket: reneberlin, GitHub: reneberlin).


Hey that works fine! Thanks a lot!

pvint commented 6 years ago

Original comment by JimmyBondi (Bitbucket: JimmyBondi, GitHub: JimmyBondi).


Hey Rene,

kannst du mir vllt helfen, bei mir geht der Code einfach nicht.

Ich bekomme nicht mal den Example Code hin. Ich sehe die Verbraucher in der Alexa app, kann die aber nicht steuern.

hoffe du kannst mir helfen, bastel schon seit Tagen daran.

pvint commented 6 years ago

Original comment by Rene Seigert (Bitbucket: reneberlin, GitHub: reneberlin).


hm wenn nicht mal der example code geht. Welchen genau nimmst du? Poste mal bitte

pvint commented 6 years ago

Original comment by Rene Seigert (Bitbucket: reneberlin, GitHub: reneberlin).


oder du probierst einfach mal meinen Code aus:

#!arduino

include <Arduino.h>
#include <ESP8266WiFi.h>
#include "fauxmoESP.h"
#include "credentials.h"
#define SERIAL_BAUDRATE 115200
#define D0 16
#define D1 5
#define D2 4
#define D3 0
#define D4 2
#define D5 14
#define D6 12
#define D7 13
#define D8 15
#define D9 3
#define D10 1
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(D4, OUTPUT);
pinMode(D5, OUTPUT);

// Turn LED on
digitalWrite(D4, 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("Port Eins"); //Dev0
fauxmo.addDevice("Port Zwei"); //Dev1
fauxmo.addDevice("Port Drei"); //Dev2
fauxmo.addDevice("Port Vier"); //Dev3
fauxmo.addDevice("Port Fuenf"); //Dev4
fauxmo.addDevice("Port Sechs"); //Dev5
fauxmo.addDevice("Port Sieben"); //Dev6
fauxmo.addDevice("Port Acht"); //Dev7

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 (device_id == 0) digitalWrite(D1, !state);
    if (device_id == 1) digitalWrite(D2, !state);
    if (device_id == 2) digitalWrite(D3, !state);
    if (device_id == 3) digitalWrite(D4, !state);
    if (device_id == 4) digitalWrite(D5, !state);
    if (device_id == 5) digitalWrite(D6, !state);
    if (device_id == 6) digitalWrite(D7, !state);
    if (device_id == 7) digitalWrite(D8, !state);
});

// Callback to retrieve current state (for GetBinaryState queries)
fauxmo.onGetState([](unsigned char device_id, const char * device_name) {
    if (device_id == 0) return digitalRead(D1);
    if (device_id == 1) return digitalRead(D2);
    if (device_id == 2) return digitalRead(D3);
    if (device_id == 3) return digitalRead(D4);
    if (device_id == 4) return digitalRead(D5);
    if (device_id == 5) return digitalRead(D6);
    if (device_id == 6) return digitalRead(D7);
    if (device_id == 7) return digitalRead(D8);
});
}
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());
}
}

der ist zwar noch nicht komplett, aber wenn du Alex sagst Sie soll Port 4 einschalten müsste die LED angehen...

pvint commented 6 years ago

Original comment by JimmyBondi (Bitbucket: JimmyBondi, GitHub: JimmyBondi).


hey vielen vielen dank für den Code

aber genau das selbe problem!

Diesen ESP verwende ich

Er schaltet einfach nicht um!

habe es mit alles Ports ausprobiert - aber geht keiner! Alexa (2. Gen) Findet die Ports alle problemlos - schaltet die aber weder an noch aus!

hier noch paar bilder:

Arduino.JPG

Arduino_2.JPG

WhatsApp Image 2017-12-27 at 22.57.58.jpeg

pvint commented 6 years ago

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


Hi Jumping in a little late. Why are you setting the LEDs to "!state"? Do they use inverse logic (are they ON when the pin is LOW)? If that's the case then you should also respond "!digitalRead(D4)" in the onGetState.

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 (device_id == 0) digitalWrite(D1, !state);
    if (device_id == 1) digitalWrite(D2, !state);
    if (device_id == 2) digitalWrite(D3, !state);
    if (device_id == 3) digitalWrite(D4, !state);
    if (device_id == 4) digitalWrite(D5, !state);
    if (device_id == 5) digitalWrite(D6, !state);
    if (device_id == 6) digitalWrite(D7, !state);
    if (device_id == 7) digitalWrite(D8, !state);
});

// Callback to retrieve current state (for GetBinaryState queries)
fauxmo.onGetState([](unsigned char device_id, const char * device_name) {
    if (device_id == 0) return !digitalRead(D1);
    if (device_id == 1) return !digitalRead(D2);
    if (device_id == 2) return !digitalRead(D3);
    if (device_id == 3) return !digitalRead(D4);
    if (device_id == 4) return !digitalRead(D5);
    if (device_id == 5) return !digitalRead(D6);
    if (device_id == 6) return !digitalRead(D7);
    if (device_id == 7) return !digitalRead(D8);
});
pvint commented 6 years ago

Original comment by mmoraes80 (Bitbucket: mmoraes80, GitHub: mmoraes80).


ESP8266 LED works with inverted logic. <!state> is the NOT statement, which means LED switches its current state every time you send the command.

pvint commented 6 years ago

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


I know what !state means. "LED switches its current state every time you send the command", no, that's not true. It means you will set the GPIO to LOW when you get a TRUE, or HIGH if you get a FALSE. That's the inverted logic behavior. But then you are using "digitalRead()" on the onGetState and you are responding your echo (or dot) with the wrong state. It should be "!digitalRead()" for an inverted logic GPIO.