15.The usage of Wi-Fi OTA(Over-The-Air)

The Arduino demo of ESP32 provides the function of OTA (updating/uploading a new program to ESP32 using Wi-Fi)

1. What is OTA?

The configuration of our BiBoard is 16MB Flash, and the specific partitions are as follows:

OTA mainly operates OTA data areas, namely APP1 and APP2 areas. The principle is:

  • BiBoard runs the firmware with OTA function, at this time, the boot points to the APP1 area.

  • The OTA command is sent to ESP32 via Wi-Fi, and the binary file of the upgrade program is transferred to the APP2 area.

  • If the transmission of APP2 is completed and the verification is successful, OTAdata points to the APP2 area, and the next time it starts from the updated firmware area (APP2), the APP1 data is retained. Next time, OTA will write to APP1 area to overwrite the old firmware.

  • If the transmission of APP2 is not completed due to a network transmission error, because APP2 has not passed the verification, OTAdata does not point to the APP2 area. The program in the APP1 area will still be executed after the reset is started, and the damaged APP2 area will be completely erased and overwritten during the next OTA.

OTA operation in Arduino program

In the demo, first configure WiFi, and configure the WiFi mode as STA (Station, base station mode). Enable the WiFi function and pass in the account password "WiFi.begin(ssid, password);"

  Serial.println("Booting");
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  while (WiFi.waitForConnectResult() != WL_CONNECTED) {
    Serial.println("Connection Failed! Rebooting...");
    delay(5000);
    ESP.restart();

When the Wi-Fi is successfully connected, the IP address will be printed via the serial port; if the connection is wrong, the ESP32 will restart.

In the demo, you can configure the port number, the OTA key or the hash of the key, and the area and type of the OTA (commented by default).

  // Port defaults to 3232
  // ArduinoOTA.setPort(3232);

  // Hostname defaults to esp3232-[MAC]
  // ArduinoOTA.setHostname("myesp32");

  // No authentication by default
  // ArduinoOTA.setPassword("admin");

  // Password can be set with it's md5 value as well
  // MD5(admin) = 21232f297a57a5a743894a0e4a801fc3
  // ArduinoOTA.setPasswordHash("21232f297a57a5a743894a0e4a801fc3");

The following are several code snippets similar to callback functions, which are used to judge the state of each stage of OTA.

  ArduinoOTA
    .onStart([]() {
      String type;
      if (ArduinoOTA.getCommand() == U_FLASH)
        type = "sketch";
      else // U_SPIFFS
        type = "filesystem";

      // NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
      Serial.println("Start updating " + type);
    })
    .onEnd([]() {
      Serial.println("\nEnd");
    })
    .onProgress([](unsigned int progress, unsigned int total) {
      Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
    })
    .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");
    });

  ArduinoOTA.begin();

After configuring according to the demo, call "ArduinoOTA.handle();" in the loop function. The following analogWrite function is to distinguish the effects of different firmware updates (by changing the value).

void loop() {
  ArduinoOTA.handle();
  analogWrite(2, 127);    // test OTA firmware
}

The first time you use the serial port to download, the python tool "esptool" is called. You can use OTA after the download is complete. In the port options, you will find an extra port based on the IP address, which is the OTA address.

Select this address, the lower right corner is the IP address of ESP32 Dev Module on your BiBoard (192.168.1.178)

At the same time, a warning will pop up: "Serial monitor is not supported on network ports such as 192.168.1.178 for the ESP32 Dev Module in this release".

The ESP32 OTA of Arduino is only suitable for updating the program and cannot complete the serial port debugging work. If you need to debug BiBoard, please connect the USB-C interface.

Download the program, as shown in the figure.

Last updated