Skip to content

Commit ee87ac0

Browse files
committed
Add a start callback for HTTP updater
The callback is called when the upgrade actually starts rather than just the initial query so that the user can know that it will not take longer and can also prepare for the upgrade by shutting down other works.
1 parent 633e48f commit ee87ac0

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-2
lines changed

Diff for: libraries/ESP8266httpUpdate/examples/httpUpdate/httpUpdate.ino

+5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717

1818
ESP8266WiFiMulti WiFiMulti;
1919

20+
void update_starting(void) {
21+
Serial.println("Update starting");
22+
}
23+
2024
void setup() {
2125

2226
USE_SERIAL.begin(115200);
@@ -40,6 +44,7 @@ void loop() {
4044
// wait for WiFi connection
4145
if((WiFiMulti.run() == WL_CONNECTED)) {
4246

47+
ESPhttpUpdate.onStart(update_starting);
4348
t_httpUpdate_return ret = ESPhttpUpdate.update("http://server/file.bin");
4449
//t_httpUpdate_return ret = ESPhttpUpdate.update("https://server/file.bin");
4550

Diff for: libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,6 @@ t_httpUpdate_return ESP8266HTTPUpdate::handleUpdate(HTTPClient * http, const cha
173173
// track these headers
174174
http->collectHeaders(headerkeys, headerkeyssize);
175175

176-
177176
int code = http->GET();
178177
int len = http->getSize();
179178

@@ -223,7 +222,7 @@ t_httpUpdate_return ESP8266HTTPUpdate::handleUpdate(HTTPClient * http, const cha
223222
lastError = HTTP_UE_TOO_LESS_SPACE;
224223
ret = HTTP_UPDATE_FAILED;
225224
} else {
226-
225+
http_update_start();
227226
WiFiClient * tcp = http->getStreamPtr();
228227

229228
WiFiUDP::stopAll();

Diff for: libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.h

+4
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@
5252
#define HTTP_UE_BIN_VERIFY_HEADER_FAILED (-106)
5353
#define HTTP_UE_BIN_FOR_WRONG_FLASH (-107)
5454

55+
#define HTTP_UPDATE_START_CALLBACK(callback) void (*callback)()
56+
5557
typedef enum {
5658
HTTP_UPDATE_FAILED,
5759
HTTP_UPDATE_NO_UPDATES,
@@ -71,12 +73,14 @@ class ESP8266HTTPUpdate {
7173

7274
int getLastError(void);
7375
String getLastErrorString(void);
76+
void onStart(HTTP_UPDATE_START_CALLBACK(callback)) { http_update_start = callback; }
7477

7578
protected:
7679
t_httpUpdate_return handleUpdate(HTTPClient * http, const char * current_version, bool reboot = true, bool spiffs = false);
7780
bool runUpdate(Stream& in, uint32_t size, String md5, int command = U_FLASH);
7881

7982
int lastError;
83+
HTTP_UPDATE_START_CALLBACK(http_update_start);
8084
};
8185

8286
extern ESP8266HTTPUpdate ESPhttpUpdate;

0 commit comments

Comments
 (0)