#define BLYNK_TEMPLATE_ID "TMPL22lhWM5NS"
#define BLYNK_TEMPLATE_NAME "2relais"
#define BLYNK_AUTH_TOKEN "UD-ApLgPro_A0QXg5S2KPQJGTlXxJxig"// Comment this out to disable prints and save space
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
char auth[] = BLYNK_AUTH_TOKEN;
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
BlynkTimer timer;
#define button1_pin 26
#define button2_pin 25
#define relay1_pin 13
#define relay2_pin 12
#define led1_pin 27
#define led2_pin 14
int relay1_state = 0;
int relay2_state = 0;
// Change the virtual pins according to the rooms
#define button1_vpin V1
#define button2_vpin V2
//------------------------------------------------------------------------------
// This function is called every time the device is connected to the Blynk.Cloud
// Request the latest state from the server
BLYNK_CONNECTED() {
Blynk.syncVirtual(button1_vpin);
Blynk.syncVirtual(button2_vpin);
}
//--------------------------------------------------------------------------
// This function is called every time the Virtual Pin state change
// i.e when web push switch from Blynk App or Web Dashboard
BLYNK_WRITE(button1_vpin) {
relay1_state = param.asInt();
digitalWrite(relay1_pin, relay1_state);
}
//--------------------------------------------------------------------------
BLYNK_WRITE(button2_vpin) {
relay2_state = param.asInt();
digitalWrite(relay2_pin, relay2_state);
}
//--------------------------------------------------------------------------
void setup() {
// Debug console
Serial.begin(115200);
//--------------------------------------------------------------------
pinMode(button1_pin, INPUT_PULLUP);
pinMode(button2_pin, INPUT_PULLUP);
pinMode(relay1_pin, OUTPUT);
pinMode(relay2_pin, OUTPUT);
pinMode(led1_pin, OUTPUT);
pinMode(led2_pin, OUTPUT);
//--------------------------------------------------------------------
// During Starting all Relays should TURN OFF
digitalWrite(relay1_pin, HIGH);
digitalWrite(relay2_pin, HIGH);
//--------------------------------------------------------------------
Blynk.begin(auth, ssid, pass);
}
void loop() {
Blynk.run();
timer.run();
// You can inject your own code or combine it with other sketches.
// Check other examples on how to communicate with Blynk. Remember
// to avoid delay() function!
listen_push_buttons();
}
void listen_push_buttons() {
if (digitalRead(button1_pin) == LOW) {
delay(200);
control_relay(1);
Blynk.virtualWrite(button1_vpin, relay1_state); // Update button state
}
else if (digitalRead(button2_pin) == LOW) {
delay(200);
control_relay(2);
Blynk.virtualWrite(button2_vpin, relay2_state); // Update button state
}
}
char notificationMsg[50]; // Déclaration de la variable pour stocker le message de notification
void control_relay(int relay) {
if (relay == 1) {
relay1_state = !relay1_state;
digitalWrite(relay1_pin, relay1_state);
digitalWrite(led1_pin, relay1_state);
// Envoyer une notification lorsque le relais 1 est activé ou désactivé
if (relay1_state) {
Blynk.logEvent("capteur", "button 1 activé");
} else {
Blynk.logEvent("capteur", "button 1 désactivé");
}
Serial.print("Relay1 State = ");
Serial.println(relay1_state);
delay(50);
}
else if (relay == 2) {
relay2_state = !relay2_state;
digitalWrite(relay2_pin, relay2_state);
digitalWrite(led2_pin, relay2_state);
// Envoyer une notification lorsque le relais 2 est activé ou désactivé
if (relay2_state) {
Blynk.logEvent("capteur", "button 2 activé");
} else {
Blynk.logEvent("capteur", "button 2 désactivé");
}
delay(50);
}
}