#define BLYNK_TEMPLATE_ID "TMPL5xSECELvz" // Define Blynk template ID for the project
#define BLYNK_TEMPLATE_NAME "Home Automation" // Define the name of the Blynk template
#define BLYNK_AUTH_TOKEN "4TmUQmuu0r66mBmRcYeRtTVrknKiRzTx" // Define Blynk authentication token for this project
#include <LiquidCrystal_I2C.h> // Include the LiquidCrystal_I2C library for LCD control
LiquidCrystal_I2C lcd(0x27, 20, 4); // Initialize an object 'lcd' of class LiquidCrystal_I2C with the specified I2C address (0x27), and LCD dimensions (20x4)
#define BLYNK_PRINT Serial // Define Blynk print to use serial communication for debugging
#include <WiFi.h> // Include the WiFi library for ESP32
#include <WiFiClient.h> // Include the WiFiClient library for ESP32
#include <BlynkSimpleEsp32.h> // Include the Blynk library for ESP32
#include "DHTesp.h" // Include the DHTesp library for DHT sensor control
BlynkTimer timer; // Declare a BlynkTimer object for handling timing events
char auth[] = BLYNK_AUTH_TOKEN; // Define the authentication token for Blynk
char ssid[] = "Wokwi-GUEST"; // Define the SSID (name) of the Wi-Fi network to connect to
char pass[] = ""; // Define the password for the Wi-Fi network by default its empty
int val = 0, va1,va2,va3,va4,va5,ge, t =15 ; // Declare several integer variables and initialize 't' to 15
float tmp,hum = 0; // Declare floating-point variables for temperature and humidity and initialize 'hum' to 0
int ledPin = 2; // Define the pin connected to an LED
int inputPin = 27; //this is pin connected to PIR
int pirState,k;
int v = 0;
//temp symbol
byte t1[8]={B00000, B00001, B00010, B00100, B00100, B00100, B00100, B00111,};
byte t2[8]={B00111, B00111, B00111, B01111,B11111, B11111, B01111, B00011,};
byte t3[8]={B00000, B10000, B01011, B00100, B00111, B00100, B00111, B11100,};
byte t4[8]={B11111, B11100, B11100, B11110,B11111, B11111, B11110, B11000,};
//humidity symbol
byte hum1[8]={B00000, B00001, B00011, B00011,B00111, B01111, B01111, B11111,};
byte hum2[8]={B11111, B11111, B11111, B01111,B00011, B00000, B00000, B00000,};
byte hum3[8]={B00000, B10000, B11000, B11000, B11100, B11110, B11110, B11111,};
byte hum4[8]={B11111, B11111, B11111, B11110, B11100, B00000, B00000, B00000,};
//Home Symbol
byte house1[8]={B00000, B00001, B00011, B00011, B00111, B01111, B01111, B11111,};
byte house2[8]={B11111, B11111, B11100, B11100, B11100, B11100, B11100, B11100,};
byte house3[8]={B00000, B10010, B11010, B11010, B11110, B11110, B11110, B11111,};
byte house4[8]={B11111, B11111, B11111, B10001, B10001, B10001, B11111, B11111,};
// Define custom characters for lock symbol
byte d[8] = { 0b00011,0b00011,0b00000,0b00000,0b00000,0b00000,0b00000,0b00000 };
// Define custom character for degree symbol
byte Lck[] = { B01110, B10001, B10001, B11111, B11011, B11011, B11111, B00000 };
DHTesp temps; // Declare an instance of the DHTesp class for temperature and humidity sensing
BLYNK_WRITE(V0){ // Blynk virtual pin V0 handler
va1 = param.asInt(); // Read the value sent from the virtual pin V0 and store it in va1
digitalWrite(5, va1); // Set the digital pin 5 to the value received from V0
}
BLYNK_WRITE(V1){ // Blynk virtual pin V1 handler
va2 = param.asInt(); // Read the value sent from the virtual pin V1 and store it in va2
digitalWrite(18, va2); // Set the digital pin 18 to the value received from V1
}
/*
BLYNK_WRITE(V2){
va3 = param.asInt();
digitalWrite(19, va3);
}
BLYNK_WRITE(V3){
va4 = param.asInt();
digitalWrite(4, va4);
}
BLYNK_WRITE(V4){
va5 = param.asInt();
digitalWrite(2, va5);
}
*/
BLYNK_WRITE(V2) { // Blynk virtual pin V2 handler, responsible for controlling PIR sensor with a relay
pirState = param.asInt(); // Read the value sent from the virtual pin V2 and store it in pirState
if(pirState == 0){ // If the value is 0 (indicating no motion)
digitalWrite(33, LOW);// Turn off the relay connected to pin 33.By defult door light will be off
k = 1; // Set k to 1
ge = 0; // Set ge to 0
}
else { // If the value is not 0 (indicating motion detected)
digitalWrite(33, HIGH); // Turn on the relay connected to pin 33, making the door light on
k= 0; // Set k to 0
ge = 1; // Set ge to 1
}
}
void myTimer() // Define a function named myTimer. after every 10 second
{
Blynk.virtualWrite(V3,tmp); //Send the value of 'tmp' (temperature) to Blynk server using virtual pin V3. This will send the data to Blynk server every 10 second
Blynk.virtualWrite(V4,hum); // Send the value of 'hum' (humidity) to Blynk server using virtual pin V4. This will send the data to Blynk server every 10 second
}
void setup()
{
Serial.begin(115200); // Initialize serial communication at a baud rate of 115200
Blynk.begin(auth, ssid, pass); // Initialize Blynk communication with provided authentication token, SSID, and password
pinMode(5, OUTPUT); // Set pin 5 as an output for the first relay
pinMode(18, OUTPUT); // Set pin 18 as an output for the second relay
pinMode(19, OUTPUT); // Set pin 19 as an output for the third relay
pinMode(4, OUTPUT); //these are first 4 relays on the right hand side
pinMode(33,OUTPUT); // Set pin 33 as an output for the single relay on the left-hand side
pinMode(2,OUTPUT); //connected to led
temps.setup(t, DHTesp::DHT22); // Setup DHT sensor with pin t and type DHT22
pinMode(ledPin, OUTPUT); //pin number 2 will be output that is led
pinMode(inputPin, INPUT); //pir Sensor
lcd.init(); // Initialize the LCD
lcd.backlight(); //initilising and starting up the LCD
digitalWrite(5, LOW); // Turn off the first relay at the starting
digitalWrite(18, LOW); // Turn off the second relay at the starting
digitalWrite(19, LOW); // Turn off the third relay at the starting
digitalWrite(4, LOW); // Turn off the fourth relay at the starting
lcd.setCursor(0,0); // Set cursor position to the first column of the first row
lcd.print("Welcome Smart Home"); // Print "Welcome Smart Home" at the current cursor position
lcd.setCursor(8,1); //Set cursor to the ninth column of the second row
lcd.print("2024"); // Print "2024" at the current cursor position
lcd.setCursor(0,2); // Set cursor to the first column of the third row
lcd.print("--------------------"); // Print a line of dashes to serve as a separator
lcd.setCursor(9,3); // Set cursor to the tenth column of the fourth row
lcd.print("- eDiYLaBs"); // Print "- eDiYLaBs" at the current cursor position
delay(3000); // Pause execution for 3000 milliseconds (3 seconds) to display the initial information
lcd.clear(); // Clear the LCD display
lcd.createChar(6, Lck); // Create a custom character with index 6 using the character Lck
lcd.createChar(1,house1); // Create a custom character with index 1 using the character house1
lcd.createChar(2,house2); // Create a custom character with index 2 using the character house2
lcd.createChar(3,house3); // Create a custom character with index 3 using the character house3
lcd.createChar(4,house4); // Create a custom character with index 4 using the character house4
lcd.setCursor(1,2); // Set cursor to the second column of the third row
lcd.write(1); // Write custom character with index 1
lcd.setCursor(1,3); // Set cursor to the second column of the fourth row
lcd.write(2); // Write custom character with index 2
lcd.setCursor(2,2); // Set cursor to the third column of the third row
lcd.write(3); // Write custom character with index 3
lcd.setCursor(2,3); // Set cursor to the third column of the fourth row
lcd.write(4); // Write custom character with index 4
lcd.setCursor(17,2); // Set cursor to the seventeenth column of the third row
lcd.write(1); // Write custom character with index 1
lcd.setCursor(17,3); // Set cursor to the seventeenth column of the fourth row
lcd.write(2); // Write custom character with index 2
lcd.setCursor(18,2); // Set cursor to the eighteenth column of the third row
lcd.write(3); // Write custom character with index 3
lcd.setCursor(18,3); // Set cursor to the eighteenth column of the fourth row
lcd.write(4); // Write custom character with index 4
lcd.setCursor(19,0); // Set cursor to the nineteenth column of the first row
lcd.write(6); // Write custom character with index 6
lcd.setCursor(9,0); // Set cursor to the ninth column of the first row
lcd.print("connected-"); // Print "connected-" at the current cursor position
lcd.setCursor(2,1); // Set cursor to the third column of the second row
lcd.print("HOME AUTOMATION"); // Print "HOME AUTOMATION" at the current cursor position
lcd.setCursor(6,2); // Set cursor to the seventh column of the
lcd.print("USING IOT"); // Print "USING IOT" at the current cursor position on the LCD
delay(3000); // Pause execution for 3000 milliseconds (3 seconds)
Blynk.virtualWrite(V2, pirState); // Send the current state of the PIR sensor to the Blynk server using virtual pin V2
timer.setInterval(1000L, myTimer); // Set up a timer to call the myTimer function every 1000 milliseconds (1 second)
// This is the function for sending data to the cloud every 10 seconds
}
void loop()// This Function is for photo resistor
{
Blynk.run(); // Run the Blynk background process to keep the connection alive
timer.run(); // Run the Blynk timer events
val = digitalRead(23); // if the lux is high the valuee will be 0
if(val == 1) // Check if the value of 'val' is equal to 1
{
digitalWrite(2,HIGH); // if the lux is low then value will be 1 the led will be on
}
else{ // If the value of 'val' is not equal to 1
digitalWrite(2,LOW); // If the lux is high (val is not 1), turn off the led connected to pin 2
}
TempAndHumidity x = temps.getTempAndHumidity(); // This section is for Humidity and Temperature
tmp = x.temperature ; //tmp is the temp value
hum = x.humidity ; //hum is the humidity value
v = digitalRead(inputPin); // Read the digital input from the PIR sensor pin (inputPin) and store it in the variable v
Serial.println(v); // Print the value of v to the Serial monitor for debugging purposes
if (v == HIGH) { // Check if the value of v is HIGH (motion detected)
digitalWrite(33, HIGH); // If motion is detected, turn on the relay connected to pin 33
k = 0 ; // Set k to 0
ge = 0; // Set ge to 0
}
else { // If the value of v is not HIGH (no motion detected)
digitalWrite(33, LOW); // If no motion is detected, turn off the relay connected to pin 33
k = 1; // Set k to 1
ge = 1; // Set ge to 1
}
Blynk.virtualWrite(V2, v); // To send the PIR status to the Cloud
if (va1 == 1){ // Check if the value of va1 is equal to 1
lcd.clear(); // Clear the LCD display
lcd.setCursor(19,0); // Set cursor to the twentieth column of the first row
lcd.write(6); // Write custom character at position 6
lcd.setCursor(0, 1);
lcd.print("SW_1= ");// This will display switch status on the LCD
lcd.print("ON ");
}
else{
lcd.clear();
lcd.setCursor(19,0);
lcd.write(6);
lcd.setCursor(0, 1);
lcd.print("SW_1= "); // This will display switch status on the LCD
lcd.print("OFF");
}
if (va2 == 1){
lcd.setCursor(11, 1);
lcd.print("SW_2= "); // This will display switch status on the LCD
lcd.print("ON ");
}
else{
lcd.setCursor(11, 1);
lcd.print("SW_2= "); // This will display switch status on the LCD
lcd.print("OFF");
}
if (va3 == 1){
lcd.setCursor(0, 2);
lcd.print("SW_3= "); // This will display switch status on the LCD
lcd.print("ON ");
}
else{
lcd.setCursor(0, 2);
lcd.print("SW_3= "); // This will display switch status on the LCD
lcd.print("OFF");
}
if (va4 == 1){
lcd.setCursor(11, 2);
lcd.print("SW_4= "); // This will display switch status on the LCD
lcd.print("ON ");
}
else{
lcd.setCursor(11, 2);
lcd.print("SW_4= "); // This will display switch status on the LCD
lcd.print("OFF");
}
if (va5 == 1){
lcd.setCursor(0, 3);
lcd.print("OD_L= "); // This will display switch status on the LCD
lcd.print("ON ");
}
else{
lcd.setCursor(0, 3);
lcd.print("OD_L= "); // This will display switch status on the LCD
lcd.print("OFF");
}
if (ge == 1){
lcd.setCursor(11, 3);
lcd.print("WR_L= "); // This will display switch status on the LCD
lcd.print("ON ");
}
else{
lcd.setCursor(11, 3);
lcd.print("WR_L= "); // This will display switch status on the LCD
lcd.print("OFF");
}
delay(1500); // Pause execution for 1500 milliseconds (1.5 seconds)
lcd.clear(); // Clear the LCD display
lcd.createChar(1,t1);
lcd.createChar(2,t2);
lcd.createChar(3,t3);
lcd.createChar(4,t4);
lcd.createChar(5, d);
lcd.createChar(6, Lck); // This section is responsible for create custom charachters/ Lock symbol
lcd.setCursor(19,0);
lcd.write(6);
lcd.setCursor(1,1);
lcd.write(1);
lcd.setCursor(1,2);
lcd.write(2);
lcd.setCursor(2,1);
lcd.write(3);
lcd.setCursor(2,2);
lcd.write(4);
lcd.setCursor(4,1);
lcd.print("Temperature :"); // Print "Temperature :" at the current cursor position
lcd.setCursor(7,2);
lcd.print(tmp);
lcd.setCursor(11,2);
lcd.write(5);
lcd.setCursor(12,2);
lcd.print("C"); // Print "C" (for Celsius) at the current cursor position
delay(750); // Pause execution for 750 milliseconds (0.75 seconds)
lcd.clear();
lcd.createChar(1,hum1); // Define a custom character with index 1 using the data stored in the array 'hum1'
lcd.createChar(2,hum2); // Define a custom character with index 1 using the data stored in the array 'hum2'
lcd.createChar(3,hum3); // Define a custom character with index 1 using the data stored in the array 'hum3'
lcd.createChar(4,hum4); // Define a custom character with index 1 using the data stored in the array 'hum4'
lcd.setCursor(19,0);
lcd.write(6);
lcd.setCursor(3,1);
lcd.write(1);
lcd.setCursor(3,2);
lcd.write(2);
lcd.setCursor(4,1);
lcd.write(3);
lcd.setCursor(4,2);
lcd.write(4);
lcd.setCursor(6,1);
lcd.print("Humidity :"); // Print "Humidity :" at the current cursor position
lcd.setCursor(7,2);
lcd.print(hum);
lcd.setCursor(12,2);
lcd.print("%"); // Print "%" (for percentage) at the current cursor position
delay(750); // Pause execution for 750 milliseconds (0.75 seconds)
}