Skip to content

Commit 215de04

Browse files
committed
Upload de firmware + webserveur ok pour ESP32 et ESP8266
1 parent 942e9d4 commit 215de04

File tree

6 files changed

+142
-55
lines changed

6 files changed

+142
-55
lines changed

lightkit/software/data/firmware.html

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,28 @@ <h1 id="title">Lightkit</h1>
1616
<h2>Firmware</h2>
1717
<p>
1818
Version actuelle:<br>
19-
<span id="current_version">-<!-- Filled by JS --></span>
19+
<span id="firmware_Version">-<!-- Filled by JS --></span>
2020
</p>
2121
<form method='POST' id="firmware_file_form">
2222
<input type='file' name='firmware_file' id='firmware_file'>
2323
<button type='submit' class="theme">Mettre à jour</button>
24-
<div id='progress_bar_container'>
24+
<div id='firmware_progress_bar'>
25+
<span id="progress_bar_label"></span>
26+
<div id='progress_bar'></div>
27+
</div>
28+
</form>
29+
<a href="index.html" class="theme">< Retour</a>
30+
</div>
31+
<div id="part_select_file">
32+
<h2>Web Serveur</h2>
33+
<p>
34+
Version actuelle:<br>
35+
<span id="webserver_version">-<!-- Filled by JS --></span>
36+
</p>
37+
<form method='POST' id="webserver_file_form">
38+
<input type='file' name='webserver_file' id='webserver_file'>
39+
<button type='submit' class="theme">Mettre à jour</button>
40+
<div id='webserver_progress_bar'>
2541
<span id="progress_bar_label"></span>
2642
<div id='progress_bar'></div>
2743
</div>
@@ -50,11 +66,11 @@ <h2>Firmware</h2>
5066
xhttp.send();
5167
}
5268

53-
function upload_file(file) {
69+
function upload_file(location, file) {
5470
let xhr = new XMLHttpRequest();
5571
var formData = new FormData();
5672

57-
formData.append("update", file);
73+
formData.append(location, file);
5874

5975
set_message("ok", "Envoi en cours...");
6076

@@ -81,37 +97,43 @@ <h2>Firmware</h2>
8197
set_message("error", "Erreur lors de l'envoi: code=" + xhr.responseText);
8298
}
8399
};
84-
xhr.open("POST", "/firmware_upload");
100+
xhr.open("POST", "/update");
85101
xhr.send(formData);
86102
}
87103

