Tuesday, September 26, 2023
ESPHow ToIoT HardwaresSensorsTutorials/DIY

ESP32 BLE with DHT11

In this tutorial i am telling to you How To use ESP32 BLE with DHT11. You need to have the ESP32 add-on installed on the Arduino IDE. Follow one of the next tutorials to prepare your Arduino IDE to work with the ESP32, if you haven’t already.

Visit these Articles http://iotbyhvm.ooo/arduino-esp32-support-on-windows-and-ubuntu/  and http://iotbyhvm.ooo/esp32-bluetooth-low-energy-ble-on-arduino-ide/

The program basically sets the UUID of the UART communication service, it reads the humidity and temperature of the DHT sensor, and transmits this data to the application on the mobile phone. Data is sent in a single variable, but in a CSV format, with temperature and humidity separated by a comma.

ESP32 BLE with DHT11

#include <BLEDevice.h>

#include <BLEServer.h>

#include <BLEUtils.h>

#include <BLE2902.h>

#include <DHT.h>

#include <iostream>

#include <string>

 BLECharacteristic * pCharacteristic;

 bool deviceConnected = false;
 const int LED = 25;  // Could be different depending on the dev board.  I used the DO32 ESP32 dev board.

  * Definition of DHT11
 #define DHTPIN 23 // DHT11 data pin
 #define DHTTYPE DHT11 // sets the sensor type, in case DHT11


 int humidity;
 int temperature;

 // See the following link if you want to generate your own UUIDs:
 // https://www.uuidgenerator.net/

 #define SERVICE_UUID "6E400001-B5A3-F393-E0A9-E50E24DCCA9E" // UART service UUID
 #define CHARACTERISTIC_UUID_RX "6E400002-B5A3-F393-E0A9-E50E24DCCA9E"
 #define DHTDATA_CHAR_UUID "6E400003-B5A3-F393-E0A9-E50E24DCCA9E"

 class MyServerCallbacks: public BLEServerCallbacks {
     void onConnect (BLEServer * pServer) {
       deviceConnected = true;

     void onDisconnect (BLEServer * pServer) {
       deviceConnected = false;

 class MyCallbacks: public BLECharacteristicCallbacks {
     void onWrite (BLECharacteristic * pCharacteristic) {
       std :: string rxValue = pCharacteristic-> getValue ();
       Serial.println (rxValue [0]);

       if (rxValue.length ()> 0) {
         Serial.println ("*********");
         Serial.print ("Received Value:");

         for (int i = 0; i  setCallbacks (new MyServerCallbacks ());

   // Create the UART service
   BLEService * pService = pServer-> createService (SERVICE_UUID);

   // Create a BLE Feature to send the data
   pCharacteristic = pService-> createCharacteristic (
                       BLECharacteristic :: PROPERTY_NOTIFY
   pCharacteristic-> addDescriptor (new BLE2902 ());

   // creates a BLE characteristic to receive the data
   BLECharacteristic * pCharacteristic = pService-> createCharacteristic (
                                          BLECharacteristic :: PROPERTY_WRITE

   pCharacteristic-> setCallbacks (new MyCallbacks ());

   // Start the service
   pService-> start ();

   // Starts the discovery of ESP32
   pServer-> getAdvertising () -> start ();
   Serial.println ("Waiting for a client to connect ...");

 void loop () {
   if (deviceConnected) {

     humidity = dht.readHumidity ();
     temperature = dht.readTemperature ();
     // test if return is valid, otherwise something is wrong.
     if (isnan (temperature) || isnan (humidity))
       Serial.println ("Failed to read from DHT");
       Serial.print ("Humidity:");
       Serial.print (humidity);
       Serial.print ("% \ t");
       Serial.print ("Temperature:");
       Serial.print (temperature);
       Serial.println ("* C");
     char humidityString [2];
     char temperatureString [2];
     dtostrf (humidity, 1, 2, humidityString);
     dtostrf (temperature, 1, 2, temperatureString);

     char dhtDataString [16];
     sprintf (dhtDataString, "% d,% d", temperature, humidity);
     pCharacteristic-> setValue (dhtDataString);
     pCharacteristic-> notify ();  // Send the value to the application!
     Serial.print ("*** Sent Data:");
     Serial.print (dhtDataString);
     Serial.println ("***");
   delay (1000);

Pin Diagram

For LED –

+ =====> PIN 25

– =====> GND

For DHT 11 Sensor-

VCC       =====> 3.3 V

DATA     =====> PIN 23

GND      =====> GND

Now Open BLE Scanner Android App and connect with “ESP32 DHT 11” and click on UUID “6E400003-B5A3-F393-E0A9-E50E24DCCA9E for Notifictaion.

Use UUID_RX “6E400002-B5A3-F393-E0A9-E50E24DCCA9E for write data. send A for turn on led andsend B for turn off led.

OR you can use nRF Connect for Mobile App.

You may like also:


Harshvardhan Mishra

Hi, I'm Harshvardhan Mishra. I am a tech blogger and an IoT Enthusiast. I am eager to learn and explore tech related stuff! also, I wanted to deliver you the same as much as the simpler way with more informative content. I generally appreciate learning by doing, rather than only learning. 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!

5 thoughts on “ESP32 BLE with DHT11

Leave a Reply

%d bloggers like this: