Introduction
Google Firebase Realtime Database is a cloud-hosted NoSQL database that syncs data in real time across all clients. Paired with the ESP32, it becomes a powerful IoT backend — allowing you to send sensor data from your hardware and instantly see it update in a web dashboard or mobile app.
What We Will Build
An ESP32 that reads temperature from a DHT11 sensor and pushes it to Firebase every 10 seconds. Any browser connected to Firebase will see the value update in real time.
Step 1: Create a Firebase Project
- Go to console.firebase.google.com and create a new project.
- Navigate to Realtime Database and create a database in test mode.
- Copy your database URL (e.g. https://your-project.firebaseio.com).
Step 2: Install the Library
In the Arduino IDE, search for and install Firebase ESP32 Client by mobizt.
The Code
#include <WiFi.h>
#include <FirebaseESP32.h>
#include <DHT.h>
#define DHTPIN 4
#define DHTTYPE DHT11
#define FIREBASE_HOST "your-project.firebaseio.com"
#define FIREBASE_AUTH "your-database-secret"
const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASSWORD";
FirebaseData firebaseData;
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(115200);
dht.begin();
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) delay(500);
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
Firebase.reconnectWiFi(true);
Serial.println("Connected to Firebase!");
}
void loop() {
float temp = dht.readTemperature();
float hum = dht.readHumidity();
if (!isnan(temp)) {
Firebase.setFloat(firebaseData, "/sensor/temperature", temp);
Firebase.setFloat(firebaseData, "/sensor/humidity", hum);
Serial.printf("Sent: %.1f C, %.1f %%\n", temp, hum);
}
delay(10000);
}
Viewing Data in Firebase
Go to your Firebase Console and open the Realtime Database. You will see the /sensor/temperature and /sensor/humidity nodes updating every 10 seconds!
Conclusion
Firebase provides a free, scalable cloud backend for your ESP32 IoT projects with zero server management. From here, you can build beautiful web and mobile dashboards to visualize your sensor data anywhere in the world.