此博客以 esp-idf 下的 simple_ota_example 为例来演示在本地 PC 端搭建 http 服务器实现 ESP32 OTA 过程。
注:OTA 实现过程可先阅读官方 ESP-IDF 编程指南里的 OTA 部分 。
1 ESP-IDF 环境配置 & 进入例程目录
参考 ESP-IDF 环境配置指南 配置 ESP-IDF 环境,然后在终端里进入 simple_ota_example 例程目录下:
cd simple_ota_example
2 设置 WiFi 账号密码:
在上述终端里执行 idf.py menuconfig
命令,设置设置 Wi-Fi 账号密码, 路径如下
menuconfig -> Example Connection Configuration -> WiFi SSID
menuconfig -> Example Connection Configuration -> WiFi Password
3 使能 HTTP OTA
由于 ESP-IDF 默认为 HTTPS OTA,需要在 menuconfig
里打开以下选项来让 HTTP OTA 成功进行
menuconfig -> Component config - > ESP HTTPS OTA -> Allow HTTP for OTA
menuconfig -> Example Configuration -> (Enable)Skip server certificate CN fieldcheck
4 准备需要 OTA 的 Bin 文件
在此处以需要升级的 Bin 为 ledc.bin
为例,将 ledc.bin
文件放在 Documents 目录下
cd Document
5 在本地建立一个 HTTP 服务器
在 PC 机 Documents 目录下开启命令行窗口,通过 执行如下命令建立本地 HTTP 服务器
cd Document
查询 python 版本,执行如下命令:
Python --version
- 如果上述指令返回的 Python 版本是 3.X ,通过如下命令建立服务器
python -m http.server 8070
- 如果上述指令返回的 Python 版本是 2.X ,通过如下命令建立服务器
python -m SimpleHTTPServer 8070
注:服务器建立好后,可通过网址
localhost:8070
在网页端查询本地服务器
6 查询本机 IP
在任意终端输入 ifconfig
,结果如下:
[19:57:34] /home/user/github/esp-idf/rel4.3/esp-idf git(release/v4.3) 🔥 ❱❱❱ ifconfig
docker0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 172.17.0.1 netmask 255.255.0.0 broadcast 172.17.255.255
ether 02:42:0c:d0:b8:4c txqueuelen 0 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
enp1s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.109 netmask 255.255.255.0 broadcast 192.168.1.255
inet6 fe80::7fe3:5db:2313:7b25 prefixlen 64 scopeid 0x20<link>
ether c8:f7:50:f3:d9:81 txqueuelen 1000 (Ethernet)
RX packets 273424 bytes 219784939 (219.7 MB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 225726 bytes 55602967 (55.6 MB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
loop txqueuelen 1000 (Local Loopback)
RX packets 9130 bytes 918487 (918.4 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 9130 bytes 918487 (918.4 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[19:57:36] [cost 0.096s] ifconfig
可以看到 PC 的 ip 地址为 192.168.1.109
,后续在填写 OTA URL 时需要用到此 ip。
7 配置 OTA URL
在 OTA 工程终端输入 idf.py menuconfig
,然后在 menuconfig 里配置 OTA URL 为 http://192.168.1.109:8070/ledc.bin
,路径如下 :
menuconfig -> Example Configuration -> firmware upgrade url endpoint
8 检查分区表
在上述 menuconfig 里检查分区表是否选择为正确包含 OTA 分区的分区表,OTA 示例里会默认做以下修改:
menuconfig -> Partiotion Table -> Factory app,two OTA definitions
menuconfig -> Serial flasher config -> Flash size -> 4 MB
9 开始 OTA
此时在 OTA 示例终端 下输入 idf.py flash monitor
编译烧写 OTA 示例至 ESP32 中即可发现 OTA 升级成功。
10 常见问题
Q1. 如果开启 HTTPS OTA,如何跳过服务器端证书检查?
- 可以尝试使能
menuconfig -> Component config -> ESP-TLS -> Allow potentially insecure options -> Skip server certificate verification by defaul
选项(请注意这样并不安全,仅供测试),如下: