The ESP32 supports OTA (Over-the-Air), allowing you to update sketches wirelessly. OTA is especially helpful when the microcontroller is hard to access or if you want to make changes without a physical connection. In this tutorial, you will learn how to set up OTA and go through a step-by-step example project: a blinking LED whose blinking frequency is modified via an OTA update.
The ArduinoOTA Library
If you have added the ESP32 boards through the Arduino IDE board manager (accessible via Tools > Board > Boards Manager), the ArduinoOTA library is usually already included. This library allows for seamless integration of OTA functionality.
If the library is missing, you can install it through the library manager. In the Arduino IDE, go to Sketch > Include Library > Manage Libraries and search for “ArduinoOTA.” Make sure to install the latest version to benefit from all the current features and security improvements.
Step 1: The First Sketch for a Blinking LED
Start with a simple sketch that makes an LED on the ESP32 blink at one-second intervals. This sketch is a foundational step for practicing OTA updates, helping you understand the basic workflow before making more complex changes. The LED is connected to GPIO pin 2 of the ESP32 through an appropriate current-limiting resistor. This sketch will serve as the basis for the OTA update.
#include <WiFi.h>
#include <ArduinoOTA.h>
const char* ssid = "Your_WIFI_NETWORK"; // Replace with your WiFi name
const char* password = "Your_WIFI_PASSWORD"; // Replace with your WiFi password
#define LED_PIN 2 // GPIO pin for the LED
unsigned long previousMillis = 0;
const long interval = 1000; // Blinking interval in milliseconds (1 second)
void setup() {
Serial.begin(115200);
pinMode(LED_PIN, OUTPUT);
// Connect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nConnected to WiFi");
// OTA setup
ArduinoOTA.setHostname("esp32_led_ota");
ArduinoOTA.setPassword("Your_Secure_Password"); // Set a strong password for OTA updates to prevent unauthorized access
ArduinoOTA.begin();
}
void loop() {
ArduinoOTA.handle(); // Check for OTA updates
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
digitalWrite(LED_PIN, !digitalRead(LED_PIN)); // Toggle LED state
}
}
___STEADY_PAYWALL___
How the Sketch Works
- Include Libraries: The sketch begins by including the required libraries
WiFi.h
andArduinoOTA.h
. The first allows the ESP32 to connect to a WiFi network, whileArduinoOTA.h
provides OTA functionality. These libraries are essential to implement the desired network and update functions on the ESP32. - Network Configuration: The variables
ssid
andpassword
store the credentials for your WiFi. They are used to connect the ESP32 to your network. - GPIO Pin Definition:
#define LED_PIN 2
defines the pin to which the LED is connected. In this case, GPIO pin 2 of the ESP32 is used. - Connect to WiFi: In the
setup()
part of the code, the connection to WiFi is established. WithWiFi.begin(ssid, password)
, the ESP32 connects to the network. The loopwhile (WiFi.status() != WL_CONNECTED)
ensures that the program waits until the connection is established. - OTA Setup: OTA functionality is also initialized in
setup()
. UsingArduinoOTA.setHostname("esp32_led_ota")
, the name of the ESP32 in the network is set. This name makes it easier to identify the device in the network, especially if you have multiple ESP32 devices. A (preferably secure) password is set withArduinoOTA.setPassword("Your_OTA_Password")
to ensure that only you can perform updates.ArduinoOTA.begin()
starts the OTA service, so the ESP32 waits for incoming updates. - Blinking LED: The
loop()
function contains the code that makes the LED blink at one-second intervals. The functionmillis()
checks if the set interval (1000 milliseconds) has passed. If this is the case, the LED state is toggled withdigitalWrite(LED_PIN, !digitalRead(LED_PIN))
. - OTA Handler: In the
loop()
function,ArduinoOTA.handle()
is called to continuously check for OTA updates. This allows for updates to be received at any time while the main program continues running.
Step 2: OTA Update to Change the Interval
Now make a change to the code to reduce the LED blink frequency to 500 ms. This change will be transmitted wirelessly via OTA to the ESP32.
Change the interval value in the sketch from 1000 to 500:
const long interval = 500; // Blinking interval in milliseconds (500 ms)
Performing the Update via OTA
Now, upload the updated sketch. As long as your ESP32 is connected to your WiFi network, it should appear as a network port in the Arduino IDE. Go to Tools > Port and select the ESP32 (esp32_led_ota
).
Upload the OTA Update: Upload the new sketch (with the 500 ms blinking interval) via the network port. Click the upload button as usual, just as you would if your ESP32 were connected via a USB cable. Make sure that the updated sketch contains the WiFi credentials and your password.
The Arduino IDE will prompt you for the OTA password. Enter the defined password (“Your_OTA_Password”), and the update will be transferred wirelessly.
Note: If you are performing updates with different passwords, the Arduino IDE may try to use an incorrect password automatically. In this case, close and reopen the IDE, and you will be prompted again for the password for your ESP32.
After the update is transferred wirelessly to your ESP32, the LED should now blink at the new half-second interval. If the update fails, ensure that the ESP32 is properly connected to the WiFi network and that the correct OTA password is used. That’s it—you now know how to update programs without having to remove the microcontroller and connect it to your computer. For more information, check out Espressif’s resources.