How to Set Up a BLE GATT Server on ESP32 for DHT11 Data Transmission

Implementing BLE GATT Server on ESP32 to Transmit DHT11 Temperature and Humidity Data

In this tutorial, we will walk through the process of implementing a Bluetooth Low Energy (BLE) GATT server on an ESP32 microcontroller to transmit temperature and humidity data collected from a DHT11 sensor. This project is ideal for developers looking to learn about BLE communication and sensor integration.

Prerequisites

  • Basic knowledge of Arduino programming
  • ESP32 development board
  • DHT11 temperature and humidity sensor
  • Arduino IDE installed on your computer
  • ESP32 board package installed in Arduino IDE

Parts/Tools

  • ESP32 development board
  • DHT11 sensor
  • Breadboard and jumper wires
  • USB cable for programming the ESP32

Steps

  1. Set Up the Hardware
    • Connect the DHT11 sensor to the ESP32:
      
                      DHT11 VCC   -> ESP32 3.3V
                      DHT11 GND   -> ESP32 GND
                      DHT11 DATA  -> ESP32 GPIO 23
                      
  2. Install Required 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 Arduino library
  3. Write the Code
    1. Open a new sketch in Arduino IDE.
    2. Include the necessary libraries at the top of your code:
      
                      #include <DHT.h>
                      #include <BLEDevice.h>
                      #include <BLEServer.h>
                      #include <BLEUtils.h>
                      #include <BLE2902.h>
                      
    3. Define the DHT11 sensor and BLE characteristics:
      
                      #define DHTPIN 23
                      #define DHTTYPE DHT11
                      DHT dht(DHTPIN, DHTTYPE);
                      
                      BLECharacteristic *pCharacteristic;
                      bool deviceConnected = false;
                      
    4. Set up the BLE server in the setup() function:
      
                      void setup() {
                          Serial.begin(115200);
                          dht.begin();
                          
                          BLEDevice::init("ESP32_DHT11");
                          BLEServer *pServer = BLEDevice::createServer();
                          pServer->setCallbacks(new MyServerCallbacks());
                          BLEService *pService = pServer->createService(SERVICE_UUID);
                          
                          pCharacteristic = pService->createCharacteristic(
                              CHARACTERISTIC_UUID,
                              BLECharacteristic::PROPERTY_NOTIFY
                          );
                          
                          pService->start();
                          pServer->getAdvertising()->start();
                      }
                      
  4. Implement Data Transmission
    1. In the loop() function, read from the DHT11 sensor and update the BLE characteristic:
      
                      void loop() {
                          if (deviceConnected) {
                              float h = dht.readHumidity();
                              float t = dht.readTemperature();
                              String data = "Temperature: " + String(t) + "°C, Humidity: " + String(h) + "%";
                              pCharacteristic->setValue(data.c_str());
                              pCharacteristic->notify();
                              delay(2000);
                          }
                      }
                      
  5. Upload the Code
    1. Connect your ESP32 to your computer via USB.
    2. Select the appropriate board and port in Arduino IDE: Tools > Board > ESP32 Dev Module.
    3. Click on the upload button to compile and upload your code.

Troubleshooting

  • ESP32 Not Connecting to BLE: Ensure that your ESP32 is powered and the code is uploaded correctly. Check for any serial output for error messages.
  • DHT11 Not Giving Readings: Double-check the wiring connections. Ensure that the DHT11 library is correctly installed.
  • No Notifications Received: Make sure your BLE client is properly connected to your ESP32 and is set to receive notifications.

Conclusion

Congratulations! You have successfully implemented a BLE GATT server on an ESP32 to transmit temperature and humidity data from a DHT11 sensor. You can now extend this project by adding more sensors or creating a mobile application to visualize the data. Happy coding!

Leave a Comment

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