ESP32 BLE Temperature and Humidity Monitoring System using DHT11 Sensor
Introduction:
This is my third ESP32 BLE project. In my first project, I controlled LEDs using the built-in BLE of ESP32. In the second project, I used a new technology involving RSSI (Received Signal Strength Indicator). With the help of RSSI, I implemented a control mechanism where the LED turns on whenever the earbuds come within the RSSI range of the ESP32 BLE. However, in this project, I am going to build a temperature and humidity monitoring system that will be based on the ESP32 BLE and DHT11 Sensor. For this, I have designed my own custom Android application.
The advent of the Internet of Things (IoT) has revolutionized the way we interact with our environment, bringing about a myriad of smart solutions that enhance efficiency and accessibility. In this context, the ESP32 BLE Temperature and Humidity Monitoring System using DHT11 stands out as a pioneering innovation, especially in the realm of environmental monitoring. This article delves into the intricate details of this system, a cutting-edge technology that harmoniously integrates the ESP32 microcontroller with the DHT11 sensor and Bluetooth Low Energy (BLE) to offer real-time data monitoring through an Android application.
At the core of this system is the ESP32, a versatile and powerful microcontroller with built-in Wi-Fi and Bluetooth capabilities, making it an ideal choice for IoT projects. When combined with the DHT11 sensor, known for its proficiency in measuring temperature and humidity, the ESP32 transforms into a robust tool for environmental data collection. The ingenuity of this system lies in its ability to transmit this data seamlessly via BLE, a technology favored for its low power consumption and high transmission efficiency, making it perfectly suited for IoT applications.
The integration of BLE with the ESP32 and DHT11 paves the way for real-time monitoring of environmental parameters through a custom-designed Android application. This application serves as the user interface, allowing users to effortlessly access, view, and analyze the collected data on their smartphones. The real-time aspect is particularly crucial, offering immediate insights and enabling prompt decision-making based on the current environmental conditions.
Furthermore, this system exemplifies a smart solution in the era of IoT, where the interconnectivity of devices and ease of access to information are paramount. Its implementation in various settings, from homes and offices to industrial environments, underscores its versatility and adaptability. The ESP32 BLE DHT11 temperature and humidity Monitoring System not only enhances environmental awareness but also contributes to energy efficiency and sustainability by enabling users to monitor and adjust their surroundings based on real-time data.
This article aims to provide a comprehensive overview of the system’s architecture, its components, and the step-by-step process of integrating the ESP32 with the DHT11 sensor and BLE for data transmission. It will also delve into the development of the Android application, highlighting the key features and functionalities that make this system a smart, user-friendly solution for real-time data monitoring. Whether you are a hobbyist, a professional in the field of IoT, or simply someone interested in smart technology solutions, this exploration into the ESP32 BLE DHT11 temperature Monitoring System promises to be an enlightening journey into the future of environmental monitoring.
Components:
*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!
What is ESP32 BLE DHT11 Monitoring System?
The ESP32 BLE Temperature and humidity monitoring system is a wireless monitoring solution that uses the ESP32 microcontroller and the DHT11 sensor to collect and transmit data. The ESP32 microcontroller is a powerful and versatile device that combines Wi-Fi and Bluetooth capabilities, making it ideal for IoT applications.
The DHT11 sensor, on the other hand, is a digital temperature and humidity sensor that provides accurate and reliable readings. When combined, these two components create a robust monitoring system that can be used in various settings.
How Does it Work?
The ESP32 BLE DHT11 temperature monitoring system works by collecting data from the DHT11 sensor and transmitting it wirelessly using Bluetooth Low Energy (BLE) technology. The ESP32 microcontroller acts as the central hub, collecting data from the sensor and transmitting it to a connected device, such as a smartphone or a computer.
Once the data is transmitted, it can be displayed in a user-friendly interface, allowing users to monitor temperature and humidity levels in real-time. This enables users to make informed decisions based on the data collected, whether it’s adjusting the thermostat in their homes or optimizing the conditions in an industrial setting.
Benefits of ESP32 BLE Temperature Monitoring System
There are several benefits to using the ESP32 BLE DHT11 temperature monitoring system:
Real-time data:Â With this system, you can access real-time data, allowing you to monitor temperature and humidity levels as they change.
Wireless connectivity:Â The use of BLE technology enables wireless connectivity, eliminating the need for cumbersome wires and cables.
Easy installation: The ESP32 BLE DHT11 temperature and humidity monitoring system is easy to install and can be set up in a matter of minutes.
Cost-effective:Â Compared to other monitoring systems, the ESP32 BLE DHT11 monitoring system is cost-effective, making it an affordable solution for both residential and industrial use.
User-friendly interface:Â The data collected by the system can be displayed in a user-friendly interface, making it easy to interpret and analyze.
Applications of ESP32 BLE DHT11 Monitoring System
The ESP32 BLE DHT11 temperature and humidity monitoring system has a wide range of applications:
Home automation:Â With this system, homeowners can monitor and control the temperature and humidity levels in their homes, ensuring optimal comfort.
Industrial settings:Â The system can be used in industrial settings to monitor temperature and humidity levels, helping to maintain optimal conditions for production processes.
Agriculture:Â Farmers can use the system to monitor temperature and humidity levels in greenhouses, ensuring optimal conditions for plant growth.
Data centers:Â The system can be used in data centers to monitor temperature and humidity levels, helping to prevent equipment failures and downtime.
Specifications of the DHT11
The DHT11 is a popular and cost-effective temperature and humidity sensor that is widely used in various electronic projects..
The DHT11 sensor is a digital sensor that can measure temperature and relative humidity. Here are some key specifications of the DHT11:
- Temperature Range: 0°C to 50°C
- Humidity Range: 20% to 90%
- Temperature Accuracy: ±2°C
- Humidity Accuracy: ±5%
- Operating Voltage: 3.3V to 5V
- Current Consumption: 2.5mA max
- Interface: Digital
How the DHT11 Works
The DHT11 sensor uses a capacitive humidity sensor and a thermistor to measure the temperature and humidity in the surrounding environment. It has a built-in microcontroller that converts the analog readings from the sensors into digital signals, making it easy to interface with microcontrollers or other digital devices.
The DHT11 temperature and humidity communicate with the external device using a single-wire, bidirectional serial protocol. The communication protocol is relatively simple, making it suitable for beginners and hobbyists.
When the DHT11 receives a request for data from the microcontroller, it starts the data acquisition process. It measures the temperature first and then the humidity. The sensor takes approximately 2 seconds to complete one measurement cycle.
The data from the DHT11 is transmitted in a 40-bit data stream. The data stream consists of the integral and decimal parts of the temperature and humidity readings, as well as a checksum to ensure data integrity.
Applications of the DHT11
The DHT11 sensor finds applications in various fields, including:
- Home automation systems
- Weather stations
- Greenhouses
- Industrial automation
- HVAC systems
Its low cost, ease of use, and reliable performance make it a popular choice among hobbyists, students, and professionals alike.
About BLE Android Application:
This custom-made Android Studio application is designed to work seamlessly with the ESP32 BLE Temperature Monitoring System. It provides a user-friendly interface that allows users to easily connect their Android devices to the ESP32 device and view real-time temperature and humidity data.
With this application, users can monitor temperature and humidity levels in their homes, offices, or any other location where the ESP32 device is installed. The application provides accurate and reliable data, allowing users to make informed decisions based on the current environmental conditions.
The ESP32 BLE DHT11 temperature and humidity Monitoring System is a great tool for those who want to ensure optimal comfort and safety in their surroundings. Whether you want to monitor the temperature and humidity levels in your baby’s room, greenhouse, or server room, this application has got you covered.
With its intuitive design and easy-to-use features, this custom Android Studio application makes it convenient for users to access and monitor their temperature and humidity. It provides a seamless experience, allowing users to stay connected and informed about their environment at all times.
ESP32 BLE Temperature Monitoring System Circuit Diagram:
the DHT11 sensor is interfaced with an ESP32 microcontroller to monitor environmental conditions like temperature and humidity via ESP32 Built-in BLE. The DHT11’s signal pin is connected to GPIO 32 on the ESP32, which allows the microcontroller to receive the temperature and humidity data from the sensor. The ground pin of the DHT11 is tied to one of the ground pins on the ESP32, ensuring a common reference point for the electrical current and a complete circuit. The VCC pin of the DHT11 is connected to the 3.3V output on the ESP32, which powers the sensor.
With this setup, the ESP32 is programmed to read the environmental data captured by the DHT11 sensor. The ESP32 is a powerful tool in this context due to its built-in Wi-Fi and Bluetooth capabilities, the latter of which is utilized here to implement the BLE (Bluetooth Low Energy) communication protocol. Through BLE, the ESP32 can transmit the collected data to a custom Android application, enabling real-time monitoring. The Android application acts as a user interface where the data can be observed, logged, or further analyzed. This system is particularly beneficial for applications where remote or wireless data collection from environmental sensors is necessary, such as in smart home systems, agricultural monitoring, or any IoT-based project that requires real-time environmental monitoring.
DHT11 Library Installation:
Start the Arduino Integrated Development Environment (IDE) on your computer. This is the software used for writing and uploading programs.
Once the IDE is open, you can access the Library Manager. This is done by going to the menu bar at the top of the IDE, selecting Sketch, then choosing Include Library, and finally clicking on Manage Libraries.
In the Library Manager, use the search bar to look for the Adafruit DHT library by typing keywords like “DHT11 or “Adafruit DHT.” Once you find it, select it and click the Install button. This will install the Adafruit DHT library to your Arduino IDE, allowing you to use the library’s functions in your sketches. But In my case, I already installed it, that why the status is showing “Installed” next to the library.
By following these steps, you’ll successfully install the Adafruit DHT11 library in the Arduino IDE, ready for use in your projects.
ESP32 BLE Temperature Monitoring System Program
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
#include <BLEDevice.h> #include <BLEServer.h> #include <BLEUtils.h> #include <BLE2902.h> #include <DHT.h> // DHT Sensor #define DHTPIN 32 // Pin which is connected to the DHT sensor. #define DHTTYPE DHT11 // DHT 11 DHT dht(DHTPIN, DHTTYPE); BLEServer* pServer; BLECharacteristic* pCharacteristic; bool deviceConnected = false; bool oldDeviceConnected = false; class MyServerCallbacks: public BLEServerCallbacks { void onConnect(BLEServer* pServer) { deviceConnected = true; BLEDevice::startAdvertising(); }; void onDisconnect(BLEServer* pServer) { deviceConnected = false; } }; void setup() { Serial.begin(115200); BLEDevice::init("SensorMonitor"); pServer = BLEDevice::createServer(); pServer->setCallbacks(new MyServerCallbacks()); dht.begin(); BLEService *pService = pServer->createService("0000180d-0000-1000-8000-00805f9b34fb"); pCharacteristic = pService->createCharacteristic( "00002a37-0000-1000-8000-00805f9b34fb", BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_NOTIFY ); pCharacteristic->addDescriptor(new BLE2902()); pService->start(); BLEAdvertising *pAdvertising = pServer->getAdvertising(); pAdvertising->start(); } void loop() { if (deviceConnected) { float h = dht.readHumidity(); float t = dht.readTemperature(); // Check if any reads failed and exit early (to try again). if (isnan(h) || isnan(t)) { Serial.println("Failed to read from DHT sensor!"); return; } // Create a comma-separated string of temperature and humidity char sensorData[40]; snprintf(sensorData, sizeof(sensorData), "%f,%f", t, h); pCharacteristic->setValue(sensorData); pCharacteristic->notify(); Serial.println("Sensor Data:"); Serial.println(sensorData); delay(2000); // Adjust the update interval as needed } // disconnecting if (!deviceConnected && oldDeviceConnected) { delay(500); // give the bluetooth stack the chance to get things ready pServer->startAdvertising(); // restart advertising Serial.println("start advertising"); oldDeviceConnected = deviceConnected; } // connecting if (deviceConnected && !oldDeviceConnected) { // do stuff here on connecting oldDeviceConnected = deviceConnected; } } |
Code explanation:
1 2 3 4 5 |
#include <BLEDevice.h> #include <BLEServer.h> #include <BLEUtils.h> #include <BLE2902.h> #include <DHT.h> |
#include <BLEDevice.h>: This line includes the BLEDevice library. This library is essential for programming BLE (Bluetooth Low Energy) functionalities. It provides the basic framework and functions required to create and manage a BLE device using your microcontroller.
#include <BLEServer.h>: By including the BLEServer library, your program gains the ability to create a BLE server. This is crucial if your project involves setting up the microcontroller as a server in the BLE communication protocol, allowing it to manage connections, send, and receive data to and from BLE clients.
#include <BLEUtils.h>: The BLEUtils library is included for utility functions related to BLE operations. While not always directly used in basic BLE applications, it offers additional functionalities and debugging tools that can be helpful in more advanced BLE projects.
#include <BLE2902.h>: This line includes the BLE2902 library. It’s typically used for BLE notifications and indications. Specifically, this library is often associated with setting up and managing descriptors for BLE characteristics, which are used to define how data is transmitted over BLE.
#include <DHT.h>: The DHT library is included to interface with DHT-series temperature and humidity sensors. This library provides a set of functions and methods to easily read temperature and humidity data from DHT sensors, such as DHT11 or DHT22.
1 2 3 4 5 6 7 |
#define DHTPIN 32 // Pin which is connected to the DHT sensor. #define DHTTYPE DHT11 // DHT 11 DHT dht(DHTPIN, DHTTYPE); BLEServer* pServer; BLECharacteristic* pCharacteristic; bool deviceConnected = false; bool oldDeviceConnected = false; |
DHTPIN and DHTTYPE. DHTPIN is set to 32, indicating that the DHT sensor is connected to digital pin 32 of the microcontroller. DHTTYPE is defined as DHT11, specifying the model of the DHT sensor being used. The DHT class instance named dht is then created using these constants, which facilitates interaction with the DHT sensor to read temperature and humidity data.
Furthermore, the snippet includes the declaration of a pointer to a BLEServer object, named pServer. This pointer will be used to reference and manage the BLE server, which is a core part of setting up the device for Bluetooth communication. Similarly, a pointer to a BLECharacteristic object, pCharacteristic, is declared. This characteristic is crucial for BLE communication as it represents the data that the server will transmit or receive.
The code also initializes two boolean variables, deviceConnected and oldDeviceConnected, both set to false. These are used to track the connection status of the BLE server with client devices. deviceConnected indicates the current connection status, changing to true when a client is connected. oldDeviceConnected serves as a memory of the previous connection state, useful for detecting changes in connection status, such as when a device disconnects or a new one connects. Overall, this snippet sets the stage for a program where sensor data is read and transmitted wirelessly via BLE.
1 2 3 4 5 6 7 8 9 10 |
class MyServerCallbacks: public BLEServerCallbacks { void onConnect(BLEServer* pServer) { deviceConnected = true; BLEDevice::startAdvertising(); }; void onDisconnect(BLEServer* pServer) { deviceConnected = false; } }; |
The code snippet defines a custom class named MyServerCallbacks which inherits from the BLEServerCallbacks class. This inheritance allows MyServerCallbacks to override certain callback functions that are triggered by events in the BLE (Bluetooth Low Energy) server lifecycle. Specifically, the class overrides two functions: onConnect and onDisconnect, which are called when a BLE client connects to or disconnects from the server, respectively.
In the onConnect function, when a client device connects to the server, it sets the global variable deviceConnected to true. This variable is likely used elsewhere in the program to track the connection status of the BLE server. After setting this variable, the function calls BLEDevice::startAdvertising(). This command ensures that the BLE server continues to advertise its presence so that other clients can find and connect to it, even after an initial connection has been established.
The onDisconnect function is simpler. It is called when a client device disconnects from the BLE server. In response, it sets the deviceConnected variable to false, updating the connection status.
Overall, this custom class is a critical part of the BLE communication setup in the program. It allows the program to respond appropriately to connection and disconnection events, maintaining an accurate record of the server’s connection status and ensuring continuous availability for client devices to connect.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
void setup() { Serial.begin(115200); BLEDevice::init("SensorMonitor"); pServer = BLEDevice::createServer(); pServer->setCallbacks(new MyServerCallbacks()); dht.begin(); BLEService *pService = pServer->createService("0000180d-0000-1000-8000-00805f9b34fb"); pCharacteristic = pService->createCharacteristic( "00002a37-0000-1000-8000-00805f9b34fb", BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_NOTIFY ); pCharacteristic->addDescriptor(new BLE2902()); pService->start(); BLEAdvertising *pAdvertising = pServer->getAdvertising(); pAdvertising->start(); } |
The function starts by initializing serial communication at a baud rate of 115200 using Serial.begin(115200). This is typically used for debugging purposes, allowing data to be sent to and from the microcontroller over the USB connection.
Next, BLEDevice::init(“SensorMonitor”) initializes the BLE device with the name “SensorMonitor”. This name is what will be displayed when other BLE devices scan for available connections.
The program then creates a BLE server with pServer = BLEDevice::createServer(). This server is the central point for BLE communications. The server’s callbacks are set to an instance of MyServerCallbacks class with pServer->setCallbacks(new MyServerCallbacks()). This allows the program to handle events like BLE client connections and disconnections.
The DHT sensor is initialized with dht.begin(). This prepares the sensor for reading temperature and humidity data.
A BLE service is created with a specific UUID (“0000180d-0000-1000-8000-00805f9b34fb”) using pServer->createService(). A BLE characteristic is then created within this service using pService->createCharacteristic(). The characteristic, identified by its own UUID (“00002a37-0000-1000-8000-00805f9b34fb”), is configured to allow reading and sending notifications to connected clients. This characteristic is where sensor data will be sent.
The line pCharacteristic->addDescriptor(new BLE2902()) adds a descriptor to the characteristic, enabling notifications/indications.
Finally, the service is started with pService->start(), and BLE advertising is initiated with pAdvertising->start(), making the device discoverable to others. This setup is crucial for establishing the BLE communication framework necessary for the device to send sensor data to connected clients wirelessly.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
void loop() { if (deviceConnected) { float h = dht.readHumidity(); float t = dht.readTemperature(); // Check if any reads failed and exit early (to try again). if (isnan(h) || isnan(t)) { Serial.println("Failed to read from DHT sensor!"); return; } // Create a comma-separated string of temperature and humidity char sensorData[40]; snprintf(sensorData, sizeof(sensorData), "%f,%f", t, h); pCharacteristic->setValue(sensorData); pCharacteristic->notify(); Serial.println("Sensor Data:"); Serial.println(sensorData); delay(2000); // Adjust the update interval as needed } // disconnecting if (!deviceConnected && oldDeviceConnected) { delay(500); // give the bluetooth stack the chance to get things ready pServer->startAdvertising(); // restart advertising Serial.println("start advertising"); oldDeviceConnected = deviceConnected; } // connecting if (deviceConnected && !oldDeviceConnected) { // do stuff here on connecting oldDeviceConnected = deviceConnected; } } |
When a BLE device is connected (checked by deviceConnected), the function reads humidity (h) and temperature (t) data from a DHT sensor. It then checks if these readings are valid using isnan(h) or isnan(t). If any of these readings fail (return NaN), it prints an error message to the serial monitor and exits the current loop iteration early, skipping the rest of the code inside the if block.
If the readings are valid, the function formats this data into a comma-separated string (sensorData) representing temperature and humidity. This string is then set as the value of a BLE characteristic (pCharacteristic->setValue(sensorData)) and a notification is sent to any connected BLE client (pCharacteristic->notify()). The sensor data is also printed to the serial monitor for debugging purposes. A delay of 2000 milliseconds (2 seconds) is introduced at the end of this block, which controls the frequency of sensor data updates.
The function also handles the BLE connection status. If the device was previously connected (oldDeviceConnected) but is now disconnected (!deviceConnected), it waits for 500 milliseconds and restarts BLE advertising (pServer->startAdvertising()). This ensures that the device becomes discoverable again for other BLE clients. A message is printed to the serial monitor indicating that advertising has restarted.
Lastly, it updates the oldDeviceConnected status to match the current deviceConnected status. This is crucial for tracking changes in connection status in subsequent iterations of the loop.
In summary, the loop() function continuously monitors and processes sensor data, manages BLE connections, and handles data transmission to connected clients, ensuring the system responds dynamically to the connection status and sensor readings.
Practical Demonstration:
As you can see, I have connected the components according to the circuit diagram, and the system is now displaying the room temperature on my Android application.
Now, when I apply some heat using my lighter, it changes the temperature and humidity readings.
Conclusion
The ESP32 BLE DHT11 temperature monitoring system is a smart and reliable solution for real-time data monitoring. With its wireless connectivity, easy installation, and user-friendly interface, it offers numerous benefits for both residential and industrial use. Whether you’re looking to monitor the temperature and humidity levels in your home or optimize conditions in an industrial setting, this system provides a cost-effective and efficient solution.
By harnessing the power of the ESP32 microcontroller and the accuracy of the DHT11 sensor, the ESP32 BLE DHT11 temperature and humidity monitoring system empowers users with real-time data, enabling them to make informed decisions and take proactive measures. Embrace this technology and experience the convenience and peace of mind that comes with having a reliable monitoring system at your fingertips.
Temperature Monitoring System Video: