Now this is the source cade that can be usesd for read and update the firebase data :
#include <Arduino.h>
#include <ESP8266WiFi.h>
// Make sure you include the main library header
#include <Firebase_ESP_Client.h>
// Provide the token generation process info.
#include "addons/TokenHelper.h"
// Provide the RTDB payload printing info.
#include "addons/RTDBHelper.h"
// Define WiFi credentials
#define WIFI_SSID "Infinix"
#define WIFI_PASSWORD "alpha3"
// Define Firebase project credentials
#define WEB_API_KEY "AIzaSyCb1YXRbtjFFE5OP-1YdgDCo1zRKkxE"
#define DATABASE_URL "https://monitor-41aaf-rtdb.asia-southeast1.firebasedatabase.app/"
// Define Firebase user credentials
#define USER_EMAIL "labinside01@gmail.com"
#define USER_PASS "Roo57325yyffyyyyy29*"
// Declare the object that holds Firebase data for requests and responses
FirebaseData fbdo;
// Declare the authentication and configuration objects
FirebaseAuth auth;
FirebaseConfig config;
// Variable to store the authentication status
bool isAuthenticated = false;
// Example function to handle async data (if you use streams)
void streamCallback(FirebaseStream data)
{
Serial.printf("stream path, %s\nevent path, %s\ndata type, %s\nevent type, %s\n\n",
data.streamPath().c_str(),
data.dataPath().c_str(),
data.dataType().c_str(),
data.eventType().c_str());
printResult(data); //See addons/RTDBHelper.h
Serial.println();
}
void setup()
{
pinMode(BUILTIN_LED, OUTPUT);
digitalWrite(BUILTIN_LED, HIGH);
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();
/* Assign the project credentials */
config.api_key = WEB_API_KEY;
config.database_url = DATABASE_URL;
/* Assign the user sign in credentials */
auth.user.email = USER_EMAIL;
auth.user.password = USER_PASS;
/* Assign the callback function for the long running token generation task */
config.token_status_callback = tokenStatusCallback; //see addons/TokenHelper.h
// Initialize the Firebase library
Firebase.begin(&config, &auth);
Firebase.reconnectWiFi(true);
digitalWrite(BUILTIN_LED, LOW);
}
void loop()
{
if (Firebase.ready()) {
if (!isAuthenticated) {
Serial.println("Firebase is authenticated and ready!");
isAuthenticated = true;
}
if (Firebase.RTDB.getInt(&fbdo, "/vehicle_1/alarm")) {
if (fbdo.dataType() == "int") {
int alarmStatus = fbdo.intData();
Serial.print("Alarm status is: ");
Serial.println(alarmStatus);
Firebase.RTDB.setInt(&fbdo, "/vehicle_1/alarm", 1);
}
} else {
Serial.println("Failed to get integer. Reason: " + fbdo.errorReason());
}
} else {
isAuthenticated = false;
Serial.println("Waiting for Firebase authentication...");
}
delay(1); // Wait 5 seconds before the next read
}