-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathPush.ino
206 lines (160 loc) · 6.86 KB
/
Push.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/**
* ABOUT:
*
* The non-blocking (async) example to push the data to the database.
*
* This example also shows how to use the query to filter your data.
*
* This example uses the UserAuth class for authentication, and the DefaultNetwork class for network interface configuration.
* See examples/App/AppInitialization and examples/App/NetworkInterfaces for more authentication and network examples.
*
* The complete usage guidelines, please read README.md or visit https://github.com/mobizt/FirebaseClient
*
* SYNTAX:
*
* 1.------------------------
*
* RealtimeDatabase::push<T>(<AsyncClient>, <path>, <value>, <AsyncResultCallback>, <uid>);
*
* T - The type of value to push.
* <AsyncClient> - The async client.
* <path> - The node path to push the value.
* <value> - The value to push.
* <AsyncResultCallback> - The async result callback (AsyncResultCallback).
* <uid> - The user specified UID of async result (optional).
*/
#include <Arduino.h>
#if defined(ESP32) || defined(ARDUINO_RASPBERRY_PI_PICO_W) || defined(ARDUINO_GIGA) || defined(ARDUINO_OPTA)
#include <WiFi.h>
#elif defined(ESP8266)
#include <ESP8266WiFi.h>
#elif __has_include(<WiFiNINA.h>) || defined(ARDUINO_NANO_RP2040_CONNECT)
#include <WiFiNINA.h>
#elif __has_include(<WiFi101.h>)
#include <WiFi101.h>
#elif __has_include(<WiFiS3.h>) || defined(ARDUINO_UNOWIFIR4)
#include <WiFiS3.h>
#elif __has_include(<WiFiC3.h>) || defined(ARDUINO_PORTENTA_C33)
#include <WiFiC3.h>
#elif __has_include(<WiFi.h>)
#include <WiFi.h>
#endif
#include <FirebaseClient.h>
#define WIFI_SSID "WIFI_AP"
#define WIFI_PASSWORD "WIFI_PASSWORD"
// The API key can be obtained from Firebase console > Project Overview > Project settings.
#define API_KEY "Web_API_KEY"
// User Email and password that already registerd or added in your project.
#define USER_EMAIL "USER_EMAIL"
#define USER_PASSWORD "USER_PASSWORD"
#define DATABASE_URL "URL"
void asyncCB(AsyncResult &aResult);
void printResult(AsyncResult &aResult);
DefaultNetwork network;
UserAuth user_auth(API_KEY, USER_EMAIL, USER_PASSWORD);
FirebaseApp app;
#if defined(ESP32) || defined(ESP8266) || defined(ARDUINO_RASPBERRY_PI_PICO_W)
#include <WiFiClientSecure.h>
WiFiClientSecure ssl_client;
#elif defined(ARDUINO_ARCH_SAMD) || defined(ARDUINO_UNOWIFIR4) || defined(ARDUINO_GIGA) || defined(ARDUINO_OPTA) || defined(ARDUINO_PORTENTA_C33) || defined(ARDUINO_NANO_RP2040_CONNECT)
#include <WiFiSSLClient.h>
WiFiSSLClient ssl_client;
#endif
using AsyncClient = AsyncClientClass;
AsyncClient aClient(ssl_client, getNetwork(network));
RealtimeDatabase Database;
bool taskComplete = false;
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);
#if defined(ESP32) || defined(ESP8266) || defined(PICO_RP2040)
ssl_client.setInsecure();
#if defined(ESP8266)
ssl_client.setBufferSizes(4096, 1024);
#endif
#endif
Serial.println("Initializing the app...");
initializeApp(aClient, app, getAuth(user_auth), asyncCB, "authTask");
// Binding the FirebaseApp for authentication handler.
// To unbind, use Database.resetApp();
app.getApp<RealtimeDatabase>(Database);
// Set your database URL (requires only for Realtime Database)
Database.url(DATABASE_URL);
}
void loop()
{
// The async task handler should run inside the main loop
// without blocking delay or bypassing with millis code blocks.
app.loop();
Database.loop();
if (app.ready() && !taskComplete)
{
taskComplete = true;
Serial.println("Pushing various values... ");
// Push int
Database.push<int>(aClient, "/test/int", 12345, asyncCB, "pushIntTask");
// Push bool
Database.push<bool>(aClient, "/test/bool", true, asyncCB, "pushBoolTask");
// Push string
Database.push<String>(aClient, "/test/string", "hello", asyncCB, "pushStringTask");
// Push json
Database.push<object_t>(aClient, "/test/json", object_t("{\"data\":123}"), asyncCB, "pushJsonTask1");
// Library does not provide JSON parser library, the following JSON writer class will be used with
// object_t for simple demonstration.
object_t json, obj1, obj2, obj3, obj4;
JsonWriter writer;
writer.create(obj1, "int/value", 9999);
writer.create(obj2, "string/value", string_t("hello"));
writer.create(obj3, "float/value", number_t(123.456, 2));
writer.join(obj4, 3 /* no. of object_t (s) to join */, obj1, obj2, obj3);
writer.create(json, "node/list", obj4);
// To print object_t
// Serial.println(json);
Database.push<object_t>(aClient, "/test/json", json, asyncCB, "pushJsonTask2");
object_t arr;
arr.initArray(); // initialize to be used as array
writer.join(arr, 4 /* no. of object_t (s) to join */, object_t("[12,34]"), object_t("[56,78]"), object_t(string_t("steve")), object_t(888));
// Note that value that sets to object_t other than JSON ({}) and Array ([]) can be valid only if it
// used as array member value as above i.e. object_t(string_t("steve")) and object_t(888).
// Push array
Database.push<object_t>(aClient, "/test/arr", arr, asyncCB, "pushArrayTask");
// Push float
Database.push<number_t>(aClient, "/test/float", number_t(123.456, 2), asyncCB, "pushFloatTask");
// Push double
Database.push<number_t>(aClient, "/test/double", number_t(1234.56789, 4), asyncCB, "pushDoubleTask");
}
}
void asyncCB(AsyncResult &aResult) { printResult(aResult); }
void printResult(AsyncResult &aResult)
{
if (aResult.isEvent())
{
Firebase.printf("Event task: %s, msg: %s, code: %d\n", aResult.uid().c_str(), aResult.appEvent().message().c_str(), aResult.appEvent().code());
}
if (aResult.isDebug())
{
Firebase.printf("Debug task: %s, msg: %s\n", aResult.uid().c_str(), aResult.debug().c_str());
}
if (aResult.isError())
{
Firebase.printf("Error task: %s, msg: %s, code: %d\n", aResult.uid().c_str(), aResult.error().message().c_str(), aResult.error().code());
}
if (aResult.available())
{
if (aResult.to<RealtimeDatabaseResult>().name().length())
Firebase.printf("task: %s, name: %s\n", aResult.uid().c_str(), aResult.to<RealtimeDatabaseResult>().name().c_str());
Firebase.printf("task: %s, payload: %s\n", aResult.uid().c_str(), aResult.c_str());
}
}