Over The Air (OTA) Upload

OTA (Over the Air) upload is the process of uploading your program to an ESP module using a Wifi connection rather than the usual USB connection. This method of uploading is useful if you cannot physically access your ESP module. The main documentation on using OTA is available here. Additional documentation on the basic Arduino WiFi functions is available

This is an advanced project and requires many skills, however, the instructions in this tutorial should be simple enough to quickly get you going.

Parts needed:
  • Wemos D1 mini or ESP8266-12 WiFi module.
  • The Wemos D1 mini development board is the easiest to work with. Everything you need to program it is built-in. All you need is a micro USB cable.
  • If you are using the ESP8266-12 then you will also need a CP2102 USB-to-serial adapter to program it, and some mini-hook jumper wires for connecting it to the USB-to serial adapter.


The Wemos D1 mini development board has a ESP8266-12 and a USB-to serial adapter built-in. So this is much easier to work with than just the barebone ESP8266. With this you can directly plug in a micro USB cable to program the on-board ESP8266-12.
Wemos D1 mini Micro USB cable ESP8266-12 USB-to-Serial adapter Mini grabber hook jumper wires
1 To program the ESP8266 WiFi module, you need to add the ESP8266 Board definition to the Arduino IDE program. You only need to do this once so if it has already been done then skip this step and go to step 2.
  • Start the Arduino program (must use version 1.6.7 or greater).
  • From the Arduino menu, select File | Preferences
  • In the Additional Board Manager URLs field, copy and paste the following:

    http://arduino.esp8266.com/stable/package_esp8266com_index.json
     
  • Click OK
Adding ESP8266 board
  • From the Arduino menu, select Tools | Board | Boards Manager
  • Find esp8266 by ESP8266 Community (usually at the very end) and click on it to select it.
  • Select the version you want from the drop-down box. Normally, you'll want the newest version.
  • Click the Install button.
  • After installation, exit and re-launch the Arduino IDE program.
Adding ESP8266 board
2 If you are using the Wemos D1 mini development board then just plug in the micro USB cable between the D1 mini board and your computer, and continue with step 4 below.

If you are using the CP2102 USB-to-serial adapter to program the ESP8266-12 or ESP8266-01 WiFi module, then click here for instructions on how to make the connections. Come back here and continue to step 3 after you have made the connections.
3 Create a new sketch and type in this program.

There are three lines flagged with **** that are needed for OTA to work



Replace "your ssid" and "your password" with the ssid and password of the WiFi network that you want to connect to.

Note: You can only connect to a 2.4 GHz wifi signal. A 5 GHz wifi signal will not work. Also you cannot use a "Guest" account that requires a response.
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h> // include the webserver library
ESP8266WebServer server(80);  // create a webserver object

#include <ESP8266HTTPUpdateServer.h>   // **** include the OTA updater library
ESP8266HTTPUpdateServer httpUpdater;   // **** create an updater object

#define statusLed 2   // built-in led for the ESP8266-12
//#define statusLed 1 // built-in led for the ESP8266-01

// this function is called when a client browses to the root
// it creates a HTML webpage to send back to the client
void indexHTML() {
  String msg = "<html>Hello world from ESP8266!</html>";
    msg += "<br>Click here <a href=\"/update\">" + WiFi.localIP().toString() + "/update</a> to update firmware.";
  server.send(200, "text/html", msg);
}

void setup() {
  Serial.begin(115200);
  pinMode(statusLed, OUTPUT);

  // connect to your WiFi
  // change the ssid and password to match your's
  WiFi.begin("your ssid", "your password");

  Serial.print("Connecting to wifi");
  while (WiFi.status() != WL_CONNECTED) {
    digitalWrite(statusLed, !digitalRead(statusLed)); // blink led
    delay(500);
    Serial.print(".");
  }
  digitalWrite(statusLed, LOW);
  Serial.println();
  Serial.print("Connected, IP address: ");
  Serial.println(WiFi.localIP());

  server.on("/", indexHTML);  // specify function to call when client browses to root
  server.begin();             // start the webserver

  httpUpdater.setup(&server); // **** start the OTA updater server
}

void loop() {
  server.handleClient();  // listen for a client connection
}
4 Uploading the initial program to the ESP8266 WiFi module.
  • From the Tools menu, select the Generic ESP8266 Module board.
  • Select the correct COM port that your ESP module is connected to.
  • Open the serial monitor and set the baud rate (bottom right corner) to 115200.
  • Click the Upload button as you normally would.
5 Running the program.
After the program is uploaded successfully, it will immediately run and you should see something like this if it connects to the wifi successfully. Note the IP address displayed.

If there are some garbage characters at the beginning of the printout then just ignore them.

If the program doesn't automatically run, then press the reset button on the Wemos board.

If it cannot connect to the wifi for about 30 seconds, then make sure that you have entered your ssid and password correctly in the code and upload the program again.

Note: You can only connect to a 2.4 GHz wifi signal. A 5 GHz wifi signal will not work. Also you cannot use a "Guest" account that requires a response.
First run of the sample OTA basic program
6 Disconnect your ESP module from your computer and connect it to a different power source, either to a power bank or a USB power adaptor. Your ESP module will run the program and should connect to your wifi just like before.
7 To update a new firmware, browse to this webserver's IP address /update. e.g.
   192.168.1.114/update

You will see this updater webpage. Click on the Choose File button to select the .bin file that you want to upload. e.g.
   BasicWebServer.ino.bin

This bin file should be located in
   Users/<account>/AppData/Local/Temp/arduino_build_xxxxxx

Then click the Update Firmware button.
OTA updater webpage
8 In order for OTA to work in your program you need to include these three lines of OTA code in your program.
  • #include <ESP8266HTTPUpdateServer.h> // **** include the OTA updater library
  • ESP8266HTTPUpdateServer httpUpdater; // **** create an updater object
  • httpUpdater.setup(&server); // **** start the OTA updater server
If you are using the WiFi_RobotsForFun.h library, you do not need to add these three lines because they are already added in the library.
#include <ESP8266HTTPUpdateServer.h>   // **** include the OTA updater library
ESP8266HTTPUpdateServer httpUpdater;   // **** create an updater object

void setup() {
      
  httpUpdater.setup(&server); // **** start the OTA updater server
  
}