Compare commits

..

2 Commits

Author SHA1 Message Date
bcb5208e4d Fully switch to PrettyOTA 2025-04-08 19:19:22 -04:00
14759902be Initial conversion to PrettyOTA 2025-04-08 18:43:00 -04:00
2 changed files with 45 additions and 27 deletions

View File

@ -13,6 +13,9 @@ platform = espressif32
board = esp32doit-devkit-v1
framework = arduino
monitor_speed = 115200
lib_deps = tzapu/WiFiManager@^2.0.17
;upload_protocol = espota
;upload_port = 192.168.2.233
upload_protocol = espota
upload_port = 192.168.2.232
lib_compat_mode = strict
lib_deps =
tzapu/WiFiManager@^2.0.17
lostincompilation/PrettyOTA@^1.1.2

View File

@ -2,9 +2,9 @@
#include <WiFiManager.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoOTA.h>
#include <PrettyOTA.h>
#define FIRMWARE "2.0"
#define BOARDFIRMWARE "2.2"
#define BUTTONPIN 23
#define DEBOUNCEMS 15
@ -17,6 +17,9 @@ String statusURL = "http://moon.basement.lan/l/boardstatus.php";
unsigned short buttonState = HIGH, lastButtonState = HIGH;
unsigned long lastStatusTime = 0;
AsyncWebServer server(80);
PrettyOTA OTAUpdates;
void sendToggleMessage(bool state) {
Serial.println("Action button triggered");
char webMsg[255] = "";
@ -34,12 +37,37 @@ unsigned long sendDeviceStatus() {
HTTPClient http;
http.begin(client, statusURL.c_str());
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
String httpData = "mac=" + WiFi.macAddress() + "&board=" + BOARD + "&firmware=" + FIRMWARE + "&service=" + SERVICE;
String httpData = "mac=" + WiFi.macAddress() + "&board=" + BOARD + "&firmware=" + BOARDFIRMWARE + "&service=" + SERVICE;
int httpResponseCode = http.POST(httpData);
http.end();
return millis();
}
// Gets called when update starts
// updateMode can be FILESYSTEM or FIRMWARE
void OnOTAStart(NSPrettyOTA::UPDATE_MODE updateMode) {
Serial.println("OTA update started");
if(updateMode == NSPrettyOTA::UPDATE_MODE::FIRMWARE)
Serial.println("Mode: Firmware");
else if(updateMode == NSPrettyOTA::UPDATE_MODE::FILESYSTEM)
Serial.println("Mode: Filesystem");
}
// Gets called while update is running
// currentSize: Number of bytes already processed
// totalSize: Total size of new firmware in bytes
void OnOTAProgress(uint32_t currentSize, uint32_t totalSize) {
Serial.printf("OTA Progress Current: %u bytes, Total: %u bytes\n", currentSize, totalSize);
}
// Gets called when update finishes
void OnOTAEnd(bool successful) {
if (successful)
Serial.println("OTA update finished successfully");
else
Serial.println("OTA update failed");
}
void setup() {
Serial.begin(115200);
pinMode(BUTTONPIN, INPUT_PULLUP);
@ -47,26 +75,14 @@ void setup() {
WiFiManager wifiManager;
wifiManager.autoConnect("LEDsActionButton");
lastStatusTime = sendDeviceStatus();
ArduinoOTA.onStart([]() {
String type;
if ( ArduinoOTA.getCommand() == U_FLASH )
type = "sketch";
else // U_SPIFFS
type = "filesystem";
Serial.println("Start OTA updating " + type);
});
ArduinoOTA.onEnd([]() {
Serial.println("\nOTA Update Completed");
});
ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("OTA Update Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
else if (error == OTA_END_ERROR) Serial.println("End Failed");
});
ArduinoOTA.begin();
OTAUpdates.Begin(&server);
OTAUpdates.SetHardwareID(SERVICE);
OTAUpdates.OverwriteAppVersion(BOARDFIRMWARE);
PRETTY_OTA_SET_CURRENT_BUILD_TIME_AND_DATE();
OTAUpdates.OnStart(OnOTAStart);
OTAUpdates.OnProgress(OnOTAProgress);
OTAUpdates.OnEnd(OnOTAEnd);
server.begin();
}
unsigned long lastDebounceTime = 0;
@ -95,5 +111,4 @@ void loop() {
}
}
lastButtonState = buttonCheck;
ArduinoOTA.handle();
}