Implementing ESP32 BLE GATT Server to Transmit DHT22 Temperature and Humidity Data Every 5 Seconds
This tutorial will guide you through the process of implementing a Bluetooth Low Energy (BLE) GATT server on an ESP32 device to transmit temperature and humidity data from a DHT22 sensor every 5 seconds. By the end of this tutorial, you will have a working BLE server that can be accessed by BLE clients, such as smartphones or computers.
Prerequisites
- Basic understanding of Arduino programming
- ESP32 development board
- DHT22 temperature and humidity sensor
- Arduino IDE installed on your computer
- ESP32 board library installed in Arduino IDE
- BLE scanner app on your smartphone (optional, for testing)
Parts/Tools
- ESP32 Development Board
- DHT22 Sensor
- Breadboard and jumper wires
- Arduino IDE
Steps
- Wiring the DHT22 Sensor
- Connect the VCC pin of the DHT22 to the 3.3V pin on the ESP32.
- Connect the GND pin of the DHT22 to a GND pin on the ESP32.
- Connect the DATA pin of the DHT22 to GPIO pin 23 on the ESP32.
- Setting Up Arduino IDE
- Open Arduino IDE.
- Go to File > Preferences and add the following URL to Additional Board Manager URLs:
https://dl.espressif.com/dl/package_esp32_index.json
. - Go to Tools > Board > Board Manager, search for “ESP32”, and install it.
- Install Required Libraries
- Go to Sketch > Include Library > Manage Libraries.
- Search for “DHT sensor library” by Adafruit and install it.
- Search for “Adafruit Unified Sensor” and install it as well.
- Create the Arduino Sketch
#include <BLEDevice.h> #include <BLEServer.h> #include <DHT.h> #define DHTPIN 23 #define DHTTYPE DHT22 #define SERVICE_UUID "12345678-1234-5678-1234-56789abcdef0" #define CHARACTERISTIC_UUID "12345678-1234-5678-1234-56789abcdef1" DHT dht(DHTPIN, DHTTYPE); BLEServer* pServer = NULL; BLECharacteristic* pCharacteristic = NULL; void setup() { Serial.begin(115200); dht.begin(); BLEDevice::init("ESP32_DHT22"); pServer = BLEDevice::createServer(); pCharacteristic = pServer->createCharacteristic( CHARACTERISTIC_UUID, BLECharacteristic::PROPERTY_NOTIFY ); pServer->getAdvertising()->start(); } void loop() { float h = dht.readHumidity(); float t = dht.readTemperature(); String data = "Humidity: " + String(h) + " % , Temperature: " + String(t) + " °C"; pCharacteristic->setValue(data.c_str()); pCharacteristic->notify(); delay(5000); }
- Upload the Sketch
- Select your ESP32 board from Tools > Board.
- Select the correct COM port from Tools > Port.
- Click the upload button in the Arduino IDE.
- Testing the BLE GATT Server
- Open a BLE scanner app on your smartphone.
- Scan for devices and look for “ESP32_DHT22”.
- Connect to the device and subscribe to notifications for the characteristic.
- You should start receiving temperature and humidity data every 5 seconds.
Troubleshooting
- Device Not Found: Ensure that your ESP32 is powered and running the correct code. Check the wiring connections of the DHT22 sensor.
- No Data Received: Check if the BLE characteristic UUIDs are correctly specified. Make sure you are subscribed to notifications in your BLE app.
- Inaccurate Readings: Verify that the DHT22 sensor is functioning properly and is not exposed to direct sunlight or drafts.
Conclusion
In this tutorial, you successfully implemented a BLE GATT server on an ESP32 to transmit temperature and humidity data from a DHT22 sensor. You can now access this data from any BLE-capable device, allowing for easy monitoring of environmental conditions. Feel free to expand on this project by adding more sensors or implementing a data logging feature.