-
Notifications
You must be signed in to change notification settings - Fork 7.6k
/
Copy pathOTAWebUpdater.ino
117 lines (107 loc) · 2.88 KB
/
OTAWebUpdater.ino
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <WiFi.h>
#include <WebServer.h>
#include <ESPmDNS.h>
#include <Update.h>
#include <Ticker.h>
#include "html.h"
#define SSID_FORMAT "ESP32-%06lX" // 12 chars total
//#define PASSWORD "test123456" // generate if remarked
WebServer server(80);
Ticker tkSecond;
uint8_t otaDone = 0;
const char *alphanum = "0123456789!@#$%^&*abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
String generatePass(uint8_t str_len) {
String buff;
for (int i = 0; i < str_len; i++) {
buff += alphanum[random(strlen(alphanum) - 1)];
}
return buff;
}
void apMode() {
char ssid[13];
char passwd[11];
long unsigned int espmac = ESP.getEfuseMac() >> 24;
snprintf(ssid, 13, SSID_FORMAT, espmac);
#ifdef PASSWORD
snprintf(passwd, 11, PASSWORD);
#else
snprintf(passwd, 11, generatePass(10).c_str());
#endif
WiFi.mode(WIFI_AP);
WiFi.softAP(ssid, passwd); // Set up the SoftAP
MDNS.begin("esp32");
Serial.printf("AP: %s, PASS: %s\n", ssid, passwd);
}
void handleUpdateEnd() {
server.sendHeader("Connection", "close");
if (Update.hasError()) {
server.send(502, "text/plain", Update.errorString());
} else {
server.sendHeader("Refresh", "10");
server.sendHeader("Location", "/");
server.send(307);
ESP.restart();
}
}
void handleUpdate() {
size_t fsize = UPDATE_SIZE_UNKNOWN;
if (server.hasArg("size")) {
fsize = server.arg("size").toInt();
}
HTTPUpload &upload = server.upload();
if (upload.status == UPLOAD_FILE_START) {
Serial.printf("Receiving Update: %s, Size: %d\n", upload.filename.c_str(), fsize);
if (!Update.begin(fsize)) {
otaDone = 0;
Update.printError(Serial);
}
} else if (upload.status == UPLOAD_FILE_WRITE) {
if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) {
Update.printError(Serial);
} else {
otaDone = 100 * Update.progress() / Update.size();
}
} else if (upload.status == UPLOAD_FILE_END) {
if (Update.end(true)) {
Serial.printf("Update Success: %u bytes\nRebooting...\n", upload.totalSize);
} else {
Serial.printf("%s\n", Update.errorString());
otaDone = 0;
}
}
}
void webServerInit() {
server.on(
"/update", HTTP_POST,
[]() {
handleUpdateEnd();
},
[]() {
handleUpdate();
}
);
server.on("/favicon.ico", HTTP_GET, []() {
server.sendHeader("Content-Encoding", "gzip");
server.send_P(200, "image/x-icon", favicon_ico_gz, favicon_ico_gz_len);
});
server.onNotFound([]() {
server.send(200, "text/html", indexHtml);
});
server.begin();
Serial.printf("Web Server ready at http://esp32.local or http://%s\n", WiFi.softAPIP().toString().c_str());
}
void everySecond() {
if (otaDone > 1) {
Serial.printf("ota: %d%%\n", otaDone);
}
}
void setup() {
Serial.begin(115200);
apMode();
webServerInit();
tkSecond.attach(1, everySecond);
}
void loop() {
delay(150);
server.handleClient();
}