Espalexa only one callback working

Hi!

I'm confused. Can anyone help me, please?

I have two devices and two callbacks, but both devices are triggering same the first call.

This is my code:

#include <WiFi.h>
#include <Espalexa.h>

// prototypes
boolean connectWifi();

//callback functions
void firstLightChanged(uint8_t brightness);
void secondLightChanged(uint8_t brightness);

// WiFi Credentials
const char* ssid = "xxxx";
const char* password = "xxxx";

// device names
String Device_1_Name = "Led Branco";
String Device_2_Name = "Led Vermelho";

boolean wifiConnected = false;

Espalexa espalexa;

void setup()
{
  Serial.begin(115200);

  // Initialise wifi connection
  wifiConnected = connectWifi();

  if (wifiConnected)
  {
    // Define your devices here.
    espalexa.addDevice(Device_1_Name, firstLightChanged); //simplest definition, default state off
    espalexa.addDevice(Device_2_Name, secondLightChanged);
    espalexa.begin();
  }
  else
  {
    while (1)
    {
      Serial.println("Cannot connect to WiFi. Please check data and reset the ESP.");
      delay(2500);
    }
  }
}

void loop()
{
  espalexa.loop();
  delay(1);
}

//our callback functions
void firstLightChanged(uint8_t brightness)
{
  //Control the device
  if (brightness)
  {
    if (brightness == 255)
    {
      digitalWrite(RGB_BUILTIN, HIGH);
      Serial.println("Led Branco Ligado");
    }
  }
  else
  {
    //digitalWrite(R1, LOW);
    digitalWrite(RGB_BUILTIN, LOW);
    Serial.println("Led Branco Desligado");
  }
}

void secondLightChanged(uint8_t brightness)
{
  //Control the device
  if (brightness)
  {
    if (brightness == 255)
    {
      rgbLedWrite(RGB_BUILTIN, RGB_BRIGHTNESS, 0, 0);  // Red
      Serial.println("Led Vermelho Ligado");
    }
  }
  else
  {
    //digitalWrite(R1, LOW);
    rgbLedWrite(RGB_BUILTIN, 0, 0, 0);  // Red
    Serial.println("Led Vermelho Desligado");
  }
}

// connect to wifi – returns true if successful or false if not
boolean connectWifi()
{
  boolean state = true;
  int i = 0;

  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  Serial.println("");
  Serial.println("Connecting to WiFi");

  // Wait for connection
  Serial.print("Connecting...");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
    if (i > 20) {
      state = false; break;
    }
    i++;
  }
  Serial.println("");
  if (state) {
    Serial.print("Connected to ");
    Serial.println(ssid);
    Serial.print("IP address: ");
    Serial.println(WiFi.localIP());
  }
  else {
    Serial.println("Connection failed.");
  }
  return state;
}

Only firstLightChanged is acting. Am I doing it wrong?

I am not familiar with this. How are you sending the distinct commands? Could the error be on that side? Somehow both <whatevers> are doing the same thing?

I assume you are only seeing the print statements from the first callback.

a7

When I call the each voice commmands the ESP32 turn the white led on (callback firstLightChanged).

I did these testes:

I set the callback secondLightChanged to both commands and it worked turning the red led on.

I reversed the order of adding devices and now only the secondLightChanged callback is acting.


if (wifiConnected)
  {
    // Define your devices here.

    espalexa.addDevice(Device_2_Name, secondLightChanged);
    espalexa.addDevice(Device_1_Name, firstLightChanged); //simplest definition, default state off
    espalexa.begin();
  }

I did a new test and changed the device names to "White" and "Red". Now I realized that only "White" is shown in the Alexa app and only after I remove it that "Red" is shown in the Alexa app. What is going on?

It is solved.

The solution is to replace this line:
sprintf_P(out, PSTR("%02X:%02X:%02X:%02X:%02X:%02X:00:11-%02X"), mac[0],mac[1],mac[2 ],mac[3],mac[4],mac[5], idx);

With this line:
sprintf_P(out, PSTR("%02X:%02X:%02X:%02X:%02X:%02X-%02X-00:11"), mac[0],mac[1],mac[2 ],mac[3],mac[4],mac[5], idx);

In the espalexa.h located in the library folder.

In my ESPAlexa system it no longer works, it just detects some devices randomly and then they disappear by themselves.

For many years I had 2 ESP8266 NodeMCUs with 15 devices each for home automation I developed, which worked perfectly, but since February 2025 I have this problem.

I use an external web server because the log is displayed in HTML and there is the integration of some sensors connected to a MQTT server and notification via Telegram APP.

I made the changes recommended by colleague delanolima, but they had no effect on the system.

Does anyone else have this problem?

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