OTA : Your Code Flowing Through Air


Have you ever uploaded code on your ESP32 that compiled successfully, but later you realized you mentioned the wrong HTTP server URL or defined the wrong pin for the LED? I can’t say about you, but it has definitely happened with me — and it’s frustrating. Because in that case, what we usually do is bring our ESP32 close to the PC, along with all the connected components, and re-upload the code using a USB cable.
But what if I told you that you don’t need to play this plug-in, plug-out game every time? This whole process can be skipped using OTA — Over-The-Air technology. As the name says, OTA allows us to flash our code to the ESP32 wirelessly, in the air. The code literally floats over Wi-Fi, and the ESP32 catches it smoothly.
Whether you’re a student working on ESP32 prototypes or someone planning to scale your ESP32-based project into a real product, OTA is super useful — and you should definitely be aware of it.
What really is OTA ?
OTA is the process by which you can wirelessly update your firmware and software on embedded devices using remote communication technologies like Wi-Fi, Bluetooth, or cellular networks. Here, the important word is update, because at the start, you need to upload the code to your device with the help of a USB cable. In that code, you also have to include the OTA library (if working in Arduino IDE) and OTA functions so your device can handle OTA requests later on.
Different Type Of OTA For ESP32
Although there exist many classifications of OTA, here we will discuss ESP32-based OTA based on the update protocol using Wi-Fi -
1) Arduino IDE OTA (Basic Wi Fi OTA)
Arduino IDE OTA is very simple and useful for students working on ESP32-based prototypes. In this, you flash your code to the ESP32 using USB. Your code should be OTA-enabled — it should have the ArduinoOTA library and some functions which we will look at later. After the code is uploaded, unplug the USB and power the ESP32 from another source. Make sure that your ESP32 and your system (the one used to upload the code) are on the same Wi-Fi network. Now go to Arduino IDE → Tools → Ports → esp32-<host name> at <IP address> (ESP32 Dev Module)
. Change the existing port to this network port. Update your code in Arduino IDE itself, click upload, and magic will happen — your updated code will get flashed to the ESP32 without using a USB cable. If it ask for password and you did not setup that in code, just use space and upload. Just make sure that both the ESP32 and your laptop/PC are on the same network.
2) Web Server OTA
In Web Server OTA, ESP32 hosts a web server with an OTA update form (using ElegantOTA). We open the web page using http://<IP address of ESP32>/update
on a browser in a phone/PC, and now we can upload the updated .bin
firmware file, and the new code will be uploaded directly from the browser. To use Web Server-based OTA on ESP32, it is good to have knowledge of libraries like ESPAsyncWebServer and AsyncElegantOTA. It is mandatory that your ESP32 and phone/PC are on the same Wi-Fi network
3) HTTP OTA
In HTTP OTA, we host a .bin
firmware file on a server, and ESP32 downloads that file and automatically flashes the new code. This server can be local or cloud-based. If the server is local and set on your PC, then again both the ESP32 and the PC should be on the same network. This means that with a local server, we can’t update firmware from a distance, making it a non-scalable option for market products. On the other hand, cloud web servers give us the option to update firmware from anywhere without being on the same network. This is very useful for real deployments and remote updates. Also, cloud web servers give us the benefit of using HTTPS, which is a more secure option.
4) MQTT OTA
MQTT OTA is ideal for mass deployments and large networks of devices. Since MQTT is a publish-subscribe protocol, the ESP32 subscribes to a topic. We publish a message (firmware URL) from the server, and the ESP32 receives the message and performs the OTA. This is also called MQTT-triggered HTTP OTA because the initial communication is done using MQTT, but the ESP32 downloads the new firmware using HTTP. This MQTT-based OTA offers high scalability and gives us more control logic, because in HTTP-only OTA, ESP32 decides when to check or update. For MQTT OTA, you need an external MQTT broker, which acts as a middleman between the publisher and the subscriber.
Example Of Arduino IDE OTA
You uploaded code initially to ESP32 via USB just for blinking the LED.
#include <WiFi.h>
#include <ArduinoOTA.h>
// Wi-Fi credentials
const char* ssid = "YOUR_SSID";
const char* password = "YOUR_WIFI_PASSWORD";
// OTA password (used to authorize OTA uploads)
const char* ota_password = "esp32secure123";
const int ledPin = 2; // Built-in LED or external LED
void setup() {
Serial.begin(115200);
pinMode(ledPin, OUTPUT);
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nWiFi connected");
// --- OTA Setup (Minimal) ---
ArduinoOTA.setPassword(ota_password); // Set OTA upload password
ArduinoOTA.begin(); // Start OTA service
// ---------------------------
}
void loop() {
ArduinoOTA.handle(); // Must be called in loop to listen for OTA uploads
// Simple LED blink
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
delay(500);
}
But, later you thought to integrate HC - SR04 (Distance Sensor) based LED blinking, so just follow the method give above and write this code in Arduino IDE and upload via network port wirelessly.
#include <WiFi.h>
#include <ArduinoOTA.h>
// Wi-Fi credentials
const char* ssid = "YOUR_SSID";
const char* password = "YOUR_WIFI_PASSWORD";
// OTA password
const char* ota_password = "esp32secure123";
// Pin definitions
const int ledPin = 2;
const int trigPin = 5;
const int echoPin = 18;
// Distance threshold in cm
const int thresholdCM = 20;
void setup() {
Serial.begin(115200);
pinMode(ledPin, OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nWiFi connected");
// OTA Setup
ArduinoOTA.setPassword(ota_password);
ArduinoOTA.begin();
}
// Measure distance using HC-SR04
long getDistance() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long duration = pulseIn(echoPin, HIGH);
long distance = duration * 0.034 / 2;
return distance;
}
void loop() {
ArduinoOTA.handle();
long distance = getDistance();
Serial.println("Distance: " + String(distance) + " cm");
if (distance < thresholdCM) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
delay(300); // Sampling delay
}
So, this was all about OTA from my side. We tried to cover basics of Over The Air updates on ESP32 including different types of OTA. These methods can be useful in different projects depending on need and scalability. Now, you can use OTA in your different projects and you don’t need to worry about plugging in - plugging out the USB cable.
Subscribe to my newsletter
Read articles from Vishal - directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
