FauxmoESP 4 channel Alexa control

Hey
I am relatively new to home automation. I already build a smart light switch successfully using end ESP01 relay board and FauxmoESP (its not particularly difficult with the example code after all).

My next project is an four channel relay board with an ESP12F.
Its sort of working if I only use 3 channels. But my Alexa can't find the board if I try to add more than 3 channels (same code just the "//" in front of any channel).

I can find all channels if I disable for example 3 & 4 > search for channel 1 & 2 > do the same again but vice versa > enable all. But then it pairs channel 1 & 3 and channel 2 & 4. When I ask Alexa to turn channel 3 on, it only activates relay 1, same with 2 and 4.

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

// Rename the credentials.sample.h file to credentials.h and 
// edit it according to your router configuration
#include "credentials.h"

fauxmoESP fauxmo;

// -----------------------------------------------------------------------------

#define SERIAL_BAUDRATE     115200

#define relay1          16
#define ID_relay1           "relay1"
#define relay2          14
#define ID_relay2           "relay2"
#define relay3          12
#define ID_relay3           "relay3"
#define relay4          13
#define ID_relay4           "relay4"


// -----------------------------------------------------------------------------

const int  button1Pin = 5;
const int  button2Pin = 4;
const int  button3Pin = 0;
const int  button4Pin = 2;

int button1State = 0;
int button2State = 0;
int button3State = 0;
int button4State = 0;

int lastButton1State = 0;
int lastButton2State = 0;
int lastButton3State = 0;
int lastButton4State = 0;

int flag1 = 0;
int flag2 = 0;
int flag3 = 0;
int flag4 = 0;

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

    // LEDs
    pinMode(relay1, OUTPUT);
    pinMode(relay2, OUTPUT);
    pinMode(relay3, OUTPUT);
    pinMode(relay4, OUTPUT);

    digitalWrite(relay1, LOW);
    digitalWrite(relay2, LOW);
    digitalWrite(relay3, LOW);
    digitalWrite(relay4, LOW);

    pinMode(button1Pin, INPUT_PULLUP);
    pinMode(button2Pin, INPUT_PULLUP);
    pinMode(button3Pin, INPUT_PULLUP);
    pinMode(button4Pin, INPUT_PULLUP);


    // Wifi
    wifiSetup();


    fauxmo.createServer(true); // not needed, this is the default value
    fauxmo.setPort(80); // This is required for gen3 devices

    fauxmo.enable(true);


    // Add virtual devices
    fauxmo.addDevice(ID_relay1);
    fauxmo.addDevice(ID_relay2);
    fauxmo.addDevice(ID_relay3);
    fauxmo.addDevice(ID_relay4);

    fauxmo.onSetState([](unsigned char device_id, const char * device_name, bool state, unsigned char value) {
        
        // Callback when a command from Alexa is received. 
        // You can use device_id or device_name to choose the element to perform an action onto (relay, LED,...)
        // State is a boolean (ON/OFF) and value a number from 0 to 255 (if you say "set kitchen light to 50%" you will receive a 128 here).
        // Just remember not to delay too much here, this is a callback, exit as soon as possible.
        // If you have to do something more involved here set a flag and process it in your main loop.
        
        Serial.printf("[MAIN] Device #%d (%s) state: %s value: %d\n", device_id, device_name, state ? "ON" : "OFF", value);

        // Checking for device_id is simpler if you are certain about the order they are loaded and it does not change.
        // Otherwise comparing the device_name is safer.

        if (strcmp(device_name, ID_relay1)==0) { 
           if (state) {
             flag1 = HIGH;
           } 
           else {
             flag1 = LOW;
           }
        }
        if (strcmp(device_name, ID_relay2)==0) { 
           if (state) {
             flag2 = HIGH;
           } 
           else {
             flag2 = LOW;
           }
        }
        if (strcmp(device_name, ID_relay3)==0) { 
           if (state) {
             flag3 = HIGH;
           } 
           else {
             flag3 = LOW;
           }
        }
        if (strcmp(device_name, ID_relay4)==0) { 
           if (state) {
             flag4 = HIGH;
           } 
           else {
             flag4 = LOW;
           }
        }

    });

}

