33#include < WiFiServer.h>
44#include < ESP8266WebServer.h>
55#include < WiFiUdp.h>
6+ #include " StreamString.h"
67#include " ESP8266HTTPUpdateServer.h"
78
89
9- const char * ESP8266HTTPUpdateServer::_serverIndex =
10- R"( <html><body><form method='POST' action='' enctype='multipart/form-data'>
10+ static const char serverIndex[] PROGMEM =
11+ R"( <html><body><form method='POST' action='' enctype='multipart/form-data'>
1112 <input type='file' name='update'>
1213 <input type='submit' value='Update'>
1314 </form>
14- </body></html>)" ;
15- const char * ESP8266HTTPUpdateServer::_failedResponse = R"( Update Failed! )" ;
16- const char * ESP8266HTTPUpdateServer::_successResponse = " <META http-equiv=\" refresh\" content=\" 15;URL=\" >Update Success! Rebooting..." ;
15+ </body></html>\n )" ;
16+ static const char successResponse[] PROGMEM =
17+ " <META http-equiv=\" refresh\" content=\" 15;URL=\" >Update Success! Rebooting...\n " ;
1718
1819ESP8266HTTPUpdateServer::ESP8266HTTPUpdateServer (bool serial_debug)
1920{
@@ -34,20 +35,29 @@ void ESP8266HTTPUpdateServer::setup(ESP8266WebServer *server, const char * path,
3435 _server->on (path, HTTP_GET, [&](){
3536 if (_username != NULL && _password != NULL && !_server->authenticate (_username, _password))
3637 return _server->requestAuthentication ();
37- _server->send (200 , " text/html" , _serverIndex );
38+ _server->send_P (200 , PSTR ( " text/html" ), serverIndex );
3839 });
3940
4041 // handler for the /update form POST (once file upload finishes)
4142 _server->on (path, HTTP_POST, [&](){
4243 if (!_authenticated)
4344 return _server->requestAuthentication ();
44- _server->send (200 , " text/html" , Update.hasError () ? _failedResponse : _successResponse);
45- ESP.restart ();
45+ if (Update.hasError ()) {
46+ _server->send (200 , F (" text/html" ), String (F (" Update error: " )) + _updaterError);
47+ } else {
48+ _server->client ().setNoDelay (true );
49+ _server->send_P (200 , PSTR (" text/html" ), successResponse);
50+ delay (100 );
51+ _server->client ().stop ();
52+ ESP.restart ();
53+ }
4654 },[&](){
4755 // handler for the file upload, get's the sketch bytes, and writes
4856 // them through the Update object
4957 HTTPUpload& upload = _server->upload ();
58+
5059 if (upload.status == UPLOAD_FILE_START){
60+ _updaterError = String ();
5161 if (_serial_output)
5262 Serial.setDebugOutput (true );
5363
@@ -63,19 +73,18 @@ void ESP8266HTTPUpdateServer::setup(ESP8266WebServer *server, const char * path,
6373 Serial.printf (" Update: %s\n " , upload.filename .c_str ());
6474 uint32_t maxSketchSpace = (ESP.getFreeSketchSpace () - 0x1000 ) & 0xFFFFF000 ;
6575 if (!Update.begin (maxSketchSpace)){// start with max available size
66- if (_serial_output) Update. printError (Serial );
76+ _setUpdaterError ( );
6777 }
68- } else if (_authenticated && upload.status == UPLOAD_FILE_WRITE){
78+ } else if (_authenticated && upload.status == UPLOAD_FILE_WRITE && !_updaterError. length () ){
6979 if (_serial_output) Serial.printf (" ." );
7080 if (Update.write (upload.buf , upload.currentSize ) != upload.currentSize ){
71- if (_serial_output) Update.printError (Serial);
72-
81+ _setUpdaterError ();
7382 }
74- } else if (_authenticated && upload.status == UPLOAD_FILE_END){
83+ } else if (_authenticated && upload.status == UPLOAD_FILE_END && !_updaterError. length () ){
7584 if (Update.end (true )){ // true to set the size to the current progress
7685 if (_serial_output) Serial.printf (" Update Success: %u\n Rebooting...\n " , upload.totalSize );
7786 } else {
78- if (_serial_output) Update. printError (Serial );
87+ _setUpdaterError ( );
7988 }
8089 if (_serial_output) Serial.setDebugOutput (false );
8190 } else if (_authenticated && upload.status == UPLOAD_FILE_ABORTED){
@@ -85,3 +94,11 @@ void ESP8266HTTPUpdateServer::setup(ESP8266WebServer *server, const char * path,
8594 delay (0 );
8695 });
8796}
97+
98+ void ESP8266HTTPUpdateServer::_setUpdaterError ()
99+ {
100+ if (_serial_output) Update.printError (Serial);
101+ StreamString str;
102+ Update.printError (str);
103+ _updaterError = str.c_str ();
104+ }
0 commit comments