I have code that turns on/off LED lights. I'm having an issue where when I turn on/off the "Blue Glare" the Red Bulb turns on/off and when I turn on/off the "Red Bulb" the Blue Glare turns on. I am using Sinric Pro and I have all the correct Device IDs next to each light but the problem still happens. Can anyone help me?
#ifdef ARDUINO_ARCH_ESP32
#include <ESPmDNS.h>
#include <WiFi.h>
#else
#include <ESP8266mDNS.h>
#include <ESP8266WiFi.h>
#endif
#include <Espalexa.h>
#include <SinricPro.h>
#include <SinricProSwitch.h>
#define RelayPin1 5
#define RelayPin2 4
#define RelayPin3 12
#define RelayPin4 14
#define RelayPin5 13
boolean connectWifi();
void firstLightChanged(uint8_t brightness);
void secondLightChanged(uint8_t brightness);
void thridLightChanged(uint8_t brightness);
void fourthLightChanged(uint8_t brightness);
void fifthLightChanged(uint8_t brightness);
// WiFi credentials
const char* ssid = "SpectrumSetup-E1DE";
const char* password = "modernsave265";
// Device Names
String Device_1_Name = "Yellow LED";
String Device_2_Name = "Orange Light";
String Device_3_Name = "Blue Glare";
String Device_4_Name = "Green Lamp";
String Device_5_Name = "Red Bulb";
boolean wifiConnected = false;
Espalexa espalexa;
// Sinric Pro credentials
#define APP_KEY "b4280e3a-98d8-4b6b-93e2-b7569058e05b"
#define APP_SECRET "c136931a-82ab-48c4-bf50-654b9c990248-a4416c8e-1196-4a96-97c4-e32d539a485b"
// Sinric Pro device IDs
#define DEVICE_ID_1 "672cce4c88a12c713ba4cdf8"
#define DEVICE_ID_2 "672cd11f8f3cc517eb126225"
#define DEVICE_ID_3 "6746b79c716c0bcb088598d6"
#define DEVICE_ID_4 "672cd1e8f36ec9e0fac07e34"
#define DEVICE_ID_5 "6746b76a039b4bdc5d2bf696"
// Sinric Pro callback functions
bool onPowerState1(const String &deviceId, bool &state) {
digitalWrite(RelayPin1, state ? HIGH : LOW);
Serial.printf("Device 1 %s\n", state ? "ON" : "OFF");
return true;
}
bool onPowerState2(const String &deviceId, bool &state) {
digitalWrite(RelayPin2, state ? HIGH : LOW);
Serial.printf("Device 2 %s\n", state ? "ON" : "OFF");
return true;
}
bool onPowerState3(const String &deviceId, bool &state) {
digitalWrite(RelayPin3, state ? HIGH : LOW);
Serial.printf("Device 3 %s\n", state ? "ON" : "OFF");
return true;
}
bool onPowerState4(const String &deviceId, bool &state) {
digitalWrite(RelayPin4, state ? HIGH : LOW);
Serial.printf("Device 4 %s\n", state ? "ON" : "OFF");
return true;
}
bool onPowerState5(const String &deviceId, bool &state) {
digitalWrite(RelayPin5, state ? HIGH : LOW);
Serial.printf("Device 5 %s\n", state ? "ON" : "OFF");
return true;
}
void setup() {
Serial.begin(9600);
pinMode(RelayPin1, OUTPUT);
pinMode(RelayPin2, OUTPUT);
pinMode(RelayPin3, OUTPUT);
pinMode(RelayPin4, OUTPUT);
pinMode(RelayPin5, OUTPUT);
wifiConnected = connectWifi();
if (wifiConnected) {
espalexa.addDevice(Device_1_Name, firstLightChanged);
espalexa.addDevice(Device_2_Name, secondLightChanged);
espalexa.addDevice(Device_3_Name, thridLightChanged);
espalexa.addDevice(Device_4_Name, fourthLightChanged);
espalexa.addDevice(Device_5_Name, fifthLightChanged);
espalexa.begin();
// Initialize Sinric Pro and attach device IDs and callbacks
SinricProSwitch &mySwitch1 = SinricPro[DEVICE_ID_1];
mySwitch1.onPowerState(onPowerState1);
SinricProSwitch &mySwitch2 = SinricPro[DEVICE_ID_2];
mySwitch2.onPowerState(onPowerState2);
SinricProSwitch &mySwitch3 = SinricPro[DEVICE_ID_3];
mySwitch3.onPowerState(onPowerState3);
SinricProSwitch &mySwitch4 = SinricPro[DEVICE_ID_4];
mySwitch4.onPowerState(onPowerState4);
SinricProSwitch &mySwitch5 = SinricPro[DEVICE_ID_5];
mySwitch5.onPowerState(onPowerState5);
SinricPro.begin(APP_KEY, APP_SECRET);
} else {
while (1) {
Serial.println("Can't Connect to WiFi. Please Try Again");
delay(2500);
}
}
}
void loop() {
espalexa.loop();
SinricPro.handle();
delay(1);
}
// Espalexa light control functions
void firstLightChanged(uint8_t brightness) { digitalWrite(RelayPin1, brightness == 255 ? HIGH : LOW); }
void secondLightChanged(uint8_t brightness) { digitalWrite(RelayPin2, brightness == 255 ? HIGH : LOW); }
void thridLightChanged(uint8_t brightness) { digitalWrite(RelayPin3, brightness == 255 ? HIGH : LOW); }
void fourthLightChanged(uint8_t brightness) { digitalWrite(RelayPin4, brightness == 255 ? HIGH : LOW); }
void fifthLightChanged(uint8_t brightness) { digitalWrite(RelayPin5, brightness == 255 ? HIGH : LOW); }
// WiFi connection function
boolean connectWifi() {
WiFi.begin(ssid, password);
Serial.println("Connecting to WiFi...");
for (int i = 0; i < 20 && WiFi.status() != WL_CONNECTED; i++) {
delay(500);
Serial.print(".");
}
Serial.println();
if (WiFi.status() == WL_CONNECTED) {
Serial.println("Connected!");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
return true;
} else {
Serial.println("Failed to connect.");
return false;
}
}