← index #8011PR #7048
Likely Duplicate · high · value 2.908
QUERY · ISSUE

Better support for OTA updates on esp32

openby bobveringaopened 2021-11-18updated 2025-10-03
port-esp32

I work on an esp32 based device where we rely heavily on MicroPython for our development workflow and device code. We started using MicroPython around 2 years ago, and during that time we have developed a decent amount of MicroPython code. Part of that code is to support OTA (Over the Air) updates. Our current OTA Update system code was developed around 2 years ago for MicroPython v1.12 and is entirely written in python.

We have recently identified our propriety code as somewhat of a risk, and we want to rewrite the code to use more native espressif functions. Ideally, we want to contribute this code back to the community as we also rely on their work for our platform to function.

Is better support for OTA updates on esp32 something that should be added to MicroPython, and if so in what capacity?

The OTA update is specific to the firmware application, not files on the file system. We intend to develop a system that can download the update from both http and https sources.

I'm interested to hear what you think about this change.

CANDIDATE · PULL REQUEST

esp32/ota: Implement ESP-IDF OTA functionality.

openby ekondayanopened 2021-03-17updated 2026-02-06
port-esp32

UPDATE: The test completed successfully on NodeMCU ESP32 by ai-thinker with ESP-IDF v4.0, v4.1, v4.2, v4.3

Implemented new functions in esp32.Partition:

  • mark_app_invalid_rollback_and_reboot()

  • check_rollback_is_possible()

  • app_description()

  • app_state()

  • ota_begin()

  • ota_write()

  • ota_write_with_offset() for ESP-IDF version >= 4.2

  • ota_end()

  • ota_abort() for ESP-IDF version >= 4.3

  • create tests

  • update documentation

For many commercial products, Over The Air updates are a very important and critical part. It must be reliable and should not brick the device. Writing a good OTA from scratch is a daunting task. For that reason the use of well tested and proven reliable libraries is much more preferable than the ones developed in the house.

USECASE

I'm developing an industrial device where the OTA is an essential part of it. Since only a few functions from esp-idf are
implemented (enough for hobby project but not enough for commercial project), I ended up duplicating the esp-idf ota functionality in python.
The result was a module with a questionable quality. I tried to predict all the possible places where it could crash, but my gut was
telling me that I could be missing something. So I decided to implement more of the the OTA functions from esp-idf and to use them in my OTA module.
I've rewritten my OTA module and replaced the redundant code with the implemented functions from esp-idf. This allowed me to reduced the size of the module significantly, increase the robustness of the code and on top of that now the code got much simpler and easier to maintain.
The total increase in size of the compiled app image is 2432 bytes.

IMPLEMENTATION

Extend the esp32.Partition class where all the OTA related functions are
prefixed with "ota_" and app related functions are prefixed with "app_".

Example:
from esp32 import Partition
app_part = Partition(Partition.RUNNING)
app_part.app_description()
app_part.app_state()
handle = app_part.ota_begin()
app_part.ota_end(handle)

New functions:
Partition.mark_app_invalid_rollback_and_reboot(cls)
Partition.check_rollback_is_possible(cls)
Partition.app_description(self)
Partition.app_state(self)
Partition.ota_begin(self, image_size = 0)
Partition.ota_write(self, handle_in, data_in)
Partition.ota_write_with_offset(self, handle_in, data_in, offset)
Partition.ota_end(self, handle_in)
Partition.ota_abort(self, handle_in) only for ESP-IDF version >= 4.3

BENEFITS

  • no code duplication
  • reduced code size
  • cleaner and easier to maintain code
  • esp-idf handles encrypted flash
  • use a reliable and well tested library written by the creators of ESP32
  • more robust OTA procedure
  • better performance

CONS

  • None :) (maybe the 2432 bytes overhead in the final compiled app image)

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied