parent
b3db252acb
commit
36b7b2c0a5
40
README.md
40
README.md
|
@ -789,6 +789,9 @@ const char* password = "your-pass";
|
||||||
const char* http_username = "admin";
|
const char* http_username = "admin";
|
||||||
const char* http_password = "admin";
|
const char* http_password = "admin";
|
||||||
|
|
||||||
|
//flag to use from web update to reboot the ESP
|
||||||
|
bool shouldReboot = false;
|
||||||
|
|
||||||
void onRequest(AsyncWebServerRequest *request){
|
void onRequest(AsyncWebServerRequest *request){
|
||||||
//Handle Unknown Request
|
//Handle Unknown Request
|
||||||
request->send(404);
|
request->send(404);
|
||||||
|
@ -830,7 +833,7 @@ void setup(){
|
||||||
// upload a file to /upload
|
// upload a file to /upload
|
||||||
server.on("/upload", HTTP_POST, [](AsyncWebServerRequest *request){
|
server.on("/upload", HTTP_POST, [](AsyncWebServerRequest *request){
|
||||||
request->send(200);
|
request->send(200);
|
||||||
}, handleUpload);
|
}, onUpload);
|
||||||
|
|
||||||
// send a file when /index is requested
|
// send a file when /index is requested
|
||||||
server.on("/index", HTTP_ANY, [](AsyncWebServerRequest *request){
|
server.on("/index", HTTP_ANY, [](AsyncWebServerRequest *request){
|
||||||
|
@ -844,6 +847,37 @@ void setup(){
|
||||||
request->send(200, "text/plain", "Login Success!");
|
request->send(200, "text/plain", "Login Success!");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Simple Firmware Update Form
|
||||||
|
server.on("/update", HTTP_GET, [](AsyncWebServerRequest *request){
|
||||||
|
request->send(200, "text/html", "<form method='POST' action='/update' enctype='multipart/form-data'><input type='file' name='update'><input type='submit' value='Update'></form>");
|
||||||
|
});
|
||||||
|
server.on("/update", HTTP_POST, [](AsyncWebServerRequest *request){
|
||||||
|
shouldReboot = !Update.hasError();
|
||||||
|
AsyncWebServerResponse *response = request->beginResponse(200, "text/plain", shouldReboot?"OK":"FAIL");
|
||||||
|
response->addHeader("Connection", "close");
|
||||||
|
request->send(response);
|
||||||
|
},[](AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final){
|
||||||
|
if(!index){
|
||||||
|
Serial.printf("Update Start: %s\n", filename.c_str());
|
||||||
|
Update.runAsync(true);
|
||||||
|
if(!Update.begin((ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000)){
|
||||||
|
Update.printError(Serial);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(!Update.hasError()){
|
||||||
|
if(Update.write(data, len) != len){
|
||||||
|
Update.printError(Serial);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(final){
|
||||||
|
if(Update.end(true)){
|
||||||
|
Serial.printf("Update Success: %uB\n", index+len);
|
||||||
|
} else {
|
||||||
|
Update.printError(Serial);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
// attach filesystem root at URL /fs
|
// attach filesystem root at URL /fs
|
||||||
server.serveStatic("/fs", SPIFFS, "/");
|
server.serveStatic("/fs", SPIFFS, "/");
|
||||||
|
|
||||||
|
@ -858,6 +892,10 @@ void setup(){
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop(){
|
void loop(){
|
||||||
|
if(shouldReboot){
|
||||||
|
Serial.println("Rebooting...");
|
||||||
|
ESP.restart();
|
||||||
|
}
|
||||||
static char temp[128];
|
static char temp[128];
|
||||||
sprintf(temp, "Seconds since boot: %u", millis()/1000);
|
sprintf(temp, "Seconds since boot: %u", millis()/1000);
|
||||||
events.send(temp, "time"); //send event "time"
|
events.send(temp, "time"); //send event "time"
|
||||||
|
|
Loading…
Reference in New Issue