If you have been a regular viewer of Techiesms you must know that we love working with ESP32. It has got so many of mesmerizing features. It has got great processing powers and BLE along with Wi-Fi.

What is OTA?

OTA stands for Over The Air. It allows uploading a new program to ESP32 using Wi-Fi instead of connecting the ESP32 to a computer via USB to do the update. It is extremely useful in situations where you don’t have physical access to the module. One such example is mentioned here.

Whats is the need of OTA?

Let us say, you have created a phenomenal project using ESP32. You fixed it in your home and powered it up. Everything is working fine and you have got comfortable into your smart home. But one day you need to make slight changes to the code or maybe change the code with updated version of libraries and stuffs or maybe add some more features to it. Will you like the mess of plugging out the whole module, removing the connections, plugging it to your PC, uploading the code, re-doing the connections and then setting it all up again?

PCBWay

Well no! No one will. That is where OTA kicks in. For establishing OTA with ESP32 you need to include few extra lines to your code and after that you can upload the codes wirelessly over the air.

How to implement OTA with ESP32?

There are two ways to perform an OTA update to your ESP32 board.

  1. Basic OTA – Updates are sent via Arduino IDE
  2. Web updater OTA – Updates are sent via a webpage/web browser.

In this tutorial we will be implementing Basic OTA. You can read about Web-updater OTA from HERE!

Steps to use Basic OTA.

For using Basic OTA feature with your ESP32 board. You just need to follow these three steps.

  1. Install Python in your PC if it is not previously installed
  2. Upload the Basic OTA code to the ESP32
  3. Now, you can push the updates through Wi-Fi

Step 1 :- Installing Python in your PC

In order to use the OTA functionality you need to have Python 2.7.xx installed in your PC. You can download it from the official website.

The installation process is pretty straight forward. Just open the installer after downloading and follow the installation wizard.

After installing Python in your PC, you can continue with the second step.

Step 2 :- Uploading the Basic OTA sketch.

The ESP32 board does not support OTA updates by default, hence you need to upload the new firmware in order to perform the OTA updates.

This is a mandatory step as this will later allow you to push the new codes wirelessly via Wi-Fi.

Firsty, you need to have the ESP32 boards package installed in your Arduino IDE. If you don’t have those then you can watch this video of mine to get started with ESP32.

After installing the boards. Go to File > Examples > ArduinoOTA > BasicOTA.

This is the sketch that you need to install in order to give your ESP32 the power of OTA.

The changes that you need to make in the code are ssid and password. You need to give this credentials so that the ESP can connect to your router in order to receive the updates. Once you are done with those, go ahead and upload the sketch.

Once you have uploaded the Basic OTA sketch. Open your Serial Monitor on a baud rate of 115200. If everything went alight then you will see IP Address printed at the end of the monitor. Note the IP address.

Now, that you have completed all the steps. It’s time to upload the new sketches over the air. Excited? So am I.

Step 2 :- Uploading the codes Over The Air

Now let us try uploading a new sketch wirelessly to ESP32.

Remember that you need to include the Basic OTA sketch with every sketch that you upload else you’ll loose the OTA functionality after uploading the sketch. For example if you want to upload a basic LED blinking sketch to then you need to include the Basic OTA sketch in it also.

Example if such sketch is given below.

#include <WiFi.h>
#include <ESPmDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>

const char* ssid = "..........";
const char* password = "..........";

//This is the part of the LED blinking code
const int led = 2; // ESP32 Pin to which onboard LED is connected
unsigned long previousMillis = 0;  // will store last time LED was updated
const long interval = 1000;  // interval at which to blink (milliseconds)
int ledState = LOW;  // ledState used to set the LED

void setup() {

pinMode(led, OUTPUT);
  
  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();
  }

  // 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");

  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();

  Serial.println("Ready");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
}

void loop() {
  ArduinoOTA.handle();  
  
//Here is the LED blinking section
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
  // save the last time you blinked the LED
  previousMillis = currentMillis;
  // if the LED is off turn it on and vice-versa:
  ledState = not(ledState);
  // set the LED with the ledState of the variable:
  digitalWrite(led,  ledState);
  }

}

Once you have written your program in such a manner that it includes the Basic OTA sketch as well then just click on Tools and go to ports section. You will see something like this ” esp32-xxxxxx at your_esp_ip_address Choose that board and hit on the upload button.

PCBWay

And Wallah! Just wait for few seconds and your code is sent to ESP32 Over The Air.

Conclusion:-

Yeah, I know it is a bit of headache to include the Basic OTA sketch to each of your code but the functionality it gives is flawless. If you are going to sell your product in the market then you need to provide your customers with an option to update the firmare when needed. That can easily be fullfilled by this ITA feature. Still the implementations depend from user to user.

About Admin

Hey! This is Priyanshu. Just a curious dumb trying to figure out how things work. Am a electronic and programming enthusiast and a passionate learner.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *