From 8065a1eaa4cb2581acf37d820dc6ac8e7fcc86c6 Mon Sep 17 00:00:00 2001
From: sidey79 <7968127+sidey79@users.noreply.github.com>
Date: Mon, 9 Apr 2018 23:08:43 +0200
Subject: [PATCH 1/5] Added OTA updater option from esp8266. Code is manly
taken from KoljaWindeler but modified in some pieces to fit current library
version
---
WiFiManager.cpp | 86 +++++++++++++++++++++++++++++++++++++++++++++++++
WiFiManager.h | 5 +++
strings_en.h | 12 +++++--
3 files changed, 101 insertions(+), 2 deletions(-)
diff --git a/WiFiManager.cpp b/WiFiManager.cpp
index aabbe8d..91134f7 100644
--- a/WiFiManager.cpp
+++ b/WiFiManager.cpp
@@ -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 += "
";
+ page += _apName;
+ page += "
";
+ 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 += "";
+ page += _apName;
+ page += "
";
+ 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();
+}
diff --git a/WiFiManager.h b/WiFiManager.h
index 6763833..ab98252 100644
--- a/WiFiManager.h
+++ b/WiFiManager.h
@@ -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);
diff --git a/strings_en.h b/strings_en.h
index 2cb9f7a..8dbe91a 100644
--- a/strings_en.h
+++ b/strings_en.h
@@ -28,6 +28,7 @@ const char * const HTTP_PORTAL_MENU[] PROGMEM = {
"
\n", // MENU_INFO
"
\n",//MENU_PARAM
"
\n", // MENU_CLOSE
+"
\n",// MENU_UPDATE
"
\n",// MENU_RESTART
"
\n", // MENU_EXIT
"
\n", // MENU_ERASE
@@ -110,6 +111,10 @@ const char HTTP_HELP[] PROGMEM =
""
"More information about WiFiManager at https://github.com/tzapu/WiFiManager.";
+const char HTTP_UPDATE[] PROGMEM = "";
+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 =
"