diff --git a/platformio.ini b/platformio.ini index ddb356a..e5ff436 100644 --- a/platformio.ini +++ b/platformio.ini @@ -10,14 +10,15 @@ [env:ESP32] platform = espressif32 -board = wemos_d1_mini32 +board = esp32dev framework = arduino monitor_speed = 115200 lib_deps = - ottowinter/ESPAsyncWebServer-esphome@^3.1.0 + ottowinter/ESPAsyncWebServer-esphome@^3.3.0 robtillaart/INA226@^0.4.4 bblanchon/ArduinoJson@^6.21.3 ArduinoLog +board_build.partitions = default.csv upload_protocol = espota upload_port = 192.168.5.242 lib_extra_dirs = libs diff --git a/src/main.cpp b/src/main.cpp index 286929a..edbc30c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include "AsyncJson.h" #include @@ -39,6 +40,52 @@ extern "C" int rom_phy_get_vdd33(); AsyncWebServer server(80); #define FORMAT_LITTLEFS_IF_FAILED true +void update_started() { + Serial.println("CALLBACK: HTTP update process started"); + } + + void update_finished() { + Serial.println("CALLBACK: HTTP update process finished"); + } + + void update_progress(int cur, int total) { + Serial.printf("CALLBACK: HTTP update process at %d of %d bytes...\n", cur, total); + } + + void update_error(int err) { + Serial.printf("CALLBACK: HTTP update fatal error code %d\n", err); + } + + void run_ota(void* parameter) { + Serial.println("RUNNING OTA"); + // WiFiClient client; + httpUpdate.onStart(update_started); + httpUpdate.onEnd(update_finished); + httpUpdate.onProgress(update_progress); + httpUpdate.onError(update_error); + Serial.println("RUNNING OTA"); + + WiFiClientSecure client; + client.setInsecure(); + + t_httpUpdate_return ret = httpUpdate.update(client, "https://iot.tobiasmaier.me/firmware/waterlevel/INA233/1_1_1.bin"); + // t_httpUpdate_return ret = httpUpdate.update(client, "https://iot.tobiasmaier.me", 443, "/firmware/waterlevel/INA233/1_0_1.bin"); + + switch (ret) { + case HTTP_UPDATE_FAILED: + Serial.printf("HTTP_UPDATE_FAILED Error (%d): %s\n", httpUpdate.getLastError(), httpUpdate.getLastErrorString().c_str()); + break; + + case HTTP_UPDATE_NO_UPDATES: + Serial.println("HTTP_UPDATE_NO_UPDATES"); + break; + + case HTTP_UPDATE_OK: + Serial.println("HTTP_UPDATE_OK"); + break; + } + } + void setup() { prefs.begin("waterlevel", false); @@ -150,9 +197,20 @@ void setup() request->send(SPIFFS, "/settings.html", "text/html", false); // TODO add proper return templating }); + server.on("/ota", HTTP_GET, [](AsyncWebServerRequest* request) { + Serial.println("OTA Task start"); + xTaskCreate(run_ota, "RunOTATask", 1024 * 8, NULL, 1, NULL); + + + request->send(200, "text/plain", "OTA done"); + }); setup_api_endpoints(); + + + + server.on("/chota.css", HTTP_GET, [](AsyncWebServerRequest* request) { request->send(SPIFFS, "/chota.css", "text/css", false); }); server.on("/gauge.js", HTTP_GET, [](AsyncWebServerRequest* request) { request->send(SPIFFS, "/gauge.js", "application/javascript", false); }); @@ -204,3 +262,5 @@ void loop() ArduinoOTA.handle(); delay(1000); } + +