ESP32 Over The Air (OTA) reprogramming

Use this section to discuss your embedded Flowcode projects.
Post Reply
BenR
Matrix Staff
Posts: 1891
http://meble-kuchenne.info.pl
Joined: Mon Dec 07, 2020 10:06 am
Has thanked: 488 times
Been thanked: 663 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 9168 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 427 times

This example will reflash the firmware on every power cycle.

OTA_Power_Cycle_Demo.fcfx
(9.01 KiB) Downloaded 416 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: 1585
Joined: Wed Dec 02, 2020 11:07 pm
Has thanked: 563 times
Been thanked: 518 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 9073 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 9073 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

sandinugraha
Posts: 14
Joined: Fri Dec 18, 2020 3:35 am
Has thanked: 4 times
Been thanked: 2 times

Re: ESP32 Over The Air (OTA) reprogramming

Post by sandinugraha »

Hi BenR,

Hello, I have tried this ability and it works well, thanks for the article. In the example above, WiFi credentials are created and saved via menuconfig. What if you want to change it in runtime? and where is the data stored? How do I access the data directly?

Thank You

Regards

sandinug

BenR
Matrix Staff
Posts: 1891
Joined: Mon Dec 07, 2020 10:06 am
Has thanked: 488 times
Been thanked: 663 times

Re: ESP32 Over The Air (OTA) reprogramming

Post by BenR »

Hmm the bootloader project you build using the IDF is where the username and pawword are stored.

I'm trying to find the source of the example_connect() function but currently not been able to spot where it's declared.

Basically what might work is storing the wifi/password into the flash using the ESP32 EEPROM component, then in the bootloader pulling the values from the Flash EEPROM and using these in the example_connect function.

So there's a starting point for you but it looks like it might be tricky to unpick all the moving parts here. First step is where does the example_connect function live and is it editable.

Have a go and let us know how you get on. I can always have another pass through the code when I have a bit more time.

BenR
Matrix Staff
Posts: 1891
Joined: Mon Dec 07, 2020 10:06 am
Has thanked: 488 times
Been thanked: 663 times

Re: ESP32 Over The Air (OTA) reprogramming

Post by BenR »

Aha looks like you may be able to simply replace example_connect(); with example_connect(ssid, passwd);

So this might be a starting point. You would then have to declare and populate the ssid and password variables.

Post Reply