#define BLYNK_TEMPLATE_ID "TMPL3J75tDH5M"
#define BLYNK_TEMPLATE_NAME "led"
#define BLYNK_AUTH_TOKEN "a1UBc-umP8ALxuvZKjO0ouONJLhgkaZG"
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include "DHT.h" // Include the DHT sensor library
// WiFi credentials
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
// DHT sensor setup
#define DHTPIN 17 // GPIO17 for the DHT sensor
#define DHTTYPE DHT22 // Use DHT22 (or DHT11 if you are using a DHT11 sensor)
DHT dht(DHTPIN, DHTTYPE);
// LED setup
const int ledPin = 2; // GPIO2 (D2) for external LED
// This function is called every time the button widget on Virtual Pin V0 changes state
BLYNK_WRITE(V0)
{
int pinValue = param.asInt(); // Get the value from the button (0 or 1)
if (pinValue) {
digitalWrite(ledPin, HIGH); // Turn on the LED
Serial.println("LED is ON");
} else {
digitalWrite(ledPin, LOW); // Turn off the LED
Serial.println("LED is OFF");
}
}
// Function to send temperature data to Blynk
void sendSensorData()
{
float temperature = dht.readTemperature(); // Read temperature in Celsius
if (isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println("°C");
Blynk.virtualWrite(V1, temperature); // Send temperature to Virtual Pin V1
}
void setup()
{
Serial.begin(115200);
pinMode(ledPin, OUTPUT); // Configure GPIO2 as an output
digitalWrite(ledPin, LOW); // Ensure the LED starts off
dht.begin(); // Initialize DHT sensor
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass); // Connect to Blynk platform
}
void loop()
{
Blynk.run(); // Run the Blynk library
sendSensorData(); // Send sensor data to Blynk
delay(2000); // Wait for 2 seconds before reading the sensor again
}