ESP32/8266 Over The Air Updates (OTA)

ESP OTA
Share:

About

In this post, I’ll show you how to do OTA(over the air updates) to your ESP32/ESP8266. This means that you don’t have to connect your ESP board to your PC by cable and can instead push firmware updates wirelessly over the network(as long as you are connected to the same network). The process of uploading the firmware remains almost the same. The only difference is you have to select a network port under Tools -> Port -> Network Ports instead of a COM port.

Note: If you need something to manage multiple boards remotely in another network consider checking out IOT Apps Story. Or if you want to give the user the option to update their IOT device you can use something like AutoConnect(update via an update server) or ESP Web Tools(update via browser).

Hardware Used

Code

This is the minimal code required for OTA to work. Upload this to your board with a cable.

Note: If you have never before worked with the ESPs in your Arduino IDE you will also need to add the ESP boards to the IDE. If you would like to know how to do this I have already covered it as part of this post(Adding The Board To The Arduino IDE section).

//Uncomment to enable debug serial print.
#define DEBUG

#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>

//Wifi network name and password.
const char* ssid = "name of your wifi network";
const char* password = "password of your wifi network";

WiFiClient espClient;

void setup() {
    //Connect to wifi.
    setup_wifi();

    //Set name and port for over the air updates.
    OTA("myESP", 8266); 
    
    #ifdef DEBUG
      Serial.begin(9600);
    #endif
}
 
void loop() {    

  //your code goes here ....

  //This has to be called at least every couple of seconds otherwise you will an error when trying to upload your new firmware.
  ArduinoOTA.handle();

  delay(2000);
}


/***************************** FUNCTIONS *****************************/

void OTA(char *hostname, int port) {

  /************** Set OTA variables ************/
  ArduinoOTA.setPort(port);
  ArduinoOTA.setHostname(hostname); //Hostname defaults to esp8266-[ChipID].
  //ArduinoOTA.setPassword("admin"); //No authentication by default.
  //ArduinoOTA.setPasswordHash("21232f297a57a5a743894a0e4a801fc3"); //Password can be set with it's md5 value as well


  /************** Set OTA callbacks ************/
  ArduinoOTA.onStart([]() {
    String type;
    if (ArduinoOTA.getCommand() == U_FLASH) {
      type = "sketch";
    } else {  // U_FS
      type = "filesystem";
    }

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

  ArduinoOTA.begin();

  #ifdef DEBUG
    Serial.println("Ready");
    Serial.print("IP address: ");
    Serial.println(WiFi.localIP());
  #endif
}


// This functions connects your ESP8266 to your router
void setup_wifi() {
  // We start by connecting to a WiFi network
  #ifdef DEBUG
    Serial.println();
    Serial.print("Connecting to ");
    Serial.println(ssid);
  #endif

  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

  while (WiFi.waitForConnectResult() != WL_CONNECTED) {
    #ifdef DEBUG
      Serial.println("Connection Failed! Rebooting...");
    #endif
    
    delay(5000);
    ESP.restart();
  }

  #ifdef DEBUG
    Serial.println("");
    Serial.print("WiFi connected - ESP IP address: ");
    Serial.println(WiFi.localIP());
  #endif
}

/*********************************************************************/
Now your board should appear under Tools -> Port -> Network Ports select it and you should be able to upload your firmware over the air wirelessly.
Share:

Leave a Reply

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

The following GDPR rules must be read and accepted:
This form collects your name, email and content so that we can keep track of the comments placed on the website. For more info check our privacy policy where you will get more info on where, how and why we store your data.

Advertisment ad adsense adlogger