// Define the Blynk print target as Serial
#define BLYNK_PRINT Serial
// Blynk template configuration
#define BLYNK_TEMPLATE_ID "TMPL23mDYSfB4"
#define BLYNK_TEMPLATE_NAME "IOTProject1"
#define BLYNK_AUTH_TOKEN "rEDNF3gphN4vbz3sFW-iw-ZPhD9IbR2o"
// Include necessary libraries
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <DHT.h>
// Initialize DHT sensor on pin 15 for temperature and humidity readings
DHT dht(15, DHT22);
// Define pin numbers for LEDs and PIR sensor
#define led1 17 // LED 1 connected to GPIO 17
#define led2 18 // LED 2 connected to GPIO 18
// Define virtual pins
#define V_TEMP V3 // Temperature
#define V_HUMID V4 // Humidity
#define V_LED1 V0 // LED 1
#define V_LED2 V1 // LED 2
// WiFi credentials
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
// Blynk timer
BlynkTimer timer;
// Blynk connected callback
BLYNK_CONNECTED() {
Blynk.syncVirtual(V_LED1); // Synchronize LED1 state
Blynk.syncVirtual(V_LED2); // Synchronize LED2 state
}
// Callback for virtual button to control LED 1
BLYNK_WRITE(V_LED1) {
int state = param.asInt(); // Get the button state (0 or 1)
digitalWrite(led1, state); // Control LED1
}
// Callback for virtual button to control LED 2
BLYNK_WRITE(V_LED2) {
int state = param.asInt(); // Get the button state (0 or 1)
digitalWrite(led2, state); // Control LED2
}
// Timer function to send DHT sensor data
void sendDHTData() {
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
Blynk.virtualWrite(V_TEMP, temperature);
Blynk.virtualWrite(V_HUMID, humidity);
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.print("%\t");
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" °C");
}
// Main setup function
void setup() {
Serial.begin(115200);
pinMode(led1, OUTPUT); // Set LED1 as output
pinMode(led2, OUTPUT); // Set LED2 as output
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
timer.setInterval(2000L, sendDHTData); // Send sensor data every 2 seconds
}
// Main loop
void loop() {
Blynk.run(); // Run Blynk
timer.run(); // Run the timer
}