Skip to content

Commit 6b287db

Browse files
authored
Update/upgrade OTAWebUpdater.ino example (#8839)
* Update/upgrade OTAWebUpdater.ino example * Wrong side of the MAC address in ssid * Better not to cheat it on an example :) * Changes requested by @me-no-dev
1 parent 6cc3072 commit 6b287db

File tree

4 files changed

+212
-169
lines changed

4 files changed

+212
-169
lines changed

Diff for: libraries/ArduinoOTA/examples/OTAWebUpdater/OTAWebUpdater.ino

-169
This file was deleted.
+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#include <WiFi.h>
2+
#include <WebServer.h>
3+
#include <ESPmDNS.h>
4+
#include <Update.h>
5+
#include <Ticker.h>
6+
#include "html.h"
7+
8+
#define SSID_FORMAT "ESP32-%06lX" // 12 chars total
9+
//#define PASSWORD "test123456" // generate if remarked
10+
11+
WebServer server(80);
12+
Ticker tkSecond;
13+
uint8_t otaDone = 0;
14+
15+
const char* alphanum = "0123456789!@#$%^&*abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
16+
String generatePass(uint8_t str_len) {
17+
String buff;
18+
for(int i = 0; i < str_len; i++)
19+
buff += alphanum[random(strlen(alphanum)-1)];
20+
return buff;
21+
}
22+
23+
void apMode() {
24+
char ssid[13];
25+
char passwd[11];
26+
long unsigned int espmac = ESP.getEfuseMac() >> 24;
27+
snprintf(ssid, 13, SSID_FORMAT, espmac);
28+
#ifdef PASSWORD
29+
snprintf(passwd, 11, PASSWORD);
30+
#else
31+
snprintf(passwd, 11, generatePass(10).c_str());
32+
#endif
33+
WiFi.mode(WIFI_AP);
34+
WiFi.softAP(ssid, passwd); // Set up the SoftAP
35+
MDNS.begin("esp32");
36+
Serial.printf("AP: %s, PASS: %s\n", ssid, passwd);
37+
}
38+
39+
void handleUpdateEnd() {
40+
server.sendHeader("Connection", "close");
41+
if (Update.hasError()) {
42+
server.send(502, "text/plain", Update.errorString());
43+
} else {
44+
server.sendHeader("Refresh", "10");
45+
server.sendHeader("Location","/");
46+
server.send(307);
47+
ESP.restart();
48+
}
49+
}
50+
51+
void handleUpdate() {
52+
size_t fsize = UPDATE_SIZE_UNKNOWN;
53+
if (server.hasArg("size")) fsize = server.arg("size").toInt();
54+
HTTPUpload& upload = server.upload();
55+
if (upload.status == UPLOAD_FILE_START) {
56+
Serial.printf("Receiving Update: %s, Size: %d\n", upload.filename.c_str(), fsize);
57+
if (!Update.begin(fsize)) {
58+
otaDone = 0;
59+
Update.printError(Serial);
60+
}
61+
} else if (upload.status == UPLOAD_FILE_WRITE) {
62+
if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) {
63+
Update.printError(Serial);
64+
} else {
65+
otaDone = 100 * Update.progress() / Update.size();
66+
}
67+
} else if (upload.status == UPLOAD_FILE_END) {
68+
if (Update.end(true)) {
69+
Serial.printf("Update Success: %u bytes\nRebooting...\n", upload.totalSize);
70+
} else {
71+
Serial.printf("%s\n", Update.errorString());
72+
otaDone = 0;
73+
}
74+
}
75+
}
76+
77+
void webServerInit() {
78+
server.on("/update", HTTP_POST, [](){handleUpdateEnd();}, [](){handleUpdate();});
79+
server.on("/favicon.ico", HTTP_GET, [](){
80+
server.sendHeader("Content-Encoding", "gzip");
81+
server.send_P(200, "image/x-icon", favicon_ico_gz, favicon_ico_gz_len);
82+
});
83+
server.onNotFound([]() {server.send(200, "text/html", indexHtml);});
84+
server.begin();
85+
Serial.printf("Web Server ready at http://esp32.local or http://%s\n", WiFi.softAPIP().toString().c_str());
86+
}
87+
88+
void everySecond() {
89+
if (otaDone > 1) Serial.printf("ota: %d%%\n", otaDone);
90+
}
91+
92+
void setup() {
93+
Serial.begin(115200);
94+
apMode();
95+
webServerInit();
96+
tkSecond.attach(1, everySecond);
97+
}
98+
99+
void loop() {
100+
delay(150);
101+
server.handleClient();
102+
}

