ESP32 OTA Over The Internet

Dominik Nowak
CTO @ HusarnetThere are a lot of examples on the internet about how to perform OTA (Over-The-Air firmware update) to your ESP32 as long as your chip is in the same LAN as your computer.
While Husarnet is basically a VPN software which works not only on your laptop, but also on your ESP32, it's easy to port exisitng OTA examples to work over the Internet. I'll show you how to to that.
#
What OTA library should I choose?There are a buch of possibilites, such as:
In this blog post, we will focus on AsyncElegantOTA, as it is based on AsyncTCP library which allows multi-connection and is a base for many great projects such as ESPAsyncWebServer.
#
Let's startI created a basic demo based on example from AsyncElegantOTA. Basically it works the same way, but not only over LAN but also over the Internet.
TL;DR
The complete project is available here, so if you are familiar with Husarnet and platformio, you can drop the rest of the article and just do the following:
After you place your Wi-Fi SSID & Password and Husarnet join code in the *.ino
file and flash your firmware to ESP32, the OTA website will be hosted on ESP32 under the following URL (on Firefox): http://ota-test:3232/update
.
#
Modify the existing exampleTo make it work over the Internet, only a few new lines were added to the basic example:
So you need to include the Husarnet library:
Define your Husarnet credentials:
And start Husarnet service:
After that, Husarnet service, working in a background, handles the traffic over the Internet, and the rest of your code can simply use AsyncTCP based code, the same way as it was running in the LAN!
To find a husarnetJoinCode
you need to setup an account at https://app.husarnet.com/ (Free account, with no credit card required, up to 5 devices is available), then create a new network and click [Add element] button. You will find your secret join code under join code
tab.
platformio.ini
file#
Create a The simplest way to deal with different versions of libraries and configurations for your ESP32 project is to use platformio IDE - an extension for Visual Studio Code.
The whole configuration for your project is included in platformio.ini
file, so you don't need to manually install appropriate libraries or setup platform specific environmental variables. In our case the file looks like that:
#
Create a custom partition table fileNote that in platformio.ini
we have defined a custom ESP32 partition table file: partitions_custom.csv
. It looks like this:
It is a slightly modifed default.csv
file. I have increased the size of partitons for app0 (the current firmware running on ESP32) and app1 (a place where a new firmware will be stored during the updating process). If your firmware.bin
file will be too large for app0 and app1 you can increase these partitions at the expense of spiffs partition or just use ESP32 with more flash memory.
#
Flashing ESP32 using a web browser over the InternetYou also need to add your laptop to the same Husarnet network as you ESP32. To do that:
- Install Husarnet Client on your laptop, by executing in the Linux terminal:
- After installation process is finished, execute the following command:
- Connect your laptop to the same Husarnet network as ESP32, by using the same join code as you previously used in the code for ESP32:
Now open your web browser and type: http://ota-test:3232/update
(on Firefox) or http://[fc94:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:d395]:3232/update
(on other browsers - Husarnet IPv6 address of your ESP32 you will find at https://app.husarnet.com/).
#
SummaryThat's all guys. I hope this simple guide about using Husarnet to enable over the Internet firmware flashing (without using any additional update server) will be useful. In case of any questions related to this blog post please contact us using Husarnet Community Forum or via email at support@husarnet.com.