I am working on an ESP32 project to measure voltage, current, power, and energy consumption using the EmonLib library, and send the data to Firebase Realtime Database. Although my ESP32 is connected to WiFi and the Serial Monitor prints the sensor values correctly, no data is showing up in Firebase.
I Have
Connected ESP32 to WiFi successfully.
Configured Firebase with API key and database URL.
Used anonymous authentication for Firebase.
Set Firebase Realtime Database rules to public for testing:
{
"rules": {
".read": true,
".write": true
}
}
Added debug prints in Serial Monitor.
Tested with a minimal sketch to upload a simple string (still no success).
Verified the Firebase URL does not have a trailing slash.
My Code:
#include <WiFi.h>
#include <Firebase_ESP_Client.h>
#include <EmonLib.h>
#define WIFI_SSID "Abdul"
#define WIFI_PASSWORD "987654321"
#define API_KEY "AIzaSyDgW-hYeBKeEEsFJvSeYiQLN5HIE31K_wg"
#define DATABASE_URL "https://fyp308-f4c78-default-rtdb.asia-southeast1.firebasedatabase.app"
FirebaseData fbdo;
FirebaseAuth auth;
FirebaseConfig config;
EnergyMonitor emon;
float kWh = 0.0;
unsigned long lastMillis = 0;
void setup() {
Serial.begin(115200);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(300);
}
config.api_key = API_KEY;
config.database_url = DATABASE_URL;
auth.user.email = "";
auth.user.password = "";
Firebase.begin(&config, &auth);
Firebase.reconnectWiFi(true);
emon.voltage(35, 41.5, 1.7);
emon.current(34, 0.015);
lastMillis = millis();
}
void loop() {
emon.calcVI(20, 2000);
float voltage = emon.Vrms;
float current = emon.Irms;
float power = emon.apparentPower;
unsigned long now = millis();
kWh += (power * (now - lastMillis)) / 3600000.0;
lastMillis = now;
Serial.printf("Voltage: %.2f, Current: %.3f, Power: %.2f, Energy: %.5f\n", voltage, current, power, kWh);
if (Firebase.ready()) {
if (!Firebase.RTDB.setFloat(&fbdo, "/EnergyMeter/Voltage", voltage))
Serial.println("Voltage upload failed: " + fbdo.errorReason());
if (!Firebase.RTDB.setFloat(&fbdo, "/EnergyMeter/Current", current))
Serial.println("Current upload failed: " + fbdo.errorReason());
if (!Firebase.RTDB.setFloat(&fbdo, "/EnergyMeter/Power", power))
Serial.println("Power upload failed: " + fbdo.errorReason());
if (!Firebase.RTDB.setFloat(&fbdo, "/EnergyMeter/Energy_kWh", kWh))
Serial.println("Energy upload failed: " + fbdo.errorReason());
} else {
Serial.println("Firebase not ready");
}
delay(5000);
}
Problem:
The Serial Monitor shows the sensor values correctly.
No data appears in Firebase Realtime Database.
No error messages printed about WiFi or Firebase connection.
Tried removing the trailing slash from DATABASE_URL.
What could be preventing my ESP32 from uploading data to Firebase?
How can I debug this issue further?