Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Added OTA updater option from esp8266 #587

Merged
merged 7 commits into from
Nov 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions WiFiManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,7 @@ void WiFiManager::setupConfigPortal() {
}

/* Setup httpd callbacks, web pages: root, wifi config pages, SO captive portal detectors and not found. */

server->on(String(FPSTR(R_root)).c_str(), std::bind(&WiFiManager::handleRoot, this));
server->on(String(FPSTR(R_wifi)).c_str(), std::bind(&WiFiManager::handleWifi, this, true));
server->on(String(FPSTR(R_wifinoscan)).c_str(), std::bind(&WiFiManager::handleWifi, this, false));
Expand All @@ -483,6 +484,9 @@ void WiFiManager::setupConfigPortal() {
server->on(String(FPSTR(R_status)).c_str(), std::bind(&WiFiManager::handleWiFiStatus, this));
server->onNotFound (std::bind(&WiFiManager::handleNotFound, 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->begin(); // Web server start
DEBUG_WM(DEBUG_VERBOSE,F("HTTP server started"));

Expand Down Expand Up @@ -2693,3 +2697,92 @@ void WiFiManager::WiFi_autoReconnect(){
// }
#endif
}

// Called when /u is requested
void WiFiManager::handleUpdate() {
DEBUG_WM(DEBUG_VERBOSE,F("<- Handle update"));
if (captivePortal()) return; // If captive portal redirect instead of displaying the page
String page = getHTTPHead(FPSTR(S_options)); // @token options
String str = FPSTR(HTTP_ROOT_MAIN);
str.replace(FPSTR(T_v), configPortalActive ? _apName : WiFi.localIP().toString()); // use ip if ap is not active for heading
page += str;

page += FPSTR(HTTP_UPDATE);
page += FPSTR(HTTP_END);

server->sendHeader(FPSTR(HTTP_HEAD_CL), String(page.length()));
server->send(200, FPSTR(HTTP_HEAD_CT), page);

}

void WiFiManager::handleUpdating(){
if (captivePortal()) return; // If captive portal redirect instead of displaying the page
// 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);

#ifdef ESP8266
WiFiUDP::stopAll();
#elif defined(ESP32)
// Think we do not need to stop WiFIUDP because we haven't started a listener
#endif
Serial.printf("Update: %s\r\n", upload.filename.c_str());
#ifdef ESP8266
uint32_t maxSketchSpace = (ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000;
#elif defined(ESP32)
uint32_t maxSketchSpace = (ESP.getFlashChipSize() - 0x1000) & 0xFFFFF000;

#endif

if (!Update.begin(maxSketchSpace)) { // start with max available size
Update.printError(Serial);
}
} else if (upload.status == UPLOAD_FILE_WRITE) {
Serial.print(".");
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("Updated: %u bytes\r\nRebooting...\r\n", upload.totalSize);
} else {
Update.printError(Serial);
}
Serial.setDebugOutput(false);
} else if (upload.status == UPLOAD_FILE_ABORTED) {
Update.end();
DEBUG_WM(F("<- Update was aborted"));
}
delay(0);
} // handleUpdating

void WiFiManager::handleUpdateDone() {
DEBUG_WM(DEBUG_VERBOSE, F("<- Handle update done"));
if (captivePortal()) return; // If captive portal redirect instead of displaying the page

String page = getHTTPHead(FPSTR(S_options)); // @token options
String str = FPSTR(HTTP_ROOT_MAIN);
str.replace(FPSTR(T_v), configPortalActive ? _apName : WiFi.localIP().toString()); // use ip if ap is not active for heading
page += str;

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->sendHeader(FPSTR(HTTP_HEAD_CL), String(page.length()));
server->send(200, FPSTR(HTTP_HEAD_CT), page);

delay(1000); // send page
if (!Update.hasError()) {
ESP.restart();
}
}
6 changes: 6 additions & 0 deletions WiFiManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@

#include <WiFi.h>
#include <esp_wifi.h>
#include <Update.h>

#define WIFI_getChipId() (uint32_t)ESP.getEfuseMac()
#define WM_WIFIOPEN WIFI_AUTH_OPEN
Expand Down Expand Up @@ -408,6 +409,11 @@ class WiFiManager
boolean configPortalHasTimeout();
uint8_t processConfigPortal();
void stopCaptivePortal();
// OTA Update handler
void handleUpdate();
void handleUpdating();
void handleUpdateDone();


// wifi platform abstractions
bool WiFi_Mode(WiFiMode_t m);
Expand Down
9 changes: 9 additions & 0 deletions strings_en.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,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
Expand Down Expand Up @@ -99,6 +100,8 @@ const char HTTP_HELP[] PROGMEM =
"<td>Parameter page</td></tr>"
"<tr><td><a href='/info'>/info</a></td>"
"<td>Information page</td></tr>"
"<tr><td><a href='/u'>/u</a></td>"
"<td>OTA Update</td></tr>"
"<tr><td><a href='/close'>/close</a></td>"
"<td>Close the captiveportal popup,configportal will remain active</td></tr>"
"<tr><td><a href='/exit'>/exit</a></td>"
Expand All @@ -110,6 +113,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) {"
Expand Down Expand Up @@ -236,6 +243,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
Expand Down