Try Embedded

Over-the-air update (ESP32-C3)

Check an update server for a newer version, download it over HTTP into the other app partition, and reboot into it.

Communication · Advanced

In a real product: Smart plug (Smart home): Installs new firmware over Wi-Fi without anyone touching the device.

Devices in the field are updated over the air. The ESP32's flash holds two app partitions, app0 and app1: the firmware runs from one while the update is written to the other, and a small otadata partition records which one to boot. If the new image does not check out, the old one keeps running.

What you'll learn

  • The A/B partition scheme, and reading which partition is running
  • A version check before downloading
  • HTTPUpdate: downloading, verifying and switching partitions in one call

How it works

The sketch prints the partition it runs from, then asks http://fw.lab/latest.json for the newest version. The server says 2 and this build is 1, so it calls httpUpdate.update, which downloads firmware.bin, writes it to app1, checks the image's length and SHA-256, marks app1 to boot and restarts. After the reboot the serial monitor shows it running from app1 at 0x150000.

The simulated update server sends the firmware the board is running ("firmware": "self" in diagram.json), so the new image is this same sketch. That is why it only updates from app0: on real hardware the new build would carry a higher VERSION and the check alone would stop it updating again.

Try this

  • Add "fault": "corrupt" to the /firmware.bin route: the download completes, the SHA-256 does not match, and the update is refused. The board keeps running app0.
  • Set the server's version to 1: nothing is downloaded.
  • Watch the download in the Network panel, then compare the partition addresses with the partition table.

sketch.ino

// An over-the-air update: asks the update server for the latest version, and when it is newer than this firmware's,
// downloads it with HTTPUpdate, which writes it to the other app partition, checks it and reboots into it.
#include <WiFi.h>
#include <HTTPClient.h>
#include <HTTPUpdate.h>
#include <esp_ota_ops.h>

const int VERSION = 1;   // this build's version
const char *SERVER = "http://fw.lab";

// The number after "version": in the server's JSON, or 0.
int latestVersion() {
  HTTPClient http;
  http.begin(String(SERVER) + "/latest.json");
  int code = http.GET();
  String body = code == 200 ? http.getString() : "";
  http.end();
  int at = body.indexOf("\"version\":");
  return at < 0 ? 0 : body.substring(at + 10).toInt();
}

void setup() {
  Serial.begin(115200);
  const esp_partition_t *running = esp_ota_get_running_partition();
  Serial.printf("running from %s at 0x%06x\n", running->label, running->address);
  WiFi.begin("TryEmbedded", "");
  while (WiFi.status() != WL_CONNECTED) delay(100);

  // The simulated server sends this same firmware as the update, so after the reboot the version check would offer
  // it again: an update here is only taken from the first partition.
  if (strcmp(running->label, "app0") != 0) {
    Serial.println("updated: running the new firmware");
    return;
  }
  int latest = latestVersion();
  Serial.printf("this is version %d, the server has %d\n", VERSION, latest);
  if (latest <= VERSION) return;

  httpUpdate.onProgress([](int done, int total) {
    static int shown = -1;
    if (done * 4 / total != shown) { shown = done * 4 / total; Serial.printf("  %d%%\n", shown * 25); }
  });
  WiFiClient client;
  t_httpUpdate_return result = httpUpdate.update(client, String(SERVER) + "/firmware.bin");
  // On success it reboots and never returns here.
  Serial.printf("update failed: %s\n", httpUpdate.getLastErrorString().c_str());
}

void loop() {}

Lessons behind it