Secure & Reliable ESP32 OTA Firmware Updates Tutorial

Wirelessly update ESP32Setting Up ESP32 as a Wi-Fi Access PointSetting Up ESP32 as a Wi-Fi Access PointMaster ESP32 AP configuration with our step-by-step guide. Set up a secure, local IoT network using practical code examples and optimization tips. firmware to fix bugs, add features, or patch vulnerabilities-no physical access required.

Table of Contents🔗

1. Why OTA UpdatesAWS IoT Core with ESP32: X.509 Certificates and Shadow UpdatesAWS IoT Core with ESP32: X.509 Certificates and Shadow UpdatesLearn to securely connect ESP32 to AWS IoT Core using X.509 certificates and device shadows, with step-by-step instructions and best practices. Matter

2. Understanding OTA Concepts

3. Prerequisites for OTA UpdatesAWS IoT Core with ESP32: X.509 Certificates and Shadow UpdatesAWS IoT Core with ESP32: X.509 Certificates and Shadow UpdatesLearn to securely connect ESP32 to AWS IoT Core using X.509 certificates and device shadows, with step-by-step instructions and best practices.

4. Setting Up Basic OTA UpdatesAWS IoT Core with ESP32: X.509 Certificates and Shadow UpdatesAWS IoT Core with ESP32: X.509 Certificates and Shadow UpdatesLearn to securely connect ESP32 to AWS IoT Core using X.509 certificates and device shadows, with step-by-step instructions and best practices.

5. Securing OTA UpdatesAWS IoT Core with ESP32: X.509 Certificates and Shadow UpdatesAWS IoT Core with ESP32: X.509 Certificates and Shadow UpdatesLearn to securely connect ESP32 to AWS IoT Core using X.509 certificates and device shadows, with step-by-step instructions and best practices. with HTTPS

6. Dual-Partition OTADual-Partition OTA: Safe Rollback and A/B Testing on ESP32Dual-Partition OTA: Safe Rollback and A/B Testing on ESP32Explore the ESP32 dual-partition OTA update process, ensuring safe rollbacks and effective A/B testing for reliable IoT deployments. for Safe Rollbacks

7. Handling OTA Failures and Rollbacks

8. Advanced Strategies: Delta UpdatesFirmware Updates over NB-IoT: Delta Updates with ESP32’s Dual PartitionFirmware Updates over NB-IoT: Delta Updates with ESP32’s Dual PartitionDiscover how delta firmware updates via NB-IoT optimize ESP32 device performance by minimizing data usage and ensuring secure, swift OTA transitions. and Dual Partitioning

9. Monitoring OTA Progress

10. Optimizing OTA for Low-BandwidthAdaptive Data Rate (ADR) Optimization for LoRaWAN on ESP32Adaptive Data Rate (ADR) Optimization for LoRaWAN on ESP32Optimize your IoT network with our ADR tutorial for ESP32 in LoRaWAN. Learn dynamic transmission tuning, power management, and troubleshooting strategies. Networks

11. TroubleshootingConnecting ESP32 to Cloud Services via Wi-FiConnecting ESP32 to Cloud Services via Wi-FiDiscover how to connect your ESP32 to AWS, Azure, and Google Cloud using secure Wi-Fi. This guide covers setup, error handling, and low power strategies. Common OTA Issues

12. Best PracticesZigbee Green Power: Ultra-Low-Power Energy Harvesting SolutionsZigbee Green Power: Ultra-Low-Power Energy Harvesting SolutionsDiscover how ZGP enables battery-free IoT devices through energy harvesting with ESP32 integrations, supporting smart home and industrial applications. for OTA Updates

Why OTA Updates Matter🔗

OTA updates are essential for maintaining and improving IoT devicesConnecting ESP32 to Cloud Services via Wi-FiConnecting ESP32 to Cloud Services via Wi-FiDiscover how to connect your ESP32 to AWS, Azure, and Google Cloud using secure Wi-Fi. This guide covers setup, error handling, and low power strategies. over their lifecycle. They allow you to:

For ESP32, OTA updatesAWS IoT Core with ESP32: X.509 Certificates and Shadow UpdatesAWS IoT Core with ESP32: X.509 Certificates and Shadow UpdatesLearn to securely connect ESP32 to AWS IoT Core using X.509 certificates and device shadows, with step-by-step instructions and best practices. can be implemented over Wi-Fi, making it a cost-effective solution for connected devices.

Understanding OTA Concepts🔗

OTA updatesAWS IoT Core with ESP32: X.509 Certificates and Shadow UpdatesAWS IoT Core with ESP32: X.509 Certificates and Shadow UpdatesLearn to securely connect ESP32 to AWS IoT Core using X.509 certificates and device shadows, with step-by-step instructions and best practices. allow firmware upgrades over Wi-Fi, bypassing the need for USB/UART connections. The ESP32 supports two primary OTA methods:

Key Components:

Prerequisites for OTA Updates🔗

1. Wi-FiArquitetura ESP32: SoC dual-core, subsistemas RF integradosArquitetura ESP32: SoC dual-core, subsistemas RF integradosDiscover the ESP32’s dual-core prowess and integrated RF subsystems for efficient, innovative IoT applications—from smart homes to industrial sensors. Connectivity: ESP32 must connect to a network (Station modeSetting Up Wi-Fi Station Mode on ESP32Setting Up Wi-Fi Station Mode on ESP32Master the ESP32 Wi-Fi Station Mode with our guide featuring configuration steps, error handling, and power-saving tips for effective IoT projects.).

2. Partition Scheme: Use a partition table with at least two OTA slots.

3. Firmware Size: Compiled binary must fit within partition size (check partitions.csv).

4. Power Stability: Ensure uninterrupted power during updates (~10-60 seconds).

Setting Up Basic OTA Updates🔗

The ESP32 supports OTA updatesAWS IoT Core with ESP32: X.509 Certificates and Shadow UpdatesAWS IoT Core with ESP32: X.509 Certificates and Shadow UpdatesLearn to securely connect ESP32 to AWS IoT Core using X.509 certificates and device shadows, with step-by-step instructions and best practices. natively via the ArduinoOTA library. Here’s how to get started:

1. Install the ArduinoOTA Library

If you’re using the Arduino IDE, install the ArduinoOTA library via the Library Manager.

2. Basic OTA Code Example

Below is a simple example to enable OTA updatesAWS IoT Core with ESP32: X.509 Certificates and Shadow UpdatesAWS IoT Core with ESP32: X.509 Certificates and Shadow UpdatesLearn to securely connect ESP32 to AWS IoT Core using X.509 certificates and device shadows, with step-by-step instructions and best practices.:

#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(1000);
    Serial.println("Connecting to WiFi...");
  }
  ArduinoOTA.begin();
  ArduinoOTA.onStart([]() {
    Serial.println("OTA update started");
  });
  ArduinoOTA.onEnd([]() {
    Serial.println("\nOTA update complete");
  });
  ArduinoOTA.onProgress([](int progress, int total) {
    Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
  });
  ArduinoOTA.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");
  });
}
void loop() {
  ArduinoOTA.handle();
}

3. Uploading the Firmware

After uploading this sketch, you can update the firmware by selecting the OTA port in the Arduino IDE and uploading a new sketch.

Limitations: No encryptionConnecting ESP32 to Cloud Services via Wi-FiConnecting ESP32 to Cloud Services via Wi-FiDiscover how to connect your ESP32 to AWS, Azure, and Google Cloud using secure Wi-Fi. This guide covers setup, error handling, and low power strategies. or integrity checks. Suitable for local networks only.

Securing OTA Updates with HTTPS🔗

