3
3
#include < WiFiServer.h>
4
4
#include < ESP8266WebServer.h>
5
5
#include < WiFiUdp.h>
6
+ #include " StreamString.h"
6
7
#include " ESP8266HTTPUpdateServer.h"
7
8
8
9
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'>
11
12
<input type='file' name='update'>
12
13
<input type='submit' value='Update'>
13
14
</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 " ;
17
18
18
19
ESP8266HTTPUpdateServer::ESP8266HTTPUpdateServer (bool serial_debug)
19
20
{
@@ -34,20 +35,29 @@ void ESP8266HTTPUpdateServer::setup(ESP8266WebServer *server, const char * path,
34
35
_server->on (path, HTTP_GET, [&](){
35
36
if (_username != NULL && _password != NULL && !_server->authenticate (_username, _password))
36
37
return _server->requestAuthentication ();
37
- _server->send (200 , " text/html" , _serverIndex );
38
+ _server->send_P (200 , PSTR ( " text/html" ), serverIndex );
38
39
});
39
40
40
41
// handler for the /update form POST (once file upload finishes)
41
42
_server->on (path, HTTP_POST, [&](){
42
43
if (!_authenticated)
43
44
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
+ }
46
54
},[&](){
47
55
// handler for the file upload, get's the sketch bytes, and writes
48
56
// them through the Update object
49
57
HTTPUpload& upload = _server->upload ();
58
+
50
59
if (upload.status == UPLOAD_FILE_START){
60
+ _updaterError = String ();
51
61
if (_serial_output)
52
62
Serial.setDebugOutput (true );
53
63
@@ -63,19 +73,18 @@ void ESP8266HTTPUpdateServer::setup(ESP8266WebServer *server, const char * path,
63
73
Serial.printf (" Update: %s\n " , upload.filename .c_str ());
64
74
uint32_t maxSketchSpace = (ESP.getFreeSketchSpace () - 0x1000 ) & 0xFFFFF000 ;
65
75
if (!Update.begin (maxSketchSpace)){// start with max available size
66
- if (_serial_output) Update. printError (Serial );
76
+ _setUpdaterError ( );
67
77
}
68
- } else if (_authenticated && upload.status == UPLOAD_FILE_WRITE){
78
+ } else if (_authenticated && upload.status == UPLOAD_FILE_WRITE && !_updaterError. length () ){
69
79
if (_serial_output) Serial.printf (" ." );
70
80
if (Update.write (upload.buf , upload.currentSize ) != upload.currentSize ){
71
- if (_serial_output) Update.printError (Serial);
72
-
81
+ _setUpdaterError ();
73
82
}
74
- } else if (_authenticated && upload.status == UPLOAD_FILE_END){
83
+ } else if (_authenticated && upload.status == UPLOAD_FILE_END && !_updaterError. length () ){
75
84
if (Update.end (true )){ // true to set the size to the current progress
76
85
if (_serial_output) Serial.printf (" Update Success: %u\n Rebooting...\n " , upload.totalSize );
77
86
} else {
78
- if (_serial_output) Update. printError (Serial );
87
+ _setUpdaterError ( );
79
88
}
80
89
if (_serial_output) Serial.setDebugOutput (false );
81
90
} else if (_authenticated && upload.status == UPLOAD_FILE_ABORTED){
@@ -85,3 +94,11 @@ void ESP8266HTTPUpdateServer::setup(ESP8266WebServer *server, const char * path,
85
94
delay (0 );
86
95
});
87
96
}
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