An ESP8266 mounted inside a wall box, a lamp base or on top of a fence post has one thing in common with every other deployed project: getting a USB cable back to it is a hassle. OTA (over-the-air) updates fix that by sending new firmware over Wi-Fi. After the first USB flash, you never need the cable again.
Quick answer: add #include <ArduinoOTA.h>, call ArduinoOTA.begin() after Wi-Fi connects and ArduinoOTA.handle() in every loop(), then upload once over USB. From then on the board appears under Tools → Port as a network port, and uploads go over Wi-Fi. Choose a Flash Size option with an OTA area larger than your sketch, set a password, and allow the Arduino IDE through your PC's firewall.
This guide covers three ways to update (from the IDE, from a browser, and by having the board pull updates from a server), how much flash OTA needs, what really happens if power fails mid-update, password and signing options, and a troubleshooting table for the errors you'll actually see. Works on NodeMCU, Wemos D1 Mini and any ESP8266 board with 1 MB of flash or more.
Three Ways to Update Over the Air
| Method | Who starts it | Best for |
|---|---|---|
| ArduinoOTA | You, from the Arduino IDE or PlatformIO | Development on your own network |
| Web updater (ESP8266HTTPUpdateServer) | Anyone with the password, from a browser | Devices in the field; updating without the IDE |
| HTTP pull (ESP8266httpUpdate) | The board itself, from a server you control | Several devices updated automatically |
Many projects use two: ArduinoOTA while developing, plus a web updater or HTTP pull once the device is installed.
Setting Up ArduinoOTA
#include <ESP8266WiFi.h>
#include <ArduinoOTA.h>
const char* ssid = "YourWiFiName";
const char* password = "YourWiFiPassword";
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
ArduinoOTA.setHostname("esp8266-project");
ArduinoOTA.setPassword("choose-a-real-password");
ArduinoOTA.onStart([]() { Serial.println("OTA starting"); });
ArduinoOTA.onEnd([]() { Serial.println("OTA complete"); });
ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("OTA error: %u\n", error);
});
ArduinoOTA.begin();
Serial.println("Ready. IP: " + WiFi.localIP().toString());
}
void loop() {
ArduinoOTA.handle(); // must run every loop iteration, unblocked
// ... rest of your project code
}Once this is flashed over USB, the board shows up under Tools → Port as a network port, named after the hostname (esp8266-project at 192.168.1.42). Select it and click Upload; the IDE asks for the OTA password and sends the sketch over Wi-Fi. The Serial Monitor doesn't work over a network port, so print debug output over Telnet or keep a USB cable handy while developing.
Behind the scenes, the IDE runs a small tool (espota.py) that sends an invitation to the board on UDP port 8266; the board then connects back to your computer to download the firmware. That's why a PC firewall that blocks incoming connections breaks OTA even though the board is reachable.
To keep the password out of the sketch in plain text, use ArduinoOTA.setPasswordHash("<md5 of the password>") instead of setPassword(). A genuinely robust WiFi connection matters more here than in most sketches, since a flaky connection can drop mid-upload; see Connecting ESP8266 NodeMCU to WiFi for a reconnect loop worth pairing with any OTA-enabled project. A static IP (the same WiFi.config() approach works on the ESP8266) also keeps the network port in the same place.
Setting Up a Web-Based Update Page
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <ESP8266HTTPUpdateServer.h>
const char* ssid = "YourWiFiName";
const char* password = "YourWiFiPassword";
ESP8266WebServer server(80);
ESP8266HTTPUpdateServer httpUpdater;
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
httpUpdater.setup(&server, "/update", "admin", "choose-a-real-password");
server.begin();
Serial.println("Update page at: http://" + WiFi.localIP().toString() + "/update");
}
void loop() {
server.handleClient();
}Visiting /update on the board's IP address shows a basic upload form -- choosing a compiled .bin file (exported from the Arduino IDE via Sketch > Export Compiled Binary) and submitting it flashes the new firmware directly from any browser, no IDE required on the updating machine at all. The same page also accepts a filesystem image (LittleFS), so web pages and config files stored on the board can be updated the same way.
HTTP Pull: Let the Board Fetch Its Own Updates
For a handful of devices, it's easier to put the .bin file on a server and have each board check for it, for example once a day or at boot:
#include <ESP8266WiFi.h>
#include <ESP8266httpUpdate.h>
void checkForUpdate() {
WiFiClient client;
ESPhttpUpdate.rebootOnUpdate(true);
t_httpUpdate_return ret = ESPhttpUpdate.update(client, "http://192.168.1.20/firmware/sensor.bin", "1.0.3");
switch (ret) {
case HTTP_UPDATE_FAILED:
Serial.printf("Update failed: %s\n", ESPhttpUpdate.getLastErrorString().c_str());
break;
case HTTP_UPDATE_NO_UPDATES:
Serial.println("Already up to date");
break;
case HTTP_UPDATE_OK:
break; // the board reboots into the new firmware
}
}The third argument is the version currently running. The board sends it to the server in a header (x-ESP8266-version), so a small script on the server can reply with a new file or with 304 Not Modified. A plain static file server simply sends the file every time, so check versions yourself before calling update() if you use one.
How Much Flash OTA Needs
During an update the new firmware is written into free flash next to the running sketch, checked, and only then copied over the old one. So the flash must have room for both at once.
In the Arduino IDE, Tools → Flash Size shows the split for each option, for example 4MB (FS: 2MB, OTA: ~1019KB). The OTA figure is the largest sketch you can update over the air. If the compile output says your sketch uses more than that, OTA fails with a Not enough space error even though a USB upload works.
| Board flash | Example setting | Max sketch for OTA |
|---|---|---|
| 4 MB (NodeMCU, D1 Mini) | 4MB (FS: 2MB, OTA: ~1019KB) | ~1019 KB |
| 4 MB, small filesystem | 4MB (FS: 1MB, OTA: ~1019KB) | ~1019 KB, more room for files |
| 1 MB (ESP-01S) | 1MB (FS: 64KB, OTA: ~470KB) | ~470 KB |
| 512 KB (old ESP-01) | — | OTA is impractical |
A typical sketch with Wi-Fi, a web server and OTA is around 350–450 KB, so a 4 MB board has plenty of headroom; a 1 MB ESP-01S is tight once you add libraries.
What Happens If Power Fails Mid-Update?
Less than people fear. While the new image is downloading, the old firmware is untouched; if power or Wi-Fi drops, the board simply boots the old sketch again. The image is checked with an MD5 hash before it's accepted. The risky window is the few seconds after the reboot, when the bootloader copies the new image over the old one. Power loss at exactly that moment can leave the board needing a USB reflash. Keep the supply stable during updates and the risk is very small.
Security
- Always set a password. Anyone on the same network can otherwise push firmware to the board.
- Keep the update port off the internet. Never port-forward the OTA or
/updateport. Basic authentication over plain HTTP sends the password unencrypted. - Sign your firmware for real deployments. The ESP8266 Arduino core can sign updates automatically: put a
private.keyandpublic.keyin the sketch folder, and the board will reject any update not signed with your key, even if someone learns the password. See Signed updates in the ESP8266 Arduino core documentation.
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| No network port in Tools → Port | mDNS blocked, different network or guest Wi-Fi | Put PC and board on the same main network; restart the IDE; check the IP in Serial |
| No response from device / No Answer | PC firewall blocks the board's connection back | Allow the Arduino IDE (and Python) through the firewall on private networks |
| Authentication Failed | Wrong or changed password | Check setPassword(); the IDE caches the last one, so restart it |
| Not enough space / Update error: ERROR[4] | Sketch larger than the OTA area | Choose a Flash Size option with more OTA space, or shrink the sketch |
| Upload starts, then stalls | Weak signal, or loop() blocked by delay() | Move closer; replace long delays with millis() timing |
| Port appears, then vanishes | ArduinoOTA.handle() not called often enough, or the board is rebooting | Call it at the top of every loop(); check the power supply |
| Update succeeds, board never returns | New sketch has a Wi-Fi bug or forgot OTA | Always keep the OTA code in every build; recover over USB |
The last row is the most important habit: every firmware you upload must contain the OTA code too. Upload one sketch without it and the next update needs a cable again.
Release Checklist
- Bench-test the first USB flash and note the board's IP and firmware version.
- Change only a version string and update over the air, to prove the path works.
- Check the compile size against the OTA figure in Tools → Flash Size.
- Keep a copy of the exact
.binthat's running, and a way to reach the USB port if you ever need it. - Print the firmware version at startup (and on a web page) so you can confirm an update landed.
FAQ
Does OTA work on the ESP-01? On the ESP-01S with 1 MB of flash, yes, for sketches under about 470 KB. The original 512 KB ESP-01 is too small.
Can I use the Serial Monitor over OTA? No. OTA only replaces firmware. For wireless logs, print to a Telnet server, MQTT or a web page.
Does OTA work with Arduino IDE 2? Yes. Network ports appear in the port list and the IDE asks for the password on upload.
Can I update the board from outside my home network? Not safely by opening a port. Use HTTP pull from a server you control, or connect to your home network by VPN first.
Is the process the same on an ESP32? Very similar: the ESP32 also has ArduinoOTA and a web updater, and because it keeps the previous firmware in a second partition, it can switch back to it if a new build fails.
Related guides: ESP8266 NodeMCU pinout · Wemos D1 Mini pinout · Install the ESP8266 in the Arduino IDE · ESP8266 Wi-Fi reconnect guide




