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 + Bluetooth Terminal (COPY–PASTE CODE)

#include <DHT.h>
#include "BluetoothSerial.h"

// -------------------- Bluetooth Check --------------------
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please enable it in ESP32 settings
#endif

// -------------------- Bluetooth Object --------------------
BluetoothSerial SerialBT;

// -------------------- DHT Setup --------------------
#define DHTPIN 4 // GPIO4 (D4)
#define DHTTYPE DHT11 // Sensor type

DHT dht(DHTPIN, DHTTYPE);

void setup() {
// Start Serial Monitor
Serial.begin(115200);

// Start Bluetooth with device name
SerialBT.begin("ESP32_DHT11");

// Startup messages
Serial.println("ESP32 DHT11 Bluetooth Started");
SerialBT.println("ESP32 DHT11 Bluetooth Started");

// Start DHT sensor
dht.begin();
}

void loop() {
// DHT11 needs at least 2 seconds delay between readings
delay(2000);

// Read humidity and temperature
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 DHT sensor");
SerialBT.println("Failed to read from DHT sensor");
return;
}

// -------------------- Serial Monitor Output --------------------
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print(" °C | Humidity: ");
Serial.print(humidity);
Serial.println(" %");

// -------------------- Bluetooth Terminal Output --------------------
SerialBT.print("Temperature: ");
SerialBT.print(temperature);
SerialBT.print(" °C | Humidity: ");
SerialBT.print(humidity);
SerialBT.println(" %");
}

🔌 Wiring (DHT11 → ESP32)

DHT11 Pin ESP32
VCC 3.3V
GND GND
DATA GPIO4

📱 How to Use Bluetooth Terminal App

Step 1

Upload code to ESP32.

Step 2

Open phone Bluetooth → pair with:
ESP32_DHT11

Step 3

Open Bluetooth Terminal App and connect.

Now you will see:

 
Temperature: 28.00 °C | Humidity: 65.00 %

🧠 Detailed Code Explanation (Teaching Style)


✅ 1) Library Include

 
#include <DHT.h>
#include "BluetoothSerial.h"

🔹 DHT.h

Used to read:

  • Temperature

  • Humidity

🔹 BluetoothSerial.h

Used to send/receive data through:

  • Bluetooth Classic Serial (like HC-05)


✅ 2) Bluetooth Enabled Check

 
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please enable it in ESP32 settings
#endif

This checks if Bluetooth is enabled in ESP32 settings.

If Bluetooth is disabled, it stops compilation and shows an error.


✅ 3) Create Bluetooth Object

 
BluetoothSerial SerialBT;

This creates a Bluetooth Serial object called SerialBT.

Now you can use:

  • SerialBT.begin()

  • SerialBT.print()

  • SerialBT.println()


✅ 4) DHT Pin and Type

 
#define DHTPIN 4
#define DHTTYPE DHT11

🔹 DHTPIN 4

DHT11 DATA pin is connected to ESP32 GPIO4.

🔹 DHTTYPE DHT11

Tells library you are using DHT11.


✅ 5) Create DHT Object

 
DHT dht(DHTPIN, DHTTYPE);

This creates a DHT object named dht.

Now ESP32 can communicate with DHT11.


✅ 6) setup() Function

 
void setup() {

Runs only once when ESP32 starts.


🔹 Serial Monitor Start

 
Serial.begin(115200);

Starts serial communication with computer.


🔹 Bluetooth Start

 
SerialBT.begin("ESP32_DHT11");

This starts Bluetooth and sets device name:

📌 Phone will see:
ESP32_DHT11


🔹 Print Startup Messages

 
Serial.println("ESP32 DHT11 Bluetooth Started");
SerialBT.println("ESP32 DHT11 Bluetooth Started");

This prints the same message:

  • On Serial Monitor

  • On Bluetooth Terminal

So user knows it started successfully.


🔹 Start DHT Sensor

 
dht.begin();

This initializes DHT11 sensor.


✅ 7) loop() Function

 
void loop() {

This runs again and again forever.


🔹 Delay

 
delay(2000);

DHT11 needs minimum 2 seconds delay.

If you remove this:

  • readings become wrong

  • or NAN


✅ 8) Read Sensor Data

 
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();

humidity

Reads humidity in percentage.

temperature

Reads temperature in Celsius.


✅ 9) Error Checking

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

If wiring is wrong or sensor fails, values become:

❌ NAN (Not a Number)

So we print error.


✅ 10) Print Data (Serial + Bluetooth)

Serial Monitor

 
Serial.print(...)

Bluetooth Terminal

 
SerialBT.print(...)

Both will show the same output.


✅ Final Output Example

Serial Monitor

 
Temperature: 28.00 °C | Humidity: 64.00 %

Bluetooth Terminal App

 
Temperature: 28.00 °C | Humidity: 64.00 %


Scroll to Top