#include <ArduinoIoTCloud.h>
#define DEVICE_BOARD_ID "5c4f20de-a79d-4a0c-8762-56763e3b4913"
#define DEVICE_SECRET "idpZHG2RQw75?OX06DeRscpx?"
#define SSID_NAME "Wokwi-GUEST"
#define SSID_NAME_PASS ""
#define LED_RED_PIN 22 // Define el pin para el LED rojo
#define LED_BLUE_PIN 21 // Define el pin para el LED azul
#define SWITCH_PIN 23 // Define el pin para el switch
#define LED_DELAY 300
int currentState = LOW; // Estado actual inicializado a LOW
int lastState = LOW; // Último estado conocido, inicializado a LOW
bool virtualSwitch = false;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
ArduinoCloud.setBoardId(DEVICE_BOARD_ID);
ArduinoCloud.setSecretDeviceKey(DEVICE_SECRET);
ArduinoCloud.addProperty(virtualSwitch, READWRITE, ON_CHANGE, onVirtualSwitchChange);
WiFiConnectionHandler ArduinoIoTPreferredConnection("Wokwi-GUEST", "");
Serial.print("connecting to WIFI");
WiFi.begin("Wokwi-GUEST", "", 6);
while(WiFi.status() != WL_CONNECTED){
delay(100);
Serial.print(".");
}
Serial.println(" Connected!");
// Configura los pines de los LEDs como salida
pinMode(LED_RED_PIN, OUTPUT);
pinMode(LED_BLUE_PIN, OUTPUT);
digitalWrite(LED_RED_PIN, LOW); // Inicia con el LED rojo apagado
digitalWrite(LED_BLUE_PIN, LOW); // Inicia con el LED azul apagado
Serial.println("\n\n\n\n\n");
ArduinoCloud.begin();
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
}
void loop() {
ArduinoCloud.update();
if (currentState == LOW) {
if (currentState != lastState)
{
banner("¡Encendido!");
}
// Enciende el LED rojo y apaga el LED azul
digitalWrite(LED_RED_PIN, HIGH);
digitalWrite(LED_BLUE_PIN, LOW);
delay(LED_DELAY); // Espera medio segundo
// Apaga el LED rojo y enciende el LED azul
digitalWrite(LED_RED_PIN, LOW);
digitalWrite(LED_BLUE_PIN, HIGH);
delay(LED_DELAY); // Espera medio segundo
lastState = currentState;
} else {
if (currentState != lastState)
{
banner("¡Apagado!");
}
// Si el switch no está activado, apaga ambos LEDs
digitalWrite(LED_RED_PIN, LOW);
digitalWrite(LED_BLUE_PIN, LOW);
lastState = currentState;
}
}
void banner(String message) {
Serial.println(message);
}
void onVirtualSwitchChange() {
int newState = virtualSwitch ? LOW : HIGH; // Ajuste para usar int en lugar de bool y manejar directamente el estado del switch
const char* currentLabel = (currentState == LOW) ? "LOW" : "HIGH";
const char* newLabel = (newState == LOW) ? "LOW" : "HIGH";
String message = "Cambio el estado del switch de " + String(currentLabel) + " a " + String(newLabel);
banner(message);
currentState = newState;
}