Skip to content

Commit 3eff570

Browse files
committed
OTA fix for ESP32
Signed-off-by: Emil Muratov <gpm@hotplug.ru>
1 parent a7420ee commit 3eff570

File tree

4 files changed

+47
-24
lines changed

4 files changed

+47
-24
lines changed

EmbUI/EmbUI.cpp

+35-21
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,17 @@
1414
#define PUB_PERIOD 10000 // Publication period, ms
1515
#define SECONDARY_PERIOD 300U // second handler timer, ms
1616

17+
// Update defs
18+
#define FW_MAGIC 0xe9
19+
#ifdef ESP32
20+
#define U_FS U_SPIFFS
21+
#endif
22+
1723
EmbUI embui;
1824

1925
void section_main_frame(Interface *interf, JsonObject *data) {}
2026
void pubCallback(Interface *interf){}
2127
String httpCallback(const String &param, const String &value, bool isSet) { return String(); }
22-
void uploadProgress(size_t len, size_t total){
23-
static int prev = 0;
24-
float part = total / 50.0;
25-
int curr = len / part;
26-
if (curr != prev) {
27-
prev = curr;
28-
for (int i = 0; i < curr; i++){
29-
LOG(print, "=");
30-
}
31-
LOG(print, "\n");
32-
}
33-
}
3428

3529
void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type, void *arg, uint8_t *data, size_t len){
3630
if(type == WS_EVT_CONNECT){
@@ -291,8 +285,7 @@ void EmbUI::begin(){
291285
request->send(200, FPSTR(PGmimetxt), out);
292286
});
293287

294-
#ifndef ESP32
295-
// Simple Firmware Update Form (ESP32 ota broken)
288+
// Simple Firmware Update Form
296289
server.on(PSTR("/update"), HTTP_GET, [](AsyncWebServerRequest *request){
297290
request->send(200, FPSTR(PGmimehtml), F("<form method='POST' action='/update' enctype='multipart/form-data'><input type='file' name='update'><input type='submit' value='Update'></form>"));
298291
});
@@ -308,10 +301,17 @@ void EmbUI::begin(){
308301
}
309302
},[](AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final){
310303
if (!index) {
311-
Update.runAsync(true);
312-
int type = (data[0] == 0xe9 || data[0] == 0x1f)? U_FLASH : U_FS;
313-
size_t size = (type == U_FLASH)? ((ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000) : (uintptr_t)&_FS_end - (uintptr_t)&_FS_start;
314-
LOG(printf_P, PSTR("Update %s Start (%u)\n"), (type == U_FLASH)? F("Firmware") : F("Filesystem"), request->contentLength());
304+
int type = (data[0] == FW_MAGIC)? U_FLASH : U_FS;
305+
306+
#ifdef ESP8266
307+
Update.runAsync(true);
308+
// TODO: разобраться почему под littlefs образ генерится чуть больше чем размер доступной памяти по константам
309+
size_t size = (type == U_FLASH)? request->contentLength() : (uintptr_t)&_FS_end - (uintptr_t)&_FS_start;
310+
#endif
311+
#ifdef ESP32
312+
size_t size = (type == U_FLASH)? request->contentLength() : UPDATE_SIZE_UNKNOWN;
313+
#endif
314+
LOG(printf_P, PSTR("Updating %s, file size:%u\n"), (type == U_FLASH)? F("Firmware") : F("Filesystem"), request->contentLength());
315315

316316
if (!Update.begin(size, type)) {
317317
Update.printError(Serial);
@@ -329,9 +329,8 @@ void EmbUI::begin(){
329329
Update.printError(Serial);
330330
}
331331
}
332-
uploadProgress(index + len, request->contentLength());
332+
embui.uploadProgress(index + len, request->contentLength());
333333
});
334-
#endif
335334

336335
//First request will return 0 results unless you start scan from somewhere else (loop/setup)
337336
//Do not request more often than 3-5 seconds
@@ -429,4 +428,19 @@ void EmbUI::set_callback(CallBack set, CallBack action, callback_function_t call
429428
default:
430429
return;
431430
}
432-
};
431+
};
432+
433+
/*
434+
* OTA update progress
435+
*/
436+
uint8_t EmbUI::uploadProgress(size_t len, size_t total){
437+
static int prev = 0;
438+
float part = total / 25.0; // logger chunks
439+
int curr = len / part;
440+
uint8_t progress = 100*len/total;
441+
if (curr != prev) {
442+
prev = curr;
443+
LOG(printf_P, PSTR("%u%%.."), progress );
444+
}
445+
return progress;
446+
}

EmbUI/EmbUI.h

+10-1
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,22 @@
2727
#include <ArduinoJson.h>
2828

2929
#ifdef ESP8266
30+
#include <Updater.h>
3031
#include <ESP8266mDNS.h> // Include the mDNS library
3132
#ifdef USE_SSDP
3233
#include <ESP8266SSDP.h>
3334
#endif
34-
#else
35+
#endif
36+
37+
#ifdef ESP32
3538
#include <ESPmDNS.h>
3639
#include <Update.h>
3740
#ifdef USE_SSDP
3841
#include <ESP32SSDP.h>
3942
#endif
4043
#endif
4144

45+
4246
#include <Ticker.h> // esp планировщик
4347

4448
#include <AsyncMqttClient.h>
@@ -305,6 +309,11 @@ class EmbUI
305309
callback_function_t _cb_STADisconnected = nullptr;
306310
callback_function_t _cb_STAGotIP = nullptr;
307311

312+
/*
313+
* OTA update progress, return upload %
314+
*/
315+
uint8_t uploadProgress(size_t len, size_t total);
316+
308317
#ifdef USE_SSDP
309318
void ssdp_begin() {
310319
String hn = param(FPSTR(P_hostname));

examples/ex_generic/src/main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// MAIN Setup
99
void setup() {
1010
Serial.begin(BAUD_RATE);
11-
Serial.print("Starting test...");
11+
Serial.println("Starting test...");
1212

1313
// Start EmbUI framework
1414
embui.begin();

examples/ex_generic/src/uistrings.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ static const char T_SETTINGS[] PROGMEM = "settings";
1515
static const char T_OPT_NETW[] PROGMEM = "networking";
1616

1717
// UI handlers
18-
static const char T_DO_OTAUPD[] PROGMEM = "otaupd";
18+
static const char T_DO_OTAUPD[] PROGMEM = "update";
1919
static const char T_SET_WIFI[] PROGMEM = "set_wifi";
2020
static const char T_SET_WIFIAP[] PROGMEM = "set_wifiAP";
2121
static const char T_SET_MQTT[] PROGMEM = "set_mqtt";

0 commit comments

Comments
 (0)