[GH-ISSUE #1367] Can this library work with Firebase_Client? #1173

Closed
opened 2026-02-28 01:28:50 +03:00 by kerem · 1 comment
Owner

Originally created by @abddi on GitHub (Feb 28, 2022).
Original GitHub issue: https://github.com/tzapu/WiFiManager/issues/1367

I have tried to do it but unfortunately i did not succeed :'(
If somebody has done it before please help!

Here is my code:

#include <ESP8266WiFi.h>
#include <Firebase_ESP_Client.h>
#include <addons/TokenHelper.h>  //Provide the token generation process info.
#include <addons/RTDBHelper.h>   //Provide the RTDB payload printing info and other helper functions.

//-----------------------------------------------------------
//This should be erased if we want to work with wifiManager
//-----------------------------------------------------------

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

/* 2. Define the API Key */
#define API_KEY "*****************************"
/* 3. Define the RTDB URL */
#define DATABASE_URL "*********************************"
/* 4. Define the user Email and password that alreadey registerd or added in your project */
#define USER_EMAIL "a1@a.com"
#define USER_PASSWORD "123456"

//Define Firebase Data object
FirebaseData stream;
FirebaseData fbdo;
FirebaseAuth auth;
FirebaseConfig config;

String uid;
String path;

#define PUMP1 2
//#define PUMP2 4

// This is the callback function called when stream from firebase was received (every time data has changed)
void streamCallback(FirebaseStream data)
{
  Serial.println("\nStream data available...");
  Serial.println("STREAM PATH: " + data.streamPath());
  Serial.println("EVENT PATH: " + data.dataPath());
  Serial.println("DATA TYPE: " + data.dataType());
  Serial.println("EVENT TYPE: " + data.eventType());

  if (data.dataType() == "boolean")
  {
    digitalWrite(PUMP1, data.boolData());
  }

  if (data.dataType() == "string")
  {
    if (data.stringData() == "on") {
      digitalWrite(D1, HIGH);
      Serial.println("Pump 2 ON");
    }
    if (data.stringData() == "off") {
      digitalWrite(D1, LOW);
      Serial.println("Pump 2 OFF");
    }
  }
}

void streamTimeoutCallback(bool timeout)
{
  if (timeout)
    Serial.println("stream timed out, resuming...\n");
  if (!stream.httpConnected())
    Serial.printf("error code: %d, reason: %s\n\n", stream.httpCode(), stream.errorReason().c_str());
}

void setup()
{
  pinMode(PUMP1, OUTPUT);
  pinMode(D1, OUTPUT);

  Serial.begin(115200);
  Serial.print("Connecting to Wi-Fi");
//-----------------------------------------------------------------
//Also here should be erased if we want to work with wifiManager
//------------------------------------------------------------------

  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  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 RTDB URL (required) */
  config.database_url = DATABASE_URL;

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

  //Recommend for ESP8266 stream, adjust the buffer size to match your stream data size
  stream.setBSSLBufferSize(2048 /* Rx in bytes, 512 - 16384 */, 512 /* Tx in bytes, 512 - 16384 */);

  //----------------------------------------------
  // 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);
  path = "esp/" + uid;

  // Set the stream callback (every change in data related to path, will trig the callback function)
  if (!Firebase.RTDB.beginStream(&stream, path.c_str()))
    Serial.printf("sream begin error, %s\n\n", stream.errorReason().c_str());

  Firebase.RTDB.setStreamCallback(&stream, streamCallback, streamTimeoutCallback);

  // Write some test data in RTDB
  if (Firebase.ready())
  {
    path = "esp/" + uid + "/pump1";
    Serial.print("Set initial pump 1 state as bool... " );
    if (Firebase.RTDB.setBool(&fbdo, path.c_str(), true))
      Serial.println("ok");
    else
      Serial.println(fbdo.errorReason().c_str());

    path = "esp/" + uid + "/pump2";
    Serial.print("Set initial pump 2 state as string... " );
    if (Firebase.RTDB.setString(&fbdo, path.c_str(), "on"))
      Serial.println("ok");
    else
      Serial.println(fbdo.errorReason().c_str());
  }

}

void loop()
{
   
  // Need to be called in order to check if stream data is avalaible
  if (Firebase.ready())
  {
    if (!Firebase.RTDB.readStream(&fbdo))
      Serial.printf("sream read error, %s\n\n", fbdo.errorReason().c_str());

    if (fbdo.streamTimeout())
    {
      Serial.println("stream timed out, resuming...\n");
      if (!fbdo.httpConnected())
        Serial.printf("error code: %d, reason: %s\n\n", fbdo.httpCode(), fbdo.errorReason().c_str());
    }
  }
}
Originally created by @abddi on GitHub (Feb 28, 2022). Original GitHub issue: https://github.com/tzapu/WiFiManager/issues/1367 I have tried to do it but unfortunately i did not succeed :'( If somebody has done it before please help! Here is my code: ``` #include <ESP8266WiFi.h> #include <Firebase_ESP_Client.h> #include <addons/TokenHelper.h> //Provide the token generation process info. #include <addons/RTDBHelper.h> //Provide the RTDB payload printing info and other helper functions. //----------------------------------------------------------- //This should be erased if we want to work with wifiManager //----------------------------------------------------------- /* 1. Define the WiFi credentials */ #define WIFI_SSID "Abdallah Agha" #define WIFI_PASSWORD "abdallah123" /* 2. Define the API Key */ #define API_KEY "*****************************" /* 3. Define the RTDB URL */ #define DATABASE_URL "*********************************" /* 4. Define the user Email and password that alreadey registerd or added in your project */ #define USER_EMAIL "a1@a.com" #define USER_PASSWORD "123456" //Define Firebase Data object FirebaseData stream; FirebaseData fbdo; FirebaseAuth auth; FirebaseConfig config; String uid; String path; #define PUMP1 2 //#define PUMP2 4 // This is the callback function called when stream from firebase was received (every time data has changed) void streamCallback(FirebaseStream data) { Serial.println("\nStream data available..."); Serial.println("STREAM PATH: " + data.streamPath()); Serial.println("EVENT PATH: " + data.dataPath()); Serial.println("DATA TYPE: " + data.dataType()); Serial.println("EVENT TYPE: " + data.eventType()); if (data.dataType() == "boolean") { digitalWrite(PUMP1, data.boolData()); } if (data.dataType() == "string") { if (data.stringData() == "on") { digitalWrite(D1, HIGH); Serial.println("Pump 2 ON"); } if (data.stringData() == "off") { digitalWrite(D1, LOW); Serial.println("Pump 2 OFF"); } } } void streamTimeoutCallback(bool timeout) { if (timeout) Serial.println("stream timed out, resuming...\n"); if (!stream.httpConnected()) Serial.printf("error code: %d, reason: %s\n\n", stream.httpCode(), stream.errorReason().c_str()); } void setup() { pinMode(PUMP1, OUTPUT); pinMode(D1, OUTPUT); Serial.begin(115200); Serial.print("Connecting to Wi-Fi"); //----------------------------------------------------------------- //Also here should be erased if we want to work with wifiManager //------------------------------------------------------------------ WiFi.begin(WIFI_SSID, WIFI_PASSWORD); 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 RTDB URL (required) */ config.database_url = DATABASE_URL; /* 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); //Recommend for ESP8266 stream, adjust the buffer size to match your stream data size stream.setBSSLBufferSize(2048 /* Rx in bytes, 512 - 16384 */, 512 /* Tx in bytes, 512 - 16384 */); //---------------------------------------------- // 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); path = "esp/" + uid; // Set the stream callback (every change in data related to path, will trig the callback function) if (!Firebase.RTDB.beginStream(&stream, path.c_str())) Serial.printf("sream begin error, %s\n\n", stream.errorReason().c_str()); Firebase.RTDB.setStreamCallback(&stream, streamCallback, streamTimeoutCallback); // Write some test data in RTDB if (Firebase.ready()) { path = "esp/" + uid + "/pump1"; Serial.print("Set initial pump 1 state as bool... " ); if (Firebase.RTDB.setBool(&fbdo, path.c_str(), true)) Serial.println("ok"); else Serial.println(fbdo.errorReason().c_str()); path = "esp/" + uid + "/pump2"; Serial.print("Set initial pump 2 state as string... " ); if (Firebase.RTDB.setString(&fbdo, path.c_str(), "on")) Serial.println("ok"); else Serial.println(fbdo.errorReason().c_str()); } } void loop() { // Need to be called in order to check if stream data is avalaible if (Firebase.ready()) { if (!Firebase.RTDB.readStream(&fbdo)) Serial.printf("sream read error, %s\n\n", fbdo.errorReason().c_str()); if (fbdo.streamTimeout()) { Serial.println("stream timed out, resuming...\n"); if (!fbdo.httpConnected()) Serial.printf("error code: %d, reason: %s\n\n", fbdo.httpCode(), fbdo.errorReason().c_str()); } } } ```
kerem closed this issue 2026-02-28 01:28:51 +03:00
Author
Owner

@abddi commented on GitHub (Feb 28, 2022):

Got it done

<!-- gh-comment-id:1054000897 --> @abddi commented on GitHub (Feb 28, 2022): Got it done
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/WiFiManager#1173
No description provided.