FirebaseClient
Introduction
Async Firebase Client library for Arduino Documentation.
This is the REST APIs Firebase Client library that supports the following Firebase and Google Cloud products in one place.
Firebase Realtime Database
Cloud Firestore Database
Cloud Messaging
Firebase Storage
Google Cloud Functions
Google Cloud Storage
Security Rules Management
This Firebase library is quite comprehensive which covers many applications.
It is suitable for all serverless applications e.g. data logging, automation and IoTs. You can choose what options and services you want to use (see Library Build Options).
There are many types of authentications supported and you can manage the user account with sign up (user/guest), delete, verify the user and reset the user password.
This library also supports auto- and one-time authentications and you can force the library to re-authenticate any time.
This library also provides the OTA firmware update functionalities by using your Realtime Database data or Firebase/Cloud Storage object (file).
Note that the Firestore data change listening and Realtime Database disconnected event are not support by the Firebase REST API.
The bare minimum code examples provide the basic guidelines for library usage concepts. There is a little more code that needs to be written but it is still simple and understandable.
You can create your own wrapper class as you want that provides the callback-free, cleaner and simpler API. See the Wrapper example for how to.
You can use security rules to fully control the Firebase services access instead of relying only on the authentication credentials and tokens. For access control using custom claims and security rule use case, see AccessControl example.
This library also provides the test tools (web client app) that are simple and ready to try without coding.
To see how Firebase Realtime Database provides the efficient, fast and reliable performance when work with this library, see StreamPerformanceTest example.
To test notification sending with Firebase Cloud Messaging by using our FCM Web Client App, see WebClient example.
Changes since v2.1.0
The library since v2.1.0 is network independent. All network data and management classes are removed.
Users have to define the macros or build options in their sketch before including the library header file. See Library Build Options section for the available options.
The SSL client can be set via AsyncClientClass::setClient() or in the AsyncClientClass constructors without the network options.
The AsyncClientClass methods that are related to the networks are removed.
Supported Devices
ESP8266, ESP32 and all 32-bit MCU except for Atmel AVR.
Installation
At Arduino IDE, go to menu Sketch -> Include Library -> Manage Libraries...
In Library Manager Window, search "firebase" in the search form then select "FirebaseClient".
Click "Install" button.
For PlatformIO IDE, use the following command.
pio lib install "FirebaseClient""
Or at PIO Home -> Library -> Registry then search FirebaseClient.
In case ESP32, PlatformIO's platform-espressif32 core is outdated. You should use pioarduino platform instead.
For Arduino IDE, download the zip file from the repository (Github page) by select Code dropdown at the top of repository, select Download ZIP
From Arduino IDE, select menu Sketch -> Include Library -> Add .ZIP Library....
Choose FirebaseClient-main.zip that previously downloaded.
Rename folder from FirebaseClient-main to FirebaseClient.
Go to menu Files -> Examples -> FirebaseClient and choose one from examples.
RP2040/RP2350 Arduino SDK installation
For Arduino IDE, the Arduino-Pico SDK can be installed from Boards Manager by searching pico and choose Raspberry Pi Pico/RP2040 to install.
For PlatformIO, the Arduino-Pico SDK can be installed via platformio.ini
[env:rpipicow]
platform = https://github.com/maxgerhardt/platform-raspberrypi.git
board = rpipicow
framework = arduino
board_build.core = earlephilhower
monitor_speed = 115200
board_build.filesystem_size = 1m
See this Arduino-Pico SDK documentation for more information.
[!NOTE]
You cannot install Arduino IDE and Arduino library in Microsoft's OneDrive folder because the OneDrive folder is the sandbox or virtual folder instead of real folder which causes the path error when compilation.
Usages
For new Firebase users, please read the Project Preparation and Setup section for preparing the Firebase project.
For new library user that the project was setup and prepared, see the bare minimun examples to start using this library with minimum code that is required by this library.
For more examples, please click here.
Working Principle
This library can be used in two modes i.e. async mode and await or sync mode.
With async mode, the task will be stored in the FIFO queue. The result of the running task can be obtained via the callback function or the proxy object that assign when calling the functions.
The AsyncClientClass is the proxy class that provides the queue for async tasks and also the information of task process when working in await or sync mode.
The async task that is stored in the queue contains the preprocess HTTP request data (headers without auth tokens and payload).
The SSL Client that is assigned with the AsyncClientClass constructor will be the network client used for all async tasks in its queue.
The result of a async task can be obtained from the AsyncResult class object. This object provides the debug, authentication process event, error information and the response payload of the request.
If no async callback function is assigned with the function, the AsyncResult class object should be assigned otherwise it will work in sync mode.
In this case, it should be polling read for new available information from the AsyncResult at the end of the loop function.
In the await or sync mode, task will be processed immediately and waits for the end of process when the response is complete or time out or error occurred.
The information (debug/error) can be obtained by the AsyncClientClass that assign with the function. The response payload or processed value will be obtained if no errors.
The authentications support by this library covers user, service account (admin) and no authentications.
There are authentication classes that store the sign-in and authentication credentials which can be used temporary as it will be duplicated to use later.
The authentication tokens are also supported e.g. custom (signed JWT token), ID and access tokens. The refresh token also supports. User can use third party app/services to create these auth token when sign-in and authentication credentials are sensitive and not allowed to store in the sketch.
These authentication tokens (excepts for Reltime Database secret) are the short-lived tokens. It will be expired in 60 minutes.
The FirebaseApp class will be used to handle the authentication task. Some function that executes in the loop function will keep the authentication process running e.g. FirebaseApp::loop.
The authentication process is the task that involved auth token generation using sign-in, auth credentials and tokens that user assigned with the initializeApp function.
The initializeApp function is the static function which also allows the authentication process to work in async and await or sync modes.
In version 2.x.x and later, when assign the timeout (> 0) to the initializeApp function, the authentication task will be processed in await or sync mode.
The callback function assigned with initializeApp function will provide the auth process event, debug and error information while authenticating in async and sync modes.
In version 2.x.x, the FirebaseApp class can also maintain the async tasks that stored in other AsyncClientClasss.
The details for all classes used in this library are available. Click the following links for details.
OTA Update
The OTA firmware update is supported using bin file stored in Firebase Storage and Google Cloud Storage buckets or base64 encoded data stored in Realtime Database.
The Arduino devices that natively supported OTA firmware update are ESP8266, ESP32 and Raspberry Pi Pico.
Since FirebaseClient v1.3.1, the OTA update in Arduino SAMD21 boards that use NINA firmware and WiFi101 firmware was supported using Internal_Storage_OTA library.
The Internal_Storage_OTA is the modified version of WiFi101OTA library which contains only four required files e.g. Internal_Storage_OTA.h, InternalStorage.h, InternalStorage.cppand OTAStorage.h.
To allow OTA update in SAMD21 Arduino boards, you have to include Internal_Storage_OTA.h in your sketch.
Then assign the InternalStorage class object to be used for Realtime Database via RealtumeDatabase::setOTAStorage, for Google Cloud Storage via CloudStorage::setOTAStorage and for Firebase Storage via Storage::setOTAStorage
In SAMD21 Arduino boards, if OTA Storage was not set before calling OTA function, the error OTA Storage was not set will be occurred.
Finally, once the OTA update was finished, in case Internal_Storage_OTA