-
-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathUpload.ino
More file actions
155 lines (119 loc) · 5.05 KB
/
Upload.ino
File metadata and controls
155 lines (119 loc) · 5.05 KB
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
/**
* The example to upload object (file) to Storage bucket.
*
* This example uses the UserAuth class for authentication.
* See examples/App/AppInitialization for more authentication examples.
*
* For the complete usage guidelines, please read README.md or visit https://github.com/mobizt/FirebaseClient
*/
#define ENABLE_USER_AUTH
#define ENABLE_STORAGE
#define ENABLE_FS
// You may have to include the storage library you used e.g. SPIFFS.h or SD.h here.
#include <FS.h>
// #include <SPIFFS.h>
// #include <SD.h>
#include <FirebaseClient.h>
#include "ExampleFunctions.h" // Provides the functions used in the examples.
#define WIFI_SSID "WIFI_AP"
#define WIFI_PASSWORD "WIFI_PASSWORD"
#define API_KEY "Web_API_KEY"
#define USER_EMAIL "USER_EMAIL"
#define USER_PASSWORD "USER_PASSWORD"
// Define the Firebase storage bucket ID e.g bucket-name.appspot.com */
#define STORAGE_BUCKET_ID "BUCKET-NAME.appspot.com"
void processData(AsyncResult &aResult);
FileConfig media_file("/media.mp4", file_operation_callback); // Can be set later with media_file.setFile("/media.mp4", file_operation_callback);
UserAuth user_auth(API_KEY, USER_EMAIL, USER_PASSWORD, 3000 /* expire period in seconds (<3600) */);
FirebaseApp app;
SSL_CLIENT ssl_client;
using AsyncClient = AsyncClientClass;
AsyncClient aClient(ssl_client);
Storage storage;
bool taskComplete = false;
AsyncResult storageResult;
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);
set_ssl_client_insecure_and_buffer(ssl_client);
Serial.println("Initializing app...");
initializeApp(aClient, app, getAuth(user_auth), auth_debug_print, "🔐 authTask");
// Or intialize the app and wait.
// initializeApp(aClient, app, getAuth(user_auth), 120 * 1000, auth_debug_print);
app.getApp<Storage>(storage);
#if defined(ENABLE_FS)
MY_FS.begin();
#endif
}
void loop()
{
// To maintain the authentication process.
app.loop();
if (app.ready() && !taskComplete)
{
taskComplete = true;
#if defined(ENABLE_FS)
Serial.println("Uploading file...");
// Async call with callback function.
storage.upload(aClient, FirebaseStorage::Parent(STORAGE_BUCKET_ID, "media.mp4"), getFile(media_file), "video/mp4", processData, "⬆️ uploadTask");
// Async call with AsyncResult for returning result.
// storage.upload(aClient, FirebaseStorage::Parent(STORAGE_BUCKET_ID, "media.mp4"), getFile(media_file), "video/mp4", storageResult);
// Sync call which waits until the operation complete.
// bool status = storage.upload(aClient, FirebaseStorage::Parent(STORAGE_BUCKET_ID, "media.mp4"), getFile(media_file), "video/mp4");
// if (status)
// Serial.println("🔼 Upload task(await), complete!✅️");
// else
// Firebase.printf("Error, msg: %s, code: %d\n", aClient.lastError().message().c_str(), aClient.lastError().code());
#endif
}
// For async call with AsyncResult.
processData(storageResult);
}
void processData(AsyncResult &aResult)
{
// Exits when no result is available when calling from the loop.
if (!aResult.isResult())
return;
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, complete!✅️\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, complete!✅️\n", aResult.uid().c_str());
Serial.print("Download URL: ");
Serial.println(aResult.uploadInfo().downloadUrl);
}
}
}