// Blynk credentials
#define BLYNK_TEMPLATE_ID "TMPL21-cOLgOZ" // Template ID for Blynk project
#define BLYNK_TEMPLATE_NAME "relay sh3booo" // Template name for Blynk project
#define BLYNK_AUTH_TOKEN "2XjYOQyGT1Vqb7ZCLWwTJnZYF76wkfCn" // Authentication token for Blynk
#define BLYNK_PRINT Serial // Allows Blynk to print debug information to the serial monitor
#include <WiFi.h> // ESP32 WiFi library for WiFi functionality
#include <WiFiClient.h> // WiFi client library
#include <BlynkSimpleEsp32.h> // Blynk library for ESP32
// WiFi credentials
char auth[] = BLYNK_AUTH_TOKEN; // Blynk authentication token
char ssid[] = "11111111"; // WiFi network name
char pass[] = "11111111"; // WiFi password
// Hardware pins for relays and buttons
#define RELAY1 23 // Pin controlling RELAY1
#define RELAY2 22 // Pin controlling RELAY2
#define BUTTON1 19 // Pin connected to BUTTON1
#define BUTTON2 18 // Pin connected to BUTTON2
bool relay1State = false; // Track the current state of RELAY1 (true = ON, false = OFF)
bool relay2State = false; // Track the current state of RELAY2 (true = ON, false = OFF)
bool lastButton1State = HIGH; // Track the previous state of BUTTON1 for debouncing
bool lastButton2State = HIGH; // Track the previous state of BUTTON2 for debouncing
// Virtual pins in Blynk app
#define VPIN_BUTTON1 V1 // Virtual pin for BUTTON1 in the Blynk app
#define VPIN_BUTTON2 V2 // Virtual pin for BUTTON2 in the Blynk app
void setup() {
Serial.begin(9600); // Initialize serial communication for debugging
// Configure relay and button pins
pinMode(RELAY1, OUTPUT); // Set RELAY1 pin as output
pinMode(RELAY2, OUTPUT); // Set RELAY2 pin as output
pinMode(BUTTON1, INPUT_PULLUP); // Set BUTTON1 as input with internal pull-up resistor
pinMode(BUTTON2, INPUT_PULLUP); // Set BUTTON2 as input with internal pull-up resistor
// Initialize relay states to OFF (LOW)
digitalWrite(RELAY1, LOW);
digitalWrite(RELAY2, LOW);
// Connect to Blynk cloud service using WiFi credentials
Blynk.begin(auth, ssid, pass);
// Sync the current relay states with the Blynk app at startup
Blynk.syncVirtual(VPIN_BUTTON1); // Sync BUTTON1 state from Blynk app
Blynk.syncVirtual(VPIN_BUTTON2); // Sync BUTTON2 state from Blynk app
}
void loop() {
Blynk.run(); // Keep Blynk connected and responsive
// Read the current state of the physical buttons
bool currentButton1State = digitalRead(BUTTON1); // Read BUTTON1 state
bool currentButton2State = digitalRead(BUTTON2); // Read BUTTON2 state
// Check if BUTTON1 is pressed and its state has changed (to prevent bouncing)
if (currentButton1State == LOW && lastButton1State == HIGH) {
relay1State = !relay1State; // Toggle RELAY1 state (ON->OFF or OFF->ON)
digitalWrite(RELAY1, relay1State ? HIGH : LOW); // Set RELAY1 based on the new state
Blynk.virtualWrite(VPIN_BUTTON1, relay1State); // Update the Blynk app with the new RELAY1 state
delay(200); // Small delay for debounce
}
// Check if BUTTON2 is pressed and its state has changed (to prevent bouncing)
if (currentButton2State == LOW && lastButton2State == HIGH) {
relay2State = !relay2State; // Toggle RELAY2 state (ON->OFF or OFF->ON)
digitalWrite(RELAY2, relay2State ? HIGH : LOW); // Set RELAY2 based on the new state
Blynk.virtualWrite(VPIN_BUTTON2, relay2State); // Update the Blynk app with the new RELAY2 state
delay(200); // Small delay for debounce
}
// Update the previous button states for the next loop iteration (for debouncing)
lastButton1State = currentButton1State;
lastButton2State = currentButton2State;
}
// Blynk handler for virtual button 1 (in the app)
// When the button in the app is pressed or toggled, this function is called
BLYNK_WRITE(VPIN_BUTTON1) {
relay1State = param.asInt(); // Read the state from the Blynk app (0 or 1)
digitalWrite(RELAY1, relay1State ? HIGH : LOW); // Set RELAY1 state based on the app input
}
// Blynk handler for virtual button 2 (in the app)
// When the button in the app is pressed or toggled, this function is called
BLYNK_WRITE(VPIN_BUTTON2) {
relay2State = param.asInt(); // Read the state from the Blynk app (0 or 1)
digitalWrite(RELAY2, relay2State ? HIGH : LOW); // Set RELAY2 state based on the app input
}