void loop() {

    // fauxmoESP uses an async TCP server but a sync UDP server
    // Therefore, we have to manually poll for UDP packets
    fauxmo.handle();

    
    button1State = digitalRead(button1Pin);
    button2State = digitalRead(button2Pin);
    button3State = digitalRead(button3Pin);
    button4State = digitalRead(button4Pin);

  if (button1State != lastButton1State) {
    if (button1State == LOW) {
      flag1 = !flag1;
    }
    delay(250);
  lastButton1State = button1State;
  }
  
  if (button2State != lastButton2State) {
    if (button2State == LOW) {
      flag2 = !flag2;
    }
    delay(250); 
  lastButton2State = button2State;
  }
  if (button3State != lastButton3State) {
    if (button3State == LOW) {
      flag3 = !flag3;
    }
    delay(250);
  lastButton3State = button3State;
  }

  if (button4State != lastButton4State) {
    if (button4State == LOW) {
      flag4 = !flag4;
    }
    delay(250);
  lastButton4State = button4State;
  }

  digitalWrite(relay1, flag1);
  digitalWrite(relay2, flag2);
  digitalWrite(relay3, flag3);
  digitalWrite(relay4, flag4);

    // This is a sample code to output free heap every 5 seconds
    // This is a cheap way to detect memory leaks
    static unsigned long last = millis();
    if (millis() - last > 5000) {
        last = millis();
        Serial.printf("[MAIN] Free heap: %d bytes\n", ESP.getFreeHeap());
    }

    // If your device state is changed by any other means (MQTT, physical button,...)
    // you can instruct the library to report the new state to Alexa on next request:
     //fauxmo.setState(flag1, true, 255);

}

Did anybody encounter the same issue?
Do you have any idea what the problem might be?

I attach a picture of the board just in case.
The wires are just for programming and have no connection during normal operation.

Do you need further information about the project?

Thank you for your help

m4tthias98:
I can find all channels if I disable for example 3 & 4 > search for channel 1 & 2 > do the same again but vice versa > enable all. But then it pairs channel 1 & 3 and channel 2 & 4. When I ask Alexa to turn channel 3 on, it only activates relay 1, same with 2 and 4.

You need to explain what this means.

How are you disabling channel 3 and 4?

Search how?

.

ieee488:
You need to explain what this means.

How are you disabling channel 3 and 4?

As I wrote by putting the "//" in front of the lines relating to the respective channel.

    // Add virtual devices
    fauxmo.addDevice(ID_relay1);
    fauxmo.addDevice(ID_relay2);
//    fauxmo.addDevice(ID_relay3);
//    fauxmo.addDevice(ID_relay4);

    fauxmo.onSetState([](unsigned char device_id, const char * device_name, bool state, unsigned char value) {
        
        // Callback when a command from Alexa is received. 
        // You can use device_id or device_name to choose the element to perform an action onto (relay, LED,...)
        // State is a boolean (ON/OFF) and value a number from 0 to 255 (if you say "set kitchen light to 50%" you will receive a 128 here).
        // Just remember not to delay too much here, this is a callback, exit as soon as possible.
        // If you have to do something more involved here set a flag and process it in your main loop.
        
        Serial.printf("[MAIN] Device #%d (%s) state: %s value: %d\n", device_id, device_name, state ? "ON" : "OFF", value);

        // Checking for device_id is simpler if you are certain about the order they are loaded and it does not change.
        // Otherwise comparing the device_name is safer.

        if (strcmp(device_name, ID_relay1)==0) { 
           if (state) {
             flag1 = HIGH;
           } 
           else {
             flag1 = LOW;
           }
        }
        if (strcmp(device_name, ID_relay2)==0) { 
           if (state) {
             flag2 = HIGH;
           } 
           else {
             flag2 = LOW;
           }
        }
/*        if (strcmp(device_name, ID_relay3)==0) { 
           if (state) {
             flag3 = HIGH;
           } 
           else {
             flag3 = LOW;
           }
        }
        if (strcmp(device_name, ID_relay4)==0) { 
           if (state) {
             flag4 = HIGH;
           } 
           else {
             flag4 = LOW;
           }
        }*/

    });

}

Search how?

By saying "Alexa, search for new devices" or pressing the discover new devices button in my Alexa app.

Looks like it is a problem with the library for Fauxmo.
You need to contact them.
I've never heard of the company.

If you cant get any help from them, i would debug/serialprint out the value of your states to make sure they are being set correctly and at the right time.

fauxmo.onSetState this function, check this one since its not specific to each of your channels, maybe the values are being kept instead of being replaced when needed and causing the issue.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.