Diff for: libraries/Update/examples/OTAWebUpdater/html.h

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
// Literal string
2+
const char* indexHtml = R"literal(
3+
<!DOCTYPE html>
4+
<link rel='icon' href='/favicon.ico' sizes='any'>
5+
<body style='width:480px'>
6+
<h2>ESP Firmware Update</h2>
7+
<form method='POST' enctype='multipart/form-data' id='upload-form'>
8+
<input type='file' id='file' name='update'>
9+
<input type='submit' value='Update'>
10+
</form>
11+
<br>
12+
<div id='prg' style='width:0;color:white;text-align:center'>0%</div>
13+
</body>
14+
<script>
15+
var prg = document.getElementById('prg');
16+
var form = document.getElementById('upload-form');
17+
form.addEventListener('submit', el=>{
18+
prg.style.backgroundColor = 'blue';
19+
el.preventDefault();
20+
var data = new FormData(form);
21+
var req = new XMLHttpRequest();
22+
var fsize = document.getElementById('file').files[0].size;
23+
req.open('POST', '/update?size=' + fsize);
24+
req.upload.addEventListener('progress', p=>{
25+
let w = Math.round(p.loaded/p.total*100) + '%';
26+
if(p.lengthComputable){
27+
prg.innerHTML = w;
28+
prg.style.width = w;
29+
}
30+
if(w == '100%') prg.style.backgroundColor = 'black';
31+
});
32+
req.send(data);
33+
});
34+
</script>
35+
)literal";
36+
37+
// Compressed gzip in C include file style
38+
// listing was created using `xxd -i favicon.ico.gz`
39+
const char favicon_ico_gz[] = {
40+
0x1f, 0x8b, 0x08, 0x08, 0x13, 0xb6, 0xa5, 0x62, 0x00, 0x03, 0x66, 0x61,
41+
0x76, 0x69, 0x63, 0x6f, 0x6e, 0x2e, 0x69, 0x63, 0x6f, 0x00, 0xa5, 0x53,
42+
0x69, 0x48, 0x54, 0x51, 0x14, 0x7e, 0x41, 0x99, 0x4b, 0xa0, 0x15, 0x24,
43+
0xb4, 0x99, 0x8e, 0x1b, 0x25, 0xa8, 0x54, 0xb4, 0x19, 0x56, 0x62, 0x41,
44+
0xab, 0x14, 0x45, 0x61, 0x51, 0x62, 0x86, 0x69, 0x1a, 0xd4, 0x60, 0xda,
45+
0xa2, 0x99, 0x4b, 0x69, 0x1b, 0x42, 0x26, 0x5a, 0x09, 0x59, 0xd8, 0xfe,
46+
0xab, 0x88, 0xa0, 0x82, 0xe8, 0x4f, 0x90,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6, 0x3b, 0x0e, 0x14, 0x50, 0xec,
91+
0xd5, 0x6b, 0x36, 0x38, 0x9c, 0x7e, 0xd8, 0x1c, 0x7e, 0x28, 0x4f, 0x0a,
92+
0xe8, 0x7c, 0xe4, 0xc4, 0xc7, 0x4f, 0x1e, 0xcc, 0x25, 0x0e, 0x84, 0x25,
93+
0x04, 0x39, 0xb9, 0x25, 0xd7, 0x88, 0xfc, 0x12, 0x33, 0xa2, 0x93, 0xd5,
94+
0xc8, 0x2b, 0x32, 0xe1, 0xc4, 0x69, 0x01, 0xcd, 0xad, 0x76, 0xf4, 0xf6,
95+
0x79, 0xe4, 0xff, 0xe0, 0xa9, 0xaf, 0xb2, 0x42, 0x40, 0x53, 0xb3, 0x0d,
96+
0x43, 0x2e, 0xe2, 0x04, 0xed, 0x34, 0x74, 0x0c, 0x7f, 0xe2, 0x11, 0x9b,
97+
0x4e, 0x79, 0xe5, 0xd5, 0x22, 0xb2, 0x88, 0x1b, 0xcf, 0x5f, 0xba, 0xe5,
98+
0x39, 0x25, 0x11, 0x09, 0xab, 0x07, 0x34, 0xef, 0x41, 0xda, 0xfb, 0x3d,
99+
0xea, 0xeb, 0x72, 0x07, 0x70, 0xf5, 0xba, 0x0d, 0x33, 0x92, 0xd5, 0xbf,
100+
0xf3, 0x8f, 0xb8, 0x34, 0x7f, 0xb1, 0x16, 0x89, 0x2b, 0x79, 0x64, 0x6c,
101+
0x1a, 0x44, 0x53, 0x9b, 0x1d, 0x65, 0x55, 0x22, 0x76, 0x12, 0xce, 0x35,
102+
0x0d, 0x22, 0x3e, 0xf4, 0x78, 0xd0, 0xfd, 0xd9, 0x43, 0x5c, 0x14, 0x30,
103+
0x23, 0xe9, 0x1f, 0xfc, 0x27, 0x9b, 0x54, 0x87, 0xf8, 0x28, 0xff, 0xdb,
104+
0x94, 0xf9, 0x2a, 0x24, 0x10, 0x4f, 0xf3, 0x68, 0xae, 0x8d, 0xf4, 0x1e,
105+
0xe6, 0x11, 0x3f, 0x24, 0x7b, 0x78, 0xe2, 0xaf, 0x5c, 0xfc, 0xf1, 0x06,
106+
0xff, 0x12, 0x69, 0xbf, 0x53, 0x68, 0x47, 0x21, 0x84, 0x91, 0xa4, 0x4f,
107+
0xf4, 0x8d, 0xbd, 0x63, 0x2f, 0xf7, 0x9f, 0xe7, 0x27, 0x40, 0xbb, 0x5a,
108+
0x53, 0x7e, 0x04, 0x00, 0x00
109+
};
110+
const int favicon_ico_gz_len = 821;

0 commit comments

Comments
 (0)