forked from gemi254/ESP32-CAM_MJPEG2SD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ota.cpp
70 lines (63 loc) · 2.21 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
60
61
62
63
64
65
66
67
68
69
70
/*
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
*/
#define USE_OTA true
#include <WebServer.h>
#include <Update.h>
#include "OTApage.h"
WebServer ota(82); // listen on port 82
void OTAprereq();
void OTAsetup() {
if (USE_OTA) {
Serial.println("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) {
Serial.printf("Update: %s\n", 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
Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize);
} else Update.printError(Serial);
}
});
/*
ota.on("/log", HTTP_GET, []() {
ota.sendHeader("Connection", "close");
doMessageLog(); // present message log for display (not part of ota)
ota.send(200, "text/plain", logBuff); // sends as download not display
free(logBuff);
}); */
ota.begin();
}
}
bool OTAlistener() {
if (USE_OTA) {
ota.handleClient();
delay(10);
return true;
} else return false;
}