Added OTA updater option from esp8266. Code is manly taken from KoljaWindeler but modified in some pieces to fit current library version

This commit is contained in:
sidey79
2018-04-09 23:08:43 +02:00
parent 82309e7a84
commit 8065a1eaa4
3 changed files with 101 additions and 2 deletions
+86
View File
@@ -400,6 +400,8 @@ void WiFiManager::setupConfigPortal() {
server->on((String)FPSTR(R_info), std::bind(&WiFiManager::handleInfo, this));
server->on((String)FPSTR(R_param), std::bind(&WiFiManager::handleParam, this));
server->on((String)FPSTR(R_paramsave), std::bind(&WiFiManager::handleParamSave, this));
server->on((String)FPSTR(R_update), std::bind(&WiFiManager::handleUpdate, this));
server->on((String)FPSTR(R_updatedone), HTTP_POST, std::bind(&WiFiManager::handleUpdateDone, this), std::bind(&WiFiManager::handleUpdating, this));
server->on((String)FPSTR(R_restart), std::bind(&WiFiManager::handleReset, this));
server->on((String)FPSTR(R_exit), std::bind(&WiFiManager::handleExit, this));
server->on((String)FPSTR(R_close), std::bind(&WiFiManager::handleClose, this));
@@ -2370,3 +2372,87 @@ void WiFiManager::WiFi_autoReconnect(){
}
#endif
}
void WiFiManager::handleUpdate() {
DEBUG_WM(F("Handle update"));
if (captivePortal()) { // If captive portal return flase to redirect back.
return;
}
String page = FPSTR(HTTP_HEAD);
page.replace("{v}", "Options");
page += FPSTR(HTTP_SCRIPT);
page += FPSTR(HTTP_STYLE);
page += _customHeadElement;
page += FPSTR(HTTP_HEAD_END);
page += "<h1>";
page += _apName;
page += "</h1>";
page += FPSTR(HTTP_ROOT_MAIN);
page += FPSTR(HTTP_UPDATE);
page += FPSTR(HTTP_END);
server->send(200, "text/html", page);
}
void WiFiManager::handleUpdating(){
if (captivePortal()) { // If captive portal return flase to redirect back.
return;
}
// handler for the file upload, get's the sketch bytes, and writes
// them through the Update object
HTTPUpload& upload = server->upload();
if (upload.status == UPLOAD_FILE_START) {
Serial.setDebugOutput(true);
WiFiUDP::stopAll();
DEBUG_WM("Update: %s\r\n", upload.filename.c_str());
uint32_t maxSketchSpace = (ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000;
if (!Update.begin(maxSketchSpace)) { // start with max available size
Update.printError(Serial);
}
} else if (upload.status == UPLOAD_FILE_WRITE) {
Serial.printf(".");
if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) {
Update.printError(Serial);
}
} else if (upload.status == UPLOAD_FILE_END) {
if (Update.end(true)) { // true to set the size to the current progress
Serial.printf("Update Success: %u\r\nRebooting...\r\n", upload.totalSize);
} else {
Update.printError(Serial);
}
Serial.setDebugOutput(false);
} else if (upload.status == UPLOAD_FILE_ABORTED) {
Update.end();
DEBUG_WM("Update was aborted");
}
delay(0);
} // handleUpdating
void WiFiManager::handleUpdateDone() {
DEBUG_WM(F("Handle update done"));
if (captivePortal()) { // If captive portal return flase to redirect back.
}
String page = FPSTR(HTTP_HEAD);
page.replace("{v}", "Options");
page += FPSTR(HTTP_SCRIPT);
page += FPSTR(HTTP_STYLE);
page += _customHeadElement;
page += FPSTR(HTTP_HEAD_END);
page += "<h1>";
page += _apName;
page += "</h1>";
page += FPSTR(HTTP_ROOT_MAIN);
if (Update.hasError()) {
page += FPSTR(HTTP_UPDATE_FAIL);
DEBUG_WM(F("update failed"));
}
else {
page += FPSTR(HTTP_UPDATE_OK);
DEBUG_WM(F("update ok"));
}
page += FPSTR(HTTP_END);
server->send(200, "text/html", page);
delay(1000); // send page
ESP.restart();
}
+5
View File
@@ -343,6 +343,11 @@ class WiFiManager
boolean stopConfigPortal();
uint8_t processConfigPortal();
void stopCaptivePortal();
// OTA Update handler
void handleUpdate();
void handleUpdating();
void handleUpdateDone();
// wifi platform abstractions
bool WiFi_Mode(WiFiMode_t m);
+10 -2
View File
@@ -28,6 +28,7 @@ const char * const HTTP_PORTAL_MENU[] PROGMEM = {
"<form action='/info' method='get'><button>Info</button></form><br/>\n", // MENU_INFO
"<form action='/param' method='get'><button>Setup</button></form><br/>\n",//MENU_PARAM
"<form action='/close' method='get'><button>Close</button></form><br/>\n", // MENU_CLOSE
"<form action='/update' method='get'><button>Update</button></form><br/>\n",// MENU_UPDATE
"<form action='/restart' method='get'><button>Restart</button></form><br/>\n",// MENU_RESTART
"<form action='/exit' method='get'><button>Exit</button></form><br/>\n", // MENU_EXIT
"<form action='/erase' method='get'><button class='D'>Erase</button></form><br/>\n", // MENU_ERASE
@@ -110,6 +111,10 @@ const char HTTP_HELP[] PROGMEM =
"</table>"
"<p/>More information about WiFiManager at <a href='https://github.com/tzapu/WiFiManager'>https://github.com/tzapu/WiFiManager</a>.";
const char HTTP_UPDATE[] PROGMEM = "<form method='POST' action='update' enctype='multipart/form-data'><input type='file' name='update'><input type='submit' value='Update'></form>";
const char HTTP_UPDATE_FAIL[] PROGMEM = "Update Failed!";
const char HTTP_UPDATE_OK[] PROGMEM = "Update OK! Rebooting now...";
#ifdef JSTEST
const char HTTP_JS[] PROGMEM =
"<script>function postAjax(url, data, success) {"
@@ -210,14 +215,15 @@ const char D_HR[] PROGMEM = "--------------------";
// -----------------------------------------------------------------------------------------------
// DO NOT EDIT BELOW THIS LINE
const uint8_t _nummenutokens = 9;
const char * const _menutokens[9] PROGMEM = {
const uint8_t _nummenutokens = 10;
const char * const _menutokens[10] PROGMEM = {
"wifi",
"wifinoscan",
"info",
"param",
"close",
"restart",
"update",
"exit",
"erase",
"sep"
@@ -235,6 +241,8 @@ const char R_exit[] PROGMEM = "/exit";
const char R_close[] PROGMEM = "/close";
const char R_erase[] PROGMEM = "/erase";
const char R_status[] PROGMEM = "/status";
const char R_update[] PROGMEM = "/u";
const char R_updatedone[] PROGMEM = "/update";
//Strings