ESP32-C3 mit PlatformIO: Over-The-Air-Updates de/eng

Words
735
Reading
4 min
17m
Listen to this post
~5 min

OTA-Updates (Over-The-Air) für den ESP32-C3 mit PlatformIO: Nie wieder Kabel stecken! / Wireless OTA Updates for ESP32-C3 with PlatformIO

[English version below]


Deutsche Version

Wer kennt das nicht? Man hat den ESP32-C3 SuperMini fertig programmiert, in ein 3D-gedrucktes Gehäuse gesteckt und irgendwo an der Wand oder am Schreibtisch platziert. Kaum bemerkt man einen kleinen Software-Bug, geht das Gefummel los: Gehäuse aufschrauben, winziges USB-C-Kabel suchen, hoffen, dass der Bootloader beim Flashen mitspielt.

Es gibt einen besseren Weg: Kabellose Firmware-Updates über das heimische WLAN (OTA).

In diesem kurzen Tutorial zeige ich dir, wie du deinen ESP32-C3 so einrichtest, dass du Code-Änderungen künftig mit nur einem Klick direkt über die Luft überträgst.

1. Die Voraussetzungen

  • Hardware: Ein ESP32-C3 SuperMini (oder ein vergleichbares ESP32-Board mit 4MB Flash).

  • Tooling: Visual Studio Code mit PlatformIO.

  • Bibliotheken: ArduinoOTA (ist beim ESP32 Arduino-Framework praktischerweise schon direkt mit an Bord).

2. Der C++ Code (Minimaler Blink- & OTA-Demo-Code)

Damit der ESP32 auf Update-Befehle lauschen kann, muss er sich mit deinem WLAN verbinden und den OTA-Dienst starten.

Wichtig hierbei: Nutze keine blockierenden delay()-Schleifen, da der ESP sonst während der Pausen nicht auf OTA-Anfragen reagieren kann. Am besten arbeitet man mit millis():

#include 
#include 

// --- DEINE WLAN-DATEN / YOUR WIFI CREDENTIALS ---
const char* ssid = "DEIN_WLAN_NAME";
const char* password = "DEIN_WLAN_PASSWORT";
const char* ota_password = "dein_ota_passwort"; // Passwortschutz / Upload password

#define LED_PIN 8
unsigned long blinkInterval = 1000; // 1 Sekunde Takt / 1 second interval
unsigned long previousMillis = 0;
bool ledState = LOW;

void setup() {
  Serial.begin(115200);
  pinMode(LED_PIN, OUTPUT);

  // Mit WLAN verbinden / Connect to WiFi
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("\nVerbunden! IP-Adresse: " + WiFi.localIP().toString());

  // OTA konfigurieren / Configure OTA
  ArduinoOTA.setHostname("esp32c3-ota-demo");
  ArduinoOTA.setPassword(ota_password);

  ArduinoOTA.onStart([]() { Serial.println("OTA Update startet..."); });
  ArduinoOTA.onEnd([]() { Serial.println("\nUpdate fertig! Neustart..."); });
  ArduinoOTA.onError([](ota_error_t error) { Serial.printf("Fehler[%u]\n", error); });

  ArduinoOTA.begin();
}

void loop() {
  // WICHTIG: Ständig im Loop aufrufen! / IMPORTANT: Call continuously in loop!
  ArduinoOTA.handle();

  // Nicht-blockierendes Blinken / Non-blocking blink
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= blinkInterval) {
    previousMillis = currentMillis;
    ledState = !ledState;
    digitalWrite(LED_PIN, ledState);
  }
}

3. Die platformio.ini Konfiguration

Damit PlatformIO weiß, wohin die Reise geht, fügen wir in der platformio.ini eine zweite Umgebung für den OTA-Upload hinzu. Du kannst hier wahlweise mit einer festen IP-Adresse oder über mDNS (.local) arbeiten:

[env:esp32-c3-supermini]
platform = espressif32 @ 6.4.0
board = esp32-c3-devkitm-1
framework = arduino
monitor_speed = 115200
build_flags =
    -DARDUINO_USB_CDC_ON_BOOT=1
    -DARDUINO_USB_MODE=1

[env:esp32-c3-supermini-ota]
extends = env:esp32-c3-supermini
upload_protocol = espota
upload_port = 192.168.178.63     ; Entweder die IP des ESPs ODER z.B. esp32c3-ota-demo.local
upload_flags =
    --auth=dein_ota_passwort    ; Muss exakt mit dem Passwort im Code übereinstimmen

4. Der magische Moment: So wird geflasht!

  1. Erster Upload (Klassisch): Lade den Code das allererste Mal über das physische USB-Kabel auf den ESP32-C3. Öffne kurz den Seriellen Monitor, um zu sehen, welche IP-Adresse dein Router dem ESP zugewiesen hat. Trage diese IP in deine platformio.ini ein.

  2. Kabel abziehen: Du kannst das USB-Kabel jetzt abziehen und den ESP stattdessen an ein ganz normales Netzteil oder eine Powerbank hängen.

  3. Code ändern: Ändere im Code zum Testen die Blinkfrequenz (z. B. von 1000 auf 100 Millisekunden).

  4. WLAN-Upload starten: Klicke in PlatformIO auf deinen gewohnten Upload-Button (oder nutze das OTA-Environment).

