forked from vitotai/BrewPiLess
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathESP32HTTPUpdateServer.cpp
More file actions
59 lines (54 loc) · 2.25 KB
/
Copy pathESP32HTTPUpdateServer.cpp
File metadata and controls
59 lines (54 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#if ESP32
#include "Config.h"
#include "ESP32HTTPUpdateServer.h"
const char* ESP32HTTPUpdateServer::_username=NULL;
const char* ESP32HTTPUpdateServer::_password=NULL;
static const char* IndexPage = R"END(
<html><body><form method='POST' action='/flash' enctype='multipart/form-data'>
<input type='file' name='update'>
<input type='submit' value='Update'>
</form></body></html>
)END";
void ESP32HTTPUpdateServer::setup(WebServer& server,String servicePath,const char* user,const char* pass) {
_username = user;
_password = pass;
server.on(servicePath, HTTP_GET, [&]() {
if(_username != NULL && _password != NULL && !server.authenticate(_username, _password))
return server.requestAuthentication();
server.sendHeader("Connection", "close");
server.send(200, "text/html", IndexPage);
});
server.on("/flash", HTTP_POST, [&]() {
server.sendHeader("Connection", "close");
if(Update.hasError())
server.send(501, "text/plain", String("Update Failed:") + String(Update.getError()));
else{
server.send(200, "text/plain", "Update Successfull!");
delay(1000);
ESP.restart();
}
}, [&]() {
HTTPUpload& upload = server.upload();
if (upload.status == UPLOAD_FILE_START) {
Serial.printf("Update: %s\n", upload.filename.c_str());
if (!Update.begin(UPDATE_SIZE_UNKNOWN)) { //start with max available size
//Update.printError(Serial);
DBG_PRINTF("Update Error:%d\n",Update.getError());
}
} else if (upload.status == UPLOAD_FILE_WRITE) {
/* flashing firmware to ESP*/
if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) {
//Update.printError(Serial);
DBG_PRINTF("Update Error:%d\n",Update.getError());
}
} 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\nRebooting...\n", upload.totalSize);
} else {
//Update.printError(Serial);
DBG_PRINTF("Update Error:%d\n",Update.getError());
}
}
});
}
#endif