Esp8266 Over the Air Firmware Update Code

Esp8266 Over the Air Firmware Update Code

ESP8266 WiFi devices
can be conveniently flashed over the air (OTA) using the
Arduino
IDE. All that is required is a little boosted code in the sketch in order to make the WiFi series port appear in the Arduino IDE.

This is very handy if you wish to install a device such every bit the
Wemos D1 Mini
in a remote location and still need to make firmware updates. It tin can too save having to keep connecting a device to the USB if it sources power past some other method.

This tutorial nosotros will embrace the necessary additions required to make a sketch compatible with OTA flashing.

Prerequisite

You will need to have the Arduino IDE installed and running. I would recommend heading over to the website and getting the latest version. You lot will also need an ESP8266 board, I would recommend the
Wemos D1 Mini
used in this series of tutorials.

This tutorial volition follow on from the previous tutorial detailing
how to add the Wemos D1 Mini to the Arduino IDE. Although this was written for the D1 Mini, I would recommend you commencement
try the code from this tutorial
on your ESP8266 device.

Writing the code

Nosotros will presume that you already have a project running on your ESP8266 lath that has been connected to yous WiFi network. For this tutorial we shall use the
example code from the previous tutorial, I would recommend checking it out if y’all take not yet continued your device to WiFi.

Connecting to WiFi

If you don’t yet have a project connected to WiFi, copy the sample code below into a new projection and salvage information technology. Upload the sketch to your lath and check that the IP address is reported back in the serial monitor window.

#include <ESP8266WiFi.h>   // Set WiFi credentials #ascertain WIFI_SSID "YOUR WIFI NETWORK SSID" #define WIFI_PASS "YOUR WIFI PASSWORD"   void setup() {   // Setup serial port   Serial.brainstorm(115200);   Serial.println();     //Begin WiFi   WiFi.brainstorm(WIFI_SSID, WIFI_PASS);     // Connecting to WiFi...   Series.print("Connecting to ");   Serial.impress(WIFI_SSID);   // Loop continuously while WiFi is not connected   while (WiFi.status() != WL_CONNECTED)   {     delay(100);     Serial.print(".");   }     // Connected to WiFi   Serial.println();   Serial.print("Continued! IP accost: ");   Serial.println(WiFi.localIP());   }   void loop() {   // put your master code here, to run repeatedly:   }
          

Add basicOTA.h

Next we need to add the
basicOTA.h
file to the project. But
download and clone
the repository
and extract it from the .cipher file. Alternatively you can create a new file and paste information technology from
the raw file available here.

Read:  Steinberg Ur22 Mkii Firmware Update for High Sierra

Add the includes

Once you accept the file, move information technology to the same directory as your project. We can then add an
include
for this file. Speech marks indicate to the IDE that the file is located in the projection directory and non the library folder. We also demand to include the
ArduinoOTA.h
library.

#include <ESP8266WiFi.h> #include <ArduinoOTA.h> #include "basicOTA.h"   // Set WiFi credentials #define WIFI_SSID "YOUR WIFI NETWORK SSID" #ascertain WIFI_PASS "YOUR WIFI Countersign"   void setup() {   // Setup serial port   Serial.begin(115200);   Series.println();     //Brainstorm WiFi   WiFi.begin(WIFI_SSID, WIFI_PASS);     // Connecting to WiFi...   Serial.print("Connecting to ");   Serial.impress(WIFI_SSID);   // Loop continuously while WiFi is not connected   while (WiFi.status() != WL_CONNECTED)   {     delay(100);     Serial.print(".");   }     // Connected to WiFi   Serial.println();   Serial.print("Connected! IP address: ");   Series.println(WiFi.localIP());   }   void loop() {   // put your main code here, to run repeatedly:   }
          

Phone call the setup OTA office

Next we demand to phone call the
setup_OTA
function from the
setup
part in our project. This will execute all of necessary steps from
basicOTA.h
needed to setup OTA flashing in our project.

