# OTA update and ESP8266/ESP32

Both ESP8266 and ESP32 Arduino SDKs provided OTA update feature. There are multiple ways to use these update functions. We first explain the simplest ways.

OTAdrive update api requires three parameters in URL. These parameters will use by OTAdrive to get the correct firmware file from the server.

  • K: APIkey of your product in OTAdrive.
  • V: Current firmware version of your device.
  • S: The unique serial number of your device to recognize device identity.

# ESP8266 OTA Update Code

Here is first and simplest code you can use to update your ESP8266 with OTAdrive.

#include <ESP8266httpUpdate.h>

void doUpdate()
{
  String url = "http://otadrive.com/deviceapi/update?";
  url += "k=c0af643b-4f90-4905-9807-db8be5164cde";
  url += "&v=1.0.0.1";
  url += "&s=" + getChipId();

  WiFiClient client;
  ESPhttpUpdate.update(client, url, "1.0.0.1");
}

Here we call update function with 3 parameters:

  • WiFiClient: We should pass a network pipe to function.
  • Url: A network address to new firmware file.
  • Version: Current firmware version. It is too important to avoid ESP chip to download repetitious firmware. This parameter lets the update function checks the firmware on the server is different or not.

# ESP32 OTA Update Code

Here is another simple code you can use to update your ESP32 with OTAdrive. It is similar to ESP8266 but header filename is different.

#include <HTTPUpdate.h>

void update()
{
  String url = "http://otadrive.com/deviceapi/update?";
  url += "k=c0af643b-4f90-4905-9807-db8be5164cde";
  url += "&v=1.0.0.1";
  url += "&s=" + getChipId();

  WiFiClient client;
  httpUpdate.update(client, url, "1.0.0.1");
}

Update function requires three parameters:

  • WiFiClient: We should pass a network pipe to function.
  • Url: A network address to new firmware file.
  • Version: Current firmware version. It is too important to avoid ESP chip to download repetitious firmware. This parameter lets the update function checks the firmware on the server is different or not.

# Version Injection Mechanism

Version injection is nothing other than a way to store a char array to flash program containing some information about your product and version. When your program compiles, all strings will store somewhere in flash memory and they copy to RAM before start main() function. Just open one of your bin files with a notepad and watch inside.

foo

As you see in the image above, your URL and other strings are available in the bin file plain. Now we want to use this fact to make a sign on our bin files. The simplest way is to make a char array containing all information we need. OTAdrive requires the following format to detect the product and version of your bin file.

_FirmwareInfo&k=00000000-0000-0000-0000-000000000000&v=0.0.0.0&FirmwareInfo_

To make sure this string available in bin file we suggest you to use the following macro and use it in your code. Remember that C/C++ compilers have an optimizer mechanism that removes all unnecessary code from bin file to compress it.

foo

So there isn't any other way to ensure this string exists in your bin file. You have to use it somewhere in your code. The following code will store version injection information without any doubt.

#include <ESP8266httpUpdate.h>

#define ProductKey "00000000-0000-0000-0000-000000000000"
#define Version "1.0.0.0"
#define MakeFirmwareInfo(k, v) "&_FirmwareInfo&k=" k "&v=" v "&FirmwareInfo_&"

void doUpdate()
{
  String url = "http://otadrive.com/DeviceApi/update?";
  WiFiClient client;
  url += "&s=" + String(CHIPID);
  url += MakeFirmwareInfo(ProductKey, Version);
  ESPhttpUpdate.update(client, url, Version);
}

You can find ESP8266 examples (opens new window) and ESP32 examples (opens new window) at our git repository (opens new window).

#