Hello, I found on the web how to connect an ESP32 to Alexa to control a lamp, everything is working only that I would like to integrate the system by manually controlling it with an manual switch (not push button).
Down there is a code tha i'm using. (without switch)
thank for your help.
/**********************************************************************************
* Download the libraries:
* https://github.com/Aircoookie/Espalexa
*
*
* Per ESP32: https://dl.espressif.com/dl/package_esp32_index.json Per ESP8266: http://arduino.esp8266.com/stable/package_esp8266com_index.json
*
**********************************************************************************/
#ifdef ARDUINO_ARCH_ESP32
#include <WiFi.h>
#else
#include <ESP8266WiFi.h>
#endif
#include <Espalexa.h>
// Inizializzo il Relè al pin D1
#define RelayPin1 5 //D1
//------- #define RelayPin2 4 //D2
boolean connectWifi();
void firstLightChanged(uint8_t brightness);
// Credenziali WI-FI
const char* ssid = "";
const char* password = "";
// Nome del dispositivo
String Device_1_Name = "Luce";
//----- String Device_2_Name = "Lampada";
boolean wifiConnected = false;
Espalexa espalexa;
void setup()
{
Serial.begin(115200);
pinMode(RelayPin1, OUTPUT);
//------- pinMode(RelayPin2, OUTPUT);
// Inizializzo una connessione wifi
wifiConnected = connectWifi();
if (wifiConnected)
{
// Metti qui il nome del tuo dispositivo
espalexa.addDevice(Device_1_Name, firstLightChanged);
//----------- espalexa.addDevice(Device_2_Name, firstLightChanged);
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);
}
void firstLightChanged(uint8_t brightness)
{
//Controllo del dispositivo
if (brightness == 255)
{
digitalWrite(RelayPin1, HIGH);
Serial.println("Device1 ON");
}
else
{
digitalWrite(RelayPin1, LOW);
Serial.println("Device1 OFF");
}
/*
if (brightness == 255)
{
digitalWrite(RelayPin2, HIGH);
Serial.println("Device2 ON");
}
else
{
digitalWrite(RelayPin2, LOW);
Serial.println("Device2 OFF");
}
*/
}
boolean connectWifi()
{
boolean state = true;
int i = 0;
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("");
Serial.println("Connecting to WiFi");
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;
}
(Code tags added by moderator - please Edit and clean up all the extra blank lines)