#define BLYNK_TEMPLATE_ID "TMPL6ILB77YxK"
#define BLYNK_TEMPLATE_NAME "Home Automation"
#define BLYNK_AUTH_TOKEN "FZE3bFXaziDMlFegp8uuKO-PavpWUuFS"
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h> // Include I2C LCD library
#include <DHT.h>
char auth[] = BLYNK_AUTH_TOKEN;
// WiFi credentials.
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
BlynkTimer timer;
#define DHTPIN 15 // Pin connected to the DHT sensor
#define DHTTYPE DHT22 // DHT 22 sensor type
#define LEDPIN 5 // Pin connected to the LED
#define LCD_ADDRESS 0x27 // LCD's I2C address
#define LCD_ROWS 2 // Number of LCD rows
#define LCD_COLUMNS 16 // Number of LCD columns
LiquidCrystal_I2C lcd(LCD_ADDRESS, LCD_ROWS, LCD_COLUMNS); //LCD object
DHT dht(DHTPIN, DHTTYPE);
#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
int relay1_state = 0;
int relay2_state = 0;
int relay3_state = 0;
int relay4_state = 0;
// virtual pins according the rooms
#define button1_vpin V1
#define button2_vpin V2
#define button3_vpin V3
#define button4_vpin V4
// This function is called every time the device is connected to the Blynk.Cloud
BLYNK_CONNECTED() {
Blynk.syncVirtual(button1_vpin);
Blynk.syncVirtual(button2_vpin);
Blynk.syncVirtual(button3_vpin);
Blynk.syncVirtual(button4_vpin);
}
// This function is called every time the Virtual Pin state change
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);
}
BLYNK_WRITE(button3_vpin) {
relay3_state = param.asInt();
digitalWrite(relay3_pin, relay3_state);
}
BLYNK_WRITE(button4_vpin) {
relay4_state = param.asInt();
digitalWrite(relay4_pin, relay4_state);
}
//--------------------------------------------------------------------------
void setup()
{
// Debug console
Serial.begin(115200);
pinMode(button1_pin, INPUT_PULLUP);
pinMode(button2_pin, INPUT_PULLUP);
pinMode(button3_pin, INPUT_PULLUP);
pinMode(button4_pin, INPUT_PULLUP);
pinMode(relay1_pin, OUTPUT);
pinMode(relay2_pin, OUTPUT);
pinMode(relay3_pin, OUTPUT);
pinMode(relay4_pin, OUTPUT);
//During Starting all Relays should TURN OFF
digitalWrite(relay1_pin, HIGH);
digitalWrite(relay2_pin, HIGH);
digitalWrite(relay3_pin, HIGH);
digitalWrite(relay4_pin, HIGH);
Blynk.begin(auth, ssid, pass);
pinMode(LEDPIN, OUTPUT); // Set LED pin as output
Serial.begin(9600); // Initialize serial communication for debugging
dht.begin(); // Initialize DHT sensor
lcd.init(); // Initialize LCD display
lcd.backlight(); // Turn on backlight (optional)
}
void loop()
{
Blynk.run();
timer.run();
listen_push_buttons();
// Read temperature from the sensor
float temp = dht.readTemperature();
float humidity = dht.readHumidity();
// Check if reading was successful
if (isnan(temp) || isnan(humidity)){
Serial.println("Failed to read from DHT sensor!");
return;
}
// Control LED based on temperature
if (temp > 40) {
digitalWrite(LEDPIN, HIGH); // Turn on LED
} else {
digitalWrite(LEDPIN, LOW); // Turn off LED
}
// Display temperature on LCD
lcd.clear();
lcd.print("Temp: ");
lcd.print(temp);
lcd.print(" C ");
lcd.setCursor(0, 1);
lcd.print("Humidity: ");
lcd.print(humidity);
lcd.print("%");
delay(1000);
}
void listen_push_buttons(){
if(digitalRead(button1_pin) == LOW){
delay(50);
control_relay(1);
Blynk.virtualWrite(button1_vpin, relay1_state); //update button state
}
else if (digitalRead(button2_pin) == LOW){
delay(50);
control_relay(2);
Blynk.virtualWrite(button2_vpin, relay2_state); //update button state
}
else if (digitalRead(button3_pin) == LOW){
delay(50);
control_relay(3);
Blynk.virtualWrite(button3_vpin, relay3_state); //update button state
}
else if (digitalRead(button4_pin) == LOW){
delay(50);
control_relay(4);
Blynk.virtualWrite(button4_vpin, relay4_state); //update button state
}
}
void control_relay(int relay){
if(relay == 1){
relay1_state = !relay1_state;
digitalWrite(relay1_pin, relay1_state);
Serial.print("Relay1 State = ");
Serial.println(relay1_state);
}
else if(relay == 2){
relay2_state = !relay2_state;
digitalWrite(relay2_pin, relay2_state);
Serial.print("Relay2 State = ");
Serial.println(relay2_state);
}
else if(relay == 3){
relay3_state = !relay3_state;
digitalWrite(relay3_pin, relay3_state);
Serial.print("Relay3 State = ");
Serial.println(relay3_state);
}
else if(relay == 4){
relay4_state = !relay4_state;
digitalWrite(relay4_pin, relay4_state);
Serial.print("Relay4 State = ");
Serial.println(relay4_state);
}
}