How to reduce reading from Firebase?

Hi everyone,
Recently i have just finished a project of mine which make lights turn on and off through Firestore unfortunately it's reading too much! just yesterday it has read around 12K in 30min so that is not okay it's too much reading for sure because firestore only gives 50K for free daily.
I'm using delay() to keep getting data every 1 min and i think the reading issue is happening because of that! but what i'm thinking of is: this is a light and i want it to respond to my command to turn ON or OFF directly so if i used this approach with delay() the bill of firebase is going to be very expensive, even though if i used my own http it also going to be very expensive.
So have somebody of you guys faced this issue before? is there a way that i can use to reduce Firestore reading in code?

Here is my code:

#if defined(ESP32)
#include <WiFi.h>
#elif defined(ESP8266)
#include <ESP8266WiFi.h>
#endif
#include <Firebase_ESP_Client.h>
#include <addons/TokenHelper.h>


/* 1. Define the WiFi credentials */
#define WIFI_SSID "Abdallah Agha"
#define WIFI_PASSWORD "abdallah123"

/* 2. Define the API Key */
#define API_KEY "AIza**************************"

/* 3. Define the project ID */
#define FIREBASE_PROJECT_ID "s**********"

/* 4. Define the user Email and password that alreadey registerd or added in your project */
#define USER_EMAIL "a@a.com"
#define USER_PASSWORD "123456"

//Define Firebase Data object
FirebaseData fbdo;

FirebaseAuth auth;
FirebaseConfig config;

String uid;

String path;

String firebaseStatus = "on";

void setup()
{

    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();

    Serial.printf("Firebase Client v%s\n\n", FIREBASE_CLIENT_VERSION);

    /* Assign the api key (required) */
    config.api_key = API_KEY;

    /* Assign the user sign in credentials */
    auth.user.email = USER_EMAIL;
    auth.user.password = USER_PASSWORD;

    /* 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);
     //----------
    //LED LIGHT
    //-----------
    pinMode(D1, OUTPUT); 

    //----------------------------------------------
   // Getting the user UID might take a few seconds
   //-----------------------------------------------
  Serial.println("Getting User UID");
  while ((auth.token.uid) == "") {
    Serial.print('.');
    delay(1000);
  }
    //-----------------
   // Print user UID
   //------------------
  uid = auth.token.uid.c_str();
  Serial.print("User UID: ");
  Serial.println(uid);
           
}
          

void loop()
{      
          //-------------------
            //Create Document
            //-------------------
            FirebaseJson content;

            
            //---------------------
            //PUMP document is here
            //---------------------
            content.set("fields/pump/stringValue", firebaseStatus);
           
            //esp is the collection id, user uid is the document id in collection info.
            path = "esp/"+uid+"";
            
            Serial.print("Create document... ");

            if (Firebase.Firestore.createDocument(&fbdo, FIREBASE_PROJECT_ID, "" /* databaseId can be (default) or empty */, path.c_str(), content.raw()))
                Serial.printf("ok\n%s\n\n", fbdo.payload().c_str());
            else
                Serial.println(fbdo.errorReason());

         //-------------------
         //Get Document
        //--------------------
       
        
        
        path = "esp/"+uid+"";

  Serial.print("Get a document... ");
  if (Firebase.Firestore.getDocument(&fbdo, FIREBASE_PROJECT_ID, "", path.c_str(), "")) {
    Serial.printf("ok\n%s\n\n", fbdo.payload().c_str());

    // Create a FirebaseJson object and set content with received payload
    FirebaseJson payload;
    payload.setJsonData(fbdo.payload().c_str());

    // Get the data from FirebaseJson object 
    FirebaseJsonData jsonData;
    payload.get(jsonData, "fields/pump/stringValue", true);
    Serial.println(jsonData.stringValue);
    
    if(jsonData.stringValue == "on"){
      digitalWrite(D1, HIGH);
    }
            
     if(jsonData.stringValue == "off"){
        digitalWrite(D1, LOW);
       }
              
          }
           else{
            Serial.println(fbdo.errorReason());
        } 
        delay(10000);
            
    }

You are connecting your ESP8266/ESP32 (which one is it???)
to the internet and use the firebase-functionality to transfer data between a switch and some second device (what is this other device???)

You want your switch to be responsive which means within a second or two the "receiver" shall react on the switch-pressing event.

You are trying to achive this by polling your firebase-database each second. Well doing it this way creates a lot of traffic over the internet. Additionally this kind of datatransfer has a lot of overhead. For a simple "on" "off" you have to transfer estimated 20 to 50 times more bytes to achieve this.

I'm not familiar with firebase at all. To reduce traffic you would have to make it work this way:
The receiving device (what is it?? what does it do??) is in a "waiting state" for a message to "rush in"
and if a message arrives react on it.

This is called event-driven. Only if an event really occurs send something. If firebase can't be used in an event-driven way I guess it is the wrong application/functionality for this.

It is always a good idea to give an overview about your project.
If your "switch" is some kind of wall-mounted switch and the receiving device is inside your local WLAN there are multiple ways to achieve this functionality:
press the switch - switch something on/off

  • UDP-messages
  • MQTT
  • HTTP-messages
  • eventually ESP-NOW
  • IFTTT
  • telegram messagebot

But you haven't written anything about what your project is doing in the end.

best regards Stefan

1 Like

Do you mean by event-driven in arduino to do something for example like this:

#include<Ticker.h>
#define SENSOR 5

Ticker  everyMinute;  

void setup(){
  Serial.begin(74880);
  pinMode(SENSOR,INPUT);
  everyMinute.attach_ms(60000,[](){ Serial.println("do something"); });
}

void loop(){
}

Hi abdallahagha,
Your demo code-example demonstrates the basic principle.

to reduce the datatraffic to a minimum the whole thing should work like

The switch gets pressed and sends a message towards the "onlineservice" of firebase.

The "onlineservice" of firebase receives this messages and reacts on this receiving by sending one single message towards your receiver.
Your receiver reacts on receiving this message.

Event-driven in the sense of
The "onlineservice" of firebase has an event of receiving a message and forwards this signal to your receiver

and event-driven your receiver, reacts on the event of receiving a message.
I have zero experience with firebase. So don't know if the onlineservice of firebase is able to work this way.

If your whole application is local and sender and receiver are connected to the same WLAN sending udp-messages would be

  • fully sufficient,
  • can be programmed event-driven,
  • could send ten messages per second without any problem and
  • has a real unlimited datavolume.

So if your project is not a secret it would be interesting to read what project you are working on

best regards Stefan

Great information and my project is not a secret and no other projects must be because everyone has the right to learn and even though if the project was a secret after all someone is gonna figure it out and do it, so i have already shared all of the code that i have worked on you can find it up ^ here actually.
Summary of what i am doing as a starter project i want to automate all of my apartment lights so i could manage to turn ON and OFF all of the lights through a mobile app

You seem to prefer proceeding slow by giving small answers.
Yeah that's one learning style and a programming style.

Ok so just another small question you can answer very quickly by a simple "yes" or "no"

So do I assume right that all your devices are connected to the one and the same WLAN-network?

by the way: if you change your mind and want to proceed faster. Simply post more information.
best regards Stefan