ESP32 BLE with DHT11
Introduction
ESP32 is a powerful microcontroller that supports Wi-Fi and Bluetooth Low Energy (BLE), making it ideal for IoT applications. When combined with the DHT11 temperature and humidity sensor, ESP32 can transmit environmental data wirelessly using BLE, allowing smartphones or other BLE-compatible devices to receive real-time temperature and humidity updates.
This guide explains how to interface DHT11 with ESP32 and transmit sensor data over BLE.
1. Components Required
- ESP32 development board
- DHT11 temperature & humidity sensor
- Jumper wires
- Arduino IDE with ESP32 and DHT libraries installed
2. Wiring the ESP32 with DHT11
The DHT11 sensor has three pins that need to be connected to the ESP32.
DHT11 Pin | ESP32 Pin |
---|---|
VCC (Power) | 3.3V or 5V |
Data | GPIO 4 (or any digital pin) |
GND | GND |
Note: A 10KΩ pull-up resistor is recommended between the Data and VCC pins.
3. Setting Up Arduino IDE
Before writing code, install the necessary libraries in Arduino IDE:
- ESP32 Board Package: Go to
Preferences
and add the ESP32 board manager URL:https://dl.espressif.com/dl/package_esp32_index.json
- Install DHT sensor library:
Adafruit DHT
. - Install Adafruit Unified Sensor library.
- Install ESP32 BLE Library (
NimBLE-Arduino
).
4. ESP32 BLE with DHT11 Code
The following code reads temperature and humidity from DHT11 and sends the data over BLE.
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
#include <DHT.h>
#define DHTPIN 4 // DHT11 data pin connected to GPIO4
#define DHTTYPE DHT11 // Define sensor type
DHT dht(DHTPIN, DHTTYPE);
// BLE Service and Characteristic UUIDs
#define SERVICE_UUID "12345678-1234-5678-1234-56789abcdef0"
#define TEMP_CHARACTERISTIC_UUID "12345678-1234-5678-1234-56789abcdef1"
#define HUM_CHARACTERISTIC_UUID "12345678-1234-5678-1234-56789abcdef2"
BLEServer* pServer = NULL;
BLECharacteristic* pTempCharacteristic;
BLECharacteristic* pHumCharacteristic;
void setup() {
Serial.begin(115200);
dht.begin();
BLEDevice::init("ESP32_DHT11");
pServer = BLEDevice::createServer();
BLEService *pService = pServer->createService(SERVICE_UUID);
// Temperature Characteristic
pTempCharacteristic = pService->createCharacteristic(
TEMP_CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_NOTIFY);
pTempCharacteristic->addDescriptor(new BLE2902());
// Humidity Characteristic
pHumCharacteristic = pService->createCharacteristic(
HUM_CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_NOTIFY);
pHumCharacteristic->addDescriptor(new BLE2902());
pService->start();
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
pAdvertising->addServiceUUID(SERVICE_UUID);
BLEDevice::startAdvertising();
Serial.println("BLE Server Started");
}
void loop() {
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
if (!isnan(temperature) && !isnan(humidity)) {
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print(" °C, Humidity: ");
Serial.print(humidity);
Serial.println(" %");
pTempCharacteristic->setValue(temperature);
pTempCharacteristic->notify();
pHumCharacteristic->setValue(humidity);
pHumCharacteristic->notify();
}
delay(2000);
}
5. Explanation of the Code
- Initialize BLE: The ESP32 sets up a BLE server named
ESP32_DHT11
. - Define Characteristics:
- One for temperature (
TEMP_CHARACTERISTIC_UUID
). - One for humidity (
HUM_CHARACTERISTIC_UUID
).
- One for temperature (
- Read Data from DHT11: Temperature and humidity readings are fetched every 2 seconds.
- Send Data Over BLE:
- The values are written to the BLE characteristics.
- BLE notifications update connected devices in real-time.
6. Testing ESP32 BLE with a Smartphone
You can use a BLE scanner app to receive data from the ESP32.
Steps:
- Download nRF Connect or BLE Scanner (available on Android/iOS).
- Open the app and scan for ESP32_DHT11.
- Connect to the ESP32 and subscribe to the temperature and humidity characteristics.
- View real-time data updates on your phone.
7. Applications
- Smart Home Monitoring: Track indoor temperature and humidity.
- IoT Weather Stations: Remotely monitor environmental conditions.
- Agriculture & Greenhouse Control: Automate irrigation and climate control.
- Healthcare & Smart Wearables: Monitor room conditions in hospitals.
8. Conclusion
This guide demonstrates how to interface DHT11 with ESP32 and transmit sensor data over BLE. By integrating BLE, ESP32 can send environmental data wirelessly, making it useful for IoT applications. You can further improve this project by using DHT22 for better accuracy or adding cloud integration for remote access.
You may like also: https://iotbyhvm.ooo/esp32-bluetooth-low-energy-ble-on-arduino-ide/
Pingback: ESP32 BLE on Arduino IDE with UART Test - IoT Hardware
Pingback: DHT11 vs DHT22: Overview - IoTbyHVM - Explore TechBytes
Pingback: DHT11 sensor with ESP8266/NodeMCU using Arduino IDE - How To
Hi Harsha,
I ahve been following work and it is really impressive contribution towards ESP32.
I think the some part of the code got truncated in the published code. The setup part of the code is missing
With Regards,
Abbas
Pingback: Android Things - OS for IoT and embedded devices - IoTbyHVM - Bits & Bytes of IoT