Skip to content

Commit

Permalink
use own OTA update
Browse files Browse the repository at this point in the history
  • Loading branch information
zivillian committed Jun 17, 2022
1 parent eb31416 commit 2085b6b
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 3 deletions.
1 change: 1 addition & 0 deletions include/pages.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <ESPAsyncWebServer.h>
#include <ModbusBridgeWiFi.h>
#include <ModbusClientRTU.h>
#include <Update.h>
#include "config.h"

void setupPages(AsyncWebServer* server, ModbusClientRTU *rtu, ModbusBridgeWiFi *bridge, Config *config);
Expand Down
1 change: 0 additions & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,5 @@ framework = arduino
lib_deps =
https://github.com/tzapu/WiFiManager.git
https://github.com/me-no-dev/ESPAsyncWebServer.git
ayushsharma82/AsyncElegantOTA @ 2.2.7
eModbus
;build_flags = -DLOG_LEVEL=LOG_LEVEL_DEBUG
2 changes: 0 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#include <AsyncTCP.h>
#include <WiFiManager.h>
#include <ESPAsyncWebServer.h>
#include <AsyncElegantOTA.h>
#include <Preferences.h>
#include <Logging.h>
#include <ModbusBridgeWiFi.h>
Expand Down Expand Up @@ -38,7 +37,6 @@ void setup() {
MBbridge.start(config.getTcpPort(), 10, 10000);
dbgln("[modbus] finished");
setupPages(&webServer, &MBclient, &MBbridge, &config);
AsyncElegantOTA.begin(&webServer);
webServer.begin();
dbgln("[setup] finished");
}
Expand Down
51 changes: 51 additions & 0 deletions src/pages.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ void setupPages(AsyncWebServer *server, ModbusClientRTU *rtu, ModbusBridgeWiFi *
sendButton(response, "Status", "status");
sendButton(response, "Config", "config");
sendButton(response, "Debug", "debug");
sendButton(response, "Firmware update", "update");
sendButton(response, "Reboot", "reboot", "r");
sendResponseTrailer(response);
request->send(response);
Expand Down Expand Up @@ -185,6 +186,56 @@ void setupPages(AsyncWebServer *server, ModbusClientRTU *rtu, ModbusBridgeWiFi *
sendResponseTrailer(response);
request->send(response);
});
server->on("/update", HTTP_GET, [](AsyncWebServerRequest *request){
dbgln("[webserver] GET /update");
auto *response = request->beginResponseStream("text/html");
sendResponseHeader(response, "Firmware Update");
response->print("<form method=\"post\" enctype=\"multipart/form-data\">"
"<input type=\"file\" name=\"file\" id=\"file\"/>"
"<p></p>"
"<button class=\"r\">Upload</button>"
"</form>"
"<p></p>");
sendButton(response, "Back", "/");
sendResponseTrailer(response);
request->send(response);
});
server->on("/update", HTTP_POST, [](AsyncWebServerRequest *request){
dbgln("[webserver] OTA finished");
if (Update.hasError()){
auto *response = request->beginResponse(500, "text/plain", "Ota failed");
response->addHeader("Connection", "close");
request->send(response);
}
else{
request->redirect("/");
}
ESP.restart();
}, [&](AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final){
dbg("[webserver] OTA progress ");dbgln(index);
if (!index) {
//TODO add MD5 Checksum and Update.setMD5
int cmd = (filename == "filesystem") ? U_SPIFFS : U_FLASH;
if (!Update.begin(UPDATE_SIZE_UNKNOWN, cmd)) { // Start with max available size
Update.printError(Serial);
return request->send(400, "text/plain", "OTA could not begin");
}
}
// Write chunked data to the free sketch space
if(len){
if (Update.write(data, len) != len) {
return request->send(400, "text/plain", "OTA could not write data");
}
}
if (final) { // if the final flag is set then this is the last frame of data
if (!Update.end(true)) { //true to set the size to the current progress
Update.printError(Serial);
return request->send(400, "text/plain", "Could not end OTA");
}
}else{
return;
}
});
server->on("/favicon.ico", [](AsyncWebServerRequest *request){
dbgln("[webserver] GET /favicon.ico");
request->send(204);//TODO add favicon
Expand Down

0 comments on commit 2085b6b

Please sign in to comment.