当前位置:网站首页>图文手把手教程--ESP32 OTA空中升级(VSCODE+IDF)
图文手把手教程--ESP32 OTA空中升级(VSCODE+IDF)
2022-08-04 10:22:00 【淘梦TaoDream】
本文内容
1)使用hello_world例程,编译生成hello_world.bin文件,并且开启HTTP本地服务器。
2)使用simple_ota_example例程,通过HTTP服务器访问hello_world.bin,进行OTA升级。
一、首先完成集成开发环境搭建:
图文手把手教程(史上最强):windows下ESP32集成开发环境搭建和HelloWorld显示(乐鑫官方推荐方法-使用VSCode安装ESP-IDF插件)
二、创建hello_world例程,编译生成hello_world.bin文件。
1)创建示例项目hello_world:VSCODE中->"查看"->”命令面板“->输入:Show Examples projects->选择Use current ESP-IDF(E:\ESP32-IDF\esp\esp-idf)->弹出示例ESP-IDF Examples,选择get-started->hello_world->Create project using example hello_world->选择示例保存的路径。
例如:E:\ESP32-IDF\project-example,因ESP-IDF框架与示例是分离的,所以示例保存的路径可以随意,但需要注意:路径不能有中文和空格,否则报错。
创建完后,如下图所示。
2)一字不改,编译、下载到ESP32开发板。
打开串口监控,显示信息如下图所示,这里说明这个,是因为后面OTA后,显示的信息要和这个一样,才证明OTA成功。
展开build,可以看到生成了hello_world.bin文件。
三、hello_world例程中运行HTTP本地服务器。
1)编译链内Python 有一个内置的 HTTP 服务器,我们使用命令运行即可。
python -m http.server 8070
2)hello_world例程中运行HTTP本地服务器,输入上面的命令后按Enter,如下图所示。
3)打开浏览器,输入地址,例如我自己的地址是:http://192.168.31.107:8070/
注意:地址的组成是IP地址+端口号(8070),IP地址是自己电脑的IPv4地址,每个人都不同。
打开成功,VSCODE中会显示GET请求。
4)进入build目录,可以看到hello_world.bin文件,点击可以正常下载。
5)右键hello_world.bin,在弹出框中“复制链接地址”,后面的OTA会用到,例如我的是:http://192.168.31.107:8070/build/hello_world.bin
注意:关闭hello_world例程,会将HTTP本地服务器也关闭,后续OTA升级需要重新打开。
四、创建simple_ota_example例程,并修改代码。
1)创建过程,按上面hello_world的来,这里不再详述。
2)创建完后,如下图所示。
3)一字不改,编译、下载到ESP32开发板。
首次下载,需要设置COM口,UART。
可以看到,代码什么也不修改,会提示wifi没有连接,如下图所示。
4)增加wifi连接代码。
#include "freertos/event_groups.h" //增加头文件
//wifi连上事件定义
static EventGroupHandle_t wifi_event_group;
const static int CONNECTED_BIT = BIT0;
//wifi事件处理
static esp_err_t wifi_event_handler(void *ctx, system_event_t *event)
{
switch (event->event_id) {
case SYSTEM_EVENT_STA_START:
esp_wifi_connect();
break;
case SYSTEM_EVENT_STA_GOT_IP:
xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
esp_wifi_connect();
xEventGroupClearBits(wifi_event_group, CONNECTED_BIT);
break;
default:
break;
}
return ESP_OK;
}
//wifi初始化
static void wifi_init(void)
{
tcpip_adapter_init();
wifi_event_group = xEventGroupCreate();
ESP_ERROR_CHECK(esp_event_loop_init(wifi_event_handler, NULL));
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
wifi_config_t wifi_config = {
.sta = {
.ssid = "Xiaomi_tao", //路由器wifi帐号
.password = "123456", //路由器wifi密码
},
};
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config));
ESP_LOGI(TAG, "start the WIFI SSID:[%s]", wifi_config.sta.ssid);
ESP_ERROR_CHECK(esp_wifi_start());
ESP_LOGI(TAG, "Waiting for wifi");
//等待wifi连上
xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT, false, true, portMAX_DELAY);
}
5)修改OTA任务函数--simple_ota_example_task(),将url改为自己本机IP地址。
//http客户端配置
esp_http_client_config_t config = {
//.url = CONFIG_EXAMPLE_FIRMWARE_UPGRADE_URL, //注释掉
.url = "http://192.168.31.107:8070/build/hello_world.bin",//增加
.cert_pem = (char *)server_cert_pem_start,
.event_handler = _http_event_handler,
.keep_alive_enable = true,
#ifdef CONFIG_EXAMPLE_FIRMWARE_UPGRADE_BIND_IF
.if_name = &ifr,
#endif
};
6)修改app_main()函数。
void app_main(void)
{
// Initialize NVS.
esp_err_t err = nvs_flash_init();
if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
// 1.OTA app partition table has a smaller NVS partition size than the non-OTA
// partition table. This size mismatch may cause NVS initialization to fail.
// 2.NVS partition contains data in new format and cannot be recognized by this version of code.
// If this happens, we erase NVS partition and initialize NVS again.
ESP_ERROR_CHECK(nvs_flash_erase());
err = nvs_flash_init();
}
ESP_ERROR_CHECK(err);
get_sha256_of_partitions();
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
/* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig.
* Read "Establishing Wi-Fi or Ethernet Connection" section in
* examples/protocols/README.md for more information about this function.
*/
//ESP_ERROR_CHECK(example_connect());//注释掉原来的
wifi_init();//增加wifi初始化
#if CONFIG_EXAMPLE_CONNECT_WIFI
/* Ensure to disable any WiFi power save mode, this allows best throughput
* and hence timings for overall OTA operation.
*/
esp_wifi_set_ps(WIFI_PS_NONE);
#endif // CONFIG_EXAMPLE_CONNECT_WIFI
xTaskCreate(&simple_ota_example_task, "ota_example_task", 8192, NULL, 5, NULL);
}
7)打开SDK可视化配置,检查flash大小设定和“partition table”分区表设定。
左下角,点击SDK可视化配置按钮,进入配置。flash大小为4MB,Partition Table选择Factory app, two OTA definitions,分区表烧写偏移地址0x8000,如果默认是这样,则不用修改,直接关闭窗口即可。
分区表的说明见官方文档:分区表 - ESP32 - — ESP-IDF 编程指南 latest 文档
关于分区表所在IDF路径:E:\ESP32-IDF\esp\esp-idf\components\partition_table
ESP32启动后,所打印的分区表。
8)重新编译、下载到ESP32开发板。
打开串口监控,可以看到wifi连接成功了,但是连接HTTP服务器失败,原因是hello_world项目被关闭了,所以运行的HTTP服务器也跟着关闭了。
五、设置VSCODE打开多个项目文件夹,并开启HTTP服务器。
1)VSCODE打开多个项目文件夹的设置方法。
文件->首选项->设置->窗口>>新建窗口->Open Folders In New Window,这里设置为on即可。
2)打开hello_world例程,开启HTTP本地服务器。
在simple_ota_example例程中,文件->打开文件夹->选择hello_world例程,这时可以看到两个例程各占一个窗口,这样在确保hello_world例程中开启HTTP本地服务器,可以同时串口监控OTA升级的过程。
hello_world例程中开启HTTP本地服务器,终端中输入命令:python -m http.server 8070
六、simple_ota_example例程中串口监控OTA升级的过程。
simple_ota_example例程中,打开串口监控,打开时会复位ESP32,如下图所示。
至此,使用本地HTTP服务器,进行OTA升级的已全部完成,愉快的玩耍吧!
完整的例程代码下载:https://download.csdn.net/download/felix_tao/86268442
使用例程,报错怎么办,解决办法如下:
1)打开VSCODE报错,c_cpp_properties.json无法找到E:\\ESP32-IDF...
解决办法:点击.vscode->c_cpp_properties.json,修改盘符即可,例如将E盘改为D盘。
2)编译工程报错:
[0/1] Re-running CMake...
FAILED: build.ninja
CreateProcess failed: The system cannot find the file specified.
ninja: error: rebuilding 'build.ninja': subcommand failed
解决方法:清除编译产生的所有文件,左下角->点击垃圾桶图标(ESP-IDF Full Clean)->清除后,接着重新编译即OK。
本文参考了以下博客,鸣谢!
2022.05 ESP32 空中升级 OTA_事在人wёi的博客-CSDN博客_esp32空中升级
边栏推荐
猜你喜欢
随机推荐
C#/VB.NET:在 Word 中设置文本对齐方式
Google Earth Engine APP——实现ui.Select() 的设定和条件判断
IDEA启动热部署
知其然,知其所以然,JS 对象创建与继承
Ansible deployment scripts - pro available without pit
为企业数字化转型提供服务_数字赋能企业转型
航企纠缠A350安全问题 空客主动取消飞机订单
LeetCode简单题之最好的扑克手牌
学习在微信小程序中判断url的文件后缀格式
MySQL:完整性约束和 表的设计原则
There are 12 balls, including 11 weight, only one, don't know is light or heavy. Three times in balance scales, find out the ball.
再次搞定 Ali 云函数计算 FC
陈春花发布声明,这场流量狂欢该到了收尾的时候
The difference between Mysql application log time and system time is eight hours
Jina 实例秀|七夕神器!比你更懂你女友的AI口红推荐
2022-08-03 第六小组 瞒春 学习笔记
CompletableFuture接口核心方法介绍
STM32入门开发 制作红外线遥控器(智能居家-万能遥控器)
微信小程序自定义组件-城市选择「建议收藏」
Camunda整体架构和相关概念