From 83f31b704af3f0e67edfce88d6add27c3173529a Mon Sep 17 00:00:00 2001 From: Willem Oldemans Date: Sun, 5 Nov 2023 12:32:12 +0100 Subject: [PATCH] add ota --- platformio.ini | 5 ++++ src/main.cpp | 5 +++- src/ota.cpp | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/ota.h | 9 ++++++++ 4 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 src/ota.cpp create mode 100644 src/ota.h diff --git a/platformio.ini b/platformio.ini index 2f0f776..91810a9 100644 --- a/platformio.ini +++ b/platformio.ini @@ -17,3 +17,8 @@ lib_deps = me-no-dev/AsyncTCP@^1.1.1 madhephaestus/ESP32Servo@^1.1.0 ottowinter/ESPAsyncWebServer-esphome@^3.1.0 +build_flags = + -DCORE_DEBUG_LEVEL=3 + -DNDEF_DEBUG=1 +upload_protocol = espota +upload_port = MiniSkidi.local diff --git a/src/main.cpp b/src/main.cpp index 40769bb..67f5dd9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,12 +4,14 @@ #include #include "motors.h" #include "webserver.h" +#include "ota.h" const char* ssid = "iot"; const char* pwd = "Rijnstraat214"; void setup(void) { + log_i("init hardware"); setUpPinModes(); Serial.begin(115200); WiFi.mode(WIFI_STA); @@ -22,11 +24,12 @@ void setup(void) IPAddress IP = WiFi.localIP(); Serial.print("AP IP address: "); Serial.println(IP); - + initOTA("MiniSkidi"); setup_webserver(); } void loop() { loop_webserver(); + loopOTA(); } \ No newline at end of file diff --git a/src/ota.cpp b/src/ota.cpp new file mode 100644 index 0000000..9ff18db --- /dev/null +++ b/src/ota.cpp @@ -0,0 +1,62 @@ +#include "ota.h" + + +void initOTA(const char* hostname) +{ + Serial.begin(115200); + Serial.println("Booting"); + while (WiFi.waitForConnectResult() != WL_CONNECTED) { + Serial.println("Connection Failed! Rebooting..."); + delay(5000); + ESP.restart(); + } + + // Port defaults to 3232 + // ArduinoOTA.setPort(3232); + + // Hostname defaults to esp3232-[MAC] + ArduinoOTA.setHostname(hostname); + + // No authentication by default + // ArduinoOTA.setPassword("admin"); + + // Password can be set with it's md5 value as well + // MD5(admin) = 21232f297a57a5a743894a0e4a801fc3 + // ArduinoOTA.setPasswordHash("21232f297a57a5a743894a0e4a801fc3"); + + ArduinoOTA + .onStart([]() { + String type; + if (ArduinoOTA.getCommand() == U_FLASH) + type = "sketch"; + else // U_SPIFFS + type = "filesystem"; + + // NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end() + Serial.println("Start updating " + type); + }) + .onEnd([]() { + Serial.println("\nEnd"); + }) + .onProgress([](unsigned int progress, unsigned int total) { + Serial.printf("Progress: %u%%\r", (progress / (total / 100))); + }) + .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(); + + Serial.println("Ready"); + Serial.print("IP address: "); + Serial.println(WiFi.localIP()); +} + +void loopOTA() { + ArduinoOTA.handle(); +} \ No newline at end of file diff --git a/src/ota.h b/src/ota.h new file mode 100644 index 0000000..85af1ed --- /dev/null +++ b/src/ota.h @@ -0,0 +1,9 @@ +#pragma once + +#include +#include +#include +#include + +void initOTA(const char* hostname); +void loopOTA(); \ No newline at end of file