#define BLYNK_TEMPLATE_ID "TMPL3wGxWRynx"
#define BLYNK_TEMPLATE_NAME "smart building"
#define BLYNK_AUTH_TOKEN "wKBw7EHF_TkbMSm0Kq_p0UTta7cL9FT5"
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include "DHTesp.h"
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
//calling blynk softtware
BlynkTimer timer;
//calling dht sensor
DHTesp dhtSensor;
const int dev1 = 23;
const int dev2 = 22;
const int dev3 = 21;
const int dev4 = 19;
const int dev5 = 18;
const int dht = 17;
const int button1 = 5;
int relay_s_dev1 = 0;
int relay_s_dev2 = 0;
int relay_s_dev3 = 0;
int relay_s_dev4 = 0;
int relay_s_dev5 = 0;
#define vpin1 V1
#define vpin2 V2
#define vpin3 V3
#define vpin4 V4
#define vpin5 V5
BLYNK_CONNECTED() {
Blynk.syncVirtual(vpin1);
Blynk.syncVirtual(vpin2);
Blynk.syncVirtual(vpin3);
Blynk.syncVirtual(vpin4);
Blynk.syncVirtual(vpin5);
}
BLYNK_WRITE(vpin1){
relay_s_dev1 = param.asInt();
digitalWrite(dev1, relay_s_dev1);
}
BLYNK_WRITE(vpin2){
relay_s_dev2 = param.asInt();
digitalWrite(dev2, relay_s_dev2);
}
BLYNK_WRITE(vpin3){
relay_s_dev3 = param.asInt();
digitalWrite(dev3, relay_s_dev3);
}
BLYNK_WRITE(vpin4){
relay_s_dev4 = param.asInt();
digitalWrite(dev4, relay_s_dev4);
}
BLYNK_WRITE(vpin5){
relay_s_dev5 = param.asInt();
digitalWrite(dev5, relay_s_dev5);
}
// Function to read temperature from DHT sensor and send to Blynk
void send_dht_val() {
TempAndHumidity data = dhtSensor.getTempAndHumidity(); // Get temperature and humidity data
float temp = data.temperature; // Store temperature value
Blynk.virtualWrite(V5, temp); // Send temperature data to Blynk app
}
// Setup function to initialize devices and WiFi connection
void setup() {
pinMode(dev1, OUTPUT); // Set LED1 as output
pinMode(dev2, OUTPUT); // Set LED2 as output
pinMode(dev3, OUTPUT); // Set Buzzer1 as output
pinMode(dev4, OUTPUT); // Set Buzzer2 as output
pinMode(dev5, OUTPUT); // (Unused) Set unused device as output
// Initialize push button as input with pull-up resistor
pinMode(button1, INPUT_PULLUP);
// Setup DHT sensor
dhtSensor.setup(dht, DHTesp::DHT22); // Initialize DHT22 sensor
Serial.begin(115200); // Start serial communication for debugging
// Initialize all outputs to HIGH (turn off devices)
digitalWrite(dev1, HIGH);
digitalWrite(dev2, HIGH);
digitalWrite(dev3, HIGH);
digitalWrite(dev4, HIGH);
// Connect to Blynk server
Blynk.begin(auth, ssid, pass);
}
// Function to check the status of the push button
void button_status() {
if (digitalRead(button1) == LOW) { // Check if button is pressed
delay(500); // Debounce delay
controlButton(1); // Control LED1
Blynk.virtualWrite(vpin1, relay_s_dev1); // Update Blynk with new state
}
}
// Function to toggle LED1 state
void controlButton(int relayIn) {
if (relayIn == 1) {
relay_s_dev1 = !relay_s_dev1; // Toggle state
digitalWrite(dev1, relay_s_dev1); // Set the new state of LED1
delay(100); // Short delay to stabilize
}
}
// Loop function to run continuously
void loop() {
Blynk.run(); // Process Blynk commands
timer.run(); // Run timer tasks
button_status(); // Check push button status
timer.setInterval(2000, send_dht_val); // Set interval for sending DHT data
}