ESP32 Over The Air (OTA) reprogramming

Use this section to discuss your embedded Flowcode projects.
Post Reply
BenR
Matrix Staff
Posts: 1726
http://meble-kuchenne.info.pl
Joined: Mon Dec 07, 2020 10:06 am
Has thanked: 438 times
Been thanked: 602 times

ESP32 Over The Air (OTA) reprogramming

Post by BenR »

Hello All,

Sometimes it's useful to be able to reflash a device without being physically next to it. Even if you are next to it it can be a nightmare trying to get close with a computer and a USB lead. With the ESP32 devices they can be reprogrammed via their WIFI connection and so with a bit of setup you can reprogram your device without all the hassle. The ESP32 when instructed will check a specific URL on the local network or internet and program itself with a pre-compiled binary firmware file.

I'll try and make this as simple to follow as possible but to make this work you will need a couple of things. Firstly you need a HTTP server to serve the ESP firmware file. This can be a webserver on the internet or it can be a local server on your local network. I'm using my local linux box which is running a fairly standard LAMP installation (Linux, Apache, MySQL and PHP). Another way of doing this would be to install something like EasyPHP which allows you to run a simple webserver from your Windows computer.

Secondly you need to navigate the ESP-IDF examples to install the OTA bootloader onto your ESP device. To do this you need to connect to your ESP device via USB and then open the ESP-IDF command prompt from your start menu. It's installed as part of the ESP32 toolchain and so if you can already compile to a ESP32 device then you should already have this.

Type in this command to navigate to the example folder.

Code: Select all

cd examples\system\ota\simple_ota_example
Next enter the configuration menu by typing.

Code: Select all

idf.py menuconfig
Config.jpg
Config.jpg (53.45 KiB) Viewed 1991 times

When the menu loads use your cursor keys to navigate and go into the example configuration by selecting the item and pressing enter. In here specify the URL to your firmware. I used the following URL to correspond to the folder on my webserver that's hosting the firmware file.

http://192.168.1.240:80/Firmware/hello-world.bin

I also checked the Skip Server certificate CN fieldcheck as I'm using a none SSL enabled server to host the firmware.

Once your done hit escape to go back and go into the example connection configuration settings. This time enter your WIFI network details SSID and Password. Press escape again when done.

If your ESP32 board has a 26MHz crystal instead of the more standard 40MHz then you will also have to go into Component Config -> ESP32 Specific -> Main XTAL Frequency and change to 26MHz. Press escape again when done.

Next press Q to quit and Y to save your settings.

Next in the command window type in the following command to build and flash the bootloader onto your ESP device. The -p setting sets the ESP32 COM port number so change this to match your specific ESP COM port. Note every time your change the configuration the build process will do a full compile and will take some time to complete so it's best to get it right first time if you can.

Code: Select all

idf.py flash -p COM22
Next to get the .bin firmware file from your flowcode project navigate to the folder containing your Flowcode project file and in there after you have compiled to hex will be a folder with the same name as your flowcode project file. Navigate into the folder and then into the build folder. In here you will find a file named "esp-project.bin" copy this file onto your webserver and rename to match the name you provided in the bootloader configuration.

If you do this as is then it will work once and allow the firmware to be loaded from the URL but this will only work once and future updates will not work without reconnecting the USB and reloading or clearing the OTA bootloader. Not ideal! So we need to add something in our Flowcode project to trigger the update process to begin.

You have some options depending on when you want to check for the latest firmware. You could trigger it from a switch press, or from a specific WIFI/Bluetooth based incoming command or you could simply reflash the firmware after every power cycle. Note after every power cycle will slow down your boot up time and eventually may cause the Flash to fail due to too many erase / write cycles.

This example checks for a switch press in the main program loop and if the switch is pressed then the firmware will be reflashed.

OTA_Switch_Demo.fcfx
(9.18 KiB) Downloaded 153 times

This example will reflash the firmware on every power cycle.

OTA_Power_Cycle_Demo.fcfx
(9.01 KiB) Downloaded 142 times

The code to invoke the firmware reflash looks like this and can be called using a C icon.

Code: Select all

esp_partition_iterator_t pi = esp_partition_find(ESP_PARTITION_TYPE_APP, ESP_PARTITION_SUBTYPE_APP_FACTORY, NULL);
if (pi != NULL)
{
  const esp_partition_t* factory = esp_partition_get(pi);
  esp_partition_iterator_release(pi);
  if(esp_ota_set_boot_partition(factory) == ESP_OK) esp_restart();	
}
As well as this code you need to add some supplementary code headers using the Build -> Project Options -> Supplementary Code menu.

Code: Select all

#include "esp_ota_ops.h"
#include "esp_flash_partitions.h"
#include "esp_partition.h"
If you want to know any more to do with OTA or using HTTPS encrypted URLs (highly recommended if reprogramming via a file on the internet) then this is a good starting point.

https://github.com/espressif/esp-idf/tr ... system/ota

If you give this a go then let us know how you're getting on :D

medelec35
Matrix Staff
Posts: 1449
Joined: Wed Dec 02, 2020 11:07 pm
Has thanked: 509 times
Been thanked: 471 times

Re: ESP32 Over The Air (OTA) reprogramming

Post by medelec35 »

Hi, Ben.
Thank you for this.
What a great idea!
I'm really keen on trying this out :D
I have got as far as sending the bin file

Code: Select all

within examples\system\ota\simple_ota_example
to the ESP32
I'm using the default name for the bin that's generated by my project and placed in a directory on my d drive:

Code: Select all

D:\EasyPHP files\esp-project.bin
I was not fully sure how to set up for EasyPHP path within the configuration setting so set it up as:
esp config menu.png
esp config menu.png (12.26 KiB) Viewed 1896 times
I was not sure if to add the full path with D or the file name as the path is stored in EasyPHP:
EasyPHP_Devserver.png
EasyPHP_Devserver.png (18.69 KiB) Viewed 1896 times
The final step is how to get the initial project file that's within PHP folder onto ESP32 without overriding the boot loader?
Martin

Post Reply