概述
转载:esp32 http空中升级
代码下载地址:
https://download.csdn.net/download/qq_31806069/11239781
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 <span class="token operator">=</span> <span class="token function">esp_https_ota</span><span class="token punctuation">(</span><span class="token operator">&</span>config<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span>ret <span class="token operator">==</span> ESP_OK<span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token function">esp_restart</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
<span class="token function">ESP_LOGE</span><span class="token punctuation">(</span>TAG<span class="token punctuation">,</span> <span class="token string">"Firmware Upgrades Failed:%dn"</span><span class="token punctuation">,</span>ret<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">while</span> <span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token function">vTaskDelay</span><span class="token punctuation">(</span><span class="token number">1000</span> <span class="token operator">/</span> portTICK_PERIOD_MS<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
}
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();
}
<span class="token function">ESP_ERROR_CHECK</span><span class="token punctuation">(</span>ret<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">app_wifi_initialise</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">ESP_LOGI</span><span class="token punctuation">(</span>TAG<span class="token punctuation">,</span> <span class="token string">"1------------------>Connected to AP, begin http examplen"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">app_wifi_wait_connected</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">ESP_LOGI</span><span class="token punctuation">(</span>TAG<span class="token punctuation">,</span> <span class="token string">"2------------------>Connected to AP, begin http examplen"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">xTaskCreate</span><span class="token punctuation">(</span><span class="token operator">&</span>http_test_task<span class="token punctuation">,</span> <span class="token string">"http_test_task"</span><span class="token punctuation">,</span> <span class="token number">8192</span><span class="token punctuation">,</span> <span class="token constant">NULL</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token constant">NULL</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">while</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token function">vTaskDelay</span><span class="token punctuation">(</span><span class="token number">1000</span> <span class="token operator">/</span> portTICK_PERIOD_MS<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"Restarting now.n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">fflush</span><span class="token punctuation">(</span><span class="token constant">stdout</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">esp_restart</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
}
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
可以看到已经开始在运行hello-world.bin的例子。
最后
以上就是彪壮跳跳糖为你收集整理的esp32 http空中升级的全部内容,希望文章能够帮你解决esp32 http空中升级所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复