vintlabs / fauxmoESP

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

Quick help with multidevice ouput settings #62

Closed pvint closed 5 years ago

pvint commented 5 years ago

Original report by Rycoviac (Bitbucket: Rycoviac, GitHub: Rycoviac).


Hey guys,

I know, this isn't exacly an issue, but i struggle with the example-code.

Firstly: my device works fine with my Echo Dot 2nd gen with ESP8266 2.3 from the boardmanager, but not with 2.4+

My question is... you have two devices per default in the example-code, but only one output "pin" and both devices (light one, light two) controls the pin/LED. Whats missing in the example code is how to set up multiple pins. For example device "light one" for LED and "light two" for a relais on 14/D5 on NodeMCU 1.0.

At the moment I stuck at a state where both devices switches both, LED and pin 14/d5. I think the example should have comments how to set a device from fauxmo.addDevice for a specific output and change it state independently. I hope you can help me and maybe the example code will be clearer in the future.

And sorry for my english...

pvint commented 5 years ago

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


Nobody able to provide a short code-snippet how you control different outputs with different names/devices? :-(

pvint commented 5 years ago

Original comment by Carlos Yz (Bitbucket: [Carlos Yamazaki](https://bitbucket.org/Carlos Yamazaki), ).


#!arduino

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

#define SERIAL_BAUDRATE                 74880

#define LED_YELLOW 4
#define LED_GREEN 5
#define LED_BLUE 0
#define LED_PINK 2
#define LED_WHITE 15

#define ID_YELLOW "yellow lamp"
#define ID_GREEN "green lamp"
#define ID_BLUE "blue lamp"
#define ID_PINK "pink lamp"
#define ID_WHITE "white lamp"

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(".");
        //Serial.println(WiFi.status());
        delay(300);
    }
    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_YELLOW,   OUTPUT);
    pinMode(LED_GREEN, OUTPUT);
    pinMode(LED_BLUE,  OUTPUT);
    pinMode(LED_PINK,  OUTPUT);
    pinMode(LED_WHITE,  OUTPUT);
    digitalWrite(LED_YELLOW,   LOW);
    digitalWrite(LED_GREEN, LOW);
    digitalWrite(LED_BLUE,  LOW);
    digitalWrite(LED_PINK,  LOW);
    digitalWrite(LED_WHITE,  LOW);

    // You have to call enable(true) once you have a WiFi connection
    // 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(ID_YELLOW);
    fauxmo.addDevice(ID_GREEN);
    fauxmo.addDevice(ID_BLUE);
    fauxmo.addDevice(ID_PINK);
    fauxmo.addDevice(ID_WHITE);

    // 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, ID_YELLOW)==0) {
          Serial.println("Yellow");
          digitalWrite(LED_YELLOW, state ? HIGH : LOW);
        } else if (strcmp(device_name, ID_GREEN)==0) {
          Serial.println("Green");
          digitalWrite(LED_GREEN, state ? HIGH : LOW);
        } else if (strcmp(device_name, ID_BLUE)==0) {
          Serial.println("Blue");
          digitalWrite(LED_BLUE, state ? HIGH : LOW);
        } else if (strcmp(device_name, ID_PINK)==0) {
          Serial.println("Pink");
          digitalWrite(LED_PINK, state ? HIGH : LOW);
        } else if (strcmp(device_name, ID_WHITE)==0) {
          Serial.println("White");
          digitalWrite(LED_WHITE, state ? HIGH : LOW);
        }
    });

/*    // Callback to retrieve current state (for GetBinaryState queries)
    fauxmo.onGetState([](unsigned char device_id, const char * device_name) {
        if (strcmp(device_name, ID_COOLER)==0)
          return digitalRead(COOLER);
        else if (strcmp(device_name, ID_GREEN)==0)
          return digitalRead(LED_GREEN);
        else if (strcmp(device_name, ID_BLUE)==0)
          return digitalRead(LED_BLUE);
    });
*/
}

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

}

Is it enough?

pvint commented 5 years ago

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


Wow, yea, realy, thank you!

This helps so much! As I sayed, it should be like this in the examples :-)

pvint commented 5 years ago

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


Thank you @Rycoviac and @CarlosYz. I have updated the basic example with this code.

pvint commented 5 years ago

Original comment by Carlos Yz (Bitbucket: [Carlos Yamazaki](https://bitbucket.org/Carlos Yamazaki), ).


At your service, Sir. I really appreciate your dedication here.