🛠 Programmation OTA : téléverser le code par Wi-Fi

ESP8266 (ESP-12E module) · avance · Schéma : IMG-esp8266-esp-12e-avance-2-esp8266-ota-arduino.svg — Module ESP8266-ESP-12E flashé une première fois par câble série puis mis à jour par Wi-Fi depuis un ordinateur, routeur en point d'accès, flash 4 Mo du module annotée.

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

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

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials