Recently i have a project of mine and i would like to convert from Firebase RTDB to Http and i use stream call back function mostly in all of my code, so can i do the same with http?
Here is my code:
#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.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.
/* 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 "a@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);
WiFiManager wifiManager;
wifiManager.autoConnect("i-Water");
Serial.println("Connectted....");
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());
}
}
}