Wireless sensors are very versatile, allowing deployment afar and afield. What happens though when you decide a software update is necessary? Climbing into the rafters of a building or driving for hours to get to a remote sensor is time consuming and perhaps not even an option. In this article, we will show you how to add just a small amount of code to your NodeMCU, ESP8266, or ESP32 project that will allow simple “Over the Air” (OTA) updates to be done from the comfort of your desk.
We will modify the code from the article, 30 Minutes or Less: Build a Wireless Sensor Network Using NodeMCU to demonstrate how to add OTA capability. At the core of this functionality is a the library, WebOTA.
Step One - Download and Install the WebOTA .zip File
Download the.zip library:
Now Install the .zip library to your Arduino IDE:
Step Two - Download the updated Arduino Sketch to the NodeMCU
The code listing is at the bottom of the article. Make sure you switch to “raw” before copying, or just download the .zip; copying and pasting can add unwanted characters to the sketch which drives the Arduino IDE crazy. There are really just a few lines of code to add the OTA capability:
#include <WebOTA.h>
// Initialize WebOTA
webota.init();
Serial.flush();
delay(500);
// Replace all delay() calls with webota.delay()
// Allows webota to listen for update requests during delay()
// For example:
webota.delay(1000);
// Finally, at the end of your sketch loop:
webota.handle();
Step Three - Test out the OTA capability
Once the sketch is running, export it as a binary file (you will find it in the sub-directory for the project, under Arduino):
If you are testing with the target NodeMCU still connected to the PC via USB, open a Serial Monitor to see the OTA “in action”. To test out the OTA capability, use the following curl command to send the binary file (for this example our filename is “Attic-NodeMCU-OTA.ino.nodemcu.bin” and the target NodeMCU IP address is: 192.168.1.6):
curl -F "file=@Attic-NodeMCU-OTA.ino.nodemcu.bin" 192.168.1.6:8080/webota
Here’s a short video of an OTA in action:
Source code: