-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathWebOTA.cpp
More file actions
468 lines (372 loc) · 10.8 KB
/
WebOTA.cpp
File metadata and controls
468 lines (372 loc) · 10.8 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
// Arduino build process info: https://github.com/arduino/Arduino/wiki/Build-Process
#define WEBOTA_VERSION "0.1.6"
#include "WebOTA.h"
#include <Arduino.h>
#include <WiFiClient.h>
#ifdef ESP32
#include <WebServer.h>
#include <ESPmDNS.h>
#include <Update.h>
#include <WiFi.h>
WebServer OTAServer(9999);
#endif
#ifdef ESP8266
#include <ESP8266WebServer.h>
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
ESP8266WebServer OTAServer(9999);
#endif
WebOTA webota;
////////////////////////////////////////////////////////////////////////////
int WebOTA::init(const unsigned int port, const char *path) {
this->port = port;
this->path = path;
// Only run this once
if (this->init_has_run) {
return 0;
}
add_http_routes(&OTAServer, path);
#ifdef ESP32
// https://github.com/espressif/arduino-esp32/issues/7708
// Fix some slowness
OTAServer.enableDelay(false);
#endif
OTAServer.begin(port);
Serial.printf("WebOTA url : http://%s.local:%d%s\r\n\r\n", this->mdns.c_str(), port, path);
// Store that init has already run
this->init_has_run = true;
return 1;
}
// One param
int WebOTA::init(const unsigned int port) {
return WebOTA::init(port, "/webota");
}
// No params
int WebOTA::init() {
return WebOTA::init(8080, "/webota");
}
int WebOTA::handle() {
// If we haven't run the init yet run it
if (!this->init_has_run) {
WebOTA::init();
}
OTAServer.handleClient();
#ifdef ESP8266
MDNS.update();
#endif
return 1;
}
long WebOTA::max_sketch_size() {
long ret = (ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000;
return ret;
}
// R Macro string literal https://en.cppreference.com/w/cpp/language/string_literal
const char INDEX_HTML[] PROGMEM = R"!^!(
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>WebOTA</title>
<script src="main.js"></script>
<link rel="stylesheet" href="main.css">
</head>
<body>
<h1>WebOTA version %s</h1>
<form method="POST" action="#" enctype="multipart/form-data" id="upload_form">
<div><input class="" type="file" name="update" id="file"></div>
<div><input class="btn" type="submit" value="Upload"></div>
</form>
<p>
<b>Board:</b> %s<br />
<b>MAC:</b> %s<br />
<b>Uptime:</b> %s<br />
</p>
<div>
<div id="prg" class="prog_bar" style=""></div>
</div>
</body>
</html>
)!^!";
const char MAIN_CSS[] PROGMEM = R"!^!(
body {
margin: 2em;
font-family: sans-serif;
}
input[type=file]::file-selector-button, .btn {
margin-right: 20px;
border: 1px solid gray;
background-image: linear-gradient(to bottom,#59ef59 0, #55982f 100%);
padding: 10px 20px;
border-radius: 4px;
color: #fff;
cursor: pointer;
transition: background .2s ease-in-out;
width: 10em;
}
.btn {
margin-top: 12px;
background-image: linear-gradient(to bottom,#78addb 0,#2d6ca2 100%);
}
.prog_bar {
margin: 12px 0;
text-shadow: 2px 2px 3px black;
padding: 5px 0;
display: none;
border: 1px solid #7c7c7c;
background-image: linear-gradient(to right,#d2d2d2 0,#2d2d2d 100%);
line-height: 1.3em;
border-radius: 4px;
text-align: center;
color: white;
font-size: 250%;
}
)!^!";
const char MAIN_JS[] PROGMEM = R"!^!(
var domReady = function(callback) {
document.readyState === "interactive" || document.readyState === "complete" ? callback() : document.addEventListener("DOMContentLoaded", callback);
};
domReady(function() {
var myform = document.getElementById('upload_form');
var filez = document.getElementById('file');
myform.onsubmit = function(event) {
event.preventDefault();
var formData = new FormData();
var file = filez.files[0];
if (!file) { return false; }
formData.append("files", file, file.name);
var xhr = new XMLHttpRequest();
xhr.upload.addEventListener("progress", function(evt) {
if (evt.lengthComputable) {
var per = Math.round((evt.loaded / evt.total) * 100);
var prg = document.getElementById('prg');
var str = per + "%";
prg.style.width = str;
prg.innerHTML = str;
prg.style.display = "block";
}
}, false);
xhr.open('POST', location.href, true);
// Set up a handler for when the request finishes.
xhr.onload = function () {
if (xhr.status === 200) {
//alert('Success');
} else {
//alert('An error occurred!');
}
};
xhr.send(formData);
}
});
)!^!";
String WebOTA::get_board_type() {
// More information: https://github.com/search?q=repo%3Aarendst%2FTasmota%20esp32s2&type=code
#if defined(ESP8266)
String BOARD_NAME = "ESP8266";
#elif defined(CONFIG_IDF_TARGET_ESP32S2)
String BOARD_NAME = "ESP32-S2";
#elif defined(CONFIG_IDF_TARGET_ESP32S3)
String BOARD_NAME = "ESP32-S3";
#elif defined(CONFIG_IDF_TARGET_ESP32C3)
String BOARD_NAME = "ESP32-C3";
#elif defined(CONFIG_IDF_TARGET_ESP32)
String BOARD_NAME = "ESP32";
#elif defined(CONFIG_ARDUINO_VARIANT)
String BOARD_NAME = CONFIG_ARDUINO_VARIANT;
#else
String BOARD_NAME = "Unknown";
#endif
return BOARD_NAME;
}
String get_mac_address() {
uint8_t mac[6];
// Put the addr in mac
WiFi.macAddress(mac);
// Build a string and return it
char buf[20] = "";
snprintf(buf, sizeof(buf), "%02X:%02X:%02X:%02X:%02X:%02X", mac[0],mac[1],mac[2],mac[3],mac[4],mac[5]);
String ret = buf;
return ret;
}
#ifdef ESP8266
int WebOTA::add_http_routes(ESP8266WebServer *server, const char *path) {
#endif
#ifdef ESP32
int WebOTA::add_http_routes(WebServer *server, const char *path) {
#endif
// Index page
server->on("/", HTTP_GET, [server]() {
server->send(200, "text/html", F("<h1>WebOTA</h1>"));
});
// Upload firmware page
server->on(path, HTTP_GET, [server,this]() {
String html = "";
if (this->custom_html != NULL) {
html = this->custom_html;
} else {
//uint32_t maxSketchSpace = this->max_sketch_size();
#if defined(ESP8266)
const char* BOARD_NAME = "ESP8266";
#elif defined(ESP32)
const char* BOARD_NAME = "ESP32";
#else
const char* BOARD_NAME = "Unknown";
#endif
String uptime_str = human_time(millis() / 1000);
String board_type = webota.get_board_type();
String mac_addr = get_mac_address();
char buf[1024];
snprintf_P(buf, sizeof(buf), INDEX_HTML, WEBOTA_VERSION, board_type, mac_addr.c_str(), uptime_str.c_str());
html = buf;
}
server->send_P(200, "text/html", html.c_str());
});
// Handling uploading firmware file
server->on(path, HTTP_POST, [server,this]() {
server->send(200, "text/plain", (Update.hasError()) ? "Update: fail\n" : "Update: OK!\n");
delay(500);
ESP.restart();
}, [server,this]() {
HTTPUpload& upload = server->upload();
if (upload.status == UPLOAD_FILE_START) {
if (_start_callback) {
_start_callback();
}
Serial.printf("Firmware update initiated: %s\r\n", upload.filename.c_str());
//uint32_t maxSketchSpace = (ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000;
uint32_t maxSketchSpace = this->max_sketch_size();
if (!Update.begin(maxSketchSpace)) { //start with max available size
if (_error_callback) {
_error_callback(OTA_BEGIN_ERROR);
}
Update.printError(Serial);
}
} else if (upload.status == UPLOAD_FILE_WRITE) {
/* flashing firmware to ESP*/
if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) {
if (_error_callback) {
_error_callback(OTA_END_ERROR);
}
Update.printError(Serial);
}
// Store the next milestone to output
uint16_t chunk_size = 51200;
static uint32_t next = 51200;
if(_progress_callback) {
_progress_callback(upload.totalSize, upload.currentSize);
}
// Check if we need to output a milestone (100k 200k 300k)
if (upload.totalSize >= next) {
Serial.printf("%dk ", next / 1024);
next += chunk_size;
}
} else if (upload.status == UPLOAD_FILE_END) {
if (Update.end(true)) { //true to set the size to the current progress
if (_end_callback) {
_end_callback();
}
Serial.printf("\r\nFirmware update successful: %u bytes\r\nRebooting...\r\n", upload.totalSize);
} else {
if (_error_callback) {
_error_callback(OTA_END_ERROR);
}
Update.printError(Serial);
}
}
});
// FILE: main.js
server->on("/main.js", HTTP_GET, [server]() {
server->send_P(200, "application/javascript", MAIN_JS);
});
// FILE: main.css
server->on("/main.css", HTTP_GET, [server]() {
server->send_P(200, "text/css", MAIN_CSS);
});
// FILE: favicon.ico
server->on("/favicon.ico", HTTP_GET, [server]() {
server->send(200, "image/vnd.microsoft.icon", "");
});
server->begin();
return 1;
}
// If the MCU is in a delay() it cannot respond to HTTP OTA requests
// We do a "fake" looping delay and listen for incoming HTTP requests while waiting
void WebOTA::delay(unsigned int ms) {
// Borrowed from mshoe007 @ https://github.com/scottchiefbaker/ESP-WebOTA/issues/8
decltype(millis()) last = millis();
while ((millis() - last) < ms) {
OTAServer.handleClient();
::delay(5);
}
}
void WebOTA::set_custom_html(char const * const html) {
this->custom_html = html;
}
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
int init_mdns(const char *host) {
// Use mdns for host name resolution
if (!MDNS.begin(host)) {
Serial.println("Error setting up MDNS responder!");
return 0;
}
Serial.printf("mDNS started : %s.local\r\n", host);
webota.mdns = host;
return 1;
}
String ip2string(IPAddress ip) {
String ret = String(ip[0]) + "." + String(ip[1]) + "." + String(ip[2]) + "." + String(ip[3]);
return ret;
}
String WebOTA::human_time(uint32_t sec) {
int days = (sec / 86400);
sec = sec % 86400;
int hours = (sec / 3600);
sec = sec % 3600;
int mins = (sec / 60);
sec = sec % 60;
char buf[24] = "";
if (days) {
snprintf(buf, sizeof(buf), "%d days %d hours\n", days, hours);
} else if (hours) {
snprintf(buf, sizeof(buf), "%d hours %d minutes\n", hours, mins);
} else {
snprintf(buf, sizeof(buf), "%d minutes %d seconds\n", mins, sec);
}
String ret = buf;
return ret;
}
WebOTA& WebOTA::onStart(THandlerFunction fn) {
_start_callback = fn;
return *this;
}
WebOTA& WebOTA::onEnd(THandlerFunction fn) {
_end_callback = fn;
return *this;
}
WebOTA& WebOTA::onProgress(THandlerFunction_Progress fn) {
_progress_callback = fn;
return *this;
}
WebOTA& WebOTA::onError(THandlerFunction_Error fn) {
_error_callback = fn;
return *this;
}
int init_wifi(const char *ssid, const char *password, const char *mdns_hostname) {
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("");
Serial.print("Connecting to Wifi");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.printf("Connected to '%s'\r\n\r\n",ssid);
String ipaddr = ip2string(WiFi.localIP());
Serial.printf("IP address : %s\r\n", ipaddr.c_str());
Serial.printf("MAC address : %s \r\n", WiFi.macAddress().c_str());
init_mdns(mdns_hostname);
return 1;
}