#include <ESP8266WiFi.h> #include <ArduinoOTA.h> #include "basicOTA.h"   // Set up WiFi credentials #define WIFI_SSID "YOUR WIFI NETWORK SSID" #ascertain WIFI_PASS "YOUR WIFI Countersign"   void setup() {   // Setup serial port   Serial.begin(115200);   Serial.println();     //Begin WiFi   WiFi.begin(WIFI_SSID, WIFI_PASS);     // Connecting to WiFi...   Serial.print("Connecting to ");   Serial.print(WIFI_SSID);   // Loop continuously while WiFi is not connected   while (WiFi.status() != WL_CONNECTED)   {     delay(100);     Serial.print(".");   }     // Continued to WiFi   Serial.println();   Serial.print("Connected! IP address: ");   Series.println(WiFi.localIP());    // Setup Firmware update over the air (OTA)   setup_OTA();   }   void loop() {   // put your main lawmaking here, to run repeatedly:   }
          

Poll ArduinoOTA.handle

We besides need to poll
ArduinoOTA.handle
in our main program loop to check for OTA firmware updates. This should be added somewhere in the
loop
function.

#include <ESP8266WiFi.h> #include <ArduinoOTA.h> #include "basicOTA.h"   // Fix WiFi credentials #define WIFI_SSID "YOUR WIFI NETWORK SSID" #ascertain WIFI_PASS "YOUR WIFI PASSWORD"   void setup() {   // Setup series port   Serial.begin(115200);   Serial.println();     //Begin WiFi   WiFi.brainstorm(WIFI_SSID, WIFI_PASS);     // Connecting to WiFi...   Serial.impress("Connecting to ");   Serial.print(WIFI_SSID);   // Loop continuously while WiFi is not connected   while (WiFi.condition() != WL_CONNECTED)   {     delay(100);     Serial.print(".");   }     // Connected to WiFi   Serial.println();   Series.impress("Connected! IP address: ");   Serial.println(WiFi.localIP());    // Setup Firmware update over the air (OTA)   setup_OTA();   }   void loop() {    // Bank check for OTA updates   ArduinoOTA.handle();   }
          

Flash the LED to debug

As we volition be connecting the Arduino IDE to WiFi and not the standard serial port, we will not exist able to receive information in the serial console.

Read:  Android Tv Box Firmware Update to 5.1

In order to ostend that we accept updated the firmware from the previous tutorial, we will add together some code to blink the LED.

#include <ESP8266WiFi.h> #include <ArduinoOTA.h> #include "basicOTA.h"   // Ready WiFi credentials #define WIFI_SSID "YOUR WIFI NETWORK SSID" #define WIFI_PASS "YOUR WIFI Countersign"   void setup() {    // Setup LED   pinMode(LED_BUILTIN, OUTPUT);    // Setup serial port   Serial.brainstorm(115200);   Serial.println();     //Begin WiFi   WiFi.begin(WIFI_SSID, WIFI_PASS);     // Connecting to WiFi...   Serial.print("Connecting to ");   Series.print(WIFI_SSID);   // Loop continuously while WiFi is non connected   while (WiFi.status() != WL_CONNECTED)   {     filibuster(100);     Serial.print(".");   }     // Connected to WiFi   Series.println();   Serial.print("Connected! IP accost: ");   Series.println(WiFi.localIP());    // Setup Firmware update over the air (OTA)   setup_OTA();   }   void loop() {    // Check for OTA updates   ArduinoOTA.handle();    // Blink LED   digitalWrite(LED_BUILTIN, HIGH);   delay(100);   digitalWrite(LED_BUILTIN, LOW);   delay(100);   }
          

Alternatively you could use a seperate serial final to read the data from the serial port if you have your device continued to USB for debugging.

However blinking the LED provides visual feedback regardless of whether the device is continued to the USB port.

Flashing

That’s information technology! Our application is now gear up to be flashed over the air. Even so we kickoff need to flash the new code to our device over the serial connexion in order to enable OTA flashing.

Become ahead and flash the code onto the device over the serial connexion. Once the new sketch has been uploaded you lot volition be able to select the WiFi serial port from
tools > Port > your_device_ip_address.


Conclusion

Adding the ability to wink your application over the air is both convenient and necessary in certain circumstances. The Arduino ESP Core has built-in OTA functionality that is like shooting fish in a barrel to leverage with some simple code.

Read:  How to Install Custom Firmware on Rg350 Without Opening It

Why notcheck out some more awesome ESP device articles here on my website to go some ideas for your next project!

Esp8266 Over the Air Firmware Update Code

You May Also Like