From 7ab8294dc77fe8e18fc42c104c704bfaca5962bf Mon Sep 17 00:00:00 2001 From: Marcel Peterkau Date: Sun, 13 Jun 2021 23:18:14 +0200 Subject: [PATCH] Added OTA-Update --- platformio.ini | 5 +++++ src/defaults.h | 7 +++++++ src/main.cpp | 45 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+) diff --git a/platformio.ini b/platformio.ini index 0b66517..52245e7 100644 --- a/platformio.ini +++ b/platformio.ini @@ -15,6 +15,11 @@ board_build.filesystem = littlefs board_build.f_flash = 80000000L board_build.ldscript = eagle.flash.4m1m.ld +upload_protocol = espota +upload_port = ESP_OTA.local +upload_flags= + --auth=UploadTheFlag + build_flags= -D DEVICE_NAME='"DE CTF Timer Prototype 1"' -D WIFI_CLIENT diff --git a/src/defaults.h b/src/defaults.h index eb59955..ebcb522 100644 --- a/src/defaults.h +++ b/src/defaults.h @@ -17,6 +17,13 @@ #define WIFI_PASS "CaptureTheFlag" #endif +#ifndef OTA_PASS +#define OTA_PASS "UploadTheFlag" +#endif + +#ifndef OTA_HOST +#define OTA_HOST "ESP_OTA" +#endif #ifndef DEVICE_NAME #define DEVICE_NAME WIFI_SSID diff --git a/src/main.cpp b/src/main.cpp index 067100c..7f1a0b3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -12,7 +12,9 @@ #include #include #include +#include #endif +#include #include #include #include @@ -382,6 +384,48 @@ void setup() Serial.print(WiFi.localIP()); } #endif + + ArduinoOTA.setPort(8266); + ArduinoOTA.setHostname(OTA_HOST); + ArduinoOTA.setPassword(OTA_PASS); + + ArduinoOTA.onStart([]() + { + String type; + if (ArduinoOTA.getCommand() == U_FLASH) + { + type = "sketch"; + } + else + { + type = "filesystem"; + LittleFS.end(); + } + Serial.println("Start updating " + type); + }); + + ArduinoOTA.onEnd([]() + { Serial.println("\nEnd"); }); + + ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) + { Serial.printf("Progress: %u%%\r", (progress / (total / 100))); }); + + ArduinoOTA.onError([](ota_error_t error) + { + Serial.printf("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(); + #ifdef CAPTIVE dnsServer.start(53, "*", WiFi.softAPIP()); #endif @@ -400,6 +444,7 @@ void loop() SerialOutputTicker.update(); InputGetterTicker.update(); PowerMonitorTicker.update(); + ArduinoOTA.handle(); #ifdef CAPTIVE dnsServer.processNextRequest();