ESP32-简单OTA升级
基于ESP-IDF4.1
1 #include "freertos/FreeRTOS.h" 2 #include "freertos/task.h" 3 #include "esp_system.h" 4 #include "esp_event.h" 5 #include "esp_log.h" 6 #include "esp_ota_ops.h" 7 #include "esp_http_client.h" 8 #include "esp_https_ota.h" 9 #include "protocol_examples_common.h" 10 #include "string.h" 11 12 #include "nvs.h" 13 #include "nvs_flash.h" 14 #include "protocol_examples_common.h" 15 16 #if CONFIG_EXAMPLE_CONNECT_WIFI 17 #include "esp_wifi.h" 18 #endif 19 20 static const char *TAG = "simple_ota_example"; 21 extern const uint8_t server_cert_pem_start[] asm("_binary_ca_cert_pem_start"); 22 extern const uint8_t server_cert_pem_end[] asm("_binary_ca_cert_pem_end"); 23 24 #define OTA_URL_SIZE 256 25 26 //http事件处理程序 27 esp_err_t _http_event_handler(esp_http_client_event_t *evt) 28 { 29 switch (evt->event_id) { 30 case HTTP_EVENT_ERROR: 31 ESP_LOGD(TAG, "HTTP_EVENT_ERROR"); 32 break; 33 case HTTP_EVENT_ON_CONNECTED: 34 ESP_LOGD(TAG, "HTTP_EVENT_ON_CONNECTED"); 35 break; 36 case HTTP_EVENT_HEADER_SENT: 37 ESP_LOGD(TAG, "HTTP_EVENT_HEADER_SENT"); 38 break; 39 case HTTP_EVENT_ON_HEADER: 40 ESP_LOGD(TAG, "HTTP_EVENT_ON_HEADER, key=%s, value=%s", evt->header_key, evt->header_value); 41 break; 42 case HTTP_EVENT_ON_DATA: 43 ESP_LOGD(TAG, "HTTP_EVENT_ON_DATA, len=%d", evt->data_len); 44 break; 45 case HTTP_EVENT_ON_FINISH: 46 ESP_LOGD(TAG, "HTTP_EVENT_ON_FINISH"); 47 break; 48 case HTTP_EVENT_DISCONNECTED: 49 ESP_LOGD(TAG, "HTTP_EVENT_DISCONNECTED"); 50 break; 51 } 52 return ESP_OK; 53 } 54 55 //OTA任务 56 void simple_ota_example_task(void *pvParameter) 57 { 58 ESP_LOGI(TAG, "Starting OTA example"); 59 60 esp_http_client_config_t config = { 61 .url = CONFIG_EXAMPLE_FIRMWARE_UPGRADE_URL, 62 .cert_pem = (char *)server_cert_pem_start, 63 .event_handler = _http_event_handler, 64 }; 65 66 #ifdef CONFIG_EXAMPLE_FIRMWARE_UPGRADE_URL_FROM_STDIN 67 char url_buf[OTA_URL_SIZE]; 68 if (strcmp(config.url, "FROM_STDIN") == 0) { 69 example_configure_stdin_stdout(); 70 fgets(url_buf, OTA_URL_SIZE, stdin); 71 int len = strlen(url_buf); 72 url_buf[len - 1] = '\0'; 73 config.url = url_buf; 74 } else { 75 ESP_LOGE(TAG, "Configuration mismatch: wrong firmware upgrade image url"); 76 abort(); 77 } 78 #endif 79 80 #ifdef CONFIG_EXAMPLE_SKIP_COMMON_NAME_CHECK 81 config.skip_cert_common_name_check = true; 82 #endif 83 84 esp_err_t ret = esp_https_ota(&config); 85 if (ret == ESP_OK) { 86 esp_restart(); 87 } else { 88 ESP_LOGE(TAG, "Firmware upgrade failed"); 89 } 90 while (1) { 91 vTaskDelay(1000 / portTICK_PERIOD_MS); 92 } 93 } 94 95 void app_main(void) 96 { 97 // 初始化非易失性存储 98 esp_err_t err = nvs_flash_init(); 99 if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) { 100 //1.OTA应用程序分区表的NVS分区大小比非OTA分区表小。这种大小不匹配可能导致NVS初始化失败. 101 //2.NVS分区包含新格式的数据,此版本的代码无法识别。如果发生这种情况,我们将擦除NVS分区并再次初始化NVS. 102 ESP_ERROR_CHECK(nvs_flash_erase()); 103 err = nvs_flash_init(); 104 } 105 ESP_ERROR_CHECK(err); 106 107 ESP_ERROR_CHECK(esp_netif_init()); 108 ESP_ERROR_CHECK(esp_event_loop_create_default()); 109 110 //连接网络 111 ESP_ERROR_CHECK(example_connect()); 112 113 #if CONFIG_EXAMPLE_CONNECT_WIFI 114 //确保禁用低功耗模式,这样可以提供最佳的吞吐量,从而为整个OTA操作节省时间 115 esp_wifi_set_ps(WIFI_PS_NONE); 116 #endif 117 118 xTaskCreate(&simple_ota_example_task, "ota_example_task", 8192, NULL, 5, NULL); 119 }
原文:https://gitee.com/EspressifSystems/esp-idf/tree/master/examples/system/ota/simple_ota_example