element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      • Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Personal Blogs
  • Community Hub
  • More
Personal Blogs
Legacy Personal Blogs Over the Air (OTA) Programming of ESP8266 - Part I
  • Blog
  • Documents
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: neilk
  • Date Created: 22 Dec 2019 5:11 PM Date Created
  • Views 6851 views
  • Likes 9 likes
  • Comments 17 comments
  • arduino_tutorials
  • esp8266
  • otaupdate
  • network_port
  • ota
Related
Recommended

Over the Air (OTA) Programming of ESP8266 - Part I

neilk
neilk
22 Dec 2019

INTRODUCTION

 

The following summary is taken from the the ESP8266 package documentation:

 

                https://arduino-esp8266.readthedocs.io/en/latest/ota_updates/readme.html

 

"OTA (Over the Air) update is the process of uploading firmware to an ESP module using a Wi-Fi connection rather than a serial port. Such functionality becomes extremely useful in case of limited or no physical access to the module.

 

OTA may be done using:

  1. Arduino IDE
  2. Web Browser
  3. HTTP Server

 

The Arduino IDE option is intended primarily for the software development phase. The other two options would be more useful after deployment, to provide the module with application updates either manually with a web browser, or automatically using an HTTP server."

In this Blog I intend to look at method 1, using the Arduino IDE. I will look at method 3, the Automatic approach, using an HTTP Server, in my next Blog.

 

I am assuming that the ESP8266 package has been correctly installed, along with Python, as described in the documentation. I am also assuming that the correct ESP8266 board has been selected in the Arduino IDE, under Tools/Board/( in my case)"LOLIN (WEMOS)  D1 R2 & mini".

 

 

OTA Firmware Updates using the Arduino IDE

 

An initial "starter" sketch, containing appropriate code segments, is loaded into the ESP8266 device via the serial port. If this initial sketch has been correctly implemented, then subsequent uploads can be carried out over the air. Once the upload is complete, the device restarts and the new firmware is executed,

 

"By default, there is no imposed security for the OTA process. It is up to the developer to ensure that updates are allowed only from legitimate / trusted sources."

 

See the documentation for advice on implementing security measures

 

An example sketch can be found in the Arduino IDE, under File/Examples/Examples for (in my case)"LOLIN (WEMOS)  D1 R2 & mini"/ArduinoOTA/BasicOTA. This sketch carries out a number of functions:

 

1. Load the necessary libraries:

 

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

 

 

2. Connect to the local network. Note the need to supply your local network ssid and pasword.

 

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

 

 

3. Give some information regarding various defaults:

 

  // Port defaults to 8266
  // ArduinoOTA.setPort(8266);


  // Hostname defaults to esp8266-[ChipID]
  // ArduinoOTA.setHostname("myesp8266");


  // No authentication by default
  // ArduinoOTA.setPassword("admin");


  // Password can be set with it's md5 value as well
  // MD5(admin) = 21232f297a57a5a743894a0e4a801fc3
  // ArduinoOTA.setPasswordHash("21232f297a57a5a743894a0e4a801fc3");

 

 

4. Initiate the OTA Process:

 

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

 

Some of the above code is actually unnecessary at the level I am describing; I have reproduced it here because it is present in the example Sketch. Later on I will show am alternative, simpler starter sketch.

 

 

5. Deal with the progress and end of the update process

 

  ArduinoOTA.onEnd([]() {
    Serial.println("\nEnd");
  });
  ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
    Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
  });

 

 

7. Handle Errors

 

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

 

 

8. Wait to be Updated

 

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


void loop()
{
  ArduinoOTA.handle();
}

 

 

This starter sketch is uploaded as usual via the serial interface. Once this has been accomplished and the messages in segment 8 seen on the Serial Monitor, the sketch should be working and the the ESP8266 device connected to the local WiFi network.

 

If garbage appears in the Serial Monitor, check baud rates - in fact, I tend to always run mine at 9,600. Once corrected, recompile and upload.

 

If you only see Booting in the Serial Monitor, and nothing else, the ESP8266  is failing to connect to your local WiFi network. Try resetting the ESP8266. If this fails, double check the ssid and password of your local WiFi network. Correct, if necessary, recompile and upload again.

 

Once the sketch is working, reset the ESP8266 and restart the Arduino IDE.

 

 

If you now go to Tools, scroll down to Port: and click it, you should now see a network port, of the form: "esp8266-239ae3 at 192.168.1.17", where 239ae3 is the lower half of the ESP8266 device's MAC address.

 

This poor quality photograph (I tried several times!) shows the network port within the Arduino IDE menu:

 

image

 

Below, I show the minimal code that I have found necessary for this to work:

 

/*************************************************************
 * BASIC OTA sketch, adapted from Example sketch - BasicOTA
 * 
 * Neil Kenyon 
 * 1 December 2019
 * 
 *************************************************************
*/


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


// Replace with your network credentials
const char* ssid = ".........";
const char* password = "........";


