Over The Air update for ESP8266 using Arduino IDE

Hello tech freaks, hobbyists, and professionals,

This post will help you to program ESP8266 over WiFi.  We are posting this due to repeated requests from our blog viewers. An over-the-air update (OTA) is the wireless delivery of new software or data to a remote device. OTA update can be done in several ways, the simplest way is by using Arduino IDE. Let’s see how it works. Before getting into the update process let’s see what all things needed to do that.

Prerequisites:

Let’s start,

  1. Open Arduino IDE and select BasicOTA sketch at File > Examples > ArduinoOTA > BasicOTA   Example
    #include <ESP8266WiFi.h>
    #include <ESP8266mDNS.h>
    #include <WiFiUdp.h>
    #include <ArduinoOTA.h>
    
    const char* ssid = "..........";
    const char* password = "..........";
    
    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();
     }
    
    // Port defaults to 8266
     // ArduinoOTA.setPort(8266);
    
    // Hostname defaults to esp8266-[ChipID]
     // ArduinoOTA.setHostname("myesp8266");
    
    // No authentication by default
     // ArduinoOTA.setPassword((const char *)"123");
    
    ArduinoOTA.onStart([]() {
     Serial.println("Start");
     });
     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();
     Serial.println("Ready");
     Serial.print("IP address: ");
     Serial.println(WiFi.localIP());
    }
    
    void loop() {
     ArduinoOTA.handle();
    }
  2. Edit the code and add your WiFi credentials.
    const char* ssid = "Your_WiFi_SSID";
    const char* password = "Your_WiFi_Password"

     

  3. In Arduino IDE, go to tools and configure serial upload parameters for your module and upload the sketch. Parameters
  4. Then open  serial monitor.If module is connected to network, you will see something like this.Serial_Monitor
  5. After some time at Tools > Ports > Network ports a new OTA port will appear.NetworkNote: If OTA port does not show up, restart Arduino IDE, if it does not help check your firewall settings.
  6. Now your device is ready for OTA update. Now you can upload any code to your device remotely. Open the Arduino IDE and select OTA port and enjoy OTA update. Note: To  upload your sketch over and over again using OTA, you need to embed OTA routines inside your sketch. Please use BasicOTA.ino as an example.
  7. An example blinking code with OTA routines inside
    #include <ESP8266WiFi.h>
    #include <ESP8266mDNS.h>
    #include <WiFiUdp.h>
    #include <ArduinoOTA.h>
    
    const char* ssid = "Your_WiFi_SSID";
    const char* password = "Your_WiFi_Password"
    
    
    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();
     }
    
    // Port defaults to 8266
     // ArduinoOTA.setPort(8266);
    
    // Hostname defaults to esp8266-[ChipID]
     // ArduinoOTA.setHostname("myesp8266");
    
    // No authentication by default
     // ArduinoOTA.setPassword((const char *)"123");
    
    ArduinoOTA.onStart([]() {
     Serial.println("Start");
     });
     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();
     Serial.println("Ready");
     Serial.print("IP address: ");
     Serial.println(WiFi.localIP());
     pinMode(2, OUTPUT);
    }
    
    void loop() {
     ArduinoOTA.handle();
     digitalWrite(2, HIGH); // turn the LED on (HIGH is the voltage level)
     delay(2000); // wait for 2 second
     digitalWrite(2, LOW); // turn the LED off by making the voltage LOW
     delay(2000); 
    }

    We will be back with more OTA updating schemes soon. Thank you.

“Nothing great in the world was accomplished without passion”

5 thoughts on “Over The Air update for ESP8266 using Arduino IDE

  1. how to connect and what power supply to give and different ways of power supply, how to host a application is not explained

  2. Build options changed, rebuilding all

    Sketch uses 244,319 bytes (23%) of program storage space. Maximum is 1,044,464 bytes.
    Global variables use 33,292 bytes (40%) of dynamic memory, leaving 48,628 bytes for local variables. Maximum is 81,920 bytes.
    20:44:55 [ERROR]: No response from device
    20:44:55 [ERROR]: No response from device

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s