#include <Arduino.h>
#include <WiFi.h>
#include <FirebaseClient.h>
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASSWORD ""
// The API key can be obtained from Firebase console > Project Overview > Project settings.
#define API_KEY "AIzaSyD_UfcSsXO2cOyqfqqsK4Xw-dDRb7HPT8A"
// User Email and password that already registerd or added in your project.
#define USER_EMAIL "[email protected]"
#define USER_PASSWORD "Rishuriya"
#define DATABASE_URL ""
void asyncCB(AsyncResult &aResult);
void printResult(AsyncResult &aResult);
DefaultNetwork network;
UserAuth user_auth(API_KEY, "[email protected]", "Rishuriya");
FirebaseApp app;
#include <WiFiClientSecure.h>
WiFiClientSecure ssl_client;
using AsyncClient = AsyncClientClass;
AsyncClient aClient(ssl_client, getNetwork(network));
RealtimeDatabase Database;
bool taskComplete = false;
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();
Firebase.printf("Firebase Client v%s\n", FIREBASE_CLIENT_VERSION);
Serial.println("Initializing app...");
ssl_client.setInsecure();
initializeApp(aClient, app, getAuth(user_auth), asyncCB, "authTask");
app.getApp<RealtimeDatabase>(Database);
Database.url(DATABASE_URL);
}
void loop()
{
// The async task handler should run inside the main loop
// without blocking delay or bypassing with millis code blocks.
app.loop();
Database.loop();
if (app.ready() && !taskComplete)
{
taskComplete = true;
Database.get(aClient, "/test/int", asyncCB, false, "getTask1");
Database.get(aClient, "/test/string", asyncCB, false, "getTask2");
// Filtering data
// For REST API, indexing the data at /test/filter/json is required when filtering the data, please see examples/Database/Extras/Sync/IndexingData/IndexingData.ino.
DatabaseOptions options;
options.filter.orderBy("Data").startAt(105).endAt(120).limitToLast(8);
Database.get(aClient, "/test/filter/json", options, asyncCB, "getTask3");
}
}
void asyncCB(AsyncResult &aResult)
{
// WARNING!
// Do not put your codes inside the callback and printResult.
printResult(aResult);
}
void printResult(AsyncResult &aResult)
{
if (aResult.isEvent())
{
Firebase.printf("Event task: %s, msg: %s, code: %d\n", aResult.uid().c_str(), aResult.appEvent().message().c_str(), aResult.appEvent().code());
}
if (aResult.isDebug())
{
Firebase.printf("Debug task: %s, msg: %s\n", aResult.uid().c_str(), aResult.debug().c_str());
}
if (aResult.isError())
{
Firebase.printf("Error task: %s, msg: %s, code: %d\n", aResult.uid().c_str(), aResult.error().message().c_str(), aResult.error().code());
}
if (aResult.available())
{
Firebase.printf("task: %s, payload: %s\n", aResult.uid().c_str(), aResult.c_str());
}
}