In modern IoT applications, monitoring environmental parameters like temperature and humidity is crucial for smart systems. The ESP32, a powerful microcontroller with built-in Wi-Fi capabilities, paired with the DHT11 sensor, enables efficient data collection. By integrating this setup with Firebase, a real-time database platform, data can be sent to the cloud for storage and visualization in real time.
Components Required:
- ESP32 microcontroller
- DHT11 temperature and humidity sensor
- Jumper wires
- Breadboard
- USB cable for ESP32
- Firebase account with a configured Realtime Database
Step 1: Setting Up Firebase
1. Create a Firebase Project:
- Go to the Firebase Console.
- Create a new project and add a Realtime Database.
2. Configure Database Rules:
- Set the database rules for read and write as
truefor testing purposes (not recommended for production).
3. Generate Database URL:
- Note the Firebase database URL (e.g.,
https://your-database-name.firebaseio.com).
Step 2: Connecting DHT11 to ESP32
Wiring Diagram:
- Connect the DHT11 pins as follows:
- VCC: 3.3V of ESP32
- GND: GND of ESP32
- DATA: GPIO pin (e.g., D4)
Step 3: Preparing the Software
1. Install Arduino IDE:
- Download and install the Arduino IDE.
2. Install ESP32 Board Package:
- Add the ESP32 board package URL in Arduino IDE preferences:
arduino Copy code https://dl.espressif.com/dl/package_esp32_index.json
- Install the ESP32 board in the Board Manager.
3. Install Required Libraries:
- Install the following libraries using the Arduino Library Manager:
- DHT sensor library by Adafruit
- Firebase ESP32 by Mobizt
Step 4: Writing the Code
Sample Code:
#include "DHT.h"
#define DHTPIN 4
#define DHTTYPE DHT11
#include <Arduino.h>
#if defined(ESP32)
#include <WiFi.h>
#elif defined(ESP8266)
#include <ESP8266WiFi.h>
#endif
#include <Firebase_ESP_Client.h>
DHT dht(DHTPIN, DHTTYPE);
//Provide the token generation process info.
#include "addons/TokenHelper.h"
//Provide the RTDB payload printing info and other helper functions.
#include "addons/RTDBHelper.h"
// Insert your network credentials
#define WIFI_SSID "YourWiFiSSID"
#define WIFI_PASSWORD " YourWiFiPassword "
// Insert Firebase project API Key
#define API_KEY "Web_API_Key"
// Insert RTDB URLefine the RTDB URL */
#define DATABASE_URL "reference URL"
//Define Firebase Data object
FirebaseData fbdo;
FirebaseAuth auth;
FirebaseConfig config;
//unsigned long sendDataPrevMillis = 0;
//int count = 0;
bool signupOK = false;
void setup(){
pinMode(DHTPIN, INPUT);
dht.begin();
Serial.begin(115200);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("Connecting to Wi-Fi");
while (WiFi.status() != WL_CONNECTED){
Serial.print(".");
delay(300);
}
Serial.println();
Serial.print("Connected with IP: ");
Serial.println(WiFi.localIP());
Serial.println();
/* Assign the api key (required) */
config.api_key = API_KEY;
/* Assign the RTDB URL (required) */
config.database_url = DATABASE_URL;
/* Sign up */
if (Firebase.signUp(&config, &auth, "", "")){
Serial.println("ok");
signupOK = true;
}
else{
Serial.printf("%sn",config.signer.signupError.message.c_str());
}
/* Assign the callback function for the long running token generation task */
config.token_status_callback = tokenStatusCallback; //see addons/TokenHelper.h
Firebase.begin(&config, &auth);
Firebase.reconnectWiFi(true);
}
void loop(){
delay(1000);
float h = dht.readHumidity();
float t = dht.readTemperature();
if (Firebase.ready() && signupOK ) {
if (Firebase.RTDB.setFloat(&fbdo, "DHT/humidity",h)){
Serial.print("Humidity: ");
Serial.println(h);
}
else {
Serial.println("FAILED");
Serial.println("REASON: " + fbdo.errorReason());
}
// Write an Float number on the database path test/float
if (Firebase.RTDB.setFloat(&fbdo, "DHT/temperature", t)){
Serial.print("Temperature: ");
Serial.println(t);
}
else {
Serial.println("FAILED");
Serial.println("REASON: " + fbdo.errorReason());
}
}
Serial.println("______________________________");
}
Step 5: Uploading the Code
- Select the appropriate ESP32 board (e.g., “ESP32 Dev Module”) in the Arduino IDE.
- Choose the correct COM port.
- Upload the code to the ESP32.
Step 6: Testing the Integration
- Open the Serial Monitor to check if the ESP32 is connecting to WiFi and Firebase successfully.
- Verify data updates in the Firebase Realtime Database under the nodes
/Temperatureand/Humidity.
Conclusion
By following this guide, you have successfully integrated an ESP32 microcontroller with a DHT11 sensor and Firebase, enabling real-time monitoring of temperature and humidity data via the cloud.