For production environments, use ESP-IDFZigbee Over-the-Air (OTA) Firmware Updates with ESP32 CoordinatorsZigbee Over-the-Air (OTA) Firmware Updates with ESP32 CoordinatorsSecure your IoT network with OTA firmware upgrades using an ESP32 coordinator. Our guide details firmware setup, packaging, security, and troubleshooting.’s esp_https_ota with cryptographic verification:

1. Generate a Secure Certificate

Use tools like OpenSSLAWS IoT Core with ESP32: X.509 Certificates and Shadow UpdatesAWS IoT Core with ESP32: X.509 Certificates and Shadow UpdatesLearn to securely connect ESP32 to AWS IoT Core using X.509 certificates and device shadows, with step-by-step instructions and best practices. to generate a self-signed certificate or obtain one from a trusted Certificate Authority (CA).

2. Modify the OTA Code for HTTPSImplementing Secure Communication over Wi-Fi on ESP32Implementing Secure Communication over Wi-Fi on ESP32This comprehensive guide secures ESP32 IoT devices using HTTPS, TLS for MQTT, proper certificate management, and network hardening practices.

Replace the default OTA implementation with a secure HTTPS server. The ESP32’sCombining Wi-Fi with Deep Sleep for Low-Power ApplicationsCombining Wi-Fi with Deep Sleep for Low-Power ApplicationsLearn how to integrate Wi-Fi and deep sleep on ESP32 to maximize battery life in IoT devices. This guide offers practical tips and step-by-step instructions. HTTPUpdate library supports HTTPSImplementing Secure Communication over Wi-Fi on ESP32Implementing Secure Communication over Wi-Fi on ESP32This comprehensive guide secures ESP32 IoT devices using HTTPS, TLS for MQTT, proper certificate management, and network hardening practices..

#include <WiFiClientSecure.h>
#include <HTTPUpdate.h>
const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";
const char* updateUrl = "https://your-server.com/firmware.bin";
void performUpdate() {
  WiFiClientSecure client;
  client.setCACert(rootCACertificate); // Add your CA certificate here
  httpUpdate.update(client, updateUrl);
}

3. Sign Firmware UpdatesAWS IoT Core with ESP32: X.509 Certificates and Shadow UpdatesAWS IoT Core with ESP32: X.509 Certificates and Shadow UpdatesLearn to securely connect ESP32 to AWS IoT Core using X.509 certificates and device shadows, with step-by-step instructions and best practices.

Use cryptographic signatures (e.g., ECDSA) to ensure the firmware hasn’t been tampered with. The ESP32 supports secure boot and flash encryptionConnecting ESP32 to Cloud Services via Wi-FiConnecting ESP32 to Cloud Services via Wi-FiDiscover how to connect your ESP32 to AWS, Azure, and Google Cloud using secure Wi-Fi. This guide covers setup, error handling, and low power strategies. for added security.

Dual-Partition OTA for Safe Rollbacks🔗

The ESP32’s dual-partition schemeFirmware Updates over NB-IoT: Delta Updates with ESP32’s Dual PartitionFirmware Updates over NB-IoT: Delta Updates with ESP32’s Dual PartitionDiscover how delta firmware updates via NB-IoT optimize ESP32 device performance by minimizing data usage and ensuring secure, swift OTA transitions. ensures safe OTA updates by maintaining two firmware partitions: one for the active firmware and one for the update.

1. Configure Partitions

Use the partitions.csv file to define the partition layout:

# Name,   Type, SubType, Offset,  Size, Flags
nvs,      data, nvs,     0x9000,  0x5000,
otadata,  data, ota,     0xE000,  0x2000,
app0,     app,  ota_0,   0x10000, 1M,
app1,     app,  ota_1,   0x110000,1M,

2. Enable Dual-Partition OTADual-Partition OTA: Safe Rollback and A/B Testing on ESP32Dual-Partition OTA: Safe Rollback and A/B Testing on ESP32Explore the ESP32 dual-partition OTA update process, ensuring safe rollbacks and effective A/B testing for reliable IoT deployments.

