#define BLYNK_TEMPLATE_ID "TMPL6qwDGZBSu" // Template ID for Blynk IoT platform
#define BLYNK_TEMPLATE_NAME "HomeAutomation" // Project name on Blynk
#define BLYNK_AUTH_TOKEN "lfu1Mh4IwQHstzn8a0StAM9yusmVvLwb" // Authentication token to connect the ESP32 to Blynk
#define BLYNK_PRINT Serial // Enables debugging via Serial Monitor
#include <LiquidCrystal_I2C.h> // Library for I2C LCD
#include <WiFi.h> // Wi-Fi library for ESP32
#include <WiFiClient.h> // Supports Wi-Fi client
#include <BlynkSimpleEsp32.h> // Blynk library for ESP32
// Create an LCD object (I2C address, 16 columns, 2 rows)
LiquidCrystal_I2C LCD = LiquidCrystal_I2C(0x27, 16, 2);
char auth[] = BLYNK_AUTH_TOKEN; // Blynk authentication token
char ssid[] = "Wokwi-GUEST"; // Wi-Fi network name
char pass[] = ""; // Wi-Fi password (empty for this network)
BlynkTimer timer; // Timer to handle periodic tasks
// GPIO pins for buttons and relays
#define button1_pin 26
#define button2_pin 25
#define button3_pin 33
#define button4_pin 32
#define relay1_pin 13
#define relay2_pin 12
#define relay3_pin 14
#define relay4_pin 27
// Variables to store the state of each relay
int relay1_state = 0;
int relay2_state = 0;
int relay3_state = 0;
int relay4_state = 0;
// Virtual pins for Blynk (used to control relays)
#define button1_vpin V1
#define button2_vpin V2
#define button3_vpin V3
#define button4_vpin V4
// Sync relay states with the Blynk server when connected
BLYNK_CONNECTED() {
Blynk.syncVirtual(button1_vpin);
Blynk.syncVirtual(button2_vpin);
Blynk.syncVirtual(button3_vpin);
Blynk.syncVirtual(button4_vpin);
}
// Handle button presses from the Blynk app for relay 1
BLYNK_WRITE(button1_vpin) {
relay1_state = param.asInt(); // Get button state from Blynk app
digitalWrite(relay1_pin, relay1_state); // Set relay state
update_lcd(1, relay1_state); // Update the LCD
}
// Handle button presses from the Blynk app for relay 2
BLYNK_WRITE(button2_vpin) {
relay2_state = param.asInt();
digitalWrite(relay2_pin, relay2_state);
update_lcd(2, relay2_state);
}
// Handle button presses from the Blynk app for relay 3
BLYNK_WRITE(button3_vpin) {
relay3_state = param.asInt();
digitalWrite(relay3_pin, relay3_state);
update_lcd(3, relay3_state);
}
// Handle button presses from the Blynk app for relay 4
BLYNK_WRITE(button4_vpin) {
relay4_state = param.asInt();
digitalWrite(relay4_pin, relay4_state);
update_lcd(4, relay4_state);
}
void setup() {
Serial.begin(115200); // Start Serial communication
// Initialize the LCD
LCD.init();
LCD.backlight();
LCD.setCursor(0, 0);
LCD.print("Connecting to");
LCD.setCursor(0, 1);
LCD.print("WiFi...");
// Configure button pins as inputs with pull-up resistors
pinMode(button1_pin, INPUT_PULLUP);
pinMode(button2_pin, INPUT_PULLUP);
pinMode(button3_pin, INPUT_PULLUP);
pinMode(button4_pin, INPUT_PULLUP);
// Configure relay pins as outputs
pinMode(relay1_pin, OUTPUT);
pinMode(relay2_pin, OUTPUT);
pinMode(relay3_pin, OUTPUT);
pinMode(relay4_pin, OUTPUT);
// Set all relays to OFF initially (HIGH in this setup means OFF)
digitalWrite(relay1_pin, HIGH);
digitalWrite(relay2_pin, HIGH);
digitalWrite(relay3_pin, HIGH);
digitalWrite(relay4_pin, HIGH);
// Connect to Wi-Fi and Blynk
Blynk.begin(auth, ssid, pass);
// Update the LCD display when Wi-Fi is connected
LCD.clear();
LCD.setCursor(0, 0);
LCD.print("WiFi Connected!");
delay(2000); // Delay for visibility
LCD.clear();
LCD.setCursor(0, 0);
LCD.print("Home Automation");
}
void loop() {
Blynk.run(); // Handle Blynk communication
timer.run(); // Handle Blynk timers
listen_push_buttons(); // Check for physical button presses
}
// Function to detect button presses and toggle relays
void listen_push_buttons() {
if (digitalRead(button1_pin) == LOW) { // Button 1 pressed
delay(200); // Debounce delay
control_relay(1); // Toggle relay 1
Blynk.virtualWrite(button1_vpin, relay1_state); // Update Blynk app
} else if (digitalRead(button2_pin) == LOW) { // Button 2 pressed
delay(200);
control_relay(2);
Blynk.virtualWrite(button2_vpin, relay2_state);
} else if (digitalRead(button3_pin) == LOW) { // Button 3 pressed
delay(200);
control_relay(3);
Blynk.virtualWrite(button3_vpin, relay3_state);
} else if (digitalRead(button4_pin) == LOW) { // Button 4 pressed
delay(200);
control_relay(4);
Blynk.virtualWrite(button4_vpin, relay4_state);
}
}
// Function to toggle the relay state
void control_relay(int relay) {
if (relay == 1) {
relay1_state = !relay1_state; // Toggle state
digitalWrite(relay1_pin, relay1_state); // Set relay state
update_lcd(1, relay1_state); // Update LCD
} else if (relay == 2) {
relay2_state = !relay2_state;
digitalWrite(relay2_pin, relay2_state);
update_lcd(2, relay2_state);
} else if (relay == 3) {
relay3_state = !relay3_state;
digitalWrite(relay3_pin, relay3_state);
update_lcd(3, relay3_state);
} else if (relay == 4) {
relay4_state = !relay4_state;
digitalWrite(relay4_pin, relay4_state);
update_lcd(4, relay4_state);
}
}
// Function to update the LCD with the relay status
void update_lcd(int relay, int state) {
LCD.clear(); // Clear the LCD
LCD.setCursor(0, 0); // First row
LCD.print("Bulb ");
LCD.print(relay);
if (state) {
LCD.print(" is ON"); // Display ON if state is 1
} else {
LCD.print(" is OFF"); // Display OFF if state is 0
}
LCD.setCursor(0, 1); // Second row
LCD.print("Home Automation");
}