Table of Contents
Overview
In this tutorial, we will learn how to program the ESP8266 OTA using the Arduino IDE. We will learn about an interesting feature of the ESP8266 NodeMCU module that will allow us to perform OTA updates over the air. This means that the ESP8266 module, like other Wi-Fi-enabled microcontrollers, supports over-the-air updating of firmware or file system files. There are several ways to perform OTA updates, but for this, we will use the AsyncElegantOTA library. This library will create a web server that will allow us to upload new files/firmware to LittleFS ESP8266 without any physical connection. All updates will be performed over the air (OTA) without physically connecting the ESP8266 module to the computer via a USB cable.
How to use AsyncElegantOTA Library to Upload Files
- first, we will create an Arduino sketch for the OTA web update. This will be uploaded to the ESP8266 via a serial connection to the computer via a USB cable. For this step, you will need the AsyncElegantOTA, AsyncTCP, and ESPAsyncWebServer libraries.
- lIn your Arduino sketch, you will need to include the following lines of code to easily enable OTA updates via the AsyncElegantOTA library:
1st Add esp8266 board
2nd Add the additional board manager URL for esp8266
https://arduino.esp8266.com/stable/package_esp8266com_index.json
Add the following library files to the Arduino ide
AsyncElegantOTA.h
https://github.com/ayushsharma82/AsyncElegantOTA
espasynctcp.h
https://github.com/me-no-dev/ESPAsyncTCP
ESPAsyncWebServer.h
https://github.com/me-no-dev/ESPAsyncWebServer
Example 1: Arduino Sketch AsyncElegantOTA ESP32
Open your Arduino IDE and go to File > New. A new file will open. Copy the code given below in that file and save it. You just need to enter your network credentials.
Other wise go to the File-> Examples->AsyncElegantOTA->ESP8266_Aysnc_Demo select it
This sketch creates an ESP32 web server that displays a simple text message at the /root URL and an ElegantOTA web page at the /update URL. The web page will allow the user to update new firmware/download files on the ESP8266.
#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <AsyncElegantOTA.h>
const char* ssid = "Devilal";
const char* password = "password";
AsyncWebServer server(80);
void setup(void) {
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send(200, "text/plain", "Hi! I am ESP8266.");
});
AsyncElegantOTA.begin(&server); // Start ElegantOTA
server.begin();
Serial.println("HTTP server started");
}
void loop(void) {
}
Access the Web Server
After uploading the code to the board, open the Serial Monitor at a baud rate of 115200. Press the ESP8266 on-board RST button. It should display the ESP IP address as follows (yours may be different):
Now, imagine that you want to modify your web server code. To do that via OTA, go to the ESP IP address followed by /update. The following web page should load.
http://192.168.250.20/update
Follow the next sections to learn how to upload new firmware using the AsyncElegantOTA.
Upload New Firmware OTA (Over-the-Air) Updates – ESP8266
Every file that you upload via OTA should be in .bin format. You can generate a .bin file from your sketch using the Arduino IDE.
With your sketch opened, you need to go to Sketch > Export Compiled Binary. A .bin file will be generated from your sketch. The generated file will be saved under your project folder.
That’s that .bin file you should upload using the AsyncElegantOTA web page if you want to upload new firmware.
Upload a New Web Server Sketch – Example
1. Copy the following code to your Arduino IDE. Don’t forget to insert your network credentials.
#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <AsyncElegantOTA.h>
#define LED_BUILTIN_3 3
#define LED_BUILTIN_2 2
const char* ssid = "Devilal";
const char* password = "password";
AsyncWebServer server(80);
void setup(void) {
// now i am adding led
pinMode(LED_BUILTIN_3, OUTPUT);
pinMode(LED_BUILTIN_2, OUTPUT);
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send(200, "text/plain", "Hi! I am ESP8266.");
});
AsyncElegantOTA.begin(&server); // Start ElegantOTA
server.begin();
Serial.println("HTTP server started");
}
void loop(void) {
digitalWrite(LED_BUILTIN_3, HIGH); // turn the LED on (HIGH is the voltage level)
digitalWrite(LED_BUILTIN_2, HIGH);
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN_3, LOW); // turn the LED off by making the voltage LOW
digitalWrite(LED_BUILTIN_2, LOW);
delay(1000);
AsyncElegantOTA.loop();
}