代碼下載地址:
https://download.csdn.net/download/qq_31806069/11239781
1、 先搭建一個HTTP服務(wù)器
先將hello_wordl例子拷貝到~/esp中威始,在hello_wordl/build中創(chuàng)建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推沸、服務(wù)器運行后竭缝,構(gòu)建目錄的內(nèi)容可以通過網(wǎng)址 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文件中函數(shù)
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
可以看到已經(jīng)開始在運行hello-world.bin的例子凿跳。