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
Code: Select all
idf.py menuconfig
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
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.
This example will reflash the firmware on every power cycle.
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();
}
Code: Select all
#include "esp_ota_ops.h"
#include "esp_flash_partitions.h"
#include "esp_partition.h"
https://github.com/espressif/esp-idf/tr ... system/ota
If you give this a go then let us know how you're getting on