working OTA

This commit is contained in:
Miro Zmrzli 2024-05-17 14:34:21 -07:00
parent b9f10bbd30
commit 2bbce4bc8d
8 changed files with 75 additions and 21 deletions

View File

@ -3,10 +3,12 @@
# first device connected # first device connected
idf.py -DROLE=SENDER build idf.py -DROLE=SENDER build
cp build/wellhub.bin /var/www/esp_ota
ret_val=$? ret_val=$?
if (($ret_val != 0)); then if (($ret_val != 0)); then
exit exit
fi fi
cp build/wellhub.bin /var/www/esp_ota

View File

@ -6,8 +6,8 @@
#include "Settings.h" #include "Settings.h"
#include "errors.h" #include "errors.h"
#include "ProvisionSoftAP.h"
#include "OTA.h" #include "OTA.h"
#include "ProvisionSoftAP.h"
#define LED_PIN 26 #define LED_PIN 26
@ -27,7 +27,7 @@ void App::init()
if(SETTINGS.wifi.num > 0) if(SETTINGS.wifi.num > 0)
{ {
// try connecting // try connecting
m_led->setPulse(0, 255, 0); m_led->setColor(0, 255, 0);
uint32_t status = m_wifi->start(); uint32_t status = m_wifi->start();
if(status == WH_OK) if(status == WH_OK)
{ {
@ -42,15 +42,17 @@ void App::init()
ProvisionSoftAP provision(80); ProvisionSoftAP provision(80);
provision.start(); provision.start();
} }
OTA ota;
ota_update::OTA ota(*this);
ota.start(); ota.start();
m_led->setColor(0, 0, 0);
} }
void App::start() void App::start()
{ {
while(true) while(true)
{ {
vTaskDelay(pdMS_TO_TICKS(10000)); vTaskDelay(pdMS_TO_TICKS(10020));
} }
} }

View File

