Introduction
OTA (Over-The-Air) updates allow you to upload new firmware to your ESP32 over Wi-Fi, without physically connecting a USB cable. This is a game-changer for deployed IoT projects — imagine updating the code on a sensor mounted on your roof without climbing up to get it!
How OTA Works
The ESP32 runs a small OTA server in the background. When you upload from the Arduino IDE, it sends the compiled binary over your local Wi-Fi network directly to the ESP32, which writes it to flash and reboots into the new code.
Step 1: Install Required Tools
OTA requires Python 3 on your computer. Install it from python.org. The ESP32 Arduino core includes the necessary OTA libraries automatically.
Step 2: Upload the OTA Base Code
This code must be uploaded once via USB. After that, all future updates go over Wi-Fi.
#include <WiFi.h>
#include <ArduinoOTA.h>
const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASSWORD";
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) delay(500);
Serial.println("IP: " + WiFi.localIP().toString());
ArduinoOTA.setHostname("esp32-iot-device");
ArduinoOTA.setPassword("your_ota_password");
ArduinoOTA.onStart([]() { Serial.println("OTA Start"); });
ArduinoOTA.onEnd([]() { Serial.println("\nOTA Done!"); });
ArduinoOTA.onProgress([](unsigned int prog, unsigned int total) {
Serial.printf("Progress: %u%%\r", (prog / (total / 100)));
});
ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("Error[%u]\n", error);
});
ArduinoOTA.begin();
Serial.println("OTA Ready!");
}
void loop() {
ArduinoOTA.handle(); // Must be called every loop!
// Your normal code here...
delay(1000);
}
Step 3: Upload Over Wi-Fi
- After uploading the base code via USB, open the Arduino IDE.
- Go to Tools > Port — you will now see a network port listed as your ESP32’s hostname.
- Select it and upload your new sketch normally. It will transfer over Wi-Fi!
Security Tip
Always set a strong OTA password with ArduinoOTA.setPassword(). Without a password, anyone on your network can overwrite your ESP32 firmware.
Conclusion
OTA updates are essential for any production IoT deployment. Once set up, you will never need to physically access your ESP32 again for firmware updates.