All articles

    Updating an ESP32 Without a Cable (OTA): Upload Code Over Wi-Fi

    How to upload new code to an ESP32 fitted somewhere awkward without connecting it to your computer: ArduinoOTA from the Arduino IDE, uploading from a web page, updating from a server or URL, the partition table, passwords and security, and how not to lock yourself out with broken code.

    4 min read

    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).

    The computer sends the new code to the ESP32 over Wi-Fi; the ESP32 writes it to the spare half of its memory and then restarts on it
    The computer sends the new code to the ESP32 over Wi-Fi; the ESP32 writes it to the spare half of its memory and then restarts on it

    How does it work?

    The ESP32's flash memory is divided into sections (partitions). In OTA mode there are two slots for the program:

    1. The current program runs from the first slot.
    2. The new code is written to the second slot while it keeps running.
    3. Once writing finishes and checks out, the ESP32 restarts on the second slot.
    4. 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
    }
    1. Upload this the first time over the cable.
    2. Close and reopen the Arduino IDE, and under Tools → Port you'll find a network port with the device's name.
    3. 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 long delay calls 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.

    Circuits Nerd
    © 2026 Circuits Nerd. All rights reserved.