Skip to content
This repository was archived by the owner on Mar 17, 2025. It is now read-only.

Commit e5ae1f4

Browse files
committed
FirebaseArduino: add typed push and set variant
Fixes #140.
1 parent 7d476b8 commit e5ae1f4

File tree

4 files changed

+154
-17
lines changed

4 files changed

+154
-17
lines changed

examples/FirebaseDemo_ESP8266/FirebaseDemo_ESP8266.ino

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ int n = 0;
4141

4242
void loop() {
4343
// set value
44-
Firebase.set("number", 42.0);
44+
Firebase.setFloat("number", 42.0);
4545
// handle error
4646
if (Firebase.failed()) {
4747
Serial.print("setting /number failed:");
@@ -51,7 +51,7 @@ void loop() {
5151
delay(1000);
5252

5353
// update value
54-
Firebase.set("number", 43.0);
54+
Firebase.setFloat("number", 43.0);
5555
delay(1000);
5656

5757
// get value
@@ -64,14 +64,14 @@ void loop() {
6464
delay(1000);
6565

6666
// set string value
67-
Firebase.set("message", "hello world");
67+
Firebase.setString("message", "hello world");
6868
delay(1000);
6969
// set bool value
70-
Firebase.set("truth", false);
70+
Firebase.setBool("truth", false);
7171
delay(1000);
7272

7373
// append a new value to /logs
74-
String name = Firebase.push("logs", n++);
74+
String name = Firebase.pushInt("logs", n++);
7575
Serial.print("pushed: /logs/");
7676
Serial.println(name);
7777
delay(1000);

examples/FirebaseRoom_ESP8266/FirebaseRoom_ESP8266.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,11 @@ void loop() {
6868
int newButton = digitalRead(buttonPin);
6969
if (newButton != button) {
7070
button = newButton;
71-
Firebase.set("pushbutton", button);
71+
Firebase.setInt("pushbutton", button);
7272
}
7373
float newLight = analogRead(lightSensorPin);
7474
if (abs(newLight - light) > 100) {
7575
light = newLight;
76-
Firebase.set("sunlight", light);
76+
Firebase.setFloat("sunlight", light);
7777
}
7878
}

src/FirebaseArduino.cpp

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,56 @@ void FirebaseArduino::begin(const char* host, const char* auth) {
2323
auth_ = auth;
2424
}
2525

26-
String FirebaseArduino::FirebaseArduino::push(const String& path, const JsonVariant& value) {
26+
String FirebaseArduino::pushInt(const String& path, int value) {
27+
return push(path, value);
28+
}
29+
30+
String FirebaseArduino::pushFloat(const String& path, float value) {
31+
return push(path, value);
32+
}
33+
34+
String FirebaseArduino::pushBool(const String& path, bool value) {
35+
return push(path, value);
36+
}
37+
38+
String FirebaseArduino::pushString(const String& path, const String& value) {
39+
JsonVariant json(value);
40+
return push(path, json);
41+
}
42+
43+
String FirebaseArduino::pushString(const String& path, const char* value) {
44+
return push(path, value);
45+
}
46+
47+
String FirebaseArduino::push(const String& path, const JsonVariant& value) {
2748
String buf;
2849
value.printTo(buf);
2950
auto push = FirebasePush(host_, auth_, path, buf, http_.get());
3051
error_ = push.error();
3152
return push.name();
3253
}
3354

55+
void FirebaseArduino::setInt(const String& path, int value) {
56+
set(path, value);
57+
}
58+
59+
void FirebaseArduino::setFloat(const String& path, float value) {
60+
set(path, value);
61+
}
62+
63+
void FirebaseArduino::setBool(const String& path, bool value) {
64+
set(path, value);
65+
}
66+
67+
void FirebaseArduino::setString(const String& path, const String& value) {
68+
JsonVariant json(value);
69+
set(path, json);
70+
}
71+
72+
void FirebaseArduino::setString(const String& path, const char* value) {
73+
set(path, value);
74+
}
75+
3476
void FirebaseArduino::set(const String& path, const JsonVariant& value) {
3577
String buf;
3678
value.printTo(buf);

src/FirebaseArduino.h

Lines changed: 104 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,36 +23,131 @@
2323
/**
2424
* Main class for Arduino clients to interact with Firebase.
2525
* This implementation is designed to follow Arduino best practices and favor
26-
* simplicity over all else.
27-
* For more complicated usecases and more control see the Firebase class in
26+
* simplicity over all else.
27+
* For more complicated usecases and more control see the Firebase class in
2828
* Firebase.h.
2929
*/
3030
class FirebaseArduino {
3131
public:
3232
/**
33-
* Must be called first. This initialize the client with the given
33+
* Must be called first. This initialize the client with the given
3434
* firebase host and credentials.
3535
* \param host Your firebase db host, usually X.firebaseio.com.
3636
* \param auth Optional credentials for the db, a secret or token.
3737
*/
3838
void begin(const char* host, const char* auth = "");
3939

4040
/**
41-
* Writes data to a new child location under the parent at path.
41+
* Appends the integer value to the node at path.
4242
* Equivalent to the REST API's POST.
4343
* You should check success() after calling.
44-
* \param path The path inside of your db to the parent object.
45-
* \param value Data that you wish to add under the parent.
46-
* \return The unique child key where the data was written.
44+
* \param path The path of the parent node.
45+
* \param value integer value that you wish to append to the node.
46+
* \return The unique key of the new child node.
47+
*/
48+
String pushInt(const String& path, int value);
49+
50+
/**
51+
* Appends the float value to the node at path.
52+
* Equivalent to the REST API's POST.
53+
* You should check success() after calling.
54+
* \param path The path of the parent node.
55+
* \param value float value that you wish to append to the node.
56+
* \return The unique key of the new child node.
57+
*/
58+
String pushFloat(const String& path, float value);
59+
60+
/**
61+
* Appends the bool value to the node at path.
62+
* Equivalent to the REST API's POST.
63+
* You should check success() after calling.
64+
* \param path The path of the parent node.
65+
* \param value bool value that you wish to append to the node.
66+
* \return The unique key of the new child node.
67+
*/
68+
String pushBool(const String& path, bool value);
69+
70+
/**
71+
* Appends the String value to the node at path.
72+
* Equivalent to the REST API's POST.
73+
* You should check success() after calling.
74+
* \param path The path of the parent node.
75+
* \param value String value that you wish to append to the node.
76+
* \return The unique key of the new child node.
77+
*/
78+
String pushString(const String& path, const String& value);
79+
80+
/**
81+
* Appends the String value to the node at path.
82+
* Equivalent to the REST API's POST.
83+
* You should check success() after calling.
84+
* \param path The path of the parent node.
85+
* \param value String value that you wish to append to the node.
86+
* \return The unique key of the new child node.
87+
*/
88+
String pushString(const String& path, const char* value);
89+
90+
/**
91+
* Appends the JSON data to the node at path.
92+
* Equivalent to the REST API's POST.
93+
* You should check success() after calling.
94+
* \param path The path of the parent node.
95+
* \param value JSON data that you wish to append to the node.
96+
* \return The unique key of the new child node.
4797
*/
4898
String push(const String& path, const JsonVariant& value);
4999

50100
/**
51-
* Writes the data in value to the node located at path equivalent to the
101+
* Writes the integer value to the node located at path equivalent to the
102+
* REST API's PUT.
103+
* You should check success() after calling.
104+
* \param path The path inside of your db to the node you wish to update.
105+
* \param value integer value that you wish to write.
106+
*/
107+
void setInt(const String& path, int value);
108+
109+
/**
110+
* Writes the float value to the node located at path equivalent to the
111+
* REST API's PUT.
112+
* You should check success() after calling.
113+
* \param path The path inside of your db to the node you wish to update.
114+
* \param value float value that you wish to write.
115+
*/
116+
void setFloat(const String& path, float value);
117+
118+
/**
119+
* Writes the bool value to the node located at path equivalent to the
52120
* REST API's PUT.
53121
* You should check success() after calling.
54122
* \param path The path inside of your db to the node you wish to update.
55-
* \param value Data that you wish to write.
123+
* \param value float value that you wish to write.
124+
*/
125+
void setBool(const String& path, bool value);
126+
127+
/**
128+
* Writes the String value to the node located at path equivalent to the
129+
* REST API's PUT.
130+
* You should check success() after calling.
131+
* \param path The path inside of your db to the node you wish to update.
132+
* \param value String value that you wish to write.
133+
*/
134+
void setString(const String& path, const String& value);
135+
136+
/**
137+
* Writes the String value to the node located at path equivalent to the
138+
* REST API's PUT.
139+
* You should check success() after calling.
140+
* \param path The path inside of your db to the node you wish to update.
141+
* \param value String value that you wish to write.
142+
*/
143+
void setString(const String& path, const char* value);
144+
145+
/**
146+
* Writes the JSON data to the node located at path.
147+
* Equivalent to the REST API's PUT.
148+
* You should check success() after calling.
149+
* \param path The path inside of your db to the node you wish to update.
150+
* \param value JSON data that you wish to write.
56151
*/
57152
void set(const String& path, const JsonVariant& value);
58153

0 commit comments

Comments
 (0)