✅ ESP32 + DHT11 + Blynk IoT
#define BLYNK_PRINT Serial
// -------------------- Blynk Credentials --------------------
#define BLYNK_TEMPLATE_ID "TMPL33lfE6G29"
#define BLYNK_TEMPLATE_NAME "ESPDHT11"
#define BLYNK_AUTH_TOKEN "Dpcq3_UYJNi8tJ238PKQ_1B5GnWhVlb7"
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
#include <DHT.h>
// -------------------- WiFi Credentials --------------------
char ssid[] = "YOUR_WIFI_NAME";
char pass[] = "YOUR_WIFI_PASSWORD";
// -------------------- DHT11 Setup --------------------
#define DHTPIN 4
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
// -------------------- Timer Object --------------------
BlynkTimer timer;
// -------------------- Function: Read DHT & Send to Blynk --------------------
void sendDHTData() {
float humidity = dht.readHumidity();
float temperature = dht.readTemperature(); // Celsius
// Check if sensor reading failed
if (isnan(humidity) || isnan(temperature)) {
Serial.println("❌ Failed to read from DHT11");
return;
}
// Print values on Serial Monitor
Serial.print("🌡 Temp: ");
Serial.print(temperature);
Serial.print(" °C | 💧 Humidity: ");
Serial.print(humidity);
Serial.println(" %");
// Send values to Blynk App using Virtual Pins
Blynk.virtualWrite(V0, temperature); // Temperature → V0
Blynk.virtualWrite(V1, humidity); // Humidity → V1
}
void setup() {
Serial.begin(115200);
Serial.println("✅ Starting ESP32 + DHT11 + Blynk...");
// Start DHT sensor
dht.begin();
// Connect ESP32 to Blynk Cloud (WiFi + Auth Token)
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
// Run sendDHTData() every 2 seconds
timer.setInterval(2000L, sendDHTData);
}
void loop() {
// Must run continuously for Blynk to work
Blynk.run();
// Must run continuously for timer to work
timer.run();
}
🔌 Wiring (DHT11 → ESP32)
| DHT11 Pin | ESP32 |
|---|---|
| VCC | 3.3V |
| GND | GND |
| DATA | GPIO4 |
📲 Blynk App Setup (IMPORTANT)
Create 2 Datastreams:
| Datastream Name | Virtual Pin | Type |
|---|---|---|
| Temperature | V0 | Double |
| Humidity | V1 | Double |
Add widgets:
-
Gauge / Label / Chart for V0
-
Gauge / Label / Chart for V1
🧠 FULL DETAILED CODE EXPLANATION (Line by Line)
✅ 1) Print Debug in Serial Monitor
This tells Blynk:
👉 Print connection messages in Serial Monitor
Example:
-
Connecting to WiFi…
-
Connected to Blynk Cloud
✅ 2) Blynk Credentials
🔹 Template ID
Identifies your project template in Blynk.
🔹 Template Name
Your template name.
🔹 Auth Token
This is like the password/key of your device.
ESP32 uses this token to connect to your Blynk dashboard.
✅ 3) Include Required Libraries
🔹 WiFi.h
Connect ESP32 to WiFi.
🔹 BlynkSimpleEsp32.h
Connect ESP32 to Blynk Cloud.
🔹 DHT.h
Read temperature and humidity.
✅ 4) WiFi Credentials
ESP32 uses this to connect to your WiFi router.
⚠️ Important:
-
WiFi must be 2.4 GHz
-
Blynk needs internet
✅ 5) DHT Sensor Setup
-
DHT data pin is connected to GPIO4
-
Sensor is DHT11
-
Creates DHT object named
dht
✅ 6) Timer Object
This is used instead of delay().
Why timer is important?
Because Blynk needs:
to run continuously.
If you use delay() too much, Blynk becomes slow or offline.
✅ 7) Function sendDHTData()
This function:
-
Reads DHT11
-
Sends values to Blynk
🔹 Read humidity
Reads humidity in %.
🔹 Read temperature
Reads temperature in Celsius.
🔹 Check sensor failure
If sensor fails, value becomes:
❌ NAN = Not a Number
So code prints error and stops.
🔹 Print values on Serial Monitor
Helps debugging.
🔹 Send values to Blynk
This sends data to Blynk app using virtual pins.
📌 Meaning:
-
Temperature goes to V0 widget
-
Humidity goes to V1 widget
✅ 8) setup() Function
Runs once when ESP32 starts.
🔹 Start serial
🔹 Start DHT sensor
🔹 Connect to Blynk
This does 3 things:
-
Connects to WiFi
-
Connects to Blynk cloud
-
Authenticates device using token
🔹 Timer interval
This means:
⏱ Every 2000 ms (2 sec)
Run:
✅ 9) loop() Function
Runs forever.
🔹 Blynk.run()
Keeps ESP32 connected to Blynk cloud.
🔹 timer.run()
Runs scheduled functions like sendDHTData().