esp32 http空中升级

2018-11-28  本文已影响338人  昨天剩下的一杯冷茶

1、 先搭建一个HTTP服务器
先将hello_wordl例子拷贝到~/esp中,在hello_wordl/build中创建get文件夹,且将hello-world.bin 放进去。

cp -r $IDF_PATH/examples/get-started/hello_world .
cd hello_world/build/
 mkdir get
 cd get/
cp ~/esp/hello_world/build/hello-world.bin .
 cd ..
python -m SimpleHTTPServer 8070

提示:
1.1、服务器运行后,构建目录的内容可以通过网址 http://localhost:8070/ 浏览到。
1.2、将你的 PC 连接到你将会在 ESP32 上面使用的同一个 AP。

2、 先将hello-world.bin 拷贝到~/esp中且命名"my_ota"
a、 先配置menuconfig
make menuconfig
--Serial flasher conifg --->
----Default serial port
------COM3
--Partition Table --->
----Partition Table (Factory app, two OTA definitions) --->
------选择Factory app,two OTA definitions

b、先添加文件
app_wifi.c
app_wifi.h
esp_https_ota.c
esp_https_ota.h

c、 修改代码

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_spi_flash.h"

#include "nvs_flash.h"
#include "app_wifi.h"
#include "esp_log.h"
#include "esp_http_client.h"
#include "esp_https_ota.h"

static const char *TAG = "MY_OTA";


esp_err_t _http_event_handler(esp_http_client_event_t *evt)
{
    switch(evt->event_id)
    {
    case HTTP_EVENT_ERROR:
        ESP_LOGD(TAG, "HTTP_EVENT_ERROR");
        break;
    case HTTP_EVENT_ON_CONNECTED:
        ESP_LOGD(TAG, "HTTP_EVENT_ON_CONNECTED");
        break;
    case HTTP_EVENT_HEADER_SENT:
        ESP_LOGD(TAG, "HTTP_EVENT_HEADER_SENT");
        break;
    case HTTP_EVENT_ON_HEADER:
        ESP_LOGD(TAG, "HTTP_EVENT_ON_HEADER, key=%s, value=%s", evt->header_key, evt->header_value);
        break;
    case HTTP_EVENT_ON_DATA:
        ESP_LOGD(TAG, "HTTP_EVENT_ON_DATA, len=%d", evt->data_len);
        if (!esp_http_client_is_chunked_response(evt->client))
        {
            // Write out data
            //printf("%.*s", evt->data_len, (char*)evt->data);
        }
        break;
    case HTTP_EVENT_ON_FINISH:
        ESP_LOGD(TAG, "HTTP_EVENT_ON_FINISH");
        break;
    case HTTP_EVENT_DISCONNECTED:
        ESP_LOGD(TAG, "HTTP_EVENT_DISCONNECTED");
        break;
    }
    return ESP_OK;
}


static void http_test_task(void *pvParameters)
{
    esp_http_client_config_t config =
    {
        //.url = "http://192.168.20.96:8070/get/5FD8.bin",
        .url = "http://192.168.20.96:8070/get/hello-world.bin",
        .event_handler = _http_event_handler,
    };

    esp_err_t ret = esp_https_ota(&config);
    if (ret == ESP_OK) {
        esp_restart();
    } else {
        ESP_LOGE(TAG, "Firmware Upgrades Failed:%d\n",ret);
    }
    while (1) {
        vTaskDelay(1000 / portTICK_PERIOD_MS);
    }

}

void app_main()
{
    printf("Hello world!\n");
    esp_err_t ret = nvs_flash_init();
    if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND)
    {
        ESP_ERROR_CHECK(nvs_flash_erase());
        ret = nvs_flash_init();
    }

    ESP_ERROR_CHECK(ret);
    app_wifi_initialise();

    ESP_LOGI(TAG, "1------------------>Connected to AP, begin http example\n");
    app_wifi_wait_connected();
    ESP_LOGI(TAG, "2------------------>Connected to AP, begin http example\n");
    xTaskCreate(&http_test_task, "http_test_task", 8192, NULL, 5, NULL);

    while(1)
    {
        vTaskDelay(1000 / portTICK_PERIOD_MS);
    }

    printf("Restarting now.\n");
    fflush(stdout);
    esp_restart();
}


d、修改esp_https_ota.c文件中函数
esp_err_t esp_https_ota(constesp_http_client_config_t *config),将几条语句屏蔽。

/*
    if (!config->cert_pem)
    {
        ESP_LOGE(TAG, "Server certificate not found in esp_http_client config");
        return ESP_FAIL;
    }
*/

3、 测试
a、make erase_flash flash monitor


image.png

可以看到已经开始在运行hello-world.bin的例子。


image.png
上一篇 下一篇

猜你喜欢

热点阅读