From da610effc9e0f60c9cf7f8d45bdd82021d9f9524 Mon Sep 17 00:00:00 2001 From: Sebastien Date: Sun, 29 Sep 2019 08:27:12 -0400 Subject: [PATCH] stable initial OTA! --- components/cmd_system/cmd_system.c | 11 +++++++++-- components/squeezelite-ota/component.mk | 2 +- components/squeezelite-ota/squeezelite-ota.c | 20 +++++++++++++++----- main/esp_app_main.c | 8 +++++--- 4 files changed, 30 insertions(+), 11 deletions(-) diff --git a/components/cmd_system/cmd_system.c b/components/cmd_system/cmd_system.c index 32f1f5c9..a8816e9e 100644 --- a/components/cmd_system/cmd_system.c +++ b/components/cmd_system/cmd_system.c @@ -24,6 +24,8 @@ #include "esp32/rom/uart.h" #include "cmd_system.h" #include "sdkconfig.h" +#include "esp_partition.h" +#include "esp_ota_ops.h" #ifdef CONFIG_FREERTOS_USE_STATS_FORMATTING_FUNCTIONS #define WITH_TASKS_INFO 1 @@ -102,9 +104,14 @@ static int restart(int argc, char **argv) void guided_factory() { ESP_LOGI(TAG, "Rebooting to factory."); - uint32_t *p_force_factory_magic = (uint32_t *)LWS_MAGIC_REBOOT_TYPE_ADS; - *p_force_factory_magic = LWS_MAGIC_REBOOT_TYPE_REQ_FACTORY; + const esp_partition_t *partition; + esp_partition_iterator_t it = esp_partition_find(ESP_PARTITION_TYPE_APP, ESP_PARTITION_SUBTYPE_APP_FACTORY, "factory"); + partition = (esp_partition_t *) esp_partition_get(it); + if(partition != NULL){ + esp_ota_set_boot_partition(partition); + } + esp_partition_iterator_release(it); esp_restart(); } diff --git a/components/squeezelite-ota/component.mk b/components/squeezelite-ota/component.mk index 2905a71a..12966858 100644 --- a/components/squeezelite-ota/component.mk +++ b/components/squeezelite-ota/component.mk @@ -6,5 +6,5 @@ # todo: add support for https COMPONENT_ADD_INCLUDEDIRS := . COMPONENT_ADD_INCLUDEDIRS += include -CFLAGS += -DLOG_LOCAL_LEVEL=ESP_LOG_DEBUG -DCONFIG_OTA_ALLOW_HTTP=1 +CFLAGS += -DLOG_LOCAL_LEVEL=ESP_LOG_INFO -DCONFIG_OTA_ALLOW_HTTP=1 COMPONENT_EMBED_TXTFILES := ${PROJECT_PATH}/server_certs/github.pem \ No newline at end of file diff --git a/components/squeezelite-ota/squeezelite-ota.c b/components/squeezelite-ota/squeezelite-ota.c index 91bd67ae..ae0d52e9 100644 --- a/components/squeezelite-ota/squeezelite-ota.c +++ b/components/squeezelite-ota/squeezelite-ota.c @@ -121,17 +121,18 @@ esp_err_t _http_event_handler(esp_http_client_event_t *evt) } break; case HTTP_EVENT_ON_DATA: + vTaskDelay(5/ portTICK_RATE_MS); if(!ota_status.bOTAStarted) { ESP_LOGD(TAG, "HTTP_EVENT_ON_DATA, status_code=%d, len=%d",esp_http_client_get_status_code(evt->client), evt->data_len); } else if(ota_status.bOTAStarted && esp_http_client_get_status_code(evt->client) == 200 ){ ota_status.ota_actual_len+=evt->data_len; - if(ota_get_pct_complete()%5 == 0) newpct = ota_get_pct_complete(); + if(ota_get_pct_complete()%2 == 0) newpct = ota_get_pct_complete(); if(lastpct!=newpct ) { wifi_manager_refresh_ota_json(); lastpct=newpct; - ESP_LOGD(TAG,"Receiving OTA data chunk len: %d, %d of %d (%d pct)", evt->data_len, ota_status.ota_actual_len, ota_status.ota_total_len, newpct); + ESP_LOGI(TAG,"Receiving OTA data chunk len: %d, %d of %d (%d pct)", evt->data_len, ota_status.ota_actual_len, ota_status.ota_total_len, newpct); ESP_LOGD(TAG,"Heap internal:%zu (min:%zu) external:%zu (min:%zu)\n", heap_caps_get_free_size(MALLOC_CAP_INTERNAL), heap_caps_get_minimum_free_size(MALLOC_CAP_INTERNAL), @@ -183,7 +184,7 @@ esp_err_t init_config(esp_http_client_config_t * conf, const char * url){ memset(conf, 0x00, sizeof(esp_http_client_config_t)); conf->cert_pem = (char *)server_cert_pem_start; conf->event_handler = _http_event_handler; - conf->buffer_size = 1024*8; + conf->buffer_size = 2048; conf->disable_auto_redirect=true; conf->skip_cert_common_name_check = false; conf->url = strdup(url); @@ -276,9 +277,18 @@ void start_ota(const char * bin_url) nvs_close(nvs); } ESP_LOGI(TAG, "Waiting for other processes to start"); - vTaskDelay(2500/ portTICK_RATE_MS); + for(int i=0;i<10;i++){ + vTaskDelay(1000/ portTICK_RATE_MS); + } +#ifdef CONFIG_ESP32_WIFI_TASK_PINNED_TO_CORE_1 +#define OTA_CORE 0 +#warning "Wifi running on core 1" +#else +#define OTA_CORE 1 + #endif ESP_LOGI(TAG, "Starting ota: %s", urlPtr); - ret=xTaskCreate(&ota_task, "ota_task", 1024*20,(void *) urlPtr, 4, NULL); + ret=xTaskCreatePinnedToCore(&ota_task, "ota_task", 1024*40, (void *)urlPtr, tskIDLE_PRIORITY+3, NULL, OTA_CORE); + if (ret != pdPASS) { ESP_LOGI(TAG, "create thread %s failed", "ota_task"); } diff --git a/main/esp_app_main.c b/main/esp_app_main.c index 1a141ea3..256db528 100644 --- a/main/esp_app_main.c +++ b/main/esp_app_main.c @@ -102,13 +102,15 @@ void app_main() wifi_manager_set_callback(WIFI_EVENT_STA_DISCONNECTED, &cb_connection_sta_disconnected); char * fwurl = get_nvs_value_alloc(NVS_TYPE_STR, "fwurl"); - if(fwurl){ + if(fwurl && strlen(fwurl)>0){ while(!bWifiConnected){ wait_for_wifi(); } ESP_LOGI(TAG,"Updating firmware from link: %s",fwurl); start_ota(fwurl); } - - console_start(); + else + { + console_start(); + } }