void setup()
{
  Serial.begin(9600);
  delay(500);
  Serial.println("\nBooting");
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  while (WiFi.waitForConnectResult() != WL_CONNECTED)
  {
    Serial.println("Connection Failed! Rebooting...");
    delay(5000);
    ESP.restart();
  }

  ArduinoOTA.onStart([]() {
    Serial.println("Start Updating");
  });
  ArduinoOTA.onEnd([]() {
    Serial.println("\nUpdate Finished");
  });
  ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
    Serial.printf("\nProgress: %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();
}

 

Now we have a problem: the Arduino IDE cannot connect simultaneously to a network port - "esp8266-239ae3 at 192.168.1.17" - and a conventional serial port. This means that you cannot both update the ESP8266 OTA and view the output from the Sketch(es) on the Serial Monitor, using a single PC/MAC.

 

In order to test the OTA process fully, and, if necessary, debug the sketches, we have to use two separate PCs or MACs, with both machines connected to the local WIFi network and running the Arduino IDE.

image

 

The PC/MAC on the right has the ESP8266 connected to it via the usual USB cable, and the appropriate serial port selected within the IDE. The Serial Monitor is open - it doesn't matter which sketch is loaded in the IDE - allowing the ESP8266 to output messages to the screen

 

The PC/MAC on the left has the ESP8266 network port selected within the IDE and is used to modify, recompile and upload the sketches.

 

Demonstrating OTA via Network Port

 

The video below begins by showing the Serial Monitor output after my modified starter sketch has been uploaded and the ESP8266 reset.

 

The video then goes on to show the Serial Monitor output during an OTA update of a trivially modified version of the starter sketch:

 

You don't have permission to edit metadata of this video.
Edit media
x
image
Upload Preview
image

 

I found it useful to include the timestamp in the Serial Monitor output, to understand just how quickly this process takes place.

 

 

After some furtherl modifications to the sketch:

 

In Declarations:

const int LED = 2;
int onTime = 1000;
int offTime = 1000;

 

In Setup():

  pinMode(LED, OUTPUT);


  Serial.println("\nI have been updated OTA");                                // This is original
  Serial.println("\nI have been updated OTA a second time!\n");               // This is is an addition

 

 

And finally, in Loop()

  digitalWrite(LED, LOW);
  Serial.print("LED ON! for ");
  Serial.print(onTime);
  Serial.println(" mS");                                    
  delay(onTime);
  
  digitalWrite(LED, HIGH);
  Serial.print("LED OFF! for ");
  Serial.print(offTime);
  Serial.println(" mS");
  delay(offTime); 

 

The sketch was re-compiled and uploaded OTA:

 

You don't have permission to edit metadata of this video.
Edit media
x
image
Upload Preview
image

 

Unfortunately, you can't see the LED flashing - honest it is flashing!!

 

How Useful is this?

 

Once you are confident that the OTA process works, there is obviously no need to use a two PC/MAC approach, as described here. You can comment out all the diagnostics and just have the ESP8266 running from an appropriate PSU, independent of a PC/MAC

 

This OTA method comes into it's own if, for example, your ESP8266 system is "not next to your PC or MAC", updating the firmware can be a pain. You either have to take a laptop/MACbook to wherever the system is - eg in my case, out in one of my greenhouses - and update via USB, or bring the system to wherever your PC/MAC lives , and update the firmware there. You then have to take the system back to wherever it operates.

 

Even if your system is "next to your PC or MAC", connecting the USB cable may mean disturbing a delicate rat's nest of jumper cables!! Always a risky exercise!!

 

However

 

If your system is powered by batteries, like my greenhouse temperature monitor, it will spend most of its time in "Deep Sleep" and therefore unavailable to be updated using this method. Hence the need to use method 3, mentioned above.

 

I will describe method 3, which uses an HTTP server, in my next blog

  • Sign in to reply

Top Comments

  • ralphjy
    ralphjy over 5 years ago +5
    Nice post. I had not looked into OTA updating before as I haven’t had a need for it. It will be very useful for developing remote sensors. I think I’ll try it with an ESP32cam that I have that doesn’t…
  • neilk
    neilk over 5 years ago in reply to ralphjy +5
    I can't claim to understand everything that's happening, but it works!! The only thing to watch out for is that your sketch can only take up 50% of the available memory - the running sketch copies the…
  • ralphjy
    ralphjy over 5 years ago in reply to neilk +4
    Thanks for pointing out the size restriction. I went to check my ESP32cam application and it will be too large to use with OTA (2.2MB). I read on github that using OTA with the minimum SPIFFS size (190KB…
  • neilk
    neilk over 5 years ago in reply to vfontanella

    Thanks, Vicente. Good tip.

     

    Neil

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • vfontanella
    vfontanella over 5 years ago

    Hi All,

    only a tip if you are doing this kind of thing, As Neil said, is not possible to use the serial monitor and upload at the same time.

    As an alternative for that I use a software called screen, which is a terminal emulator ( for Linux and Mac OS).

    Doing so, you can upload and monitor in your terminal what is returning from the serial monitor.

    You only need to open your terminal and type:

    $screen /dev/cu.YOUR_SERIAL_PORT 115200

    Hope this help someone.

    cheers,

    Vicente.

    • Cancel
    • Vote Up +4 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • neilk
    neilk over 5 years ago in reply to dubbie

    Sounds like that could have been a bit of a nightmare!!

     

    OTA can make life so much easier - you just have to believe in the magicimage

     

    Neil

    • Cancel
    • Vote Up +3 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • dubbie
    dubbie over 5 years ago in reply to neilk

    Neil,

     

    I think it would be too. I've wanted to do OTA since I started with PIC although it didn't seem possible then, just not enough memory. Working with a large class of students, sometimes over 40, I would have to provide programming cables (RS232C) then custom cables and programmer and then towards the end of my time, USB cables and programmers, and getting them all handed out and then back it was always a challenge. I used to collect sack fulls of hardware and reports from the hand in room.

     

    Dubbie

    • Cancel
    • Vote Up +3 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • neilk
    neilk over 5 years ago in reply to dubbie

    Sounds ideal for you Dubbie.

     

    Neil

    • Cancel
    • Vote Up +3 Vote Down
    • Sign in to reply
    • More
    • Cancel
>
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube