🛠 Programmation OTA : téléverser le code par Wi-Fi
Objectives
Update the ESP8266-ESP-12E program without ever reconnecting a cable: the Arduino core includes the OTA protocol (ArduinoOTA). After an initial mandatory upload via serial cable, the module appears in Arduino IDE's "network" port and accepts updates over Wi-Fi. Understanding the layout of the 4 MB flash is key: with the OTA partition scheme, the flash contains two program slots (sketch A and sketch B) plus the file system — so an OTA sketch cannot exceed half of the usable flash. Choose the correct flash preset in Arduino IDE to avoid boot loops.
Hardware
- 1 ESP8266-ESP-12E module (4 MB flash, board "NodeMCU 1.0 (ESP-12E Module)")
- 1 USB-to-serial cable for the first upload (GPIO0 tied to ground during reset = flash mode)
- 2.4 GHz Wi-Fi network; the computer and the module must be on the same subnet (mDNS/broadcast)
- Arduino IDE with the esp8266 core installed
Complete code
#include <ESP8266WiFi.h>
#include <ArduinoOTA.h>
const char* SSID = "mon-reseau";
const char* PASS = "mot-de-passe";
void setup {
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.begin(SSID, PASS);
while (WiFi.status != WL_CONNECTED) { delay(250); Serial.print("."); }
Serial.printf("\nIP = %s\n", WiFi.localIP.toString.c_str);
ArduinoOTA.setHostname("esp12e-01");
ArduinoOTA.setPassword("secret-ota"); // optionnel mais recommandé
ArduinoOTA.onStart([] {
Serial.printf("OTA debut : %s\n", ArduinoOTA.getCommand == U_FLASH ? "sketch" : "fs");
});
ArduinoOTA.onEnd([] { Serial.println("\nOTA termine, redemarrage"); });
ArduinoOTA.onProgress([](unsigned int prog, unsigned int total) {
static unsigned int dernier = 0;
if (prog * 100 / total / 10 != dernier) {
dernier = prog * 100 / total / 10;
Serial.printf("%u%% ", dernier * 10);
}
});
ArduinoOTA.onError([](ota_error_t e) { Serial.printf("Erreur OTA %u\n", e); ESP.restart; });
ArduinoOTA.begin;
}
void loop {
ArduinoOTA.handle; // doit etre appele souvent : pas de blocage ici
// ... application ...
yield;
}
Expected verification
After the first upload via cable, the console displays the module's IP address. In Arduino IDE, a new "esp12e-01 @ 192.168.1.x" port appears (it may take a few seconds, or you may need to restart the IDE). Select it, modify the code (for example, change a message), and upload: progress is shown in the serial console, the module restarts, and the new program runs — without a cable. In the menu, the "OTA" upload size has replaced "Serial".
Common errors
- Wrong flash preset: using "Flash Size 512 KB" on a 4 MB ESP-12E, or vice versa on a real 512 KB chip, causes the bootloader to fail to find the sketch → boot loop. Choose "NodeMCU 1.0 (ESP-12E Module)": 4 MB flash, DIO mode.
- Trying to upload an OTA sketch larger than ~1.5 MB: the standard partition scheme reserves two sketch slots; beyond that, the upload fails (size error).
- The network port does not appear: a firewall blocking multicast/broadcast, or the PC and module being on different VLANs; the remote host's "esp8266-ota" port is also an option.
- Forgetting ArduinoOTA.handle in loop or blocking for 30 seconds in the code: the update fails due to timeout.
- An OTA password is set but not entered in the IDE (Ports menu → password requested): the upload is silently rejected.
- The first upload cannot be OTA: it is the serial cable that installs the receiver, with GPIO0 tied to ground during reset.
Other tutorials for this board
Code examples for this board
clignotante.inoArduinobouton-led.inoArduinorespiration.inoArduinodht11.inoArduinoluxmetre.inoArduinoserveur-web.inoArduino