0

Summary: I want to turn the LED light ON and OFF from firestore through the userID so the work in code has been done by createDocument and getDocument, and it works fine, but the issue here the LED light wont turn ON or OFF. I might be mistaken with my approach, so you can find below the Serial Monitor results and code.

Serial Monitor Results:

Firebase Client v2.7.0

Token info: type = id token, status = on request
Token info: type = id token, status = ready
Getting User UID
User UID: 1klR12zX5yN6bG9VbhTEfopEuoC2
Create document... Document already exists: projects/smart-tank-ea899/databases/(default)/documents/esp/1klR12zX5yN6bG9VbhTEfopEuoC2
Get a document... ok
{
  "name": "projects/smart-tank-ea899/databases/(default)/documents/esp/1klR12zX5yN6bG9VbhTEfopEuoC2",
  "fields": {
    "pump": {
      "stringValue": "off"
    }
  },
  "createTime": "2021-12-02T09:32:03.080835Z",
  "updateTime": "2021-12-02T09:35:27.274606Z"
}
Get a document... ok
{
  "name": "projects/smart-tank-ea899/databases/(default)/documents/esp/1klR12zX5yN6bG9VbhTEfopEuoC2",
  "fields": {
    "pump": {
      "stringValue": "on"
    }
  },
  "createTime": "2021-12-02T09:32:03.080835Z",
  "updateTime": "2021-12-02T09:43:57.716274Z"
}

Code there is no error and it seems like that it works:

#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 "AIzaS***************"

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

/* 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;

            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+"";
        String mask = "pump";
        Serial.print("Get a document... ");
        if (Firebase.Firestore.getDocument(&fbdo, FIREBASE_PROJECT_ID, "", path.c_str(), mask.c_str())){
            Serial.printf("ok\n%s\n\n", fbdo.payload().c_str());
            
            if(mask == "on"){
              digitalWrite(D1, HIGH);
              }
            
            if(mask == "off"){
              digitalWrite(D1, LOW);
              }
              
          }
           else{
            Serial.println(fbdo.errorReason());
        } 
            
    }

enter image description here

Abddi
  • 25
  • 6
  • Why are you retrieving the document and not just the string? And why are you comparing the `mask` variable? – gre_gor Dec 04 '21 at 02:02
  • I just solved it by using firebase json and you are right I was wrong with the mask approach. – Abddi Dec 04 '21 at 07:18
  • @Abddi Can you post the solution to your issue as an answer here so that anyone who has a similar issue can fix it by seeing your answer. – Priyashree Bhadra Dec 10 '21 at 04:03

0 Answers0