-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
CreateDocument.ino
263 lines (198 loc) · 8.49 KB
/
CreateDocument.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/**
* SYNTAX:
*
* Firestore::Documents::createDocument(<AsyncClient>, <Firestore::Parent>, <documentPath>, <DocumentMask>, <Document>, <AsyncResultCallback>, <uid>);
*
* Firestore::Documents::createDocument(<AsyncClient>, <Firestore::Parent>, <collectionId>, <documentId>, <DocumentMask>, <Document>, <AsyncResultCallback>, <uid>);
*
* <AsyncClient> - The async client.
* <Firestore::Parent> - The Firestore::Parent object included project Id and database Id in its constructor.
* <documentPath> - The relative path of document to create in the collection.
* <DocumentMask> - The fields to return. If not set, returns all fields. Use comma (,) to separate between the field names.
* <collectionId> - The relative path of document collection id to create the document.
* <documentId> - The document id of document to be created.
* <Document> - The Firestore document.
* <AsyncResultCallback> - The async result callback (AsyncResultCallback).
* <uid> - The user specified UID of async result (optional).
*
* The Firebase project Id should be only the name without the firebaseio.com.
* The Firestore database id should be (default) or empty "".
*
* The complete usage guidelines, please visit https://github.com/mobizt/FirebaseClient
*/
#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"
#define FIREBASE_PROJECT_ID "PROJECT_ID"
void asyncCB(AsyncResult &aResult);
void printResult(AsyncResult &aResult);
String getTimestampString(uint64_t sec, uint32_t nano);
DefaultNetwork network; // initilize with boolean parameter to enable/disable network reconnection
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));
Firestore::Documents Docs;
bool taskCompleted = false;
int cnt = 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();
Firebase.printf("Firebase Client v%s\n", FIREBASE_CLIENT_VERSION);
Serial.println("Initializing app...");
#if defined(ESP32) || defined(ESP8266) || defined(PICO_RP2040)
ssl_client.setInsecure();
#if defined(ESP8266)
ssl_client.setBufferSizes(4096, 1024);
#endif
#endif
initializeApp(aClient, app, getAuth(user_auth), asyncCB, "authTask");
// Binding the FirebaseApp for authentication handler.
// To unbind, use Docs.resetApp();
app.getApp<Firestore::Documents>(Docs);
}
void loop()
{
// The async task handler should run inside the main loop
// without blocking delay or bypassing with millis code blocks.
app.loop();
Docs.loop();
if (app.ready() && !taskCompleted)
{
taskCompleted = true;
// Note: If new document created under non-existent ancestor documents, that document will not appear in queries and snapshot
// https://cloud.google.com/firestore/docs/using-console#non-existent_ancestor_documents.
// We will create the document in the parent path "a0/b?
// a0 is the collection id, b? is the document id in collection a0.
String documentPath = "a0/b";
documentPath += cnt;
// If the document path contains space e.g. "a b c/d e f"
// It should encode the space as %20 then the path will be "a%20b%20c/d%20e%20f"
// double (obsoleted)
// Values::DoubleValue dblV(1234.567891);
// double value with precision.
Values::DoubleValue dblV(number_t(1234.567891, 6));
// boolean
Values::BooleanValue bolV(true);
// integer
Values::IntegerValue intV(random(500, 1000));
// null
Values::NullValue nullV;
String doc_path = "projects/";
doc_path += FIREBASE_PROJECT_ID;
doc_path += "/databases/(default)/documents/coll_id/doc_id"; // coll_id and doc_id are your collection id and document id
// reference
Values::ReferenceValue refV(doc_path);
// timestamp
Values::TimestampValue tsV(getTimestampString(1712674441, 999999999));
// bytes
Values::BytesValue bytesV("aGVsbG8=");
// string
Values::StringValue strV("hello");
// array
Values::ArrayValue arrV(Values::StringValue("test"));
arrV.add(Values::IntegerValue(20)).add(Values::BooleanValue(true));
// map
Values::MapValue mapV("name", Values::StringValue("wrench"));
mapV.add("mass", Values::StringValue("1.3kg")).add("count", Values::IntegerValue(3));
// lat long (Obsoleated)
// Values::GeoPointValue geoV(1.486284, 23.678198);
// lat long with precision
Values::GeoPointValue geoV(number_t(1.486284, 6), number_t(23.678198, 6));
Document<Values::Value> doc("myDouble", Values::Value(dblV));
doc.add("myBool", Values::Value(bolV)).add("myInt", Values::Value(intV)).add("myNull", Values::Value(nullV));
doc.add("myRef", Values::Value(refV)).add("myTimestamp", Values::Value(tsV)).add("myBytes", Values::Value(bytesV));
doc.add("myString", Values::Value(strV)).add("myArr", Values::Value(arrV)).add("myMap", Values::Value(mapV));
doc.add("myGeo", Values::Value(geoV));
// The value of Values::xxxValue, Values::Value and Document can be printed on Serial.
Serial.println("Create document... ");
Docs.createDocument(aClient, Firestore::Parent(FIREBASE_PROJECT_ID), documentPath, DocumentMask(), doc, asyncCB, "createDocumentTask");
}
}
void asyncCB(AsyncResult &aResult)
{
// WARNING!
// Do not put your codes inside the callback and printResult.
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())
{
Firebase.printf("task: %s, payload: %s\n", aResult.uid().c_str(), aResult.c_str());
}
}
String getTimestampString(uint64_t sec, uint32_t nano)
{
if (sec > 0x3afff4417f)
sec = 0x3afff4417f;
if (nano > 0x3b9ac9ff)
nano = 0x3b9ac9ff;
time_t now;
struct tm ts;
char buf[80];
now = sec;
ts = *localtime(&now);
String format = "%Y-%m-%dT%H:%M:%S";
if (nano > 0)
{
String fraction = String(double(nano) / 1000000000.0f, 9);
fraction.remove(0, 1);
format += fraction;
}
format += "Z";
strftime(buf, sizeof(buf), format.c_str(), &ts);
return buf;
}