Skip to content

Commit

Permalink
Rewrite the Authentications and RTDB examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
mobizt committed May 19, 2021
1 parent 4d2a7d9 commit 0b6ec47
Show file tree
Hide file tree
Showing 118 changed files with 1,915 additions and 5,423 deletions.
13 changes: 1 addition & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Firebase Arduino Client Library for ESP8266 and ESP32


Google's Firebase Arduino Client Library for ESP8266 and ESP32 v2.2.1
Google's Firebase Arduino Client Library for ESP8266 and ESP32 v2.2.2


This library supports ESP8266 and ESP32 MCU from Espressif. The following are platforms in which the libraries are also available (RTDB only).
Expand Down Expand Up @@ -182,17 +182,6 @@ fbdo.setResponseSize(1024); //minimum size is 1024 bytes
```


### Important Information

In ESP8266 Arduino Core SDK v3.x.x

The free heap is significantly reduced as much as 5-6 kB from v2.7.4.

This may lead to out of memory sitation when two Firebase Data objects are used simultaneously (when sessions connected).

Minimize the reserved memory for BearSSL will gain the free heap a bit but may not enough for your usage.

You can stay with Core SDK v2.7.4 until this memory issue was solve in the Core SDK.



Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
/** This example will show how to authenticate using
* the legacy token or database secret with the new APIs (using config and auth data).
*/

#if defined(ESP32)
#include <WiFi.h>
#elif defined(ESP8266)
Expand Down Expand Up @@ -40,7 +41,6 @@ FirebaseAuth auth;
/* Define the FirebaseConfig data for config data */
FirebaseConfig config;

String path = "/Test";
unsigned long dataMillis = 0;
int count = 0;

Expand All @@ -61,6 +61,8 @@ void setup()
Serial.println(WiFi.localIP());
Serial.println();

Serial.printf("Firebase Client v%s\n\n", FIREBASE_CLIENT_VERSION);

/* Assign the certificate file (optional) */
//config.cert.file = "/cert.cer";
//config.cert.file_storage = StorageType::FLASH;
Expand All @@ -73,31 +75,16 @@ void setup()

/* Initialize the library with the Firebase authen and config */
Firebase.begin(&config, &auth);

//Or use legacy authenticate method
//Firebase.begin(DATABASE_URL, DATABASE_SECRET);
}

void loop()
{
if (millis() - dataMillis > 5000)
{
dataMillis = millis();

Serial.println("------------------------------------");
Serial.println("Set int test...");

String node = path + "/int";

if (Firebase.RTDB.set(&fbdo, node.c_str(), count++))
{
Serial.println("PASSED");
Serial.println("------------------------------------");
Serial.println();
}
else
{
Serial.println("FAILED");
Serial.println("REASON: " + fbdo.errorReason());
Serial.println("------------------------------------");
Serial.println();
}
Serial.printf("Set int... %s\n", Firebase.RTDB.setInt(&fbdo, "/test/int", count++) ? "ok" : fbdo.errorReason().c_str());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@

/**
* Created by K. Suwatchai (Mobizt)
*
* Email: [email protected]
*
* Github: https://github.com/mobizt
*
* Copyright (c) 2021 mobizt
*
*/

/** This example will show how to authenticate as admin using
* the Service Account file to create the access token to sign in internally.
*
* The library will connect to NTP server to get the time (in case your device
* time was not set)
* and waiting for the time to be ready.
*
* If the waiting is timed out and the system time is not ready, the custom and
* OAuth2.0 acces tokens generation will fail
* because of invalid expiration time in JWT token that used in the id/access
* token request.
*/

#if defined(ESP32)
#include <WiFi.h>
#elif defined(ESP8266)
#include <ESP8266WiFi.h>
#endif
#include <Firebase_ESP_Client.h>

//Provide the token generation process info.
#include "addons/TokenHelper.h"
//Provide the RTDB payload printing info and other helper functions.
#include "addons/RTDBHelper.h"

/* 1. Define the WiFi credentials */
#define WIFI_SSID "WIFI_AP"
#define WIFI_PASSWORD "WIFI_PASSWORD"

/* 2. If work with RTDB, define the RTDB URL */
#define DATABASE_URL "URL" //<databaseName>.firebaseio.com or <databaseName>.<region>.firebasedatabase.app

/* 3. Define the Firebase Data object */
FirebaseData fbdo;

/* 4. Define the FirebaseAuth data for authentication data */
FirebaseAuth auth;

/* 5. Define the FirebaseConfig data for config data */
FirebaseConfig config;

unsigned long dataMillis = 0;
int count = 0;

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

Serial.printf("Firebase Client v%s\n\n", FIREBASE_CLIENT_VERSION);

/* Assign the certificate file (optional) */
config.cert.file = "/gsr2.pem";
config.cert.file_storage = mem_storage_type_flash; //or mem_storage_type_sd

/* The file systems for flash and SD/SDMMC can be changed in FirebaseFS.h. */

/* Assign the sevice account JSON file and the file storage type (required) */
config.service_account.json.path = "/service_account_file.json"; //change this for your json file
config.service_account.json.storage_type = mem_storage_type_flash; //or mem_storage_type_sd

/** The user UID set to empty to sign in as admin */
auth.token.uid = "";

/* Assign the RTDB URL */
config.database_url = DATABASE_URL;

/** The scope of the OAuth2.0 authentication
* If you wan't this access token for others Google Cloud Services.
*/
//config.signer.tokens.scope = "Google Scope 1 Url, Google Scope 2 Url,..";

Firebase.reconnectWiFi(true);

/* Assign the callback function for the long running token generation task */
config.token_status_callback = tokenStatusCallback; //see addons/TokenHelper.h

/** Assign the maximum retry of token generation */
config.max_token_generation_retry = 5;

/** To set system time with the timestamp from RTC
* The internal NTP server time acquisition
* of token generation process will be skipped,
* if the system time is already set.
*
* sec is the seconds from midnight Jan 1, 1970
*/
//Firebase.setSystemTime(sec);

/* Now we start to signin using access token */

/** Initialize the library with the Firebase authen and config.
*
* The device time will be set by sending request to the NTP server
* befor token generation and exchanging.
*
* The signed RSA256 jwt token will be created and used for access token exchanging.
*
* Theses process may take time to complete.
*/
Firebase.begin(&config, &auth);

/* The access token (C++ string) can be accessed from config.signer.tokens.access_token. */
}

void loop()
{
if (Firebase.ready() && millis() - dataMillis > 5000)
{
dataMillis = millis();
Serial.printf("Set int... %s\n", Firebase.RTDB.setInt(&fbdo, "/test/int", count++) ? "ok" : fbdo.errorReason().c_str());
}
}
Loading

0 comments on commit 0b6ec47

Please sign in to comment.