From 2bbce4bc8d63e36fddc4ae57926e5af1b220a4c2 Mon Sep 17 00:00:00 2001 From: MiroZ Date: Fri, 17 May 2024 14:34:21 -0700 Subject: [PATCH] working OTA --- build.sh | 4 +++- main/App.cpp | 10 ++++++---- main/App.h | 4 +++- main/OTA.cpp | 45 +++++++++++++++++++++++++++++++++++---------- main/OTA.h | 8 +++++++- ota.sh | 17 +++++++++++++++++ sdkconfig | 4 ++-- sdkconfig.old | 4 ++-- 8 files changed, 75 insertions(+), 21 deletions(-) create mode 100755 ota.sh diff --git a/build.sh b/build.sh index 7fd126e..394aa74 100755 --- a/build.sh +++ b/build.sh @@ -3,10 +3,12 @@ # first device connected idf.py -DROLE=SENDER build + +cp build/wellhub.bin /var/www/esp_ota + ret_val=$? if (($ret_val != 0)); then exit fi -cp build/wellhub.bin /var/www/esp_ota diff --git a/main/App.cpp b/main/App.cpp index ecde379..187ea79 100644 --- a/main/App.cpp +++ b/main/App.cpp @@ -6,8 +6,8 @@ #include "Settings.h" #include "errors.h" -#include "ProvisionSoftAP.h" #include "OTA.h" +#include "ProvisionSoftAP.h" #define LED_PIN 26 @@ -27,7 +27,7 @@ void App::init() if(SETTINGS.wifi.num > 0) { // try connecting - m_led->setPulse(0, 255, 0); + m_led->setColor(0, 255, 0); uint32_t status = m_wifi->start(); if(status == WH_OK) { @@ -42,15 +42,17 @@ void App::init() ProvisionSoftAP provision(80); provision.start(); } - OTA ota; + + ota_update::OTA ota(*this); ota.start(); + m_led->setColor(0, 0, 0); } void App::start() { while(true) { - vTaskDelay(pdMS_TO_TICKS(10000)); + vTaskDelay(pdMS_TO_TICKS(10020)); } } diff --git a/main/App.h b/main/App.h index 7bf3358..4280473 100644 --- a/main/App.h +++ b/main/App.h @@ -12,9 +12,11 @@ class App { protected: - Led * m_led = nullptr; Wifi * m_wifi = nullptr; +public: + Led * m_led = nullptr; + public: App(); void init(); diff --git a/main/OTA.cpp b/main/OTA.cpp index 95c82f6..da3c973 100644 --- a/main/OTA.cpp +++ b/main/OTA.cpp @@ -4,24 +4,29 @@ * Created on: Apr 23, 2019 * Author: miro */ +#include "App.h" #include "esp_system.h" +#include "esp_wifi.h" #include "esp_log.h" #include "esp_ota_ops.h" - +#include "esp_http_client.h" #include "esp_flash_partitions.h" #include "esp_partition.h" #include "string.h" #include "OTA.h" -#define EXAMPLE_SERVER_URL "http://192.168.1.131/wellhub.bin" + +#define OTA_URL "http://192.168.1.131/wellhub.bin" static const char *TAG = "OTA"; #define _FORCE_UPDATE +namespace ota_update +{ -OTA::OTA() +OTA::OTA(App & app) : m_app(app) { } @@ -66,7 +71,7 @@ void OTA::start() running->address); esp_http_client_config_t config = { }; - config.url = EXAMPLE_SERVER_URL; + config.url = OTA_URL; esp_http_client_handle_t client = esp_http_client_init(&config); if (client == NULL) @@ -82,6 +87,10 @@ void OTA::start() } esp_http_client_fetch_headers(client); + int remote_len = esp_http_client_get_content_length(client); + + ESP_LOGI(TAG,"remote len %d", remote_len); + update_partition = esp_ota_get_next_update_partition(NULL); ESP_LOGI(TAG, "Writing to partition subtype %d at offset 0x%x", update_partition->subtype, update_partition->address); @@ -102,7 +111,7 @@ void OTA::start() } else if (data_read > 0) { - printf("."); + //printf("."); if (image_header_was_checked == false) { // this is the first 1k of received image @@ -119,7 +128,7 @@ void OTA::start() ESP_LOGI(TAG, " c time: %s", new_app_info.time); ESP_LOGI(TAG, " c date: %s", new_app_info.date); ESP_LOGI(TAG, "idf ver: %s", new_app_info.idf_ver); -// ESP_LOGI(TAG, " sha: %s", new_app_info.app_elf_sha256); + //ESP_LOGI(TAG, " sha: %s", new_app_info.app_elf_sha256); esp_app_desc_t running_app_info; if (esp_ota_get_partition_description(running, &running_app_info) == ESP_OK) @@ -131,7 +140,7 @@ void OTA::start() ESP_LOGI(TAG, " c time: %s", running_app_info.time); ESP_LOGI(TAG, " c date: %s", running_app_info.date); ESP_LOGI(TAG, "idf ver: %s", running_app_info.idf_ver); -// ESP_LOGI(TAG, " sha: %s", running_app_info.app_elf_sha256); + // ESP_LOGI(TAG, " sha: %s", running_app_info.app_elf_sha256); } const esp_partition_t* last_invalid_app = esp_ota_get_last_invalid_partition(); @@ -157,14 +166,17 @@ void OTA::start() #ifndef FORCE_UPDATE if (memcmp(new_app_info.version, running_app_info.version, sizeof(new_app_info.version) + sizeof(new_app_info.project_name) + sizeof(new_app_info.time) + sizeof(new_app_info.date)) == 0) { - ESP_LOGW(TAG, "Current running version is the same as a new. We will not continue the update."); + ESP_LOGW(TAG, "Current running version is the same as a remote. No need for update."); httpCleanup(client); return; } #endif + m_app.m_led->setPulse(255, 0, 255); + image_header_was_checked = true; err = esp_ota_begin(update_partition, OTA_SIZE_UNKNOWN, &update_handle); + if (err != ESP_OK) { ESP_LOGE(TAG, "esp_ota_begin failed (%s)", esp_err_to_name(err)); @@ -175,10 +187,11 @@ void OTA::start() } else { - ESP_LOGE(TAG, "received package is not fit len"); + ESP_LOGE(TAG, "received package does not fit len"); httpCleanup(client); taskFatalError(); } + } err = esp_ota_write(update_handle, (const void *) otaWriteData, data_read); @@ -188,7 +201,16 @@ void OTA::start() taskFatalError(); } binary_file_length += data_read; - ESP_LOGD(TAG, "Written image length %d", binary_file_length); + + int progress = binary_file_length*100/remote_len; + static int last_reported = -1; + + if(last_reported != progress) + { + last_reported = progress; + ESP_LOGI(TAG, "%d%%", last_reported); + } + } else if (data_read == 0) { @@ -208,6 +230,8 @@ void OTA::start() taskFatalError(); } + ESP_LOGI(TAG, "esp_ota_end"); + err = esp_ota_set_boot_partition(update_partition); if (err != ESP_OK) { @@ -222,4 +246,5 @@ void OTA::start() OTA::~OTA() { +} } \ No newline at end of file diff --git a/main/OTA.h b/main/OTA.h index 0b62bf4..c2fa5a9 100644 --- a/main/OTA.h +++ b/main/OTA.h @@ -12,8 +12,12 @@ #define BUFFSIZE 1024 #define HASH_LEN 32 /* SHA-256 digest length */ +namespace ota_update +{ +#include "App.h" #include "esp_http_client.h" + class OTA { public: @@ -22,13 +26,15 @@ public: private: char otaWriteData[BUFFSIZE + 1] = { 0 }; void httpCleanup(esp_http_client_handle_t client); + App & m_app; private: void taskFatalError(); public: - OTA(); + OTA(App & app); virtual ~OTA(); }; +} #endif /* __OTA_H__ */ \ No newline at end of file diff --git a/ota.sh b/ota.sh new file mode 100755 index 0000000..f35955a --- /dev/null +++ b/ota.sh @@ -0,0 +1,17 @@ +#!/bin/bash + +# first device connected + +idf.py clean +idf.py -DROLE=SENDER build + +ret_val=$? + +if (($ret_val != 0)); then + exit +fi + +cp build/wellhub.bin /var/www/esp_ota + +idf.py -p /dev/ttyUSB1 monitor -B 450000 + diff --git a/sdkconfig b/sdkconfig index 8c15246..c1131ad 100644 --- a/sdkconfig +++ b/sdkconfig @@ -586,7 +586,7 @@ CONFIG_ESP_SYSTEM_PANIC_PRINT_REBOOT=y CONFIG_ESP_SYSTEM_EVENT_QUEUE_SIZE=32 CONFIG_ESP_SYSTEM_EVENT_TASK_STACK_SIZE=2304 -CONFIG_ESP_MAIN_TASK_STACK_SIZE=3584 +CONFIG_ESP_MAIN_TASK_STACK_SIZE=4584 CONFIG_ESP_MAIN_TASK_AFFINITY_CPU0=y # CONFIG_ESP_MAIN_TASK_AFFINITY_CPU1 is not set # CONFIG_ESP_MAIN_TASK_AFFINITY_NO_AFFINITY is not set @@ -1452,7 +1452,7 @@ CONFIG_ESP32S2_PANIC_PRINT_REBOOT=y # CONFIG_ESP32S2_PANIC_GDBSTUB is not set CONFIG_SYSTEM_EVENT_QUEUE_SIZE=32 CONFIG_SYSTEM_EVENT_TASK_STACK_SIZE=2304 -CONFIG_MAIN_TASK_STACK_SIZE=3584 +CONFIG_MAIN_TASK_STACK_SIZE=4584 # CONFIG_CONSOLE_UART_DEFAULT is not set CONFIG_CONSOLE_UART_CUSTOM=y # CONFIG_ESP_CONSOLE_UART_NONE is not set diff --git a/sdkconfig.old b/sdkconfig.old index 98b54fc..0dcc2da 100644 --- a/sdkconfig.old +++ b/sdkconfig.old @@ -45,7 +45,7 @@ CONFIG_ENABLE_ARDUINO_DEPENDS=y CONFIG_ARDUINO_RUN_CORE1=y # CONFIG_ARDUINO_RUN_NO_AFFINITY is not set CONFIG_ARDUINO_RUNNING_CORE=1 -CONFIG_ARDUINO_LOOP_STACK_SIZE=8192 +CONFIG_ARDUINO_LOOP_STACK_SIZE=1024 # CONFIG_ARDUINO_EVENT_RUN_CORE0 is not set CONFIG_ARDUINO_EVENT_RUN_CORE1=y # CONFIG_ARDUINO_EVENT_RUN_NO_AFFINITY is not set @@ -586,7 +586,7 @@ CONFIG_ESP_SYSTEM_PANIC_PRINT_REBOOT=y CONFIG_ESP_SYSTEM_EVENT_QUEUE_SIZE=32 CONFIG_ESP_SYSTEM_EVENT_TASK_STACK_SIZE=2304 -CONFIG_ESP_MAIN_TASK_STACK_SIZE=3584 +CONFIG_ESP_MAIN_TASK_STACK_SIZE=35840 CONFIG_ESP_MAIN_TASK_AFFINITY_CPU0=y # CONFIG_ESP_MAIN_TASK_AFFINITY_CPU1 is not set # CONFIG_ESP_MAIN_TASK_AFFINITY_NO_AFFINITY is not set