-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Upload.ino
277 lines (228 loc) · 9.55 KB
/
Upload.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/**
* SYNTAX:
*
* FileConfig::FileConfig(<file_name>, <file_callback>);
*
* <file_name> - The filename included path of file that will be used.
* <file_callback> - The callback function that provides file operation.
*
* The file_callback function parameters included the File reference returned from file operation, filename for file operation and file_operating_mode.
* The file_operating_mode included file_mode_open_read, file_mode_open_write, file_mode_open_append and file_mode_open_remove.
*
* The file name can be a name of source (input) and target (output) file that used in upload and download.
*
* SYNTAX:
*
* CloudStorage::upload(<AsyncClient>, <FirebaseStorage::Parent>, <file_config_data>, <GoogleCloudStorage::uploadOptions>, <AsyncResultCallback>, <uid>);
*
* <AsyncClient> - The async client.
* <GoogleCloudStorage::Parent> - The GoogleCloudStorage::Parent object included Storage bucket Id and object in its constructor.
* <file_config_data> - The filesystem data (file_config_data) obtained from FileConfig class object.
* <GoogleCloudStorage::uploadOptions> - The GoogleCloudStorage::uploadOptions that holds the information for insert options, properties and types of upload.
* For the insert options (options.insertOptions), see https://cloud.google.com/storage/docs/json_api/v1/objects/insert#optional-parameters
* For insert properties (options.insertProps), see https://cloud.google.com/storage/docs/json_api/v1/objects/insert#optional-properties
* <AsyncResultCallback> - The async result callback (AsyncResultCallback).
* <uid> - The user specified UID of async result (optional).
*
* The bucketid is the Storage bucket Id of object to upload.
* The object is the object to be stored in the Storage bucket.
*
* 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
// In ESP32 Core SDK v3.x.x, to use filesystem in this library,
// the File object should be defined globally
// and the library's internal defined FS object should be set with
// this global FS object in fileCallback function.
#include <FS.h>
File myFile;
#if defined(ESP32)
#include <SPIFFS.h>
#endif
#define MY_FS SPIFFS
#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"
/**
* This information can be taken from the service account JSON file.
*
* To download service account file, from the Firebase console, goto project settings,
* select "Service accounts" tab and click at "Generate new private key" button
*/
#define FIREBASE_PROJECT_ID "PROJECT_ID"
#define FIREBASE_CLIENT_EMAIL "CLIENT_EMAIL"
const char PRIVATE_KEY[] PROGMEM = "-----BEGIN PRIVATE KEY-----XXXXXXXXXXXX-----END PRIVATE KEY-----\n";
// Define the Firebase storage bucket ID e.g bucket-name.appspot.com */
#define STORAGE_BUCKET_ID "BUCKET-NAME.appspot.com"
void timeStatusCB(uint32_t &ts);
void asyncCB(AsyncResult &aResult);
void printResult(AsyncResult &aResult);
#if defined(ENABLE_FS)
void fileCallback(File &file, const char *filename, file_operating_mode mode);
FileConfig media_file("/media.mp4", fileCallback); // Can be set later with media_file.setFile("/media.mp4", fileCallback);
#endif
DefaultNetwork network; // initilize with boolean parameter to enable/disable network reconnection
// ServiceAuth is required for Google Cloud Storage functions.
ServiceAuth sa_auth(timeStatusCB, FIREBASE_CLIENT_EMAIL, FIREBASE_PROJECT_ID, PRIVATE_KEY, 3000 /* expire period in seconds (<= 3600) */);
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));
CloudStorage cstorage;
bool taskCompleted = 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);
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(sa_auth), asyncCB, "authTask");
// Binding the FirebaseApp for authentication handler.
// To unbind, use cstorage.resetApp();
app.getApp<CloudStorage>(cstorage);
#if defined(ENABLE_FS)
MY_FS.begin();
#endif
}
void loop()
{
// The JWT token processor required for ServiceAuth and CustomAuth authentications.
// JWT is a static object of JWTClass and it's not thread safe.
// In multi-threaded operations (multi-FirebaseApp), you have to define JWTClass for each FirebaseApp,
// and set it to the FirebaseApp via FirebaseApp::setJWTProcessor(<JWTClass>), before calling initializeApp.
JWT.loop(app.getAuth());
app.loop();
cstorage.loop();
if (app.ready() && !taskCompleted)
{
taskCompleted = true;
Serial.println("Upload file...");
GoogleCloudStorage::uploadOptions options;
options.mime = "video/mp4";
options.uploadType = GoogleCloudStorage::upload_type_resumable;
// options.uploadType = GoogleCloudStorage::upload_type_simple;
#if defined(ENABLE_FS)
cstorage.upload(aClient, GoogleCloudStorage::Parent(STORAGE_BUCKET_ID, "media.mp4"), getFile(media_file), options, asyncCB, "uploadTask");
#endif
}
}
void timeStatusCB(uint32_t &ts)
{
#if defined(ESP8266) || defined(ESP32) || defined(CORE_ARDUINO_PICO)
if (time(nullptr) < FIREBASE_DEFAULT_TS)
{
configTime(3 * 3600, 0, "pool.ntp.org");
while (time(nullptr) < FIREBASE_DEFAULT_TS)
{
delay(100);
}
}
ts = time(nullptr);
#elif __has_include(<WiFiNINA.h>) || __has_include(<WiFi101.h>)
ts = WiFi.getTime();
#endif
}
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.downloadProgress())
{
Firebase.printf("Downloaded, task: %s, %d%s (%d of %d)\n", aResult.uid().c_str(), aResult.downloadInfo().progress, "%", aResult.downloadInfo().downloaded, aResult.downloadInfo().total);
if (aResult.downloadInfo().total == aResult.downloadInfo().downloaded)
{
Firebase.printf("Download task: %s, completed!\n", aResult.uid().c_str());
}
}
if (aResult.uploadProgress())
{
Firebase.printf("Uploaded, task: %s, %d%s (%d of %d)\n", aResult.uid().c_str(), aResult.uploadInfo().progress, "%", aResult.uploadInfo().uploaded, aResult.uploadInfo().total);
if (aResult.uploadInfo().total == aResult.uploadInfo().uploaded)
{
Firebase.printf("Upload task: %s, completed!\n", aResult.uid().c_str());
Serial.print("Download URL: ");
Serial.println(aResult.uploadInfo().downloadUrl);
}
}
}
#if defined(ENABLE_FS)
void fileCallback(File &file, const char *filename, file_operating_mode mode)
{
// FILE_OPEN_MODE_READ, FILE_OPEN_MODE_WRITE and FILE_OPEN_MODE_APPEND are defined in this library
// MY_FS is defined in this example
switch (mode)
{
case file_mode_open_read:
myFile = MY_FS.open(filename, FILE_OPEN_MODE_READ);
break;
case file_mode_open_write:
myFile = MY_FS.open(filename, FILE_OPEN_MODE_WRITE);
break;
case file_mode_open_append:
myFile = MY_FS.open(filename, FILE_OPEN_MODE_APPEND);
break;
case file_mode_remove:
MY_FS.remove(filename);
break;
default:
break;
}
// Set the internal FS object with global File object.
file = myFile;
}
#endif