// Blync is a low-code IoT software platform for businesses and developers
// Create customer-ready IoT apps with no code
// In our project we create a web dashboard to contol the switches
#define BLYNK_TEMPLATE_ID "TMPL2lAv0TrpL"
#define BLYNK_TEMPLATE_NAME "Smart Home"
#define BLYNK_AUTH_TOKEN "VexMSvhBD8RBD7vna94qbsQ20XJritP9"
#include <WiFi.h>
#include <WiFiClient.h>
#include "DHTesp.h" // digital humidity and temperature sensor
#include <LiquidCrystal_I2C.h>
#include <BlynkSimpleEsp32.h>
#define BLYNK_PRINT Serial
#define I2C_ADDR 0x27
#define LCD_COLUMNS 16
#define LCD_LINES 2
#define button1_pin 5
#define button2_pin 17
#define button3_pin 16
#define button4_pin 4
// the relays are used to control the buttons and lights.
// The buttons are connected to the control coils of the relays,
// and the lights are connected to the contacts of the relays.
// When a button is pressed, it energizes the control coil of the relay,
// which closes the contacts of the relay.
// This allows current to flow to the light, which turns it on.
// it prevents any electrical problems in the load circuit from affecting the control circuit.
#define relay1_pin 23
#define relay2_pin 2
#define relay3_pin 19
#define relay4_pin 18
//the virtual pins
#define button1_vpin V1
#define button2_vpin V2
#define button3_vpin V3
#define button4_vpin V4
// Blync
// Timer feature to send data in intervals.
// It allows you to send data periodically with given intervals and not interfere with other Blynk library routines
BlynkTimer timer;
char auth[] = BLYNK_AUTH_TOKEN;
// set the network credentials
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
//DHT
DHTesp dhtSensor;
const int DHT_PIN = 15;
// ultras sound sensor
// measures the distance to an object using ultrasonic sound waves
int Trig_pin =12; //emits a high frequency sound, the sound travels through the air
int Echo_pin =13; //receives the reflected sound
long duration;
float Speed_of_sound =0.034;
float dist_in_cm;
//Liquid Crystal
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
int relay1_state = 0;
int relay2_state = 0;
int relay3_state = 0;
int relay4_state = 0;
// This function is called every time the device is connected to the Blynk.Cloud
// Request the latest state from the server
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
//i.e when web push switch from Web Dashboard
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 listen_push_buttons(){
if(digitalRead(button1_pin) == LOW){
//delay(200);
control_relay(1);
Blynk.virtualWrite(button1_vpin, relay1_state); //update button state
}
else if (digitalRead(button2_pin) == LOW){
//delay(200);
control_relay(2);
Blynk.virtualWrite(button2_vpin, relay2_state); //update button state
}
else if (digitalRead(button3_pin) == LOW){
//delay(200);
control_relay(3);
Blynk.virtualWrite(button3_vpin, relay3_state); //update button state
}
else if (digitalRead(button4_pin) == LOW){
//delay(200);
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);
//delay(50);
}
else if(relay == 2){
relay2_state = !relay2_state;
digitalWrite(relay2_pin, relay2_state);
//delay(50);
}
else if(relay == 3){
relay3_state = !relay3_state;
digitalWrite(relay3_pin, relay3_state);
//delay(50);
}
else if(relay == 4){
relay4_state = !relay4_state;
digitalWrite(relay4_pin, relay4_state);
//delay(50);
}
}
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);
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
lcd.init();
lcd.backlight();
pinMode(Trig_pin, OUTPUT);
pinMode(Echo_pin,INPUT);
}
void loop()
{
// DHT and LCD
lcd.setCursor(0, 0);
TempAndHumidity data = dhtSensor.getTempAndHumidity();
Serial.println("Temp: " + String(data.temperature, 1) + "°C");
Serial.println("Humidity: " + String(data.humidity, 1) + "%");
Serial.println("---");
lcd.print("T:" + String(data.temperature, 1) + "\xDF"+"C,");
lcd.print("H:" + String(data.humidity, 1) + "%");
Blynk.run();
timer.run();
listen_push_buttons();
// LCD and ultras sound sensor
lcd.setCursor(0, 1);
digitalWrite(Trig_pin, LOW);
delayMicroseconds(2);
digitalWrite(Trig_pin, HIGH);
delayMicroseconds(10);
digitalWrite(Trig_pin,LOW);
duration =pulseIn(Echo_pin,HIGH);
dist_in_cm=duration*Speed_of_sound/2;
lcd.print("D:" + String(dist_in_cm, 1) + "cm");
Serial.print("distance in cm :");
Serial.println(dist_in_cm);
}