From 898bea1f7500bcf1a191b2c2ae2c53abad9fac54 Mon Sep 17 00:00:00 2001 From: Sayan Paul Date: Thu, 14 Mar 2024 00:53:17 +0530 Subject: [PATCH] ota feature --- src/blite.cpp | 46 +++++++++++++++++++++++++++++++++++++++++++++- src/blite.h | 6 ++++++ 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/src/blite.cpp b/src/blite.cpp index 2108c8b..a7a52f1 100644 --- a/src/blite.cpp +++ b/src/blite.cpp @@ -104,6 +104,8 @@ void Blite::setup(){ digitalWrite(LED_BUILTIN, HIGH); this->defineM12(true); this->defineM34(true); + String newHostname = "buildybee"; + WiFi.hostname(newHostname.c_str()); WiFi.disconnect(); WiFi.mode(WIFI_OFF); @@ -133,15 +135,17 @@ int Blite::readADC(){ } void Blite::setupServer(String HTML_CONTENT) { - this->webServer.on("/", HTTP_GET, [&]() { + this->webServer.on("/", HTTP_GET, [=]() { this->webServer.send(200, "text/html", HTML_CONTENT); }); this->webServer.begin(); this->serverSetupDone = true; + this->otaSetup(); } void Blite::renderServer() { this->webServer.handleClient(); + this->otaLoop(); } void Blite::smartRenderServer(String HTML_CONTENT){ @@ -150,3 +154,43 @@ void Blite::smartRenderServer(String HTML_CONTENT){ } this->renderServer(); } + +void Blite::otaSetup(){ + + ArduinoOTA.onStart([]() { + String type; + if (ArduinoOTA.getCommand() == U_FLASH) { + type = "sketch"; + } else { // U_FS + type = "filesystem"; + } + + // NOTE: if updating FS this would be the place to unmount FS using FS.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(); +} + +void Blite::otaLoop(){ + ArduinoOTA.handle(); +} \ No newline at end of file diff --git a/src/blite.h b/src/blite.h index 09378ae..706619a 100644 --- a/src/blite.h +++ b/src/blite.h @@ -20,6 +20,9 @@ #include #include #include +#include +#include +#include class Blite { public: @@ -46,6 +49,9 @@ void setupServer(String HTML_CONTENT); void renderServer(); void smartRenderServer(String HTML_CONTENT); +void otaSetup(); +void otaLoop(); + private: int m1,m2,m3,m4,speed; ESP8266WebServer webServer = ESP8266WebServer(80);