-1

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?

2
  • 1
    If you have any web-server (or better web-proxy) with logging try to set DATABASE_URL to it and check logs. You can also use some diagnostic tools in wifi router if it has such to sniff traffic or at least to see if it exists. Commented May 22 at 18:13
  • I would suggest that you check out the examples of the library. Basically, you are trying to access a https website without configuring a SSL certificate. BTW, the library is deprecated. Commented May 24 at 0:51

1 Answer 1

0

To further debug this, on each IF statement for error check, also print if the invoked functions are success.

if (Firebase.ready()) {

    Serial.println("Firebase Ready!");

    if (!Firebase.RTDB.setFloat(&fbdo, "/EnergyMeter/Voltage", voltage))
      Serial.println("Voltage upload failed: " + fbdo.errorReason());
    else
      Serial.println("Firebase Voltage uploaded!");

    if (!Firebase.RTDB.setFloat(&fbdo, "/EnergyMeter/Current", current))
      Serial.println("Current upload failed: " + fbdo.errorReason());
    else
      Serial.println("Firebase Current uploaded!");

    if (!Firebase.RTDB.setFloat(&fbdo, "/EnergyMeter/Power", power))
      Serial.println("Power upload failed: " + fbdo.errorReason());
     else
      Serial.println("Firebase Power uploaded!");

    if (!Firebase.RTDB.setFloat(&fbdo, "/EnergyMeter/Energy_kWh", kWh))
      Serial.println("Energy upload failed: " + fbdo.errorReason());
    else
      Serial.println("Firebase Energy uploaded!");
    
} else {
    Serial.println("Firebase not ready");
}

if they are printing success and still no data in the Firebase, kindly check the API and URL of your Firebase to verify if you are looking at the right database, maybe it's sending but on a different database that you previously created. (A friendly reminder also to not share your personal API and URL here :)

#define API_KEY "AIzaSyDgW-hYeBKeEEsFJvSeYiQLN5HIE31K_wg"
#define DATABASE_URL "https://fyp308-f4c78-default-rtdb.asia-southeast1.firebasedatabase.app"

It is possible also that you mat not properly set your Firebase Database. Try creating another one and use this reference as an example.
https://randomnerdtutorials.com/esp32-firebase-realtime-database/

Lastly, you may use another Firebase library, I prefer to use this
https://github.com/mobizt/Firebase-ESP32
It also provides example codes on sending ang reading data to Firebase
https://gist.github.com/TrickSumo/2f16c122b2f59c1e7b0846514dd945cd
https://gist.github.com/TrickSumo/ae215a3b81e3e07a62e9689a9d8795a0

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.