diff --git a/README.md b/README.md index c718849f..aac48bf7 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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. @@ -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/) diff --git a/examples/App/AppInitialization/Async/Callback/NoAuth/NoAuth.ino b/examples/App/AppInitialization/Async/Callback/NoAuth/NoAuth.ino index 6626fc78..85f6db10 100644 --- a/examples/App/AppInitialization/Async/Callback/NoAuth/NoAuth.ino +++ b/examples/App/AppInitialization/Async/Callback/NoAuth/NoAuth.ino @@ -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; @@ -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()); - } -} +} \ No newline at end of file diff --git a/examples/App/AppInitialization/Async/Callback/TokenAuth/LegacyToken/LegacyToken.ino b/examples/App/AppInitialization/Async/Callback/TokenAuth/LegacyToken/LegacyToken.ino index 453432ba..9867247f 100644 --- a/examples/App/AppInitialization/Async/Callback/TokenAuth/LegacyToken/LegacyToken.ino +++ b/examples/App/AppInitialization/Async/Callback/TokenAuth/LegacyToken/LegacyToken.ino @@ -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); @@ -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()); - } } \ No newline at end of file diff --git a/examples/README.md b/examples/README.md index 2b05dfc1..e223e9ae 100644 --- a/examples/README.md +++ b/examples/README.md @@ -313,6 +313,11 @@ │ │ ├───StreamConcurentcy │ │ ├───StreamGSM │ │ └───Update +│ ├───Simple +│ │ ├───SimpleDatabaseSecret +│ │ ├───SimpleNoAuth +│ │ ├───StreamDatabaseSecret +│ │ └───StreamNoAuth │ └───Sync │ ├───CustomPushID │ ├───ETAG diff --git a/examples/RealtimeDatabase/README.md b/examples/RealtimeDatabase/README.md index f7bfc3ca..f5484623 100644 --- a/examples/RealtimeDatabase/README.md +++ b/examples/RealtimeDatabase/README.md @@ -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/) diff --git a/examples/RealtimeDatabase/Simple/SimpleDatabaseSecret/SimpleDatabaseSecret.ino b/examples/RealtimeDatabase/Simple/SimpleDatabaseSecret/SimpleDatabaseSecret.ino new file mode 100644 index 00000000..6f3bdb63 --- /dev/null +++ b/examples/RealtimeDatabase/Simple/SimpleDatabaseSecret/SimpleDatabaseSecret.ino @@ -0,0 +1,183 @@ +#include +#if defined(ESP32) || defined(ARDUINO_RASPBERRY_PI_PICO_W) || defined(ARDUINO_GIGA) +#include +#elif defined(ESP8266) +#include +#endif + +#include +#include + +#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(Database); + + Database.url(DATABASE_URL); + + client.setAsyncResult(result); + + // Set and get integer value + + Serial.print("Set int... "); + bool status = Database.set(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(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(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(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(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(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(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(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(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(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(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(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(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(client, "/test/array"); + if (client.lastError().code() == 0) + Serial.println(v7); + else + printError(client.lastError().code(), client.lastError().message()); + +} + +void loop() +{ +} \ No newline at end of file diff --git a/examples/RealtimeDatabase/Simple/SimpleNoAuth/SimpleNoAuth.ino b/examples/RealtimeDatabase/Simple/SimpleNoAuth/SimpleNoAuth.ino new file mode 100644 index 00000000..ed0c7c43 --- /dev/null +++ b/examples/RealtimeDatabase/Simple/SimpleNoAuth/SimpleNoAuth.ino @@ -0,0 +1,189 @@ +/** This example requires the Realtime database security rules setup as following. + { + "rules": { + ".read": true, + ".write": true + } +} +*/ + +#include +#if defined(ESP32) || defined(ARDUINO_RASPBERRY_PI_PICO_W) || defined(ARDUINO_GIGA) +#include +#elif defined(ESP8266) +#include +#endif + +#include +#include + +#define WIFI_SSID "WIFI_AP" +#define WIFI_PASSWORD "WIFI_PASSWORD" + +#define DATABASE_URL "URL" + +WiFiClientSecure ssl; +DefaultNetwork network; +AsyncClientClass client(ssl, getNetwork(network)); + +FirebaseApp app; +RealtimeDatabase Database; +AsyncResult result; +NoAuth noAuth; + +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(noAuth)); + + app.getApp(Database); + + Database.url(DATABASE_URL); + + client.setAsyncResult(result); + + // Set and get integer value + + Serial.print("Set int... "); + bool status = Database.set(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(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(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(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(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(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(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(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(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(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(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(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(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(client, "/test/array"); + if (client.lastError().code() == 0) + Serial.println(v7); + else + printError(client.lastError().code(), client.lastError().message()); +} + +void loop() +{ +} \ No newline at end of file diff --git a/examples/RealtimeDatabase/Simple/StreamDatabaseSecret/StreamDatabaseSecret.ino b/examples/RealtimeDatabase/Simple/StreamDatabaseSecret/StreamDatabaseSecret.ino new file mode 100644 index 00000000..e037b1bd --- /dev/null +++ b/examples/RealtimeDatabase/Simple/StreamDatabaseSecret/StreamDatabaseSecret.ino @@ -0,0 +1,118 @@ +#include +#if defined(ESP32) || defined(ARDUINO_RASPBERRY_PI_PICO_W) || defined(ARDUINO_GIGA) +#include +#elif defined(ESP8266) +#include +#endif + +#include +#include + +#define WIFI_SSID "WIFI_AP" +#define WIFI_PASSWORD "WIFI_PASSWORD" + +#define DATABASE_SECRET "DATABASE_SECRET" +#define DATABASE_URL "URL" + +WiFiClientSecure ssl1, ssl2; +DefaultNetwork network; +AsyncClientClass client1(ssl1, getNetwork(network)), client2(ssl2, getNetwork(network)); + +FirebaseApp app; +RealtimeDatabase Database; +AsyncResult result1, result2; +LegacyToken dbSecret(DATABASE_SECRET); + +unsigned long ms = 0; + +void printResult(AsyncResult &aResult) +{ + + 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()) + { + RealtimeDatabaseResult &RTDB = aResult.to(); + if (RTDB.isStream()) + { + Serial.println("----------------------------"); + Firebase.printf("task: %s\n", aResult.uid().c_str()); + Firebase.printf("event: %s\n", RTDB.event().c_str()); + Firebase.printf("path: %s\n", RTDB.dataPath().c_str()); + Firebase.printf("data: %s\n", RTDB.to()); + Firebase.printf("type: %d\n", RTDB.type()); + + // The stream event from RealtimeDatabaseResult can be converted to the values as following. + bool v1 = RTDB.to(); + int v2 = RTDB.to(); + float v3 = RTDB.to(); + double v4 = RTDB.to(); + String v5 = RTDB.to(); + } + else + { + Serial.println("----------------------------"); + Firebase.printf("task: %s, payload: %s\n", aResult.uid().c_str(), aResult.c_str()); + } + } +} + +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); + + ssl1.setInsecure(); + ssl2.setInsecure(); +#if defined(ESP8266) + ssl1.setBufferSizes(1024, 1024); + ssl2.setBufferSizes(1024, 1024); +#endif + + initializeApp(client1, app, getAuth(dbSecret)); + + app.getApp(Database); + + Database.url(DATABASE_URL); + + client1.setAsyncResult(result1); + client2.setAsyncResult(result2); + + Database.get(client1, "/test/stream", result1, true); +} + +void loop() +{ + Database.loop(); + + if (millis() - ms > 20000 || ms == 0) + { + ms = millis(); + Database.set(client2, "/test/stream/ts", random(100, 999), result2); + } + + printResult(result1); + printResult(result2); +} \ No newline at end of file diff --git a/examples/RealtimeDatabase/Simple/StreamNoAuth/StreamNoAuth.ino b/examples/RealtimeDatabase/Simple/StreamNoAuth/StreamNoAuth.ino new file mode 100644 index 00000000..dea7fa2c --- /dev/null +++ b/examples/RealtimeDatabase/Simple/StreamNoAuth/StreamNoAuth.ino @@ -0,0 +1,127 @@ +/** This example requires the Realtime database security rules setup as following. + { + "rules": { + ".read": true, + ".write": true + } +} +*/ + +#include +#if defined(ESP32) || defined(ARDUINO_RASPBERRY_PI_PICO_W) || defined(ARDUINO_GIGA) +#include +#elif defined(ESP8266) +#include +#endif + +#include +#include + +#define WIFI_SSID "WIFI_AP" +#define WIFI_PASSWORD "WIFI_PASSWORD" + +#define DATABASE_SECRET "DATABASE_SECRET" +#define DATABASE_URL "URL" + +WiFiClientSecure ssl1, ssl2; +DefaultNetwork network; +AsyncClientClass client1(ssl1, getNetwork(network)), client2(ssl2, getNetwork(network)); + +FirebaseApp app; +RealtimeDatabase Database; +AsyncResult result1, result2; +NoAuth noAuth; + +unsigned long ms = 0; + +void printResult(AsyncResult &aResult) +{ + + 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()) + { + RealtimeDatabaseResult &RTDB = aResult.to(); + if (RTDB.isStream()) + { + Serial.println("----------------------------"); + Firebase.printf("task: %s\n", aResult.uid().c_str()); + Firebase.printf("event: %s\n", RTDB.event().c_str()); + Firebase.printf("path: %s\n", RTDB.dataPath().c_str()); + Firebase.printf("data: %s\n", RTDB.to()); + Firebase.printf("type: %d\n", RTDB.type()); + + // The stream event from RealtimeDatabaseResult can be converted to the values as following. + bool v1 = RTDB.to(); + int v2 = RTDB.to(); + float v3 = RTDB.to(); + double v4 = RTDB.to(); + String v5 = RTDB.to(); + } + else + { + Serial.println("----------------------------"); + Firebase.printf("task: %s, payload: %s\n", aResult.uid().c_str(), aResult.c_str()); + } + } +} + +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); + + ssl1.setInsecure(); + ssl2.setInsecure(); +#if defined(ESP8266) + ssl1.setBufferSizes(1024, 1024); + ssl2.setBufferSizes(1024, 1024); +#endif + + initializeApp(client1, app, getAuth(noAuth)); + + app.getApp(Database); + + Database.url(DATABASE_URL); + + client1.setAsyncResult(result1); + client2.setAsyncResult(result2); + + Database.get(client1, "/test/stream", result1, true); +} + +void loop() +{ + Database.loop(); + + if (millis() - ms > 20000 || ms == 0) + { + ms = millis(); + Database.set(client2, "/test/stream/ts", random(100, 999), result2); + } + + printResult(result1); + printResult(result2); +} \ No newline at end of file diff --git a/examples/RealtimeDatabase/Sync/Set/Set.ino b/examples/RealtimeDatabase/Sync/Set/Set.ino index 89c16b4c..c1fda4b3 100644 --- a/examples/RealtimeDatabase/Sync/Set/Set.ino +++ b/examples/RealtimeDatabase/Sync/Set/Set.ino @@ -123,23 +123,27 @@ void setup() Serial.println("Synchronous Set... "); // Set int + + Serial.print("Set int... "); bool status = Database.set(aClient, "/test/int", 12345); if (status) - Serial.println("Set int is ok"); + Serial.println("ok"); else printError(aClient.lastError().code(), aClient.lastError().message()); // Set bool + Serial.print("Set bool... "); status = Database.set(aClient, "/test/bool", true); if (status) - Serial.println("Set bool is ok"); + Serial.println("ok"); else printError(aClient.lastError().code(), aClient.lastError().message()); // Set string + Serial.print("Set String... "); status = Database.set(aClient, "/test/string", "hello"); if (status) - Serial.println("Set string is ok"); + Serial.println("ok"); else printError(aClient.lastError().code(), aClient.lastError().message()); @@ -151,11 +155,12 @@ void setup() object_t json; JsonWriter writer; writer.create(json, "test/data", 123); // -> {"test":{"data":123}} - // Or set the seialized JSON string to object_t as object_t("{\"test\":{\"data\":123}}") + // Or set the seialized JSON string to object_t as object_t("{\"test\":{\"data\":123}}") + Serial.print("Set JSON... "); status = Database.set(aClient, "/test/json", json); if (status) - Serial.println("Set json is ok"); + Serial.println("ok"); else printError(aClient.lastError().code(), aClient.lastError().message()); @@ -164,25 +169,30 @@ void setup() object_t arr; arr.initArray(); // To use as Array placeholder writer.join(arr, 4, object_t(1), object_t(2), object_t(string_t("test")), object_t(boolean_t(true))); // -> [1,2,"test",true] - // Or set the seialized JSON Array string to the object_t as object_t("[1,2,\"test\",true]") + // Or set the seialized JSON Array string to the object_t as object_t("[1,2,\"test\",true]") + Serial.print("Set Array... "); status = Database.set(aClient, "/test/arr", arr); if (status) - Serial.println("Set array is ok"); + Serial.println("ok"); else printError(aClient.lastError().code(), aClient.lastError().message()); // Set float + + Serial.print("Set float... "); status = Database.set(aClient, "/test/float", number_t(123.456, 2)); if (status) - Serial.println("Set float is ok"); + Serial.println("ok"); else printError(aClient.lastError().code(), aClient.lastError().message()); // Set double + + Serial.print("Set double... "); status = Database.set(aClient, "/test/double", number_t(1234.56789, 4)); if (status) - Serial.println("Set double is ok"); + Serial.println("ok"); else printError(aClient.lastError().code(), aClient.lastError().message()); } diff --git a/library.json b/library.json index 6dbbb913..90a3226a 100644 --- a/library.json +++ b/library.json @@ -1,6 +1,6 @@ { "name": "FirebaseClient", - "version": "1.2.9", + "version": "1.2.10", "keywords": "communication, REST, esp32, esp8266, arduino", "description": "Async Firebase Client library for Arduino.", "repository": { diff --git a/library.properties b/library.properties index 04cc4a69..5e4136b5 100644 --- a/library.properties +++ b/library.properties @@ -1,6 +1,6 @@ name=FirebaseClient -version=1.2.9 +version=1.2.10 author=Mobizt diff --git a/src/core/Core.h b/src/core/Core.h index 9ab2ca1c..7e2e6b70 100644 --- a/src/core/Core.h +++ b/src/core/Core.h @@ -7,7 +7,7 @@ #undef FIREBASE_CLIENT_VERSION #endif -#define FIREBASE_CLIENT_VERSION "1.2.9" +#define FIREBASE_CLIENT_VERSION "1.2.10" static void sys_idle() {