How to Set Up ESP32 BLE GATT Server for DHT22 Sensor Data Transmission

Introduction

In this tutorial, we will learn how to implement an ESP32 BLE GATT Server to transmit temperature and humidity readings from a DHT22 sensor. This project allows your ESP32 to act as a Bluetooth Low Energy (BLE) server, which can be accessed by BLE-enabled devices to retrieve sensor data.

Prerequisites

  • Basic knowledge of Arduino IDE and programming
  • ESP32 development board
  • DHT22 temperature and humidity sensor
  • Jumper wires
  • Arduino IDE installed on your computer
  • ESP32 board package added to the Arduino IDE

Parts/Tools

  • ESP32 Development Board
  • DHT22 Sensor
  • Breadboard (optional)
  • Jumper wires

Steps

  1. Set up the hardware
    1. Connect the DHT22 sensor to the ESP32:
    2. 
                  DHT22 Pin 1 (VCC)   -> ESP32 3.3V
                  DHT22 Pin 2 (Data)  -> ESP32 GPIO 23
                  DHT22 Pin 4 (GND)   -> ESP32 GND
                  
  2. Install necessary libraries
    1. Open Arduino IDE.
    2. Go to Sketch > Include Library > Manage Libraries.
    3. Search for and install the following libraries:
      • DHT sensor library by Adafruit
      • ESP32 BLE library
  3. Write the code
    1. Open a new sketch in Arduino IDE.
    2. Copy and paste the following code:
    3. 
                  #include <DHT.h>
                  #include <BLEDevice.h>
                  #include <BLEServer.h>
      
                  #define DHTPIN 23
                  #define DHTTYPE DHT22
      
                  DHT dht(DHTPIN, DHTTYPE);
      
                  // Create BLE Server
                  BLEServer *pServer = NULL;
                  BLECharacteristic *pCharacteristic = NULL;
      
                  // Define service and characteristic UUIDs
                  #define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
                  #define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-3d7b7a3a3e78"
      
                  void setup() {
                      Serial.begin(115200);
                      dht.begin();
      
                      // Create BLE Device
                      BLEDevice::init("ESP32_DHT22");
      
                      // Create BLE Server
                      pServer = BLEDevice::createServer();
                      pCharacteristic = pServer->createCharacteristic(
                          CHARACTERISTIC_UUID,
                          BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_NOTIFY
                      );
      
                      pServer->getAdvertising()->start();
                      Serial.println("Waiting for a client connection...");
                  }
      
                  void loop() {
                      float h = dht.readHumidity();
                      float t = dht.readTemperature();
      
                      if (isnan(h) || isnan(t)) {
                          Serial.println("Failed to read from DHT sensor!");
                          return;
                      }
      
                      String data = "Temperature: " + String(t) + " °C, Humidity: " + String(h) + " %";
                      pCharacteristic->setValue(data.c_str());
                      pCharacteristic->notify();
                      delay(2000); // Update every 2 seconds
                  }
                  
  4. Upload the code
    1. Select the correct board and port from Tools.
    2. Click on the Upload button.
    3. Wait for the code to compile and upload to the ESP32.
  5. Connect to the ESP32 GATT Server
    1. Use a BLE scanner app (e.g., nRF Connect) on your smartphone.
    2. Scan for devices and connect to “ESP32_DHT22”.
    3. Locate the service with UUID “4fafc201-1fb5-459e-8fcc-c5c9c331914b” and the characteristic UUID “beb5483e-36e1-4688-b7f5-3d7b7a3a3e78”.
    4. Read the characteristic value to see the temperature and humidity data.

Troubleshooting

  • If the DHT sensor readings are incorrect or fail to read, check the connections and ensure the sensor is functional.
  • If the BLE connection is not established, ensure that Bluetooth is enabled on your device and that you are within range of the ESP32.
  • Check for any compilation errors in the Arduino IDE and ensure that the correct board and libraries are selected.

Conclusion

Congratulations! You have successfully implemented an ESP32 BLE GATT server that transmits temperature and humidity readings from a DHT22 sensor. You can now access this data remotely using BLE-enabled devices. This project can be extended further by integrating additional sensors or implementing more complex data processing.

Leave a Comment

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