Update and test

Native OTA

How firmware is updated after shipment. Partition tables start on board one.

Update and test OTA update ESP32 family

Official path: examples/system/ota/native_ota_example

01 Overview

Native OTA is the standard remote update solution in ESP-IDF: the device pulls a new image from the network, writes it to a spare partition, and switches the boot partition to complete the update.

It solves the maintenance problem for deployed devices: no disassembly, no return to factory, just fix bugs, add features, or change configuration over the air.

Typical use cases include smart home devices, industrial sensors, and data loggers that run for long periods and are hard to access physically.

It is not suitable for boards that cannot stay online reliably, nor for applications requiring real-time performance—the update process involves a brief reboot.

OTA is not a later plugin; it is a partition decision that must be made from the first board.

02 Hardware

  • Flash size: Must accommodate two OTA slots (each ~1.5MB) plus factory and NVS partitions; 4MB is a comfortable start.
  • Partition table: Must be defined before production, including factory, ota_0, ota_1, nvs, and aligned addresses.
  • Power stability: Power loss during update can corrupt partitions; rely on partition design and brown-out detection (e.g., external voltage monitor).
  • Antenna and RF: Ensure Wi-Fi antenna matching and sufficient signal strength to avoid download failures.
  • Connectors: Reserve UART or JTAG for debugging and recovery mode.
  • Reset circuit: Ensure a reliable reset button for entering download mode.
  • Layout notes: Keep power traces wide to avoid voltage drops during high-current update operations.

03 Software flow

1. Configure the partition table with two OTA slots and a factory partition.

2. Initialize NVS to store the current boot partition and rollback counters.

3. Connect to Wi-Fi and obtain an IP address.

4. Download the new firmware over HTTPS to the idle OTA partition, verifying signature and hash.

5. Set the boot partition to the new image using esp_ota_set_boot_partition().

6. Reboot the device to boot from the new partition.

7. After boot, verify the firmware runs correctly; if it fails, roll back to the old partition.

04 Core points

  • OTA is a partition decision, not a later plugin. Partition tables must be fixed during hardware design.
  • Signature verification is mandatory. For public internet updates, use HTTPS and signing to prevent man-in-the-middle attacks.
  • Define rollback strategy. On failure, automatically roll back to the old version to avoid bricking.
  • Do not copy OTA flows from other chips. ESP-IDF OTA APIs have specific requirements like partition alignment and erase operations.
  • Custom work: update channel (HTTP/HTTPS), signing algorithm, rollback policy, and update trigger conditions.

中文

升级与产测

Native OTA 远程升级

出货后固件怎么安全更新。分区表从第一块板就要定。

升级与产测 远程升级 ESP32 family

官方路径: examples/system/ota/native_ota_example

01 项目概述

Native OTA 是 ESP-IDF 提供的标准远程升级方案:设备从网络拉取新固件,写入备用分区,然后切换启动分区完成更新。

它解决的核心问题是已部署设备的维护:不用拆机、不用返厂,就能修 bug、加功能、改配置。

典型场景包括智能家居、工业传感器、数据采集器等需要长期运行且难以物理接触的设备。

它不适合网络尚不稳定的开发板,也不适合需要实时性极高的场景——升级过程会短暂重启。

OTA 不是后加功能,而是从第一块板就要规划的分区决策。

02 项目硬件描述

  • Flash 容量: 至少需要两个 OTA 分区(各占约 1.5MB),加上工厂分区和 NVS,常见 4MB Flash 起步更从容。
  • 分区表: 必须在出厂前定义好,包含 factory、ota_0、ota_1、nvs 等分区,且地址对齐。
  • 电源稳定性: 升级过程中掉电可能导致分区损坏,需靠分区设计和掉电检测(如外部电压监控)来保证。
  • 天线与射频: 确保 Wi-Fi 天线匹配,信号强度足够,否则下载失败率高。
  • 连接器: 预留 UART 或 JTAG 接口,用于调试和恢复模式。
  • 复位电路: 确保复位按键可靠,便于进入下载模式。
  • 布局注意: 电源走线要宽,避免升级时大电流导致电压跌落。

03 项目软件流程描述

1. 配置分区表,包含两个 OTA 槽位和一个工厂分区。

2. 初始化 NVS,用于存储当前启动分区和回滚计数。

3. 连接 Wi-Fi,获取 IP 地址。

4. 通过 HTTPS 下载新固件到空闲 OTA 分区,校验签名和哈希。

5. 设置启动分区为新固件,调用 esp_ota_set_boot_partition()。

6. 重启设备,从新分区启动。

7. 启动后验证固件运行正常,若失败则回滚到旧分区。

04 项目核心点

  • OTA 是分区决策,不是后加功能。 分区表在硬件设计阶段就要确定。
  • 签名校验不可省略。 公网升级必须使用 HTTPS 和签名,防止中间人攻击。
  • 回滚策略要明确。 失败后要能自动回滚到旧版本,避免设备变砖。
  • 不要照搬其他芯片的 OTA 流程。 ESP-IDF 的 OTA API 有特定要求,如分区对齐、擦除操作。
  • 可定制项: 升级通道(HTTP/HTTPS)、签名算法、回滚策略、升级触发条件。