Skip to content

Commit 66a9cec

Browse files
committed
Add double, JSON array supports, generic get function and fix error invalid reason report.
1 parent dbc0c60 commit 66a9cec

File tree

7 files changed

+219
-68
lines changed

7 files changed

+219
-68
lines changed

README.md

Lines changed: 96 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Firebase RTDB Arduino Client for ARM/AVR WIFI Dev Boards
22

33

4-
Google's Firebase Realtime Database Arduino Library for ARM/AVR WIFI Development Boards based on WiFiNINA library, v 1.2.1
4+
Google's Firebase Realtime Database Arduino Library for ARM/AVR WIFI Development Boards based on WiFiNINA library, v 1.2.2
55

66
This client library provides the most reliable operations for read, store, and update the Firebase RTDB through the REST API.
77

@@ -40,14 +40,11 @@ This following devices were tested and work well.
4040

4141
## Features
4242

43+
* **Read data** at the defined database path using **get** or specific functions e.g. **getInt**, **getDouble**, **getFloat**, **getBool**, **getString**, **getJSON** and **getArray**.
4344

44-
* **Not required fingerprint** or **certificate data** to connect.
45+
* **Store data** at the defined database path using set functions e.g. **setInt**, **setDouble**, **setFloat**, **setBool**, **setString**, **setJSON** and **setArray**.
4546

46-
* **Read data** at the defined database path using get functions e.g. **getInt**, **getFloat**, **getBool**, **getString** and **getJSON**.
47-
48-
* **Store data** at the defined database path using set functions e.g. **setInt**, **setFloat**, **setBool**, **setString** and **setJSON**.
49-
50-
* **Append data** to the defined database path using push functions e.g. **pushInt**, **pushFloat**, **pushBool**, **pushString** and **pushJSON**.
47+
* **Append data** to the defined database path using push functions e.g. **pushInt**, **pushFloat**, **pushBool**, **pushString**, **pushJSON** and **pushArray**.
5148

5249
* **Update data** at the defined database path using **updateNode** and **updateNodeSilent** functions.
5350

@@ -75,6 +72,8 @@ This following devices were tested and work well.
7572

7673
This library required [WiFiNINA Library](https://github.com/arduino-libraries/WiFiNINA) to be installed which can be installed through **Boards Manager**
7774

75+
Update WiFiNINA firmware and install server SSL certificate, see this [issue #18](https://github.com/mobizt/Firebase-Arduino-WiFiNINA/issues/18) for how to.
76+
7877

7978
## Installing
8079

@@ -109,11 +108,11 @@ Go to menu **Files** -> **Examples** -> **Firebase-Arduino-WiFiNIN A-master** an
109108

110109
//2. Declare the Firebase Data object in global scope
111110

112-
FirebaseData firebaseData;
111+
FirebaseData fbdo;
113112

114113
//3. Setup Firebase credential in setup()
115114

116-
Firebase.begin("yout_project_id.firebaseio.com", "your_Firebase_database_secret", "your_wifi_ssid", "your_wifi_password");
115+
Firebase.begin("yout_project_id.firebaseio.com", "database secret", "wifi ssid", "wifi password");
117116

118117
//4. Optional, set AP reconnection in setup()
119118

@@ -157,20 +156,20 @@ Here is the example usage to read integer value from defined database path "/tes
157156

158157
int val = 0;
159158

160-
if (Firebase.getInt(firebaseData, "/test/int")) {
159+
if (Firebase.getInt(fbdo, "/test/int")) {
161160

162161
//Success, then read the payload value
163162

164163
//Make sure payload value returned from server is integer
165164
//This prevent you to get garbage data
166-
if (firebaseData.dataType() == "int")) {
167-
val = firebaseData.intData();
165+
if (fbdo.dataType() == "int")) {
166+
val = fbdo.intData();
168167
Serial.println(val);
169168
}
170169

171170
} else {
172171
//Failed, then print out the error detail
173-
Serial.println(firebaseData.errorReason());
172+
Serial.println(fbdo.errorReason());
174173
}
175174

176175
```
@@ -192,17 +191,17 @@ Below is the example usage to store or set float value to database at "/test/flo
192191
```cpp
193192
194193
195-
if (Firebase.setFloat(firebaseData, "/test/float_data", 123.456789)){
194+
if (Firebase.setFloat(fbdo, "/test/float_data", 123.456789)){
196195
197196
//Success, then read the payload value return from server
198197
//This confirmed that your data was set to database as float number
199198
200-
if (firebaseData.dataType() == "float")
201-
Serial.println(firebaseData.floatData());
199+
if (fbdo.dataType() == "float")
200+
Serial.println(fbdo.floatData());
202201
203202
} else {
204203
//Failed, then print out the error detail
205-
Serial.println(firebaseData.errorReason());
204+
Serial.println(fbdo.errorReason());
206205
}
207206
208207
```
@@ -211,16 +210,16 @@ if (Firebase.setFloat(firebaseData, "/test/float_data", 123.456789)){
211210

212211

213212

214-
**To append new data to database, `push<Data Type>` should be called e.g. pushInt, pushFloat, pushDouble, pushBool, pushString and pushJSON.**
213+
**To append new data to database, `push<Data Type>` should be called e.g. pushInt, pushFloat, pushDouble, pushBool, pushString, pushJSON and pushArray.**
215214

216215

217216
With push operation, server will return payload (key or name of newly appended node) to client.
218217

219218
Working with JSON data allow us to read or store multiple data at once because JSON data can store many key/value pairs, array of object and nested objects.
220219

221-
Function setJSON will set/replace value at defined database path with value in JSON data, and also create child nodes.
220+
Function setJSON or setArray will set/replace value at defined database path with value in JSON or array data, and also create child nodes.
222221

223-
While in function pushJSON, all key/value in JSON data will be appended to the defined database path as new node.
222+
While in function pushJSON or pushArray , all key/value in JSON or array data will be appended to the defined database path as new node.
224223

225224
Below is the example for appending new data (using JSON) to the path "/test/append.
226225

@@ -231,23 +230,23 @@ Below is the example for appending new data (using JSON) to the path "/test/appe
231230

232231
String jsonData = "{\"parent_001\":\"parent 001 text\", \"parent 002\":{\"child_of_002\":123.456}}";
233232

234-
if (Firebase.pushJSON(firebaseData, "/test/append", jsonData)) {
233+
if (Firebase.pushJSON(fbdo, "/test/append", jsonData)) {
235234

236235
//Success, then read the payload value
237236

238237
//Database path to be appended
239-
Serial.println(firebaseData.dataPath()); //Should be "/test/append"
238+
Serial.println(fbdo.dataPath()); //Should be "/test/append"
240239

241240
//New created key/name
242-
Serial.println(firebaseData.pushName());
241+
Serial.println(fbdo.pushName());
243242

244243
//Absolute path of new appended data
245-
Serial.println(firebaseData.dataPath() + "/"+ firebaseData.pushName());
244+
Serial.println(fbdo.dataPath() + "/"+ fbdo.pushName());
246245

247246

248247
} else {
249248
//Failed, then print out the error detail
250-
Serial.println(firebaseData.errorReason());
249+
Serial.println(fbdo.errorReason());
251250
}
252251

253252
```
@@ -276,25 +275,25 @@ Below is the example for database update at "/test" using JSON data.
276275

277276
String updateData = "{\"data1\":\"value1\", \"data2\":{\"_data2\":\"_value2\"}}";
278277

279-
if (Firebase.updateNode(firebaseData, "/test/update", updateData)) {
278+
if (Firebase.updateNode(fbdo, "/test/update", updateData)) {
280279

281280
//Success, then try to read the payload value
282281

283282
//Database path that updated
284-
Serial.println(firebaseData.dataPath());
283+
Serial.println(fbdo.dataPath());
285284

286285
//Data type at updated database path
287-
Serial.println(firebaseData.dataType()); //Should be "json"
286+
Serial.println(fbdo.dataType()); //Should be "json"
288287

289288
//Print the JSON string payload that returned from server
290-
Serial.println(firebaseData.jsonData()); //Should mathes the value in updateData variable
289+
Serial.println(fbdo.jsonData()); //Should mathes the value in updateData variable
291290

292291
//Actual sent payload JSON data
293292
Serial.println(updateData);
294293

295294
} else {
296295
//Failed, then print out the error detail
297-
Serial.println(firebaseData.errorReason());
296+
Serial.println(fbdo.errorReason());
298297
}
299298

300299
```
@@ -310,7 +309,7 @@ Below example will delete data and its child nodes at "/test/append"
310309

311310
```cpp
312311

313-
Firebase.deleteNode(firebaseData, "/test/append");
312+
Firebase.deleteNode(fbdo, "/test/append");
314313

315314
```
316315

@@ -368,15 +367,15 @@ query.endAt(8);
368367
query.limitToLast(5);
369368

370369

371-
if (Firebase.getJSON(firebaseData, "/test/data", query))
370+
if (Firebase.getJSON(fbdo, "/test/data", query))
372371
{
373372
//Success, then try to read the JSON payload value
374-
Serial.println(firebaseData.jsonData());
373+
Serial.println(fbdo.jsonData());
375374
}
376375
else
377376
{
378377
//Failed to get JSON data at defined database path, print out the error reason
379-
Serial.println(firebaseData.errorReason());
378+
Serial.println(fbdo.errorReason());
380379
}
381380

382381
//Release memory used by query object
@@ -420,44 +419,44 @@ Here is the example use of stream to handle the changes or updates at "/test/dat
420419

421420
//In setup(), set the streaming path to "/test/data" and begin stream connection
422421

423-
if (!Firebase.beginStream(firebaseData, "/test/data"))
422+
if (!Firebase.beginStream(fbdo, "/test/data"))
424423
{
425424
//Could not begin stream connection, then print out the error detail
426-
Serial.println(firebaseData.errorReason());
425+
Serial.println(fbdo.errorReason());
427426
}
428427

429428
//In loop()
430429

431430

432-
if (!Firebase.readStream(firebaseData))
431+
if (!Firebase.readStream(fbdo))
433432
{
434433
//If read stream was failed, print the error detail.
435-
Serial.println(firebaseData.errorReason());
434+
Serial.println(fbdo.errorReason());
436435
}
437436

438-
if (firebaseData.streamTimeout())
437+
if (fbdo.streamTimeout())
439438
{
440439
//If stream timeout, just notify
441440
Serial.println("Stream timeout, resume streaming...");
442441
Serial.println();
443442
}
444443

445-
if (firebaseData.streamAvailable())
444+
if (fbdo.streamAvailable())
446445
{
447446

448447
//Print out value
449448
//Stream data can be many types which can be determined from function dataType
450449

451-
if (firebaseData.dataType() == "int")
452-
Serial.println(firebaseData.intData());
453-
else if (firebaseData.dataType() == "float")
454-
Serial.println(firebaseData.floatData());
455-
else if (firebaseData.dataType() == "boolean")
456-
Serial.println(firebaseData.boolData());
457-
else if (firebaseData.dataType() == "string")
458-
Serial.println(firebaseData.stringData());
459-
else if (firebaseData.dataType() == "json")
460-
Serial.println(firebaseData.jsonData());
450+
if (fbdo.dataType() == "int")
451+
Serial.println(fbdo.intData());
452+
else if (fbdo.dataType() == "float")
453+
Serial.println(fbdo.floatData());
454+
else if (fbdo.dataType() == "boolean")
455+
Serial.println(fbdo.boolData());
456+
else if (fbdo.dataType() == "string")
457+
Serial.println(fbdo.stringData());
458+
else if (fbdo.dataType() == "json")
459+
Serial.println(fbdo.jsonData());
461460

462461
}
463462

@@ -633,6 +632,26 @@ bool pushJSON(FirebaseData &fbdo, const String &path, const String &jsonString);
633632
634633
635634
635+
**Append new child nodes's key and value (using JSON array data) to the defined database path.**
636+
637+
param **`fbdo`** - Firebase Data Object to hold data and instances.
638+
639+
param **`path`** - Target database path which key and value in JSON data will be appended.
640+
641+
param **`arrayString`** - The appended JSON array string (should be valid JSON array data).
642+
643+
return **`Boolean`** type status indicates the success of operation.
644+
645+
The new appended node's key will be stored in Firebase Data object,
646+
which its value can be accessed via function \<firebase data object\>.pushName().
647+
648+
```cpp
649+
bool pushJSON(FirebaseData &fbdo, const String &path, const String &arrayString);
650+
```
651+
652+
653+
654+
636655

637656

638657
**Append new Firebase server's timestamp to the defined database path.**
@@ -811,6 +830,33 @@ bool setJSON(FirebaseData &fbdo, const String &path, const String &jsonString);
811830
812831
813832
833+
**Set child nodes's key and value (using JSON array data) to the defined database path.**
834+
835+
This will replace any child nodes inside the defined path with node' s key
836+
and value defined in JSON array data.
837+
838+
param **`fbdo`** - Firebase Data Object to hold data and instances.
839+
840+
param **`path`** - Target database path which key and value in JSON array data will be replaced or set.
841+
842+
param **`arrayString`** - The JSON string to set (should be valid JSON array data).
843+
844+
return **`Boolean`** type status indicates the success of operation.
845+
846+
Call \<firebase data object\>.dataType to determine what type of data that successfully
847+
stores in database.
848+
849+
Call \<firebase data object\>.arrayData will return the JSON array string value of
850+
payload returned from server.
851+
852+
```cpp
853+
bool setArray(FirebaseData &fbdo, const String &path, const String &arrayString);
854+
```
855+
856+
857+
858+
859+
814860

815861
**Set Firebase server's timestamp to the defined database path**
816862

examples/Basic/Basic.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ void setup()
8888

8989
Serial.print("Get int... ");
9090

91-
if (Firebase.getInt(fbdo, path + "/int/data"))
91+
if (Firebase.getInt(fbdo, path + "/int/data")) // also can use Firebase.get(fbdo, path)
9292
{
9393
Serial.println("ok");
9494
Serial.println("path: " + fbdo.dataPath());

keywords.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@ pushFloat KEYWORD2
2222
pushBool KEYWORD2
2323
pushString KEYWORD2
2424
pushJSON KEYWORD2
25+
pushArray KEYWORD2
2526
pushTimestamp KEYWORD2
2627
setInt KEYWORD2
2728
setFloat KEYWORD2
2829
setBool KEYWORD2
2930
setString KEYWORD2
3031
setJSON KEYWORD2
32+
setArray KEYWORD2
3133
setTimestamp KEYWORD2
3234
updateNode KEYWORD2
3335
updateNodeSilent KEYWORD2
@@ -36,6 +38,7 @@ getFloat KEYWORD2
3638
getBool KEYWORD2
3739
getString KEYWORD2
3840
getJSON KEYWORD2
41+
getArray KEYWORD2
3942
deleteNode KEYWORD2
4043
beginStream KEYWORD2
4144
readStream KEYWORD2
@@ -73,6 +76,7 @@ bufferOverflow KEYWORD2
7376
end KEYWORD2
7477
payload KEYWORD2
7578
queryFilter KEYWORD2
79+
get KEYWORD2
7680

7781
###############################################
7882
# Methods for QueryFilter (KEYWORD2)

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name=Firebase Arduino based on WiFiNINA
22

3-
version=1.2.1
3+
version=1.2.2
44

55
author=Mobizt
66

0 commit comments

Comments
 (0)