#define BLYNK_TEMPLATE_ID "TMPL3LJ4dfKmk" //identifiers for a template in the Blynk app (used for the virtual devices).
#define BLYNK_TEMPLATE_NAME "Temperature Sensor"
#define BLYNK_AUTH_TOKEN "5UU61mGz1levQiAmZJ4T9C8LHL-2UEPo"// authentication token used to authorize the device to connect with the Blynk app.
#define BLYNK_PRINT Serial //directs the debug output to the Serial monitor, so you can see logs for debugging.
#include <WiFi.h> // Handles Wi-Fi connectivity for the ESP32.
#include <BlynkSimpleEsp32.h> // Enables Blynk integration on the ESP32.
#include <DHTesp.h> // Library for DHT sensor and Library for reading temperature and humidity data from a DHT sensor
char auth[] = BLYNK_AUTH_TOKEN; // Auth Token for Blynk
char ssid[] = "Wokwi-GUEST"; // WiFi SSID
char pass[] = ""; // WiFi password
const int DHT_PIN = 15; // DHT22 sensor is connected to gpio pin 15
// LED pins
byte LED_R = 26;
byte LED_Y = 27;
byte LED_G = 14;
byte LED_B = 12;
// Simulated temperature and heater state
float simulatedTemperature=25.0; // Starting temperature (25°C)
float heatIncrement = 0.5; // Temperature increase when heater is on
int heatStatus = 0; // Heater status (0 = off, 1 = on)
DHTesp dht; // DHT sensor object
BlynkTimer timer; // An object of the BlynkTimer class that manages periodic tasks like sending sensor data.
// Function to simulate temperature changes and send sensor data
void sendSensor() {
TempAndHumidity data = dht.getTempAndHumidity();
// Simulate temperature behavior based on heater status
if (heatStatus == 1) {
simulatedTemperature+=heatIncrement; // Increase temperature when heater is on
} else {
simulatedTemperature-=0.1; // Gradual cooling when heater is off
if (simulatedTemperature < 20.0) { // Prevent temperature from going below 20°C
simulatedTemperature = 20.0;
}
}
// Print the simulated data to Serial Monitor
Serial.print("Temperature: ");
Serial.print(simulatedTemperature);
Serial.println("°C");
Serial.print("Humidity: ");
Serial.print(data.humidity);
Serial.println("%");
// Send simulated temperature and humidity to Blynk
Blynk.virtualWrite(V4, simulatedTemperature); //sends the simulated temperature to virtual pin V4 in the Blynk app.
Blynk.virtualWrite(V5, data.humidity); // sends the simulated humidity to virtual pin V5 in the Blynk app.
}
// Virtual pin V0 to control the red LED
BLYNK_WRITE(V0) {
int value = param.asInt();
digitalWrite(LED_R, value);
Blynk.virtualWrite(V0, value); // Optionally send LED status to Blynk app
}
// Virtual pin V1 to control the yellow LED
BLYNK_WRITE(V1) {
int value = param.asInt();
digitalWrite(LED_Y, value);
Blynk.virtualWrite(V1, value); // Optionally send LED status to Blynk app
}
// Virtual pin V2 to control the green LED
BLYNK_WRITE(V2) {
int value = param.asInt();
digitalWrite(LED_G, value);
Blynk.virtualWrite(V2, value); // Optionally send LED status to Blynk app
}
// Virtual pin V3 to control the blue LED
BLYNK_WRITE(V3) {
int value = param.asInt();
digitalWrite(LED_B, value);
Blynk.virtualWrite(V3, value); // Optionally send LED status to Blynk app
}
// Virtual pin V6 to control the heater (heat source)
BLYNK_WRITE(V6) {
heatStatus = param.asInt(); // 0 = heater off, 1 = heater on
Blynk.virtualWrite(V6, heatStatus); // Send heat status to Blynk app
}
void setup() {
Serial.begin(115200); // Initialize Serial Monitor for debugging
dht.setup(DHT_PIN, DHTesp::DHT22); // Initialize DHT22 sensor
pinMode(LED_R, OUTPUT);
pinMode(LED_Y, OUTPUT);
pinMode(LED_G, OUTPUT);
pinMode(LED_B, OUTPUT);
// Initialize Blynk with provided credentials
Blynk.begin(auth, ssid, pass);
// Set the timer interval to send sensor data every 1000ms (1 second)
timer.setInterval(1000, sendSensor);
}
void loop() {
Blynk.run(); // Keeps the Blynk connection alive and handles incoming and outgoing Blynk commands.
timer.run(); // Runs the timer, which periodically calls sendSensor() to send temperature and humidity data.
}