CPP: OTA - lvidarte/esp8266 GitHub Wiki

Over-The-Air (OTA) update is the wireless delivery of new firmware.

The ESP8266 has an amount of flash memory available to it. Let us divide that memory into two equal halves. When an ESP8266 ships, your application will be loaded into the 1 st half of flash and will ignore the second half. From time to time, it will "call home" via the Internet and ask if there is a replacement set of firmware (a new version). If there is, then it will download that new firmware into the 2 nd half of flash. If that fully succeeds, the device will reboot and start running the new firmware from the 2 nd half of flash ... it will now ignore the 1 st half. A subsequent replacement of the firmware with yet another version will be loaded into the 1 st half and the story repeats. Effectively, we are thus able to flip-flop between two versions.

A full OTA pipeline could be as following:

imgs/ota.png

Here we will focus on the last part, from the PHP script to the NodeMCU

Download the project

Checkout this PlatformIO project code here

Build the firmware

Move to src/c++/ota directory and exec

$ bin/build.sh

Run OTA server

Move to src/c++/ota directory and exec

$ bin/server.sh

That starts the php server on port 3000

Code

The relevant function is

void checkUpdate ()
{
  Serial.print("Checking for firmware update... ");

  t_httpUpdate_return ret = ESPhttpUpdate.update(
    OTA_SERVER_HOST, OTA_SERVER_PORT, OTA_SERVER_PATH, FIRMWARE_VERSION
  );

  switch(ret) {
    case HTTP_UPDATE_FAILED:
        Serial.printf(
          "Updated failed. Error (%d): %s\n",
          ESPhttpUpdate.getLastError(),
          ESPhttpUpdate.getLastErrorString().c_str()
        );
        break;
    case HTTP_UPDATE_NO_UPDATES:
        Serial.println("No update found.");
        break;
    case HTTP_UPDATE_OK:
        Serial.println("Updated OK. Rebooting.");
        ESP.restart();
        break;
  }
}

Aditional Doc