OTA (Over the Air) update or ota updates arduino is the process of loading the firmware to ESP module using Wi-Fi connection rather than a serial port. Such functionality became extremely useful in case of limited or no physical access to the module. A separate partition is created to hold new firmware. So there are two partition one for the working and other for new firmware. When the OTA request is made new firmware is first downloaded in the new partition and then the pointer is changed to the new firmware address. If there’s any problem while downloading or flashing the previous partition’s address is pointed.

Here we will focus on OTA via Arduino IDE.

Note: OTA is not supported on chipset with 512KB of space.

OTA can be done using: –
  • Arduino IDE
  • Web Browser
  • HTTP Server
ota updates arduino Prerequisite: –

Note: Windows users should select “Add python.exe to Path” while installing python 2.7

Let’s start:-
1. Connect your ESP8266 to your computer with a USB cable

2. Uploading Basic OTA.ino with serial communication

3. Go to Tools to select your ESP board model. You also need to choose your ESP’s COM port

4. Open Arduino IDE and select Basic OTA sketch at File > Examples > ArduinoOTA > BasicOTA

NOTE:- you need to change the sketch example with your SSID and password. (e.g. SSID- XXXXXX, Password-XXXXXXXXX)

5. Press the “Upload” button in the Arduino IDE and wait for the “Done uploading” message.

6. Open the Arduino IDE serial monitor at a baud rate of 115200. If you’ve entered the right network credentials you should see your ESP IP address after a few seconds.

7. Uploading New sketch over the Air (OTA): -Now your ESP8266 is ready to receive OTA firmware updates. You can unplug your ESP8266 from your computer and power it through any power source (for example a power bank). If your ESP8266 has a wireless connection to your router, you should be fine to upload new firmware.

8. Go to your Arduino IDE. Open Tools tab select the Port option and you should see something like this: esp8266-xxxxxx at your_esp_ip_address.

9. Copy the following sketch to your Arduino IDE and upload it to your ESP8266. This sketch blinks the ESP12-E NodeMCU kit built-in LED every second.

Source code:-

10. Press the “Upload” button in the Arduino IDE and wait for the “Done uploading” message.

11. Your ESP should have its built-in LED blinking every second.

Code Explanation:-

1) Include all dependent file for OTA

  • #include<ESP8266WiFi.h>
  • #include<esp8266mdns.h>
  • #include<WiFiUdp.h>
  • #include<ArduinoOTA.h>

2) Set network credentials

  • const char* ssid = “XXXXXXX”;
  • const char* password = “XXXXXX”;

3) Setup Wi-Fi connection

  • void setup() {
  •   Serial.begin(115200);
  •   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();
  •   }

4) ArduinoOTA.onStart function will check the file type, download the sketch, change the pointer of the pointer and reset.

  • 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);
  •   });
  •   ArduinoOTA.onEnd([]() {
  •     Serial.println(“nEnd”);
  •   });

5) OTA.onError function gives the types of error occur during the flashing of code

  •     ArduinoOTA.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”);
  •   });

6) OTA begin function will start executing OTA functionality

  • ArduinoOTA.begin();

7) In the loop function OTA Handle function handle obtained from esp_ota_begin. This will continuously chech for the OTA request.

  • ArduinoOTA.handle();
If you are still facing issue regarding ota updates arduino Feel free to Ask Doubts in the Comment Box Below and Don’t Forget to Follow us on 👍 Social Networks😉