-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Simple examples for Realtime database usage
- Loading branch information
Showing
13 changed files
with
660 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
183 changes: 183 additions & 0 deletions
183
examples/RealtimeDatabase/Simple/SimpleDatabaseSecret/SimpleDatabaseSecret.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
{ | ||
} |
Oops, something went wrong.