The ArduinoOTA library automatically handles dual-partition updates. If the new firmware fails, the device reverts to the previous version.

Handling OTA Failures and Rollbacks🔗

The ESP32Setting Up ESP32 as a Wi-Fi Access PointSetting Up ESP32 as a Wi-Fi Access PointMaster ESP32 AP configuration with our step-by-step guide. Set up a secure, local IoT network using practical code examples and optimization tips. bootloader automatically rolls back if:

  • The new firmware crashes on boot.
  • The OTA sequence number is invalid.
  • Checksum validation fails.

Force a Rollback Manually:

esptool.py --port /dev/ttyUSB0 erase_region 0x150000 0x300000

Advanced Strategies: Delta Updates and Dual Partitioning🔗

1. Delta UpdatesFirmware Updates over NB-IoT: Delta Updates with ESP32’s Dual PartitionFirmware Updates over NB-IoT: Delta Updates with ESP32’s Dual PartitionDiscover how delta firmware updates via NB-IoT optimize ESP32 device performance by minimizing data usage and ensuring secure, swift OTA transitions.:

2. Dual Partitioning:

# partitions.csv
ota_0,  app,  ota_0,  0x10000,  0x1A0000
ota_1,  app,  ota_1,  0x1B0000, 0x1A0000
  • Test new firmware in ota_1 while keeping ota_0 as a backup.

Monitoring OTA Progress🔗

To track OTA updates in real-time, use MQTT or HTTP to send progress updates to a cloud platformConnecting ESP32 to Cloud Services via Wi-FiConnecting ESP32 to Cloud Services via Wi-FiDiscover how to connect your ESP32 to AWS, Azure, and Google Cloud using secure Wi-Fi. This guide covers setup, error handling, and low power strategies..

void sendOTAStatus(const char* status) {
  if (WiFi.status() == WL_CONNECTED) {
    HTTPClient http;
    http.begin("http://your-server.com/ota-status");
    http.addHeader("Content-Type", "application/json");
    http.POST(String("{\"status\":\"") + status + "\"}");
    http.end();
  }
}

Optimizing OTA for Low-Bandwidth Networks🔗

For devices on low-bandwidth networks (e.g., LTE-M or NB-IoT), use delta updates to minimize payload size. Delta updatesFirmware Updates over NB-IoT: Delta Updates with ESP32’s Dual PartitionFirmware Updates over NB-IoT: Delta Updates with ESP32’s Dual PartitionDiscover how delta firmware updates via NB-IoT optimize ESP32 device performance by minimizing data usage and ensuring secure, swift OTA transitions. only send the differences between the current and new firmware.

1. Generate Delta UpdatesFirmware Updates over NB-IoT: Delta Updates with ESP32’s Dual PartitionFirmware Updates over NB-IoT: Delta Updates with ESP32’s Dual PartitionDiscover how delta firmware updates via NB-IoT optimize ESP32 device performance by minimizing data usage and ensuring secure, swift OTA transitions.

Use tools like bsdiff or xdelta to create binary patches.

2. Apply Delta UpdatesFirmware Updates over NB-IoT: Delta Updates with ESP32’s Dual PartitionFirmware Updates over NB-IoT: Delta Updates with ESP32’s Dual PartitionDiscover how delta firmware updates via NB-IoT optimize ESP32 device performance by minimizing data usage and ensuring secure, swift OTA transitions.

Modify the OTA code to apply delta updatesFirmware Updates over NB-IoT: Delta Updates with ESP32’s Dual PartitionFirmware Updates over NB-IoT: Delta Updates with ESP32’s Dual PartitionDiscover how delta firmware updates via NB-IoT optimize ESP32 device performance by minimizing data usage and ensuring secure, swift OTA transitions.:

