ESP8266 OTA (Over the Air) Updates With Arduino IDE

by Jun 13, 2020Arduino IDE Programming, NodeMCU

Imagine if we can upload the code without using USB Cable. The data then can be transmitted “Over the air” ESP8266 OTA offers a facility of uploading the code wirelessly and this is what we call OTA updates. In this tutorial we will discuss how this feature can be used with any NodeMCU board.

Contents:

ESP8266

OTA: Introduction

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. Furthermore, we can use one central location that can send an update to multiple ESPs sharing same network.

This functionality is extremely useful in case of no physical access to the ESP module. It helps reduce the amount of time spent for updating each ESP module at the time of maintenance.

OTA updates can be done in various ways like through server, web updater etc. But one of the easy and better way is to do using Arduino IDE.

Sending the first OTA code Serially

Basically, the process of OTA updates can be set into two steps: first, you need to send the below code serially. After this, you can send the code wirelessly. But you need to write the OTA code with your main code every time you upload it. We will now begin with step-1.

First of all, setup your ESP module. You can check it out here. Now, go to Files > Examples > ArduinoOTA. Refer the diagram shown below.

Follow these steps

We will go with the Basic OTA code. You have to open it and and set the SSID and password as per your availability.

Simply upload it and open the Serial monitor. Set the Baud Rate to 115200 and then press the RST / EN button on the ESP. Hopefully, you should see the IP address provided by the local host. You can note that IP address.

IP address

Over the Air transmission

Now you have successfully completed the first step. We are going to write a simple code of blinking the built in LED in the board using OTA updates. You can see the code given below.

#include <WiFi.h>
#include <ESPmDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>

const char* ssid = ".............";
const char* password = "..................";

//variabls for blinking an LED with Millis
const int led = 2; // ESP32 Pin to which onboard LED is connected
unsigned long previousMillis = 0;  // will store last time LED was updated
const long interval = 1000;  // interval at which to blink (milliseconds)
int ledState = LOW;  // ledState used to set the LED

void setup() {
  pinMode(led, OUTPUT);
  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 3232
  // ArduinoOTA.setPort(3232);

  // Hostname defaults to esp3232-[MAC]
  // ArduinoOTA.setHostname("myesp32");

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

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

  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);
    })
    .onEnd([]() {
      Serial.println("\nEnd");
    })
    .onProgress([](unsigned int progress, unsigned int total) {
      Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
    })
    .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();
  //loop to blink without delay
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
  // save the last time you blinked the LED
  previousMillis = currentMillis;
  // if the LED is off turn it on and vice-versa:
  ledState = not(ledState);
  // set the LED with the ledState of the variable:
  digitalWrite(led,  ledState);
  }
}

The important thing you need to keep in mind is to give appropriate power source to the NodeMCU and no cable is required now. You need to select the Network port for OTA updates. Select the IP address you noted above. Refer the image below.

Select the network port

Now upload the code and you will see the Over the Air uploading of the code. After that, the on board LED will start blinking as per the code.

Conclusion

So, you have seen that the code was uploaded wirelessly (Over the Air). You can do the necessary changes as per your code and then the procedure is same. You can also see the actual videos below depicting the same.

I hope you were able to complete this OTA updates’ article. Feel free to write in the comment section. Happy Learning!

Creating a multiplication Skill in Alexa using python

Written By Anmol Punetha

Hey, I am Anmol. I am a tech blogger and an electronics engineering student at the same time. As you're reading my blog, you are getting a generous amount of information in simpler words. Hope it's of use. Thank you for visiting. Keep reading!

RELATED POSTS

Coding  MicroPython using NodeMCU ESP8266

Coding MicroPython using NodeMCU ESP8266

Before diving into the NodeMCU with MicroPython we should know some key concepts of embedded programming such as flashing , firmware etc. Development platforms for NodeMCU_ESP8266: ESPlorerLuaIDEArduino IDE etc Programming languages for NodeMCU_ESP8266:...

Introduction to MicroPython and ESP8266

Introduction to MicroPython and ESP8266

For ages, C and C++ have ruled over the embedded system industry. Fast prototyping is an important aspect of the industry today. In fact MicroPython is the best suited for fast prototyping. Students and engineers are becoming very familiar with the Python programming...

