ESPHow ToIoT HardwaresSensorsTutorials/DIY

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 PinESP32 Pin
VCC (Power)3.3V or 5V
DataGPIO 4 (or any digital pin)
GNDGND

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:

  1. ESP32 Board Package: Go to Preferences and add the ESP32 board manager URL: https://dl.espressif.com/dl/package_esp32_index.json
  2. Install DHT sensor library: Adafruit DHT.
  3. Install Adafruit Unified Sensor library.
  4. 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.

C
#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

  1. Initialize BLE: The ESP32 sets up a BLE server named ESP32_DHT11.
  2. Define Characteristics:
    • One for temperature (TEMP_CHARACTERISTIC_UUID).
    • One for humidity (HUM_CHARACTERISTIC_UUID).
  3. Read Data from DHT11: Temperature and humidity readings are fetched every 2 seconds.
  4. 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:

  1. Download nRF Connect or BLE Scanner (available on Android/iOS).
  2. Open the app and scan for ESP32_DHT11.
  3. Connect to the ESP32 and subscribe to the temperature and humidity characteristics.
  4. 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/

Harshvardhan Mishra

Hi, I'm Harshvardhan Mishra. Tech enthusiast and IT professional with a B.Tech in IT, PG Diploma in IoT from CDAC, and 6 years of industry experience. Founder of HVM Smart Solutions, blending technology for real-world solutions. As a passionate technical author, I simplify complex concepts for diverse audiences. Let's connect and explore the tech world together! If you want to help support me on my journey, consider sharing my articles, or Buy me a Coffee! Thank you for reading my blog! Happy learning! Linkedin

5 thoughts on “ESP32 BLE with DHT11

Leave a Reply

Your email address will not be published. Required fields are marked *