ESP32 OTA updates

Flashing new versions of your applications through the USB connection is convenient and fast during development time and as long as your module is on a breadboard and close to your PC. But once you have your project put into a casing and applied to the place it is supposed to be, it is getting inconvenient if you want to update your code.

That’s where OTA comes in. It allows you to update your module code over your local WiFi. It is working reliable and I use it a lot on my ESP8266 modules.

OTA is available for the ESP32 as well and quite easy to implement. All you need is to include the library headers

and activate OTA. It is important to activate it only after a WiFi connection is established, because beside of listening to update requests, OTA implements as well to advertise your device through mDNS/Bonjour.

I wrote a small function that activates OTA and adds some additional information for the mDNS advertisement. Optional you can as well define callbacks for the different stages of the OTA update. I use this in this project to show the status of the OTA update on the TFT display. Another important thing here is to stop as many background activities once an update has started. Otherwise you will see a lot of failed OTA updates, because the task scheduler might switch to other waiting tasks and the OTA gets a timeout error.

I actually use a global flag bool otaRunning that can be checked by other activities and let them stop themselves.

Now here is the code of my activateOTA() function

mDNS/Bonjour is used e.g. in Arduino IDE to list your boards IP address in the list of available programming ports. As you can see at the end of the above code, I add some additional informations into the mDNS service to make it easier to identify the modul.

To catch OTA update requests, you need to implement into your loop() a check:

And here is how it looks like:


Leave a Reply

Your email address will not be published. Required fields are marked *

Free Link Directory