diff --git a/CMakeLists.txt b/CMakeLists.txt index 6b8f3d2..cbb03a9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -32,6 +32,7 @@ file(GLOB SRCFILES NukiWrapper.cpp NukiOpenerWrapper.cpp MqttTopics.h + Ota.cpp WebCfgServer.cpp PresenceDetection.cpp PreferencesKeys.h diff --git a/Ota.cpp b/Ota.cpp new file mode 100644 index 0000000..cb3a925 --- /dev/null +++ b/Ota.cpp @@ -0,0 +1,29 @@ +#include +#include "Ota.h" + +#define FULL_PACKET 1436 // HTTP_UPLOAD_BUFLEN in WebServer,h + +void Ota::updateFirmware(uint8_t buf, size_t size) +{ + if (!_updateFlag) + { //If it's the first packet of OTA since bootup, begin OTA + Serial.println("BeginOTA"); + esp_ota_begin(esp_ota_get_next_update_partition(NULL), OTA_SIZE_UNKNOWN, &otaHandler); + _updateFlag = true; + } + esp_ota_write(otaHandler, (const void*)buf, size); + if (size != FULL_PACKET) + { + esp_ota_end(otaHandler); + Serial.println("EndOTA"); + if (ESP_OK == esp_ota_set_boot_partition(esp_ota_get_next_update_partition(NULL))) + { + delay(2000); + esp_restart(); + } + else + { + Serial.println("Upload Error"); + } + } +} diff --git a/Ota.h b/Ota.h new file mode 100644 index 0000000..4b3a38f --- /dev/null +++ b/Ota.h @@ -0,0 +1,15 @@ +#pragma once + +#include +#include +#include "esp_ota_ops.h" + +class Ota +{ +public: + void updateFirmware(uint8_t buf, size_t size); + +private: + bool _updateFlag = false; + esp_ota_handle_t otaHandler = 0; +}; diff --git a/Version.h b/Version.h index dc8683d..7fbaec4 100644 --- a/Version.h +++ b/Version.h @@ -1,3 +1,3 @@ #pragma once -#define nuki_hub_version "3.1" \ No newline at end of file +#define nuki_hub_version "3.2" \ No newline at end of file diff --git a/WebCfgServer.cpp b/WebCfgServer.cpp index 2835989..1a9a235 100644 --- a/WebCfgServer.cpp +++ b/WebCfgServer.cpp @@ -662,21 +662,10 @@ void WebCfgServer::handleOtaUpload() filename = "/" + filename; } Serial.print("handleFileUpload Name: "); Serial.println(filename); -// fsUploadFile = FILESYSTEM.open(filename, "w"); - filename = String(); } else if (upload.status == UPLOAD_FILE_WRITE) { - //DBG_OUTPUT_PORT.print("handleFileUpload Data: "); DBG_OUTPUT_PORT.println(upload.currentSize); -// if (fsUploadFile) { -// fsUploadFile.write(upload.buf, upload.currentSize); -// } - for(int i=0; i < upload.currentSize; i++) - { - Serial.print((char)upload.buf[i]); - } + Serial.println(upload.currentSize); + _ota.updateFirmware(*upload.buf, upload.currentSize); } else if (upload.status == UPLOAD_FILE_END) { -// if (fsUploadFile) { -// fsUploadFile.close(); -// } Serial.println(); Serial.print("handleFileUpload Size: "); Serial.println(upload.totalSize); } diff --git a/WebCfgServer.h b/WebCfgServer.h index 2b9a1ce..4bba569 100644 --- a/WebCfgServer.h +++ b/WebCfgServer.h @@ -5,6 +5,7 @@ #include "NukiWrapper.h" #include "Network.h" #include "NukiOpenerWrapper.h" +#include "Ota.h" enum class TokenType { @@ -54,6 +55,7 @@ private: NukiOpenerWrapper* _nukiOpener; Network* _network; Preferences* _preferences; + Ota _ota; bool _hasCredentials = false; char _credUser[20] = {0};