88-
/**
89-
* Start function to load the page
90-
*/
91-
function page_start()
92-
{
93-
const form = document.querySelector('#firmware_file_form');
94-
95-
// Hide the box
96-
set_message("hide");
104+
function add_file_listener(selector, location) {
105+
const form = document.querySelector(selector + '_form');
97106

98-
// Handle form validation
99107
form.addEventListener('submit', (e) => {
100108
e.preventDefault();
101109

102-
const files = document.querySelector('[type=file]').files;
110+
const files = document.querySelector(selector).files;
103111

104112
if (files.length != 1) {
105113
set_message("error", "Veillez selectionner un fichier");
106114
return;
107115
}
108116

109117
// Do the actual upload
110-
upload_file(files[0]);
118+
upload_file(location, files[0]);
111119
});
120+
}
121+
122+
/**
123+
* Start function to load the page
124+
*/
125+
function page_start()
126+
{
127+
const form = document.querySelector('#firmware_file_form');
128+
129+
// Hide the box
130+
set_message("hide");
131+
132+
add_file_listener('#firmware_file', 'firmware');
133+
add_file_listener('#webserver_file', 'filesystem');
112134

113135
cgi_request('/get_version', '', function(textReply) {
114-
document.getElementById('current_version').innerHTML = textReply;
136+
document.getElementById('firmware_Version').innerHTML = textReply;
115137
});
116138
}
117139
</script>

lightkit/software/src/file_sys/file_sys.cpp

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,6 @@
66
*/
77

88
#include "file_sys.hpp"
9-
#include "global.hpp"
10-
11-
#ifdef ESP32
12-
#include "SPIFFS.h"
13-
#else
14-
#include "LittleFS.h"
15-
#endif
169

1710
int file_sys_init(void)
1811
{
@@ -37,6 +30,25 @@ int file_sys_init(void)
3730
return -1;
3831
}
3932

33+
uint32_t file_sys_get_max_size(void)
34+
{
35+
#if defined(FS_IS_LITTLEFS)
36+
37+
FSInfo info;
38+
G_FileSystem.info(info);
39+
return info.totalBytes;
40+
41+
#elif defined(FS_IS_SPIFFS)
42+
43+
return G_FileSystem.totalBytes();
44+
45+
#else
46+
47+
return 0;
48+
49+
#endif
50+
}
51+
4052
bool file_sys_exist(String & path)
4153
{
4254
return G_FileSystem.exists(path);

lightkit/software/src/file_sys/file_sys.hpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,19 @@
1111
#include <Arduino.h>
1212
#include <FS.h>
1313

14+
#include "global.hpp"
15+
16+
#ifdef FS_IS_SPIFFS
17+
#include "SPIFFS.h"
18+
#else
19+
#include "LittleFS.h"
20+
#endif
21+
1422
// File system
15-
int file_sys_init(void);
16-
bool file_sys_exist(String & path);
17-
File file_sys_open(String & path, const char * mode);
18-
void file_sys_end(void);
23+
int file_sys_init(void);
24+
uint32_t file_sys_get_max_size(void);
25+
bool file_sys_exist(String & path);
26+
File file_sys_open(String & path, const char * mode);
27+
void file_sys_end(void);
1928

2029
#endif /* FILE_SYS_FILE_SYS_H */

lightkit/software/src/global.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#define BUILD_DATE __DATE__ " " __TIME__
2222
#define LIGHTKIT_VERSION APP_VERSION " build " BUILD_DATE
2323

24+
2425
/* ===========================
2526
* COMMON TO ALL BOARDS
2627
* ===========================

lightkit/software/src/web/web_server.cpp

Lines changed: 62 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <ESP8266WebServer.h>
1313
#endif
1414
#include <ArduinoJson.h>
15+
#include <StreamString.h>
1516
#include <string>
1617

1718
#include "cmd/cmd.hpp"
@@ -28,6 +29,9 @@
2829
// Main handle for webserver
2930
G_WebServer server(WEB_SERVER_HTTP_PORT);
3031

32+
// Internals
33+
static String updaterError;
34+
3135
static String getContentType(String filename)
3236
{
3337
if (server.hasArg("download"))
@@ -80,43 +84,75 @@ static void handle_bad_parameter(void)
8084
server.send(200, "text/plain", "Bad parameter");
8185
}
8286

83-
static void handle_firmware_upload(void)
87+
static void handle_update_done(void)
8488
{
89+
// if (!_authenticated)
90+
// return server.requestAuthentication();
91+
server.client().setNoDelay(true);
8592
String msg = String(Update.getError());
8693
server.sendHeader("Connection", "close");
8794
server.send(200, "text/plain", msg);
95+
96+
if (!Update.hasError()) {
97+
delay(100);
98+
server.client().stop();
99+
100+
// Reset to apply in 1s to let time to send HTTP response
101+
script_delayed_reset(1000);
102+
}
103+
}
104+
105+
static void handle_update_data_progress(uint32_t progress, uint32_t size)
106+
{
107+
log_info("Uploading : %d%% (%d kB)", (progress * 100) / size, progress / 1000);
108+
}
109+
110+
static void handle_update_error()
111+
{
112+
StreamString str;
113+
Update.printError(str);
114+
updaterError = str.c_str();
88115
}
89116

90-
static void handle_firmware_data(void)
117+
static void handle_update_data(void)
91118
{
92-
HTTPUpload & upload = server.upload();
119+
// handler for the file upload, get's the sketch bytes, and writes
120+
// them through the Update object
121+
HTTPUpload & upload = server.upload();
122+
uint32_t maxSketchSpace = (ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000;
123+
uint32_t maxFSSpace = file_sys_get_max_size();
93124

94125
if (upload.status == UPLOAD_FILE_START) {
95-
log_info("upload.totalSize = %d", upload.totalSize);
96-
97-
// Warning : contentLength is not the file length :
98-
// size of entire post request, file size + headers and other request data.
99-
// But it gives a good estimation of the amount of data needed.
100-
// Using max size will overwrite filesystem and therefore break the webserver
101-
// log_info("Starting update with: %s (%d kB)", upload.filename.c_str(), upload.contentLength / 1000);
102-
// if (!Update.begin(upload.contentLength)) {
103-
// Update.printError(Serial);
104-
// }
105-
} else if (upload.status == UPLOAD_FILE_WRITE) {
106-
// Write the buffer (2048 bytes max)
107-
// if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) {
108-
// Update.printError(Serial);
109-
// }
110-
} else if (upload.status == UPLOAD_FILE_END) {
111-
if (Update.end(true)) { // true: to set the size to the current progress
112-
log_info("Firmware update DONE");
113-
114-
// Reset to apply in 1s to let time to send HTTP response
115-
script_delayed_reset(1000);
126+
updaterError.clear();
127+
128+
log_info("Update: %s (%s)\n", upload.filename.c_str(), upload.name.c_str());
129+
130+
if (upload.name == "filesystem") {
131+
if (!Update.begin(maxFSSpace, U_CMD_FS)) { //start with max available size
132+
handle_update_error();
133+
}
134+
} else {
135+
if (!Update.begin(maxSketchSpace, U_FLASH)) { //start with max available size
136+
handle_update_error();
137+
}
138+
}
139+
} else if (upload.status == UPLOAD_FILE_WRITE && !updaterError.length()) {
140+
handle_update_data_progress(upload.totalSize, maxSketchSpace);
141+
142+
if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) {
143+
handle_update_error();
144+
}
145+
} else if (upload.status == UPLOAD_FILE_END && !updaterError.length()) {
146+
if (Update.end(true)) { //true to set the size to the current progress
147+
log_info("Update Success: %ukB\n", upload.totalSize / 1000);
116148
} else {
117-
Update.printError(Serial);
149+
handle_update_error();
118150
}
151+
} else if (upload.status == UPLOAD_FILE_ABORTED) {
152+
Update.end();
153+
log_warn("Update was aborted");
119154
}
155+
delay(0);
120156
}
121157

122158
/**
@@ -455,7 +491,7 @@ int web_server_init(void)
455491

456492
// --- Firmware Upload ---
457493
server.on(
458-
"/firmware_upload", HTTP_POST, []() { handle_firmware_upload(); }, []() { handle_firmware_data(); });
494+
"/update", HTTP_POST, []() { handle_update_done(); }, []() { handle_update_data(); });
459495

460496
// --- Get/Set interface ---
461497
server.on("/get_version", HTTP_GET, []() {

lightkit/software/src/web/web_server.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@
1212

1313
#define WEB_SERVER_HTTP_PORT 80 /**< Server port for web interface */
1414

15+
// Chooses between implementation of Updater.h
16+
#ifdef FS_IS_SPIFFS
17+
#define U_CMD_FS U_SPIFFS
18+
#else
19+
#define U_CMD_FS U_FS
20+
#endif
21+
1522
// Web Server
1623
int web_server_init(void);
1724
void web_server_main(void);

0 commit comments

Comments
 (0)