Using NodeMCU ESP8266 with Arduino IDE

Using NodeMCU ESP8266 with Arduino IDE

The NodeMCU_ESP8266 can be used in various development platforms like the ESPlorer, LuaIDE, Arduino IDE etc. In this tutorial you will learn how to use it with the Arduino IDE. First of all, you need to download the Arduino software. Click here to download it. You can...

Introduction to NodeMCU ESP8266

Introduction to NodeMCU ESP8266

The ESP8266 is, the name of a microcontroller designed by Espressif Systems. It is a self-contained WiFi networking solution offering as a bridge from the existing microcontroller to WiFi and is also capable of running self-contained applications. For less than $3, it...

Arduino Code Explanation

Arduino Code Explanation

As soon as you open the Arduino IDE, you will see a window of the Arduino code , consisting of basically two main parts: setup and loop. The entire code written in the Arduino IDE is known as sketch. A sketch is the name that Arduino uses for a program. It's the unit...

Arduino Uno – A basic introduction

Arduino Uno – A basic introduction

Microcontrollers are widely used in embedded devices and make the devices reliable and cheap for our requirements. They contain CPU, RAM, ROM and some form of Input/Output ports. Microcontrollers are generally a computer on a single integrated circuit which is better...

VIDEOS – FOLLOW US ON YOUTUBE

EXPLORE OUR IOT PROJECTS

IoT Smart Gardening System – ESP8266, MQTT, Adafruit IO

Gardening is always a very calming pastime. However, our gardens' plants may not always receive the care they require due to our active lifestyles. What if we could remotely keep an eye on their health and provide them with the attention they require? In this article,...

How to Simulate IoT projects using Cisco Packet Tracer

In this tutorial, let's learn how to simulate the IoT project using the Cisco packet tracer. As an example, we shall build a simple Home Automation project to control and monitor devices. Introduction Firstly, let's quickly look at the overview of the software. Packet...

All you need to know about integrating NodeMCU with Ubidots over MQTT

In this tutorial, let's discuss Integrating NodeMCU and Ubidots IoT platform. As an illustration, we shall interface the DHT11 sensor to monitor temperature and Humidity. Additionally, an led bulb is controlled using the dashboard. Besides, the implementation will be...

All you need to know about integrating NodeMCU with Ubidots over Https

In this tutorial, let's discuss Integrating NodeMCU and Ubidots IoT platform. As an illustration, we shall interface the DHT11 sensor to monitor temperature and Humidity. Additionally, an led bulb is controlled using the dashboard. Besides, the implementation will be...

How to design a Wireless Blind Stick using nRF24L01 Module?

Introduction Let's learn to design a low-cost wireless blind stick using the nRF24L01 transceiver module. So the complete project is divided into the transmitter part and receiver part. Thus, the Transmitter part consists of an Arduino Nano microcontroller, ultrasonic...

Sending Temperature data to ThingSpeak Cloud and Visualize

In this article, we are going to learn “How to send temperature data to ThingSpeak Cloud?”. We can then visualize the temperature data uploaded to ThingSpeak Cloud anywhere in the world. But "What is ThingSpeak?” ThingSpeak is an open-source IoT platform that allows...

Amaze your friend with latest tricks of Raspberry Pi and Firebase

Introduction to our Raspberry Pi and Firebase trick Let me introduce you to the latest trick of Raspberry Pi and Firebase we'll be using to fool them. It begins with a small circuit to connect a temperature sensor and an Infrared sensor with Raspberry Pi. The circuit...

How to implement Machine Learning on IoT based Data?

Introduction The industrial scope for the convergence of the Internet of Things(IoT) and Machine learning(ML) is wide and informative. IoT renders an enormous amount of data from various sensors. On the other hand, ML opens up insight hidden in the acquired data....

Smart Display Board based on IoT and Google Firebase

Introduction In this tutorial, we are going to build a Smart Display Board based on IoT and Google Firebase by using NodeMCU8266 (or you can even use NodeMCU32) and LCD. Generally, in shops, hotels, offices, railway stations, notice/ display boards are used. They are...

Smart Gardening System – GO GREEN Project

Automation of farm activities can transform agricultural domain from being manual into a dynamic field to yield higher production with less human intervention. The project Green is developed to manage farms using modern information and communication technologies....