forked from s60sc/ESP32-CAM_MJPEG2SD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathota.cpp
59 lines (54 loc) · 1.87 KB
/
ota.cpp
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
/*
To apply web based OTA update.
In Arduino IDE:
- select Tools / Partition Scheme / Minimal SPIFFS
- select Sketch / Export compiled Binary
On browser, enter <ESP32-cam ip address>:82, e.g 192.168.1.100:82
On returned page, select Choose file and navigate to .bin file in sketch folder, then press Update
s60sc 2020
*/
#include "myConfig.h"
#include "OTApage.h"
WebServer ota(82); // listen on port 82
void OTAsetup() {
if (USE_OTA) {
LOG_INF("OTA on port 82");
ota.on("/", HTTP_GET, []() {
// stop timer isrs, and free up heap space, or crashes esp32
OTAprereq();
ota.sendHeader("Connection", "close");
ota.send(200, "text/html", OTApage);
});
ota.on("/update", HTTP_POST, []() {
ota.sendHeader("Connection", "close");
ota.send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK");
ESP.restart();
}, []() {
HTTPUpload& upload = ota.upload();
if (upload.status == UPLOAD_FILE_START) {
LOG_INF("Update: %s", 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) {
// if crashes, check that correct partition scheme has been selected
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
LOG_INF("Update Success: %u, Rebooting...", upload.totalSize);
delay(1000);
} else Update.printError(Serial);
}
});
ota.begin();
}
}
bool OTAlistener() {
if (USE_OTA) {
ota.handleClient();
delay(5);
return true;
} else return false;
}