Blog Datasheets Home About me Clients My work Services Contact

G2Labs Grzegorz Grzęda

Implementing OTA (Over-The-Air) Updates on ESP32 Projects

June 16, 2024

Implementing OTA (Over-The-Air) Updates on ESP32 Projects

Updating firmware on embedded devices can often be a challenging task, requiring physical access to the device for reflashing. However, with the advent of Over-The-Air (OTA) updates, an embedded device’s firmware can be updated remotely. In this blog post, we will explore how to implement OTA updates on ESP32 projects using the Arduino framework.

Understanding OTA Updates

OTA updates allow developers to remotely update the firmware of a device without requiring any physical connections. The firmware upgrade process usually involves setting up a server that hosts the updated firmware binary and configuring the device to fetch and install the new firmware.

Pre-requisites

Before diving into OTA updates, ensure that you have the following:

  1. An ESP32 development board
  2. Arduino IDE set up with the ESP32 boards installed
  3. A Wi-Fi network for wireless communication

Setting up the Project

Start by creating a new Arduino project for your ESP32 board. Navigate to File > Examples > ArduinoOTA > BasicOTA to open the example sketch. This example provides a basic implementation of OTA updates where the device connects to a Wi-Fi network and checks for firmware updates.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <WiFi.h>
#include <ESPmDNS.h>
#include <WiFiUdp.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(1000);
    Serial.println("Connecting to WiFi...");
  }
  ArduinoOTA.begin();
  Serial.println("Ready for OTA updates");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
}

void loop() {
  ArduinoOTA.handle();
}

Replace YOUR_SSID and YOUR_PASSWORD with the credentials for your Wi-Fi network.

Uploading Initial Firmware

Before we can enable OTA updates, we need to upload the initial firmware using the traditional upload method. Connect your ESP32 board and select the appropriate board and port in the Arduino IDE. Upload the sketch to the board as you would with any other sketch.

Setting Up the OTA Update Server

To provide the firmware for OTA updates, we need to set up a server. This server can be hosted on a local machine, cloud hosting, or even a Raspberry Pi. For simplicity, we will use ArduinoOTA.setHostname() to provide a hostname that the ESP32 will use to reach the update server on the local network:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <WiFi.h>
#include <ESPmDNS.h>
#include <WiFiUdp.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(1000);
    Serial.println("Connecting to WiFi...");
  }
  ArduinoOTA.setHostname("esp32-ota-example");
  ArduinoOTA.begin();
  Serial.println("Ready for OTA updates");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
}

void loop() {
  ArduinoOTA.handle();
}

Once you’ve set the hostname, re-upload the sketch to the ESP32 board.

Testing the OTA Update

To test the OTA update process, make a modification to your existing sketch. For instance, add a line that prints a custom message in the loop() function:

1
2
3
4
void loop() {
  ArduinoOTA.handle();
  Serial.println("Hello from OTA update!");
}

Save the file and navigate to Sketch > Export compiled Binary to generate a firmware binary.

Next, host the firmware binary on your OTA server using a tool like Arduino ESP8266/ESP32 CI/CD Tools or any server hosting solution of your choice.

Once the firmware binary is hosted, ensure that the ESP32 device and your computer are connected to the same Wi-Fi network. Now, open the Arduino IDE, select the ESP32 board, and navigate to Sketch > Upload Using > OTA. This will trigger the OTA update process.

The ESP32 board will connect to the OTA server and fetch the new firmware binary. It will then install the new firmware and reboot automatically. Monitor the serial output to observe the update process.

Conclusion

In this blog post, we explored how to implement OTA updates on ESP32 projects using the Arduino framework. We walked through the initial setup, configuring the OTA update server, and the testing process. OTA updates enable seamless firmware updates of embedded devices, saving time and effort, especially for remote devices that are difficult to access physically.

Feel free to explore more advanced features, such as authentication, versioning, and error handling, to make your OTA update mechanism more robust and secure. Happy coding and updating!

Remember, always test and validate your OTA update process thoroughly before deploying it to production devices.


➡️ Low Power Deep Sleep Modes in ESP32: Exploring Power Saving Techniques


⬅️ Linux cat command


Go back to Posts.