Course Content
📘 MODULE 6 – Smart Pet Feeder
0/1
📘 MODULE 7 – Smart Water Management System
0/2
📘 MODULE 8 – Water Quality Monitoring System
0/2
📘 MODULE 10 – Gas Leakage Detection System
0/2
Mastering IoT with 11 Real-World Projects and 1 mega project

✅ 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

 
#define BLYNK_PRINT Serial

This tells Blynk:
👉 Print connection messages in Serial Monitor
Example:

  • Connecting to WiFi…

  • Connected to Blynk Cloud


✅ 2) Blynk Credentials

 
#define BLYNK_TEMPLATE_ID "TMPL33lfE6G29"
#define BLYNK_TEMPLATE_NAME "ESPDHT11"
#define BLYNK_AUTH_TOKEN "...."

🔹 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

 
#include <WiFi.h>

#include <BlynkSimpleEsp32.h>
#include <DHT.h>

🔹 WiFi.h

Connect ESP32 to WiFi.

🔹 BlynkSimpleEsp32.h

Connect ESP32 to Blynk Cloud.

🔹 DHT.h

Read temperature and humidity.


✅ 4) WiFi Credentials

 
char ssid[] = "YOUR_WIFI_NAME";
char pass[] = "YOUR_WIFI_PASSWORD";

ESP32 uses this to connect to your WiFi router.

⚠️ Important:

  • WiFi must be 2.4 GHz

  • Blynk needs internet


✅ 5) DHT Sensor Setup

 
#define DHTPIN 4
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
  • DHT data pin is connected to GPIO4

  • Sensor is DHT11

  • Creates DHT object named dht


✅ 6) Timer Object

 
BlynkTimer timer;

This is used instead of delay().

Why timer is important?

Because Blynk needs:

 
Blynk.run();

to run continuously.

If you use delay() too much, Blynk becomes slow or offline.


✅ 7) Function sendDHTData()

 
void sendDHTData() {

This function:

  • Reads DHT11

  • Sends values to Blynk


🔹 Read humidity

 
float humidity = dht.readHumidity();

Reads humidity in %.


🔹 Read temperature

 
float temperature = dht.readTemperature();

Reads temperature in Celsius.


🔹 Check sensor failure

 
if (isnan(humidity) || isnan(temperature))

If sensor fails, value becomes:

❌ NAN = Not a Number

So code prints error and stops.


🔹 Print values on Serial Monitor

 
Serial.print(...)

Helps debugging.


🔹 Send values to Blynk

 
Blynk.virtualWrite(V0, temperature);
Blynk.virtualWrite(V1, humidity);

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

 
Serial.begin(115200);

🔹 Start DHT sensor

 
dht.begin();

🔹 Connect to Blynk

 
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);

This does 3 things:

  1. Connects to WiFi

  2. Connects to Blynk cloud

  3. Authenticates device using token


🔹 Timer interval

 
timer.setInterval(2000L, sendDHTData);

This means:
⏱ Every 2000 ms (2 sec)
Run:

 
sendDHTData();

✅ 9) loop() Function

Runs forever.

 
void loop()
{
Blynk.run();
timer.run();
}

🔹 Blynk.run()

Keeps ESP32 connected to Blynk cloud.

🔹 timer.run()

Runs scheduled functions like sendDHTData().

Scroll to Top