✅ 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:
🧠 Detailed Code Explanation (Teaching Style)
✅ 1) Library Include
🔹 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
This checks if Bluetooth is enabled in ESP32 settings.
If Bluetooth is disabled, it stops compilation and shows an error.
✅ 3) Create Bluetooth Object
This creates a Bluetooth Serial object called SerialBT.
Now you can use:
-
SerialBT.begin() -
SerialBT.print() -
SerialBT.println()
✅ 4) DHT Pin and Type
🔹 DHTPIN 4
DHT11 DATA pin is connected to ESP32 GPIO4.
🔹 DHTTYPE DHT11
Tells library you are using DHT11.
✅ 5) Create DHT Object
This creates a DHT object named dht.
Now ESP32 can communicate with DHT11.
✅ 6) setup() Function
Runs only once when ESP32 starts.
🔹 Serial Monitor Start
Starts serial communication with computer.
🔹 Bluetooth Start
This starts Bluetooth and sets device name:
📌 Phone will see:
✅ ESP32_DHT11
🔹 Print Startup Messages
This prints the same message:
-
On Serial Monitor
-
On Bluetooth Terminal
So user knows it started successfully.
🔹 Start DHT Sensor
This initializes DHT11 sensor.
✅ 7) loop() Function
This runs again and again forever.
🔹 Delay
DHT11 needs minimum 2 seconds delay.
If you remove this:
-
readings become wrong
-
or NAN
✅ 8) Read Sensor Data
humidity
Reads humidity in percentage.
temperature
Reads temperature in Celsius.
✅ 9) Error Checking
If wiring is wrong or sensor fails, values become:
❌ NAN (Not a Number)
So we print error.
✅ 10) Print Data (Serial + Bluetooth)
Serial Monitor
Bluetooth Terminal
Both will show the same output.