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