Upgrade firmware from web interface

pull/24/head
Łukasz Nidecki 2021-03-01 14:47:34 +01:00
parent cc0866b7e3
commit beff64acd7
8 changed files with 111 additions and 5 deletions

View File

@ -4,8 +4,8 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>TTGO-T-Beam-LoRa-APRS <!--VERSION--></title>
<link rel="stylesheet" href="/style.css" type="text/css">
<script src="/js.js" type="text/javascript"></script>
<link rel="stylesheet" href="style.css" type="text/css">
<script src="js.js" type="text/javascript"></script>
<link rel="icon" href="data:,">
</head>
<body>
@ -127,6 +127,23 @@
</div>
</article>
</section>
<section>
<div class="grid-container full">
<h2 class="u-full-width">Update</h2>
</div>
<article>
<div class="grid-container full">
<form action="/update" method="post" enctype="multipart/form-data" id="upload_form" onsubmit="updateFileUpload(event)">
<div>
<label id="file-input" for="file">Choose file...</label>
<div><progress id="file-progress" max="100"></progress></div>
<input type="file" name="update" id="file" onchange="onFileChange(this)">
<input class="button-primary" type="submit" value="Update">
</div>
</form>
</div>
</article>
</section>
</div>
<footer>
<center><b>Contributors in order of appearance:</b> OE1ACM, OE3CJB, SQ9MDD, SQ5RWU, DJ1AN</center>

View File

@ -39,4 +39,52 @@ window.onload = function () {
};
xhttp.open("GET", "/cfg", true);
xhttp.send();
};
};
function onFileChange(obj){
var fileName = obj.value.split('\\');
document.getElementById('file-input').innerHTML = fileName[fileName.length-1];
};
function updateFileUpload(event) {
event.preventDefault();
const file_input = document.getElementById("file-input");
const file_progress = document.getElementById("file-progress");
const data = new FormData(event.target);
const xhr = new XMLHttpRequest();
file_progress.classList.add("show");
file_progress.value = 0;
xhr.upload.onload = () => {
window.location.reload();
};
// listen for `upload.error` event
xhr.upload.onerror = () => {
alert("Error!");
}
// listen for `upload.abort` event
xhr.upload.onabort = () => {
console.error('Upload cancelled.');
}
xhr.upload.onprogress = (event) => {
// event.loaded returns how many bytes are downloaded
// event.total returns the total number of bytes
// event.total is only available if server sends `Content-Length` header
console.log(`Uploaded ${event.loaded} of ${event.total} bytes`);
let progress = 100 * (event.loaded / event.total);
if (progress == 100){
file_input.innerHTML = "Upgrade in progress. Please wait until page reloads!";
file_progress.removeAttribute('value');
} else {
file_input.innerHTML = "Uploaded: " + Math.round(progress) +"%";
file_progress.value = progress;
}
}
xhr.open('POST', '/update');
xhr.send(data);
}

View File

@ -849,4 +849,13 @@ header {
width: 250px;
height: 228px;
margin: 0 auto;
}
#file-progress {
display: none;
width: 80%;
}
#file-progress.show {
display: inline;
}

View File

@ -4,6 +4,7 @@
#include <WebServer.h>
#include <ESPmDNS.h>
#include <Preferences.h>
#include <Update.h>
#ifndef TASK_WEBSERVER
#define TASK_WEBSERVER

6
partitions.csv 100644
View File

@ -0,0 +1,6 @@
# Name, Type, SubType, Offset, Size, Flags
nvs, data, nvs, 0x9000, 0x5000,
otadata, data, ota, 0xe000, 0x2000,
app0, app, ota_0, 0x10000, 0x1C0000,
app1, app, ota_1, 0x1D0000,0x1C0000,
spiffs, data, spiffs, 0x390000,0x70000,
1 # Name Type SubType Offset Size Flags
2 nvs data nvs 0x9000 0x5000
3 otadata data ota 0xe000 0x2000
4 app0 app ota_0 0x10000 0x1C0000
5 app1 app ota_1 0x1D0000 0x1C0000
6 spiffs data spiffs 0x390000 0x70000

View File

@ -14,7 +14,7 @@ framework = arduino
monitor_speed = 115200
build_flags = -Wl,--gc-sections,--relax
board_build.partitions = no_ota.csv
board_build.partitions = partitions.csv
board_build.embed_files =
data_embed/index.html.out
data_embed/style.css.out

View File

@ -175,6 +175,30 @@ void handle_SaveAPRSCfg() {
server.on("/cfg", handle_Cfg);
server.on("/save_aprs_cfg", handle_SaveAPRSCfg);
server.on("/restore", handle_Restore);
server.on("/update", HTTP_POST, []() {
server.sendHeader("Connection", "close");
server.send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK");
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);
}
} else if (upload.status == UPLOAD_FILE_WRITE) {
/* flashing firmware to ESP*/
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("Update Success: %u\nRebooting...\n", upload.totalSize);
} else {
Update.printError(Serial);
}
}
});
server.onNotFound(handle_NotFound);
String wifi_password = preferences.getString(PREF_WIFI_PASSWORD);
@ -189,6 +213,7 @@ void handle_SaveAPRSCfg() {
Serial.println((int)WiFi.status());
vTaskDelay(500/portTICK_PERIOD_MS);
}
Serial.println("Connected. IP: " + WiFi.localIP().toString());
}
server.begin();

View File

@ -1,6 +1,6 @@
FILENAME_BUILDNO = '.pio/versioning'
FILENAME_VERSION_H = 'include/version.h'
version = 'v0.1.'
version = 'v0.2.'
import datetime