当前位置:网站首页>Graphical Hands-on Tutorial--ESP32 OTA Over-the-Air Upgrade (VSCODE+IDF)
Graphical Hands-on Tutorial--ESP32 OTA Over-the-Air Upgrade (VSCODE+IDF)
2022-08-04 10:33: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开发板.
打开串口监控,The displayed information is shown in the figure below,这里说明这个,是因为后面OTA后,The displayed information should be the same as this one,才证明OTA成功.
展开build,可以看到生成了hello_world.bin文件.
三、hello_world例程中运行HTTP本地服务器.
1)within the build chainPython 有一个内置的 HTTP 服务器,We can run it with the command.
python -m http.server 8070
2)hello_world例程中运行HTTP本地服务器,Press after entering the above commandEnter,如下图所示.
3)打开浏览器,输入地址,For example my own address is:http://192.168.31.107:8070/
注意:The composition of the address isIP地址+端口号(8070),IP地址是自己电脑的IPv4地址,每个人都不同.
打开成功,VSCODE中会显示GET请求.
4)进入build目录,可以看到hello_world.bin文件,Click to download normally.
5)右键hello_world.bin,在弹出框中“复制链接地址”,后面的OTA会用到,例如我的是:http://192.168.31.107:8070/build/hello_world.bin
注意:关闭hello_world例程,会将HTTPThe local server is also down,后续OTAThe upgrade needs to be reopened.
四、创建simple_ota_example例程,并修改代码.
1)创建过程,按上面hello_world的来,这里不再详述.
2)创建完后,如下图所示.
3)一字不改,编译、下载到ESP32开发板.
首次下载,需要设置COM口,UART.
可以看到,The code does not modify anything,会提示wifi没有连接,如下图所示.
4)增加wifi连接代码.
#include "freertos/event_groups.h" //增加头文件
//wifiAttach the event definition
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(),将urlChange to your own machineIP地址.
//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可视化配置,检查flashSize settings and “partition table”Partition table settings.
左下角,点击SDKVisual configuration button,进入配置.flash大小为4MB,Partition Table选择Factory app, two OTA definitions,Partition table programming offset address0x8000,If so by default,则不用修改,直接关闭窗口即可.
See the official documentation for the description of the partition table:分区表 - ESP32 - — ESP-IDF 编程指南 latest 文档
About where the partition table is locatedIDF路径:E:\ESP32-IDF\esp\esp-idf\components\partition_table
ESP32启动后,The printed partition table.
8)重新编译、下载到ESP32开发板.
打开串口监控,可以看到wifi连接成功了,但是连接HTTP服务器失败,原因是hello_worldProject is closed,So it worksHTTPThe server also shut down.
五、设置VSCODEOpen multiple project folders,并开启HTTP服务器.
1)VSCODEThe setup method for opening multiple project folders.
文件->首选项->设置->窗口>>新建窗口->Open Folders In New Window,这里设置为on即可.
2)打开hello_world例程,开启HTTP本地服务器.
在simple_ota_example例程中,文件->打开文件夹->选择hello_world例程,At this time, you can see that the two routines each occupy a window,This ensureshello_worldOpen during routineHTTP本地服务器,Serial monitoring can be performed at the same timeOTA升级的过程.
hello_worldOpen during routineHTTP本地服务器,终端中输入命令:python -m http.server 8070
六、simple_ota_exampleSerial monitoring in the routineOTA升级的过程.
simple_ota_example例程中,打开串口监控,Resets when openedESP32,如下图所示.
至此,使用本地HTTP服务器,进行OTAThe upgrade has been completed,愉快的玩耍吧!
完整的例程代码下载: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_It's up to peoplewёi的博客-CSDN博客_esp32空中升级
第二十章 ESP32air upgrades(OTA)_开源一小步的博客-CSDN博客_esp32 ota
ESP32Partition table diagram_SimonLiu009的博客-CSDN博客_esp32分区详解
边栏推荐
- iMeta | Baidu certification is completed, search "iMeta" directly to the publisher's homepage and submission link
- 知其然,知其所以然,JS 对象创建与继承
- Digital management insight into retail and e-commerce operations - retail password
- 自己实现一个枚举validation校验器
- LeetCode 54. 螺旋矩阵 蛇形矩阵式输出字符串
- cubemx stm32 afm3000模块 气体流量传感器 驱动代码
- 无代码平台描述文字入门教程
- SVG 的 path 属性绘制图形
- Jina 实例秀|基于神经搜索的网络安全威胁检测(一)
- 在 .NET MAUI 中如何更好地自定义控件
猜你喜欢
图文手把手教程--ESP32 一键配网(Smartconfig、Airkiss)
MySQL核心SQL:结构化查询语句SQL、库操作、表操作、CRUD
Jina 实例秀|七夕神器!比你更懂你女友的AI口红推荐
Digital management insight into retail and e-commerce operations - retail password
zabbix部署
JUC (1) threads and processes, concurrency and parallelism, thread state, locks, producers and consumers
HTB-Nibbles
【c】操作符详解(二)
iMeta | Baidu certification is completed, search "iMeta" directly to the publisher's homepage and submission link
Acwing 3208. Z字形扫描 偏移量+扩展图
随机推荐
redis解决分布式session问题
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.
华为开源:聚焦开源基础软件,共建健康繁荣生态
STM32前言知识总结
RL78开发环境
无代码平台多项选择入门教程
Introduction to the core methods of the CompletableFuture interface
什么是终端特权管理
黑马瑞吉外卖之员工账号的禁用和启用以及编辑修改
CompletableFuture接口核心方法介绍
Win10电脑经常发出叮咚声音怎么关闭
HCIP 第十七天
Camunda整体架构和相关概念
粤黔协作,山海同心!578种贵州特色农产品走进粤港澳大湾区
LeetCode 54. 螺旋矩阵 蛇形矩阵式输出字符串
高级转录组分析和R数据可视化火热报名中(2022.10)
MySQL之my.cnf配置文件
无代码平台多行文字入门教程
Meishe Q&A Room | Meiying VS Meishe Cloud Editing
[论文翻译] Unpaired Image-to-Image Translation using Adversarial Consistency Loss