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
- 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
- Connect the DHT11 sensor to the ESP32:
- Install Required Libraries
- Open Arduino IDE.
- Go to Sketch > Include Library > Manage Libraries….
- Search for and install the following libraries:
- DHT sensor library by Adafruit
- ESP32 BLE Arduino library
- Write the Code
- Open a new sketch in Arduino IDE.
- 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>
- Define the DHT11 sensor and BLE characteristics:
#define DHTPIN 23 #define DHTTYPE DHT11 DHT dht(DHTPIN, DHTTYPE); BLECharacteristic *pCharacteristic; bool deviceConnected = false;
- 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(); }
- Implement Data Transmission
- 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); } }
- In the loop() function, read from the DHT11 sensor and update the BLE characteristic:
- Upload the Code
- Connect your ESP32 to your computer via USB.
- Select the appropriate board and port in Arduino IDE: Tools > Board > ESP32 Dev Module.
- 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!