@ -12,9 +12,11 @@
class App class App
{ {
protected: protected:
Led * m_led = nullptr;
Wifi * m_wifi = nullptr; Wifi * m_wifi = nullptr;
public:
Led * m_led = nullptr;
public: public:
App(); App();
void init(); void init();

View File

@ -4,24 +4,29 @@
* Created on: Apr 23, 2019 * Created on: Apr 23, 2019
* Author: miro * Author: miro
*/ */
#include "App.h"
#include "esp_system.h" #include "esp_system.h"
#include "esp_wifi.h"
#include "esp_log.h" #include "esp_log.h"
#include "esp_ota_ops.h" #include "esp_ota_ops.h"
#include "esp_http_client.h"
#include "esp_flash_partitions.h" #include "esp_flash_partitions.h"
#include "esp_partition.h" #include "esp_partition.h"
#include "string.h" #include "string.h"
#include "OTA.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"; static const char *TAG = "OTA";
#define _FORCE_UPDATE #define _FORCE_UPDATE
namespace ota_update
{
OTA::OTA() OTA::OTA(App & app) : m_app(app)
{ {
} }
@ -66,7 +71,7 @@ void OTA::start()
running->address); running->address);
esp_http_client_config_t config = { }; 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); esp_http_client_handle_t client = esp_http_client_init(&config);
if (client == NULL) if (client == NULL)
@ -82,6 +87,10 @@ void OTA::start()
} }
esp_http_client_fetch_headers(client); 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); update_partition = esp_ota_get_next_update_partition(NULL);
ESP_LOGI(TAG, "Writing to partition subtype %d at offset 0x%x", update_partition->subtype, ESP_LOGI(TAG, "Writing to partition subtype %d at offset 0x%x", update_partition->subtype,
update_partition->address); update_partition->address);
@ -102,7 +111,7 @@ void OTA::start()
} }
else if (data_read > 0) else if (data_read > 0)
{ {
printf("."); //printf(".");
if (image_header_was_checked == false) if (image_header_was_checked == false)
{ {
// this is the first 1k of received image // 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 time: %s", new_app_info.time);
ESP_LOGI(TAG, " c date: %s", new_app_info.date); ESP_LOGI(TAG, " c date: %s", new_app_info.date);
ESP_LOGI(TAG, "idf ver: %s", new_app_info.idf_ver); 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; esp_app_desc_t running_app_info;
if (esp_ota_get_partition_description(running, &running_app_info) == ESP_OK) 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 time: %s", running_app_info.time);
ESP_LOGI(TAG, " c date: %s", running_app_info.date); ESP_LOGI(TAG, " c date: %s", running_app_info.date);
ESP_LOGI(TAG, "idf ver: %s", running_app_info.idf_ver); 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(); const esp_partition_t* last_invalid_app = esp_ota_get_last_invalid_partition();
@ -157,14 +166,17 @@ void OTA::start()
#ifndef FORCE_UPDATE #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) 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); httpCleanup(client);
return; return;
} }
#endif #endif
m_app.m_led->setPulse(255, 0, 255);
image_header_was_checked = true; image_header_was_checked = true;
err = esp_ota_begin(update_partition, OTA_SIZE_UNKNOWN, &update_handle); err = esp_ota_begin(update_partition, OTA_SIZE_UNKNOWN, &update_handle);
if (err != ESP_OK) if (err != ESP_OK)
{ {
ESP_LOGE(TAG, "esp_ota_begin failed (%s)", esp_err_to_name(err)); ESP_LOGE(TAG, "esp_ota_begin failed (%s)", esp_err_to_name(err));
@ -175,10 +187,11 @@ void OTA::start()
} }
else else
{ {
ESP_LOGE(TAG, "received package is not fit len"); ESP_LOGE(TAG, "received package does not fit len");
httpCleanup(client); httpCleanup(client);
taskFatalError(); taskFatalError();
} }
} }
err = esp_ota_write(update_handle, (const void *) otaWriteData, data_read); err = esp_ota_write(update_handle, (const void *) otaWriteData, data_read);
@ -188,7 +201,16 @@ void OTA::start()
taskFatalError(); taskFatalError();
} }
binary_file_length += data_read; 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) else if (data_read == 0)
{ {
@ -208,6 +230,8 @@ void OTA::start()
taskFatalError(); taskFatalError();
} }
ESP_LOGI(TAG, "esp_ota_end");
err = esp_ota_set_boot_partition(update_partition); err = esp_ota_set_boot_partition(update_partition);
if (err != ESP_OK) if (err != ESP_OK)
{ {
@ -223,3 +247,4 @@ void OTA::start()
OTA::~OTA() OTA::~OTA()
{ {
} }
}

View File

@ -12,8 +12,12 @@
#define BUFFSIZE 1024 #define BUFFSIZE 1024
#define HASH_LEN 32 /* SHA-256 digest length */ #define HASH_LEN 32 /* SHA-256 digest length */
namespace ota_update
{
#include "App.h"
#include "esp_http_client.h" #include "esp_http_client.h"
class OTA class OTA
{ {
public: public:
@ -22,13 +26,15 @@ public:
private: private:
char otaWriteData[BUFFSIZE + 1] = { 0 }; char otaWriteData[BUFFSIZE + 1] = { 0 };
void httpCleanup(esp_http_client_handle_t client); void httpCleanup(esp_http_client_handle_t client);
App & m_app;
private: private:
void taskFatalError(); void taskFatalError();
public: public:
OTA(); OTA(App & app);
virtual ~OTA(); virtual ~OTA();
}; };
}
#endif /* __OTA_H__ */ #endif /* __OTA_H__ */

17
ota.sh Executable file
View File

@ -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

View File

@ -586,7 +586,7 @@ CONFIG_ESP_SYSTEM_PANIC_PRINT_REBOOT=y
CONFIG_ESP_SYSTEM_EVENT_QUEUE_SIZE=32 CONFIG_ESP_SYSTEM_EVENT_QUEUE_SIZE=32
CONFIG_ESP_SYSTEM_EVENT_TASK_STACK_SIZE=2304 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_CPU0=y
# CONFIG_ESP_MAIN_TASK_AFFINITY_CPU1 is not set # CONFIG_ESP_MAIN_TASK_AFFINITY_CPU1 is not set
# CONFIG_ESP_MAIN_TASK_AFFINITY_NO_AFFINITY 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_ESP32S2_PANIC_GDBSTUB is not set
CONFIG_SYSTEM_EVENT_QUEUE_SIZE=32 CONFIG_SYSTEM_EVENT_QUEUE_SIZE=32
CONFIG_SYSTEM_EVENT_TASK_STACK_SIZE=2304 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_DEFAULT is not set
CONFIG_CONSOLE_UART_CUSTOM=y CONFIG_CONSOLE_UART_CUSTOM=y
# CONFIG_ESP_CONSOLE_UART_NONE is not set # CONFIG_ESP_CONSOLE_UART_NONE is not set

View File

@ -45,7 +45,7 @@ CONFIG_ENABLE_ARDUINO_DEPENDS=y
CONFIG_ARDUINO_RUN_CORE1=y CONFIG_ARDUINO_RUN_CORE1=y
# CONFIG_ARDUINO_RUN_NO_AFFINITY is not set # CONFIG_ARDUINO_RUN_NO_AFFINITY is not set
CONFIG_ARDUINO_RUNNING_CORE=1 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_CORE0 is not set
CONFIG_ARDUINO_EVENT_RUN_CORE1=y CONFIG_ARDUINO_EVENT_RUN_CORE1=y
# CONFIG_ARDUINO_EVENT_RUN_NO_AFFINITY is not set # 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_QUEUE_SIZE=32
CONFIG_ESP_SYSTEM_EVENT_TASK_STACK_SIZE=2304 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_CPU0=y
# CONFIG_ESP_MAIN_TASK_AFFINITY_CPU1 is not set # CONFIG_ESP_MAIN_TASK_AFFINITY_CPU1 is not set
# CONFIG_ESP_MAIN_TASK_AFFINITY_NO_AFFINITY is not set # CONFIG_ESP_MAIN_TASK_AFFINITY_NO_AFFINITY is not set