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:
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.
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.
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.
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!