
Hey everyone! Ever build an awesome ESP32 project, mount it up on the ceiling or out in the garden, and then realize you need to fix a bug in your code? Hauling your laptop out to the yard with a tangled USB cable isn’t exactly the “smart home” dream.
That’s where Over-The-Air (OTA) updates come to the rescue! OTA lets you flash new firmware to your ESP32 wirelessly over your local Wi-Fi network. It’s an absolute game-changer for deploying embedded devices in hard-to-reach places.
To get started, you’ll need the ArduinoOTA library. Here is a basic boilerplate to get OTA running on your ESP32.
#include <WiFi.h>
#include <ESPmDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
const char* ssid = "YOUR_WIFI_SSID";
const char* password = "YOUR_WIFI_PASSWORD";
void setup() {
Serial.begin(115200);
Serial.println("Booting");
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("Connection Failed! Rebooting...");
delay(5000);
ESP.restart();
}
// Port defaults to 3232
// ArduinoOTA.setPort(3232);
// Hostname defaults to esp3232-[MAC]
ArduinoOTA.setHostname("my-esp32-ota");
// No authentication by default
// ArduinoOTA.setPassword("admin");
ArduinoOTA
.onStart([]() {
String type;
if (ArduinoOTA.getCommand() == U_FLASH)
type = "sketch";
else // U_SPIFFS
type = "filesystem";
Serial.println("Start updating " + type);
})
.onEnd([]() {
Serial.println("\nEnd");
})
.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
})
.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
else if (error == OTA_END_ERROR) Serial.println("End Failed");
});
ArduinoOTA.begin();
Serial.println("Ready");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
void loop() {
ArduinoOTA.handle();
// Your regular code goes here!
}
my-esp32-ota at 192.168.1.x).Stay tuned for more advanced OTA features, like adding a password or doing web-based updates!
To follow along with this lesson, you’ll need the following components: