14.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);"
1
Serial.println("Booting");
2
WiFi.mode(WIFI_STA);
3
WiFi.begin(ssid, password);
4
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
5
Serial.println("Connection Failed! Rebooting...");
6
delay(5000);
7
ESP.restart();
Copied!
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).
1
// Port defaults to 3232
2
// ArduinoOTA.setPort(3232);
3
4
// Hostname defaults to esp3232-[MAC]
5
// ArduinoOTA.setHostname("myesp32");
6
7
// No authentication by default
8
// ArduinoOTA.setPassword("admin");
9
10
// Password can be set with it's md5 value as well
11
// MD5(admin) = 21232f297a57a5a743894a0e4a801fc3
12
// ArduinoOTA.setPasswordHash("21232f297a57a5a743894a0e4a801fc3");
Copied!
The following are several code snippets similar to callback functions, which are used to judge the state of each stage of OTA.
1
ArduinoOTA
2
.onStart([]() {
3
String type;
4
if (ArduinoOTA.getCommand() == U_FLASH)
5
type = "sketch";
6
else // U_SPIFFS
7
type = "filesystem";
8
9
// NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
10
Serial.println("Start updating " + type);
11
})
12
.onEnd([]() {
13
Serial.println("\nEnd");
14
})
15
.onProgress([](unsigned int progress, unsigned int total) {
16
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
17
})
18
.onError([](ota_error_t error) {
19
Serial.printf("Error[%u]: ", error);
20
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
21
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
22
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
23
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
24
else if (error == OTA_END_ERROR) Serial.println("End Failed");
25
});
26
27
ArduinoOTA.begin();
Copied!
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).
1
void loop() {
2
ArduinoOTA.handle();
3
analogWrite(2, 127); // test OTA firmware
4
}
Copied!
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 modified 3mo ago