/**
* SYNTAX:
*
* initializeApp(<AsyncClient>, <FirebaseApp>, <user_auth_data>);
*
* <AsyncClient> - The async client.
* <FirebaseApp> - The authentication and access token handler.
* <user_auth_data> - The user auth data (user_auth_data struct) that holds the user input sign-in credentials and token.
*
* The <user_auth_data> can be obtained from the following sign-in credentials, access key, auth token providers classs via getAuth function i.e.
* CustomAuth, ServiceAuth, UserAuth, NoAuth, CustomToken, AccessToken, IDToken, LegacyToken.
*
* SYNTAX:
*
* LegacyToken::LegacyToken(<database_secret>);
* LegacyToken::LegacyToken(<file_config_data>);
* LegacyToken::save(<file_config_data>);
*
* <database_secret> - The Realtime Database secret.
* <file_config_data> - The filesystem data (file_config_data) obtained from FileConfig class object of file that the LegacyToken credentials will be saved to or read from.
*
* The complete usage guidelines, please visit https://github.com/mobizt/FirebaseClient
*
*/
#include <Arduino.h>
#if defined(ESP32) || defined(ARDUINO_RASPBERRY_PI_PICO_W) || defined(ARDUINO_GIGA)
#include <WiFi.h>
#elif defined(ESP8266)
#include <ESP8266WiFi.h>
#elif __has_include(<WiFi.h>)
#include <WiFi.h>
#endif
#include <FirebaseClient.h>
#define WIFI_SSID "Wokwi-GUEST" // your WiFi SSID
#define WIFI_PASSWORD "" // your WiFi PASSWORD
//#define DATABASE_URL "https://esp32-iot-a129f-default-rtdb.asia-southeast1.firebasedatabase.app"
//[#define DATABASE_SECRET "kEgmch7kd4Z7Las1JTU1bxh9fUW2ecHo36Wou88n"
#define DATABASE_URL "https://projekini-default-rtdb.firebaseio.com/"
#define DATABASE_SECRET "uPSLRTv5sFLhFICPEgCFlP8kqXSPFAbRCJyKN7Xv"
void asyncCB(AsyncResult &aResult);
void printResult(AsyncResult &aResult);
void printError(int code, const String &msg);
DefaultNetwork network; // initilize with boolean parameter to enable/disable network reconnection
LegacyToken legacy_token(DATABASE_SECRET);
FirebaseApp app;
RealtimeDatabase Database;
#if defined(ESP32) || defined(ESP8266) || defined(ARDUINO_RASPBERRY_PI_PICO_W)
#include <WiFiClientSecure.h>
WiFiClientSecure ssl_client;
#endif
using AsyncClient = AsyncClientClass;
AsyncClient aClient(ssl_client, getNetwork(network));
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);
#if defined(ESP32) || defined(ESP8266) || defined(PICO_RP2040)
ssl_client.setInsecure();
#if defined(ESP8266)
ssl_client.setBufferSizes(4096, 1024);
#endif
#endif
// The async result and async result callback are not needed for legacy token.
initializeApp(aClient, app, getAuth(legacy_token));
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();
// Battery Life
Serial.print("Set battery... ");
boolean status = Database.set<String>(aClient, "/Database/batere", "Battery Full");
if (status)
Serial.println("ok");
else
printError(aClient.lastError().code(), aClient.lastError().message());
// Geolocation 1
status = Database.set<int>(aClient, "/Database/latitude", 104.0531);
if (status)
Serial.println("Set latitude is ok");
else
printError(aClient.lastError().code(), aClient.lastError().message());
// Geolocation 2
status = Database.set<int>(aClient, "/Database/longitude", 1.05);
if (status)
Serial.println("Set longitude is ok");
else
printError(aClient.lastError().code(), aClient.lastError().message());
}
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());
}
}
void printError(int code, const String &msg)
{
Firebase.printf("Error, msg: %s, code: %d\n", msg.c_str(), code);
}