
TutorialSeptember 30, 202410 min read1100 views
Implementing OTA Updates on ESP32
Over-The-Air (OTA) updates are a critical feature for any IoT product. It allows you to update the firmware remotely without physical USB access. Let's build a basic OTA system for ESP32.
Introduction to ArduinoOTA
The ArduinoOTA library uses multicast DNS (mDNS) to advertise the device on the local network, allowing PlatformIO or the Arduino IDE to push binaries wirelessly.
Complete Source Code
#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);
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 esp32-[MAC]
ArduinoOTA.setHostname("quadever-device");
// 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. IP address: ");
Serial.println(WiFi.localIP());
}
void loop() {
ArduinoOTA.handle();
}Updating in PlatformIO
Simply add the target IP to your platformio.ini:
upload_protocol = espota
upload_port = 192.168.1.150 ; Your ESP32 IP#ESP32#OTA#Firmware#PlatformIO
Found this helpful?
Consider supporting my work or sharing this article with other developers.
