Skip to content

Commit

Permalink
Add Simple examples for Realtime database usage
Browse files Browse the repository at this point in the history
  • Loading branch information
mobizt committed Jun 6, 2024
1 parent 4f2eaff commit d5b6003
Show file tree
Hide file tree
Showing 13 changed files with 660 additions and 93 deletions.
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/mobizt/FirebaseClient/.github%2Fworkflows%2Fcompile_library.yml?logo=github&label=compile) [![Github Stars](https://img.shields.io/github/stars/mobizt/FirebaseClient?logo=github)](https://github.com/mobizt/FirebaseClient/stargazers) ![Github Issues](https://img.shields.io/github/issues/mobizt/FirebaseClient?logo=github)

![GitHub Release](https://img.shields.io/github/v/release/mobizt/FirebaseClient) ![Arduino](https://img.shields.io/badge/Arduino-v1.2.9-57C207?logo=arduino) ![PlatformIO](https://badges.registry.platformio.org/packages/mobizt/library/FirebaseClient.svg) ![GitHub Release Date](https://img.shields.io/github/release-date/mobizt/FirebaseClient)
![GitHub Release](https://img.shields.io/github/v/release/mobizt/FirebaseClient) ![Arduino](https://img.shields.io/badge/Arduino-v1.2.10-57C207?logo=arduino) ![PlatformIO](https://badges.registry.platformio.org/packages/mobizt/library/FirebaseClient.svg) ![GitHub Release Date](https://img.shields.io/github/release-date/mobizt/FirebaseClient)

[![GitHub Sponsors](https://img.shields.io/github/sponsors/mobizt?logo=github)](https://github.com/sponsors/mobizt)

Revision `2024-06-06T04:01:15Z`
Revision `2024-06-06T05:45:03Z`

## Table of Contents

Expand Down Expand Up @@ -358,9 +358,9 @@ The authorization token will be refresh or recreated automatically as long as th
>
> Anyway, library also provides the option for non-secure usage i.e. using database secret for Realtime database and no authorization token if the security rules are allowed. This topic will be mentioned later with `LegacyToken` and `NoAuth` classes usage.
>
> You can get started using this library with simple [sync functions](https://github.com/mobizt/FirebaseClient/blob/main/examples/RealtimeDatabase/Sync/Get/Get.ino) using [non-secure authentication method](https://github.com/mobizt/FirebaseClient/blob/main/examples/App/AppInitialization/Sync/TokenAuth/LegacyToken/LegacyToken.ino) which is similar to other legacy Firebase library.
> You can get started using this library with [Simple examples](https://github.com/mobizt/FirebaseClient/blob/main/examples/RealtimeDatabase/Simple) using non-secure authentication method which is similar to other legacy Firebase library.
>
> For async and secure usages, you have to read the documentation thouroughly and follow the library provided examples to get familiar with the library usage.
> For secure and more elaborate usages, you have to read the documentation thouroughly and follow the library provided examples to get familiar with the library usage.
The authorization token types that can be used for Firebase/Google APIs authorization are `ID token` and `access token` which will be called shorter as `auth tokens` in this library.

Expand Down Expand Up @@ -1816,6 +1816,11 @@ The examples provided by this library can be divided into two use cases i.e. `As
* [StreamConcurentcy](/examples/RealtimeDatabase/Async/NoCallback/StreamConcurentcy/)
* [StreamGSM](/examples/RealtimeDatabase/Async/NoCallback/StreamGSM/)
* [Update](/examples/RealtimeDatabase/Async/NoCallback/Update/)
* [Simple](/examples/RealtimeDatabase/Simple/)
* [SimpleDatabaseSecret](/examples/RealtimeDatabase/Simple/SimpleDatabaseSecret/)
* [SimpleNoAuth](/examples/RealtimeDatabase/Simple/SimpleNoAuth/)
* [StreamDatabaseSecret](/examples/RealtimeDatabase/Simple/StreamDatabaseSecret/)
* [StreamNoAuth](/examples/RealtimeDatabase/Simple/StreamNoAuth/)
* [Sync](/examples/RealtimeDatabase/Sync/)
* [CustomPushID](/examples/RealtimeDatabase/Sync/CustomPushID/)
* [ETAG](/examples/RealtimeDatabase/Sync/ETAG/)
Expand Down
40 changes: 2 additions & 38 deletions examples/App/AppInitialization/Async/Callback/NoAuth/NoAuth.ino
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ void asyncCB(AsyncResult &aResult);

void printResult(AsyncResult &aResult);

DefaultNetwork network; // initilize with boolean parameter to enable/disable network reconnection
DefaultNetwork network;

NoAuth no_auth;

Expand Down Expand Up @@ -92,44 +92,8 @@ void setup()
// The async result and async result callback are not needed for no authentication.
initializeApp(aClient, app, getAuth(no_auth));

// To re-initialize the app, call initializeApp again.
}

void loop()
{
// The async task handler should run inside the main loop
// without blocking delay or bypassing with millis code blocks.

app.loop();
}

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());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,6 @@

#define DATABASE_SECRET "DATABASE_SECRET"

void asyncCB(AsyncResult &aResult);

void printResult(AsyncResult &aResult);

DefaultNetwork network; // initilize with boolean parameter to enable/disable network reconnection

LegacyToken legacy_token(DATABASE_SECRET);
Expand Down Expand Up @@ -101,39 +97,4 @@ void setup()

void loop()
{
// The async task handler should run inside the main loop
// without blocking delay or bypassing with millis code blocks.

app.loop();
}

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());
}
}
5 changes: 5 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,11 @@
│ │ ├───StreamConcurentcy
│ │ ├───StreamGSM
│ │ └───Update
│ ├───Simple
│ │ ├───SimpleDatabaseSecret
│ │ ├───SimpleNoAuth
│ │ ├───StreamDatabaseSecret
│ │ └───StreamNoAuth
│ └───Sync
│ ├───CustomPushID
│ ├───ETAG
Expand Down
5 changes: 5 additions & 0 deletions examples/RealtimeDatabase/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
* [StreamConcurentcy](/examples/RealtimeDatabase/Async/NoCallback/StreamConcurentcy/)
* [StreamGSM](/examples/RealtimeDatabase/Async/NoCallback/StreamGSM/)
* [Update](/examples/RealtimeDatabase/Async/NoCallback/Update/)
* [Simple](/examples/RealtimeDatabase/Simple/)
* [SimpleDatabaseSecret](/examples/RealtimeDatabase/Simple/SimpleDatabaseSecret/)
* [SimpleNoAuth](/examples/RealtimeDatabase/Simple/SimpleNoAuth/)
* [StreamDatabaseSecret](/examples/RealtimeDatabase/Simple/StreamDatabaseSecret/)
* [StreamNoAuth](/examples/RealtimeDatabase/Simple/StreamNoAuth/)
* [Sync](/examples/RealtimeDatabase/Sync/)
* [CustomPushID](/examples/RealtimeDatabase/Sync/CustomPushID/)
* [ETAG](/examples/RealtimeDatabase/Sync/ETAG/)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
#include <Arduino.h>
#if defined(ESP32) || defined(ARDUINO_RASPBERRY_PI_PICO_W) || defined(ARDUINO_GIGA)
#include <WiFi.h>
#elif defined(ESP8266)
#include <ESP8266WiFi.h>
#endif

#include <FirebaseClient.h>
#include <WiFiClientSecure.h>

#define WIFI_SSID "WIFI_AP"
#define WIFI_PASSWORD "WIFI_PASSWORD"

#define DATABASE_SECRET "DATABASE_SECRET"
#define DATABASE_URL "URL"

WiFiClientSecure ssl;
DefaultNetwork network;
AsyncClientClass client(ssl, getNetwork(network));

FirebaseApp app;
RealtimeDatabase Database;
AsyncResult result;
LegacyToken dbSecret(DATABASE_SECRET);

void printError(int code, const String &msg)
{
Firebase.printf("Error, msg: %s, code: %d\n", msg.c_str(), code);
}

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

ssl.setInsecure();
#if defined(ESP8266)
ssl.setBufferSizes(1024, 1024);
#endif

initializeApp(client, app, getAuth(dbSecret));

app.getApp<RealtimeDatabase>(Database);

Database.url(DATABASE_URL);

client.setAsyncResult(result);

// Set and get integer value

Serial.print("Set int... ");
bool status = Database.set<int>(client, "/test/int", 12345);
if (status)
Serial.println("ok");
else
printError(client.lastError().code(), client.lastError().message());

Serial.print("Get int... ");
int v1 = Database.get<int>(client, "/test/int");
if (client.lastError().code() == 0)
Serial.println(v1);
else
printError(client.lastError().code(), client.lastError().message());

// Set and get Boolean value

Serial.print("Set bool... ");
status = Database.set<bool>(client, "/test/bool", true);
if (status)
Serial.println("ok");
else
printError(client.lastError().code(), client.lastError().message());

Serial.print("Get bool... ");
bool v2 = Database.get<bool>(client, "/test/bool");
if (client.lastError().code() == 0)
Serial.println(v2);
else
printError(client.lastError().code(), client.lastError().message());

// Set and get String value

Serial.print("Set string... ");
status = Database.set<String>(client, "/test/string", "hello");
if (status)
Serial.println("ok");
else
printError(client.lastError().code(), client.lastError().message());

Serial.print("Get string... ");
String v3 = Database.get<String>(client, "/test/string");
if (client.lastError().code() == 0)
Serial.println(v3);
else
printError(client.lastError().code(), client.lastError().message());

// Set and get float value

Serial.print("Set float... ");
status = Database.set<number_t>(client, "/test/float", number_t(123.456, 2));
if (status)
Serial.println("ok");
else
printError(client.lastError().code(), client.lastError().message());

Serial.print("Get float... ");
float v4 = Database.get<float>(client, "/test/float");
if (client.lastError().code() == 0)
Serial.println(v4);
else
printError(client.lastError().code(), client.lastError().message());

// Set and get double value

Serial.print("Set double... ");

status = Database.set<number_t>(client, "/test/double", number_t(1234.56789, 4));
if (status)
Serial.println("ok");
else
printError(client.lastError().code(), client.lastError().message());

Serial.print("Get double... ");
double v5 = Database.get<double>(client, "/test/double");
if (client.lastError().code() == 0)
Serial.println(v5);
else
printError(client.lastError().code(), client.lastError().message());

// Set and get JSON value

Serial.print("Set JSON... ");

status = Database.set<object_t>(client, "/test/json", object_t("{\"test\":{\"data\":123}}"));
if (status)
Serial.println("ok");
else
printError(client.lastError().code(), client.lastError().message());

Serial.print("Get JSON... ");
String v6 = Database.get<String>(client, "/test/json");
if (client.lastError().code() == 0)
Serial.println(v6);
else
printError(client.lastError().code(), client.lastError().message());


// Set and get Array value

Serial.print("Set Array... ");

status = Database.set<object_t>(client, "/test/array", object_t("[1,2,\"test\",true]"));
if (status)
Serial.println("ok");
else
printError(client.lastError().code(), client.lastError().message());

Serial.print("Get Array... ");
String v7 = Database.get<String>(client, "/test/array");
if (client.lastError().code() == 0)
Serial.println(v7);
else
printError(client.lastError().code(), client.lastError().message());

}

void loop()
{
}
Loading

0 comments on commit d5b6003

Please sign in to comment.