PlatformIO baut die Firmware lokal zusammen, erkennt, dass kein USB-Port aktiv ist, kontaktiert den ESP32 über das WLAN, schickt das Update rüber, und nach wenigen Sekunden blinkt die LED im neuen Takt – komplett kabellos!

Happy Coding & Tschüss Kabelsalat! 🚀


English Version

We've all been there: you've finished programming your ESP32-C3 SuperMini, tucked it neatly into a 3D-printed enclosure, and mounted it somewhere on the wall or under your desk. The moment you spot a tiny bug in your code, the hassle begins—unscrewing the case, hunting for a short USB-C cable, and hoping the bootloader cooperates.

There is a much better way: wireless firmware updates over your home Wi-Fi using OTA (Over-The-Air).

In this short tutorial, I'll show you how to set up your ESP32-C3 so you can push future code changes over the air with a single click.

1. Prerequisites

  • Hardware: An ESP32-C3 SuperMini (or any comparable ESP32 board with 4MB Flash).

  • Tooling: Visual Studio Code with PlatformIO.

  • Libraries: ArduinoOTA (conveniently built right into the ESP32 Arduino framework).

2. The C++ Code (Minimal Blink & OTA Demo)

For the ESP32 to listen for incoming updates, it needs to connect to your Wi-Fi and initialize the OTA service.

One crucial rule: Avoid blocking delay() loops, as the microcontroller won't be able to respond to OTA requests while paused. Using millis() for non-blocking timing is the way to go:

#include 
#include 

// --- YOUR WIFI CREDENTIALS ---
const char* ssid = "YOUR_WIFI_SSID";
const char* password = "YOUR_WIFI_PASSWORD";
const char* ota_password = "your_ota_password"; // Upload password

#define LED_PIN 8
unsigned long blinkInterval = 1000; // 1 second interval
unsigned long previousMillis = 0;
bool ledState = LOW;

void setup() {
  Serial.begin(115200);
  pinMode(LED_PIN, OUTPUT);

  // Connect to WiFi
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("\nConnected! IP Address: " + WiFi.localIP().toString());

  // Configure OTA
  ArduinoOTA.setHostname("esp32c3-ota-demo");
  ArduinoOTA.setPassword(ota_password);

  ArduinoOTA.onStart([]() { Serial.println("OTA Update starting..."); });
  ArduinoOTA.onEnd([]() { Serial.println("\nUpdate finished! Rebooting..."); });
  ArduinoOTA.onError([](ota_error_t error) { Serial.printf("Error[%u]\n", error); });

  ArduinoOTA.begin();
}

void loop() {
  // IMPORTANT: Call continuously in loop!
  ArduinoOTA.handle();

  // Non-blocking blink
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= blinkInterval) {
    previousMillis = currentMillis;
    ledState = !ledState;
    digitalWrite(LED_PIN, ledState);
  }
}

3. The platformio.ini Configuration

To tell PlatformIO where to send the compiled firmware, we add a second environment to platformio.ini dedicated to OTA uploads. You can target your board either via its static local IP address or dynamically via mDNS (.local):

[env:esp32-c3-supermini]
platform = espressif32 @ 6.4.0
board = esp32-c3-devkitm-1
framework = arduino
monitor_speed = 115200
build_flags =
    -DARDUINO_USB_CDC_ON_BOOT=1
    -DARDUINO_USB_MODE=1

[env:esp32-c3-supermini-ota]
extends = env:esp32-c3-supermini
upload_protocol = espota
upload_port = 192.168.178.63     ; Your ESP's IP address OR e.g. esp32c3-ota-demo.local
upload_flags =
    --auth=dein_ota_passwort    ; Must match the password defined in your C++ code

4. The Magic Moment: Flashing Wirelessly!

  1. First Upload (Wired): Flash the code onto your ESP32-C3 via USB for the very first time. Open the Serial Monitor briefly to check which IP address your router assigned to the board, and enter it into your platformio.ini.

  2. Unplug the Cable: You can now disconnect the USB data cable from your PC and power the ESP32 from any regular USB wall charger or power bank.

  3. Modify Your Code: Change the blink interval in your code to test it (e.g., from 1000 down to 100 milliseconds).

  4. Trigger the Wi-Fi Upload: Hit your regular Upload button in PlatformIO (or select the OTA environment).

PlatformIO compiles the binary locally, detects no active serial port, reaches out to your ESP32 over Wi-Fi, transfers the firmware, and within seconds your LED blinks at the new speed—completely wire-free!

Happy coding & goodbye cable clutter! 🚀

ESP32-C3 mit PlatformIO: Over-The-Air-Updates de/eng | Ecency