Over-the-air firmware updates
ESP32 devices check a server for new firmware, download it, verify it and roll back if it fails to boot.
Build it step by step
0 of 8 doneChoose a partition scheme
Use one with two app slots, such as Default 4MB with spiffs.
Flash version 1
Upload the sketch with FW_VERSION set to 1.
Change something visible
Edit a message and set FW_VERSION to 2.
Export the binary
Use Export Compiled Binary and copy firmware.bin.
Host the files
Write 2 in version.txt and start the web server.
Watch the update
The serial monitor shows the download and restart.
Test the rollback
Build a version 3 that never connects and see it revert.
Update the second board
Check both pick up the new version.
How do you build an over-the-air firmware updates?
ESP32 devices check a server for new firmware, download it, verify it and roll back if it fails to boot. You need ESP32 DevKit V1 × 2 and 5 V 2 A USB adapter. It takes 8 steps and about 240 minutes.
- Level
- Intermediate
- Time
- About 240 min
- Parts
- 2
Parts list
- Development BoardsBuyESP32 DevKit V1 × 2
- PowerBuy
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| 5 V adapter | Each ESP32 USB | Power |
| Computer on the same network | Hosts version.txt and firmware.bin | Update server |
Code
The board looks at a small version file on a server, and if the number is higher than the one built into it, downloads firmware.bin and restarts into it. The ESP32 keeps two application slots, so an update never overwrites the running program. The new image runs on trial: it is marked good only after it connects to Wi-Fi, and if it crashes or cannot connect, the next reset goes back to the previous version. To host the files, put version.txt (containing just the new number) and the exported binary firmware.bin from Arduino IDE (Sketch, Export Compiled Binary) in a folder and run python3 -m http.server 8000 there. Plain HTTP can be tampered with: use it only on a trusted network, and for real products use HTTPS and signed firmware. Arduino IDE with the ESP32 boards package, board "ESP32 Dev Module".
Runs on a virtual ESP32 Dev Module in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).
// Checks http://SERVER/version.txt, downloads firmware.bin if newer. Libraries: HTTPUpdate (built in).
#include <WiFi.h>
#include <HTTPClient.h>
#include <HTTPUpdate.h>
#include "esp_ota_ops.h"
const char* WIFI_SSID = "YOUR_WIFI_NAME";
const char* WIFI_PASS = "YOUR_WIFI_PASSWORD";
const char* SERVER = "http://YOUR_SERVER_ADDRESS:8000"; // plain HTTP: keep it on a trusted network
const int FW_VERSION = 1; // raise this for each new build
bool verifyRollbackLater() { return true; } // keep a new image on trial until we mark it good
void checkForUpdate() {
WiFiClient client;
HTTPClient http;
http.begin(client, String(SERVER) + "/version.txt");
if (http.GET() != 200) { http.end(); return; }
int latest = http.getString().toInt();
http.end();
Serial.printf("running %d, server has %d\n", FW_VERSION, latest);
if (latest <= FW_VERSION) return;
Serial.println("updating...");
t_httpUpdate_return r = httpUpdate.update(client, String(SERVER) + "/firmware.bin");
if (r == HTTP_UPDATE_FAILED) Serial.println(httpUpdate.getLastErrorString()); // on success it restarts by itself
}
void setup() {
Serial.begin(115200);
Serial.printf("firmware version %d\n", FW_VERSION);
WiFi.begin(WIFI_SSID, WIFI_PASS);
unsigned long t = millis();
while (WiFi.status() != WL_CONNECTED && millis() - t < 20000) delay(300);
if (WiFi.status() == WL_CONNECTED) {
esp_ota_mark_app_valid_cancel_rollback(); // we booted and connected, so this image is good
checkForUpdate();
}
// If the new image cannot connect, it is not marked valid and the next reset returns to the old one
}
void loop() {
// your normal code
delay(60000);
checkForUpdate(); // check again every minute
}Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Server version equal | No update |
| Server version higher | Download, restart and run the new version |
| New image that cannot connect | Rolls back to the previous version |
| Server unreachable | Keeps running the old firmware |
If it does not work
Update fails
Check the server address, firewall and the file name.
Not enough space
Use a partition scheme with two app slots.
Rollback does not work
Check your core version supports app rollback.
Loop of updates
Make sure the version in the code matches version.txt after updating.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the over-the-air firmware updates?
ESP32 DevKit V1 × 2 and 5 V 2 A USB adapter.
How long does the over-the-air firmware updates take to build?
About 240 minutes for a intermediate maker, in 8 steps.
How do I know the over-the-air firmware updates works?
Server version equal: No update. Server version higher: Download, restart and run the new version. New image that cannot connect: Rolls back to the previous version.
Update fails: what should I check?
Check the server address, firewall and the file name.
Not enough space: what should I check?
Use a partition scheme with two app slots.
Rollback does not work: what should I check?
Check your core version supports app rollback.
Loop of updates: what should I check?
Make sure the version in the code matches version.txt after updating.