void applyDeltaUpdate(const char* deltaUrl) {
  WiFiClientSecure client;
  client.setCACert(rootCACertificate);
  httpUpdate.update(client, deltaUrl, "delta");
}

Troubleshooting Common OTA Issues🔗

IssueSolution
OTA timeoutIncrease CONFIG_OTA_RECV_TIMEOUT in SDK.
Invalid imageCheck SHA-256 checksum of the firmware.
Wi-Fi disconnectsDisable power-saving modes during OTA.
Insufficient memoryOptimize firmware size or expand partitions.

Best Practices for OTA Updates🔗

1. Test Updates Thoroughly

Always test firmware updatesAWS IoT Core with ESP32: X.509 Certificates and Shadow UpdatesAWS IoT Core with ESP32: X.509 Certificates and Shadow UpdatesLearn to securely connect ESP32 to AWS IoT Core using X.509 certificates and device shadows, with step-by-step instructions and best practices. on a small subset of devices before rolling them out widely.

2. Use Secure Boot and Flash EncryptionConnecting ESP32 to Cloud Services via Wi-FiConnecting ESP32 to Cloud Services via Wi-FiDiscover how to connect your ESP32 to AWS, Azure, and Google Cloud using secure Wi-Fi. This guide covers setup, error handling, and low power strategies.

Protect your firmware from tampering by enabling secure boot and flash encryptionConnecting ESP32 to Cloud Services via Wi-FiConnecting ESP32 to Cloud Services via Wi-FiDiscover how to connect your ESP32 to AWS, Azure, and Google Cloud using secure Wi-Fi. This guide covers setup, error handling, and low power strategies..

3. Monitor Device Health

After an update, monitor device logs and performance to ensure stability.

4. Plan for Rollbacks

Always have a rollback plan in case an update fails or introduces issues.

5. Optimize Update Frequency

Balance the need for updates with the cost of bandwidthAdaptive Data Rate (ADR) Optimization for LoRaWAN on ESP32Adaptive Data Rate (ADR) Optimization for LoRaWAN on ESP32Optimize your IoT network with our ADR tutorial for ESP32 in LoRaWAN. Learn dynamic transmission tuning, power management, and troubleshooting strategies. and device downtime.

Real-World ExampleConnecting ESP32 to Cloud Services via Wi-FiConnecting ESP32 to Cloud Services via Wi-FiDiscover how to connect your ESP32 to AWS, Azure, and Google Cloud using secure Wi-Fi. This guide covers setup, error handling, and low power strategies.:

A smart farm uses ESP32 sensors with dual partitioning. Delta updatesFirmware Updates over NB-IoT: Delta Updates with ESP32’s Dual PartitionFirmware Updates over NB-IoT: Delta Updates with ESP32’s Dual PartitionDiscover how delta firmware updates via NB-IoT optimize ESP32 device performance by minimizing data usage and ensuring secure, swift OTA transitions. are pushed monthly via a private HTTPS server, reducing data usage by 70% compared to full updates. Failed updates trigger automatic rollback, ensuring 99.9% uptime.

Key Takeaway:

OTA isn’t just convenience-it’s a security necessity. Always sign updates and encrypt communication channels.

By implementing OTA updatesZigbee Over-the-Air (OTA) Firmware Updates with ESP32 CoordinatorsZigbee Over-the-Air (OTA) Firmware Updates with ESP32 CoordinatorsSecure your IoT network with OTA firmware upgrades using an ESP32 coordinator. Our guide details firmware setup, packaging, security, and troubleshooting. on your ESP32, you can keep your IoT devices up-to-date, secure, and future-proof. Whether you’re managing a handful of devices or a large-scale deployment, OTA updates are a critical tool for modern IoT development.

Happy coding and safe updating!

Author: Marcelo V. Souza - Engenheiro de Sistemas e Entusiasta em IoT e Desenvolvimento de Software, com foco em inovação tecnológica.

References🔗

Share article

Related Articles