You've installed an ESP32 project in the ceiling, inside a fuse box or at a far-away client, and found a small mistake in the code. Are you going to take everything apart and plug it into your computer? No. The ESP32 can be updated over Wi-Fi, and the method is called OTA (over the air).
How does it work?
The ESP32's flash memory is divided into sections (partitions). In OTA mode there are two slots for the program:
- The current program runs from the first slot.
- The new code is written to the second slot while it keeps running.
- Once writing finishes and checks out, the ESP32 restarts on the second slot.
- If something goes wrong mid-upload (the network drops, say), the old program is still there and running.
So the partition table must have two OTA slots. The default setting in the Arduino IDE (Default 4MB with spiffs) does.
Method 1: ArduinoOTA from the Arduino IDE
The easiest when you and the ESP32 are on the same network:
#include <WiFi.h>
#include <ArduinoOTA.h>
const char* ssid = "network name";
const char* pass = "network password";
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) delay(500);
ArduinoOTA.setHostname("garden-sensor"); // the name shown in the IDE
ArduinoOTA.setPassword("a-strong-password"); // very important
ArduinoOTA.begin();
Serial.println(WiFi.localIP());
}
void loop() {
ArduinoOTA.handle(); // must be called constantly
// the rest of your code here, without long delays
}
- Upload this the first time over the cable.
- Close and reopen the Arduino IDE, and under Tools → Port you'll find a network port with the device's name.
- Select it and upload as usual; it will ask for the password.
Be careful: every new sketch must include the same OTA code. Upload one without it and you're back to the cable.
Method 2: an upload web page
If the device is at a client's or not on your network, you can create a web page on the ESP32 with an upload file button. You export the .bin file from the Arduino IDE (Sketch → Export Compiled Binary), open the device's address in a browser and upload it.
The ESP32's Update library does the work, and libraries such as ElegantOTA give you the page ready-made. If your project already has a web server, read a web server on the ESP32.
Method 3: updating from a URL
For many devices or products: each device asks a server from time to time "is there a new version?", and if so downloads it from an HTTPS URL and updates itself. The HTTPUpdate library does this.
It lets you update 100 devices in different places in one step, but it needs organisation: a version number in the code, a server, and testing on one device first.
Security
- Always set a password for ArduinoOTA and the upload page. Without one, anyone on the same network can upload code to your device.
- For updates from the internet: HTTPS with the server's certificate checked, and ideally a signed update file.
- Don't leave the upload page open to the internet without protection.
Don't lock yourself out
The most common mistake: uploading new code that hangs or won't connect to Wi-Fi, so you can't upload again.
- Test the code on a device on your desk before uploading it to the remote one.
- Call
ArduinoOTA.handle()often, and don't put longdelaycalls in the loop. Read millis instead of delay. - If the code might hang, use a watchdog to restart it.
- ESP-IDF has a rollback feature: if the new program doesn't confirm it's running properly after starting, the device returns to the old one by itself.
OTA in ESPHome
If you use ESPHome for your smart home, OTA is built in with no code: the first time over the cable, then updates with one click from Home Assistant. Read ESPHome with Home Assistant.
If you're developing an ESP32 product and need an update system for devices at your customers', see our hardware design service.
FAQ
The device doesn't show under network ports. Why? Check that the computer and ESP32 are on the same network, that the code calls ArduinoOTA.begin(), and restart the Arduino IDE. Sometimes the computer's firewall blocks discovery.
Does OTA erase saved data? Only the program changes. Data in NVS (Preferences) or the file system stays, unless you change the partition table.
Can I use OTA on an ESP32 with 2 MB of flash? It's hard, because both slots must fit the program. With 4 MB or more it's comfortable.