Saturday, December 21, 2024
ESPHow ToIoT HardwaresTutorials/DIY

NodeMCU ESP8266 OTA (Over-the-Air) using Arduino IDE

This tutorial based on Programming NodeMCU ESP8266 OTA (Over-the-Air) using Arduino IDE. If you are not friendly with NodeMCU using Arduino IDE, Visit this: Arduino Support for ESP8266 with simple test code

Recommended: GPIO pins of ESP8266 and How to use efficiently

What is ESP8266?

The ESP8266 series, or family, of Wi-Fi chips is produced by Espressif Systems, a fabless semiconductor company operating out of Shanghai, China. The ESP8266 series presently includes the ESP8266EX and ESP8285 chips.

ESP8266EX (simply referred to as ESP8266) is a system-on-chip (SoC) which integrates a 32-bit Tensilica microcontroller, standard digital peripheral interfaces, antenna switches, RF balun, power amplifier, low noise receive amplifier, filters and power management modules into a small package. It provides capabilities for 2.4 GHz Wi-Fi (802.11 b/g/n, supporting WPA/WPA2), general-purpose input/output (16 GPIO), Inter-Integrated Circuit (I²C), analog-to-digital conversion (10-bit ADC), Serial Peripheral Interface (SPI), I²S interfaces with DMA (sharing pins with GPIO), UART (on dedicated pins, plus a transmit-only UART can be enabled on GPIO2), and pulse-width modulation (PWM). The processor core, called “L106” by Espressif, is based on Tensilica’s Diamond Standard 106Micro 32-bit processor controller core and runs at 80 MHz (or overclocked to 160 MHz). It has a 64 KiB boot ROM, 32 KiB instruction RAM, and 80 KiB user data RAM. (Also, 32  KiB instruction cache RAM and 16 KiB ETS system data RAM.) External flash memory can be accessed through SPI. The silicon chip itself is housed within a 5 mm × 5 mm Quad Flat No-Leads package with 33 connection pads — 8 pads along each side and one large thermal/ground pad in the center.  Read about For Overview, Datasheet, Technical Reference.

ESP8266 NodeMCU

NodeMCU is an open source IoT platform. It includes firmware which runs on the low cost Wi-Fi enabled ESP8266 Wi-Fi SoC from Espressif Systems, and hardware which is based on the ESP-12 module. It has GPIO, SPI, I2C, ADC, PWM AND UART pins.

OTA Programming

OTA Programming

(Over the Air) is a process which allows devices to upgrade their firmware or software wirelessly without any physical access. It uses wireless technology like Wi-Fi, Bluetooth, GPRS or 4G/3G rather than wired serial communication. OTA is used to reprogram the node devices etc. OTA updates are generally sent for updating the software, resolving the bugs, adding some features etc. Here in this tutorial, we will send OTA update to ESP8266 NodeMCU to blink an LED.

Preparing NodeMCU to receive OTA Update Wirelessly

First connect the NodeMCU ESP8266 with the PC using micro USB cable. Then, to upload the firmware using OTA, we need to upload the sketch serially using micro USB to generate the ESP IP address. This is the necessary step to upload the firmware wirelessly next time. Select the serial port to which cable is attached from Tools -> Port.

ESP8266 comes with libraries and examples which can be directly accessed from Arduino IDE. Open Arduino IDE and then Open BasicOTA example.

(Visit this post : Arduino Support for ESP8266 with simple test code If your NodeMCU bord is not configured with Arduino IDE.)

Edit the sketch by replacing “your-ssid” and “your-password” by your Wi-Fi SSID and password and then upload the sketch.

Open serial monitor after uploading the program successfully. Set the Baud Rate of 115200 on Serial Monitor and press Reset button on NodeMCU ESP8266.

Now NodeMCU ESP8266 will get connected to Wi-Fi and IP address of the ESP will display on the serial monitor.

ESP8266 Blinking LED program for OTA Transfer

Before uploading the OTA blinking LED sketch go to Tools and change PORT to ESP IP address for uploading the firmware wirelessly to the NodeMCU.

ota

Now upload the below given sketch of blinking LED on NodeMCU wirelessly using Arduino IDE and make sure that your PC and ESP are connected to same Wi-Fi network. Connect a LED With D0 pin.

After uploading the code successfully, LED on NodeMCU ESP8266 will start blinking every 1 second. You can also set host name and password in the sketch for security while uploading firmware on ESP.

Arduino IDE Sketch

#include <ESP8266WiFi.h>         //provides ESP8266 specific Wi-Fi routines we are calling to connect to network.
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>  // OTA library

#ifndef STASSID
#define STASSID "your-ssid"
#define STAPSK  "your-password"
#endif

const char* ssid = STASSID; 
const char* password = STAPSK;

const int blink_led = D0;  // LED pin on NodeMCU ESP8266

void setup() {
  pinMode(blink_led,OUTPUT); 
  
  Serial.begin(115200);           //Set Baud Rate
  Serial.println("Booting");
                                                   // Step to connect ESP with the Wi-Fi
  WiFi.mode(WIFI_STA);               //Set ESP as station mode
  WiFi.begin(ssid, password);      //Wi-Fi Credentials

  while (WiFi.waitForConnectResult() != WL_CONNECTED)      //Connecting  ESP to wi-fi takes some time, so wait till it gets connected
{
    Serial.println("Connection Failed! Rebooting...");
    delay(1000);
    ESP.restart();
  }

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

  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);
  });
  ArduinoOTA.onEnd([]() {
    Serial.println("\nEnd");
  });
  ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
    Serial.printf("Progress: %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();          //OTA initialization 
  Serial.println("Ready");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());     // Display the IP address of the ESP on the serial monitor
}

void loop() {
  ArduinoOTA.handle();

 // code to blink led every 1 second
  digitalWrite(blink_led,HIGH);
  delay(1000);  
  digitalWrite(blink_led,LOW);
  delay(1000);
}

I hope you like this post “NodeMCU ESP8266 OTA (Over-the-Air) using Arduino IDE”. Do you have any questions? Leave a comment down below!

Thanks for reading. If you like this post probably you might like my next ones, so please support me by subscribing my blog.

We have other tutorials with ESP32 that you may find useful:

You may like also:

Harshvardhan Mishra

Hi, I'm Harshvardhan Mishra. Tech enthusiast and IT professional with a B.Tech in IT, PG Diploma in IoT from CDAC, and 6 years of industry experience. Founder of HVM Smart Solutions, blending technology for real-world solutions. As a passionate technical author, I simplify complex concepts for diverse audiences. Let's connect and explore the tech world together! If you want to help support me on my journey, consider sharing my articles, or Buy me a Coffee! Thank you for reading my blog! Happy learning! Linkedin

One thought on “NodeMCU ESP8266 OTA (Over-the-Air) using Arduino IDE

Leave a Reply

Your email address will not be published. Required fields are marked *