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?