-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOTAUpdate.cpp
82 lines (75 loc) · 2.36 KB
/
OTAUpdate.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
71
72
73
74
75
76
77
78
79
80
81
#include "OTAUpdate.h"
#include <ArduinoOTA.h>
#include "Tools.h"
OTAUpdate::OTAUpdate() {
m_log = "";
}
void OTAUpdate::Log(String text, bool newLine) {
m_log += text + (newLine ? "\n" : "");
Serial.print(text + (newLine ? "\r\n" : ""));
}
static ESP32WebServer *webServer;
void OTAUpdate::Begin(ESP32WebServer *server, OtaStartCallbackType otaStartCallback) {
// === Push ====================================================================================
m_server = server;
m_otaStartCallback = otaStartCallback;
m_log = "";
webServer = server;
// Push
m_server->on("/ota/firmware.bin", HTTP_POST, [this]() {
m_server->sendHeader("Connection", "close");
m_server->sendHeader("Access-Control-Allow-Origin", "*");
Log("");
Log(Update.hasError() ? "ERROR: OTA update failed" : "OTA update finished");
m_server->send(200, "text/plain", m_log);
delay(2000);
ESP.restart();
}, [this]() {
HTTPUpload &upload = m_server->upload();
if (upload.status == UPLOAD_FILE_START) {
if (m_otaStartCallback != nullptr) {
m_otaStartCallback();
}
m_log = "";
Log("Start receiving '", false);
Log(upload.filename, false);
Log("'");
if (!Update.begin()){
Update.printError(Serial);
Log("ERROR: UPLOAD_FILE_START");
}
}
else if (upload.status == UPLOAD_FILE_WRITE) {
if (Update.write(upload.buf, upload.currentSize) != upload.currentSize){
Update.printError(Serial);
Log("ERROR: UPLOAD_FILE_WRITE");
}
}
else if (upload.status == UPLOAD_FILE_END){
if (Update.end(true)) {
Log("Firmware size: ", false);
Log(String(upload.totalSize));
Log("Rebooting ESP32 ...");
}
else {
Update.printError(Serial);
Log("ERROR: UPLOAD_FILE_END");
}
}
yield();
});
// === Pull ====================================================================================
ArduinoOTA.setHostname(String("precipitationSensor_" + Tools::GetChipId()).c_str());
ArduinoOTA.onStart([this]() {
if (m_otaStartCallback != nullptr) {
m_otaStartCallback();
}
});
ArduinoOTA.onEnd([]() {});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {});
ArduinoOTA.onError([](ota_error_t error) {});
ArduinoOTA.begin();
}
void OTAUpdate::Handle() {
ArduinoOTA.handle();
}