With the use of boards such nodeMCU and others that also incorporate the ESP8266 module it is possible, very simply, to upload sketches wirelessly, regardless of the USB connection, from the arduino development environment. This greatly facilitates the maintenance and updating of all those IoT devices that we design in this way.
Implementation method is very simple. If we have support for ESP8266 installed in the arduino IDE, a sample folder for this architecture called arduinoOTA will appear in the menu. It is a library that contains the code needed for our module to respond requests to reprogram flash memory via Wi-Fi. New sketch will be received in binary format, stored it in memory and, once its integrity has been verified program mr¡emory will be overwritten with this data, after which it will generate a reset and …. we already have the new code running.
If we examine the code of the examples as BasicOTA, it seems that many additional lines of code must be included in our project. However, this is not so. It is assumed that our code already includes Wi-Fi access libraries and its management. So, the only thing we have to add are two lines: ArduinoOTA.begin (); inside the setup () and ArduinoOTA.handle () function; in our main loop ().
There are also some callback functions that are executed when certain events occur either the start of the programming or a connection error for example. These functions are available to write our own code on them so that our device does something concrete when a certain condition occurs. They appear in the BasicOTA sketch but it is not necessary that we include all these lines in our project.
In short, we must include at least in our code:
In addition to the lines mentioned above:
ArduinoOTA.begin(); within the setup() function and always after the start of the wifi functions after verifying that you have successfully connected to the network.
ArduinoOTA.handle(); within the loop() function.
So, a minimalist version of the well-known “blink.ino” with OTA functionality that, by the way, means “Over The Air” would look like this:
I almost forgot a little detail: in theory we need to have python 2.7 installed in the system (beware! Later versions like 3.5 do not work) since OTA support uses python scripts for the dump process via Wi-Fi. Python 2.7 can be downloaded from its official website here. However, I have recently installed Arduino on a new PC and I have verified that the OTA functionality works perfectly without having installed Python. The most recent versions of Arduino may not require anything special for this.
Once we have written a sketch like the previous one that incorporates support for OTA, we must dump it to the board via USB port as we usually do with everyone. From this point, once our board starts and connects to the network it will be detected by the arduino IDE, although sometimes it resists and we have to close and re-open arduino. Our device appears in the Tools / Ports menu as a “network port” with the IP assigned to it. We have only to select it and from then on we upload the sketches via Wi-Fi in a simple and efficient way.
Already with the minimum system working we can customize some aspects such as changing the host name of our board (the one that identifies it and appears in the ports menu) which by default is esp8266 – chipID, and it is very convenient to customize if we have several IoT devices, with the following line of code:
The TCP / IP port that will be used for the dump is, as it could not be otherwise, 8266 but can be changed with a call to the ArduinoOTA.setPort(num) method.
And, last but not least, there is the option to set a password to protect our device from unauthorized reprogramming attempts. In this way when we proceed to upload a new sketch from the IDE we are asked to enter it. We will activate this option by calling the ArduinoOTA.setPassword(“password”) method.
The negative aspect is that the board we use must have twice as much program memory space as our code occupies (since the new code is temporarily stored during the download process and only once its integrity has been verified is overwritten replacing the current program).
It would also be interesting to have the OTA option available through a Bluetooth connection for those devices that do not use Wi-Fi connection.