-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathDownloadFileOTA.ino
163 lines (134 loc) · 5.53 KB
/
DownloadFileOTA.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
/**
* Created by K. Suwatchai (Mobizt)
*
* Email: [email protected]
*
* Github: https://github.com/mobizt/Firebase-ESP-Client
*
* Copyright (c) 2023 mobizt
*
*/
// This example shows how to update firmware (bin file) from Google Cloud Storage bucket.
#include <Arduino.h>
#if defined(ESP32) || defined(ARDUINO_RASPBERRY_PI_PICO_W)
#include <WiFi.h>
#elif defined(ESP8266)
#include <ESP8266WiFi.h>
#elif __has_include(<WiFiNINA.h>)
#include <WiFiNINA.h>
#elif __has_include(<WiFi101.h>)
#include <WiFi101.h>
#elif __has_include(<WiFiS3.h>)
#include <WiFiS3.h>
#endif
#include <Firebase_ESP_Client.h>
// Provide the token generation process info.
#include <addons/TokenHelper.h>
/* 1. Define the WiFi credentials */
#define WIFI_SSID "WIFI_AP"
#define WIFI_PASSWORD "WIFI_PASSWORD"
/* 2. Define the Firebase storage bucket ID e.g bucket-name.appspot.com or Google Cloud Storage bucket name */
#define STORAGE_BUCKET_ID "BUCKET-NAME.appspot.com"
/* 3 The following Service Account credentials required for OAuth2.0 authen in Google Cloud Storage JSON API download */
#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 Firebase Data object
FirebaseData fbdo;
FirebaseAuth auth;
FirebaseConfig config;
bool taskCompleted = false;
#if defined(ARDUINO_RASPBERRY_PI_PICO_W)
WiFiMulti multi;
#endif
void setup()
{
Serial.begin(115200);
#if defined(ARDUINO_RASPBERRY_PI_PICO_W)
multi.addAP(WIFI_SSID, WIFI_PASSWORD);
multi.run();
#else
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
#endif
Serial.print("Connecting to Wi-Fi");
unsigned long ms = millis();
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(300);
#if defined(ARDUINO_RASPBERRY_PI_PICO_W)
if (millis() - ms > 10000)
break;
#endif
}
Serial.println();
Serial.print("Connected with IP: ");
Serial.println(WiFi.localIP());
Serial.println();
Serial.printf("Firebase Client v%s\n\n", FIREBASE_CLIENT_VERSION);
/* Assign the Service Account credentials for OAuth2.0 authen */
config.service_account.data.client_email = FIREBASE_CLIENT_EMAIL;
config.service_account.data.project_id = FIREBASE_PROJECT_ID;
config.service_account.data.private_key = PRIVATE_KEY;
// The WiFi credentials are required for Pico W
// due to it does not have reconnect feature.
#if defined(ARDUINO_RASPBERRY_PI_PICO_W)
config.wifi.clearAP();
config.wifi.addAP(WIFI_SSID, WIFI_PASSWORD);
#endif
/* Assign the callback function for the long running token generation task */
config.token_status_callback = tokenStatusCallback; // see addons/TokenHelper.h
// Comment or pass false value when WiFi reconnection will control by your code or third party library e.g. WiFiManager
Firebase.reconnectNetwork(true);
// Since v4.4.x, BearSSL engine was used, the SSL buffer need to be set.
// Large data transmission may require larger RX buffer, otherwise the connection issue or data read time out can be occurred.
// For OTA file download in this example, the RX size will set to 16k during OTA file download and turns to
// the size that set by setBSSLBufferSize.
fbdo.setBSSLBufferSize(4096 /* Rx buffer size in bytes from 512 - 16384 */, 1024 /* Tx buffer size in bytes from 512 - 16384 */);
/* Assign download buffer size in byte */
// Data to be downloaded will read as multiple chunks with this size, to compromise between speed and memory used for buffering.
config.gcs.download_buffer_size = 2048;
Firebase.begin(&config, &auth);
}
// The Google Cloud Storage download callback function
void gcsDownloadCallback(DownloadStatusInfo info)
{
if (info.status == firebase_gcs_download_status_init)
{
Serial.printf("Downloading firmware %s (%d)\n", info.remoteFileName.c_str(), info.fileSize);
}
else if (info.status == firebase_gcs_download_status_download)
{
Serial.printf("Downloaded %d%s, Elapsed time %d ms\n", (int)info.progress, "%", info.elapsedTime);
}
else if (info.status == firebase_gcs_download_status_complete)
{
Serial.println("Update firmware completed.");
Serial.println();
Serial.println("Restarting...\n\n");
delay(2000);
#if defined(ESP32) || defined(ESP8266)
ESP.restart();
#elif defined(ARDUINO_RASPBERRY_PI_PICO_W)
rp2040.restart();
#endif
}
else if (info.status == firebase_gcs_download_status_error)
{
Serial.printf("Download firmware failed, %s\n", info.errorMsg.c_str());
}
}
void loop()
{
// Firebase.ready() should be called repeatedly to handle authentication tasks.
if (Firebase.ready() && !taskCompleted)
{
taskCompleted = true;
Serial.println("\nDownload firmware file OTA with Google Cloud Storage JSON API...\n");
// This function will allocate 16k+ memory for internal SSL client.
// In Pico, the free space of device should be larger than the firmware file size.
// You can upload blank filesystem image to clear the space.
if (!Firebase.GCStorage.downloadOTA(&fbdo, STORAGE_BUCKET_ID /* Firebase Storage bucket id */, "<firmware.bin>" /* path of firmware file stored in the bucket */, gcsDownloadCallback /* callback function */))
Serial.println(fbdo.errorReason());
}
}