// Blynk authentication token
#define BLYNK_TEMPLATE_ID "TMPL2_WWPOhUU"
#define BLYNK_TEMPLATE_NAME "HOME AUTOMATION CLASS EXAM"
#define BLYNK_AUTH_TOKEN "wNJ0EHaUkGC30Wh3PV6tCpoJeJu2y3jw"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
// WiFi credentials
const char* ssid = "Wokwi-GUEST";
const char* pass = "";
// Initialize the LCD with I2C address 0x27, 16 columns, and 2 rows
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Define pins for buttons
const int button1 = 26;
const int button2 = 25;
const int button3 = 33;
const int button4 = 32;
// Define pins for relays controlling LEDs
const int relay1 = 13;
const int relay2 = 12;
const int relay3 = 14;
const int relay4 = 27;
// Variables to track relay states
bool relayState1 = LOW;
bool relayState2 = LOW;
bool relayState3 = LOW;
bool relayState4 = LOW;
// Debounce timing
unsigned long lastDebounceTime1 = 0;
unsigned long lastDebounceTime2 = 0;
unsigned long lastDebounceTime3 = 0;
unsigned long lastDebounceTime4 = 0;
const unsigned long debounceDelay = 50; // debounce delay in milliseconds
// Blynk virtual pins
#define VPIN_Television V13
#define VPIN_Fan V14
#define VPIN_Oven V27
#define VPIN_Lights V12
// Function to update LCD and relay state
void updateDeviceState(const char* room, bool state, int relayPin) {
digitalWrite(relayPin, state);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(room);
lcd.setCursor(0, 1);
lcd.print(state ? "ON" : "OFF");
}
// Blynk handlers for virtual pins
BLYNK_WRITE(VPIN_Television) {
relayState1 = param.asInt();
updateDeviceState("Television:", relayState1, relay1);
Blynk.virtualWrite(VPIN_Television, relayState1);
}
BLYNK_WRITE(VPIN_Fan) {
relayState2 = param.asInt();
updateDeviceState("Fan:", relayState2, relay2);
Blynk.virtualWrite(VPIN_Fan, relayState2);
}
BLYNK_WRITE(VPIN_Oven) {
relayState3 = param.asInt();
updateDeviceState("Oven:", relayState3, relay3);
Blynk.virtualWrite(VPIN_Oven, relayState3);
}
BLYNK_WRITE(VPIN_Lights) {
relayState4 = param.asInt();
updateDeviceState("Lights:", relayState4, relay4);
Blynk.virtualWrite(VPIN_Lights, relayState4);
}
void setup() {
// Initialize relay pins as outputs and turn them off by default
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
pinMode(relay3, OUTPUT);
pinMode(relay4, OUTPUT);
digitalWrite(relay1, LOW);
digitalWrite(relay2, LOW);
digitalWrite(relay3, LOW);
digitalWrite(relay4, LOW);
// Initialize button pins as inputs with pull-up resistors
pinMode(button1, INPUT_PULLUP);
pinMode(button2, INPUT_PULLUP);
pinMode(button3, INPUT_PULLUP);
pinMode(button4, INPUT_PULLUP);
// Start serial communication at 9600 bps
Serial.begin(9600);
// Initialize the LCD and display the welcome message
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Home Automation");
// Connect to WiFi and Blynk
WiFi.begin(ssid, pass);
lcd.setCursor(0, 1);
lcd.print("Connecting WiFi..");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
// Start Blynk
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("System Ready!");
delay(2000);
lcd.clear();
}
void loop() {
Blynk.run(); // Run Blynk
// Check for button1 (Television) press
if (digitalRead(button1) == LOW && (millis() - lastDebounceTime1) > debounceDelay) {
relayState1 = !relayState1;
updateDeviceState("Television:", relayState1, relay1);
Blynk.virtualWrite(VPIN_Television, relayState1);
lastDebounceTime1 = millis();
}
// Check for button2 (Fan) press
if (digitalRead(button2) == LOW && (millis() - lastDebounceTime2) > debounceDelay) {
relayState2 = !relayState2;
updateDeviceState("Fan:", relayState2, relay2);
Blynk.virtualWrite(VPIN_Fan, relayState2);
lastDebounceTime2 = millis();
}
// Check for button3 (Oven) press
if (digitalRead(button3) == LOW && (millis() - lastDebounceTime3) > debounceDelay) {
relayState3 = !relayState3;
updateDeviceState("Oven:", relayState3, relay3);
Blynk.virtualWrite(VPIN_Oven, relayState3);
lastDebounceTime3 = millis();
}
// Check for button4 (Lights) press
if (digitalRead(button4) == LOW && (millis() - lastDebounceTime4) > debounceDelay) {
relayState4 = !relayState4;
updateDeviceState("Lights:", relayState4, relay4);
Blynk.virtualWrite(VPIN_Lights, relayState4);
lastDebounceTime4 = millis();
}
// Check if there is any serial input from the keyboard
if (Serial.available() > 0) {
char command = Serial.read();
// Control relays based on keyboard input
switch (command) {
case '1':
relayState1 = !relayState1;
updateDeviceState("Television:", relayState1, relay1);
Blynk.virtualWrite(VPIN_Television, relayState1);
Serial.println("Television toggled");
break;
case '2':
relayState2 = !relayState2;
updateDeviceState("Fan:", relayState2, relay2);
Blynk.virtualWrite(VPIN_Fan, relayState2);
Serial.println("Fan toggled");
break;
case '3':
relayState3 = !relayState3;
updateDeviceState("Oven:", relayState3, relay3);
Blynk.virtualWrite(VPIN_Oven, relayState3);
Serial.println("Oven toggled");
break;
case '4':
relayState4 = !relayState4;
updateDeviceState("Lights:", relayState4, relay4);
Blynk.virtualWrite(VPIN_Lights, relayState4);
Serial.println("lights toggled");
break;
default:
Serial.println("Invalid command. Use 1, 2, 3, or 4.");
break;
}
}
}