Wireless Sketch Updates for ESP8266

With the onboard WiFi capability that the ESP8266 and ESP32 boards provide, the natural next question is, can they be reprogrammed this way, too?

As it turns out, they can — and it’s fairly straightforward, thanks to the ArduinoOTA library (OTA standing for Over-The-Air.) Once programmed with an initial sketch to get the OTA functionality up and running (as well as provide the SSID and password for your WiFi), ESP8266 (and ESP32) boards can accept new sketches via their WiFi connection. There is even provision in the stock Arduino IDE to do this.

Random Nerd Tutorials has an excellent tutorial (as I guess you’d expect) on how to get started with this. If you’re in a hurry, here are my impressions after playing with it for an evening or so:

  • (The tutorial says Python 2.7 is required. I had it already, so I can’t say for sure.)
  • Add the Arduino core for your ESP8266 board (NodeMCU etc.) if you haven’t already.
  • Open the “Files–>Examples–>Ardruino OTA–>Basic OTA” example sketch. (If you can’t find it, the code is available here.
  • Modify the SSID and password lines to match your local WiFi network setup.
  • Compile and upload your sketch, using a USB cord like a cave dweller this one last time.
  • Since you’re connected via USB, you have Serial Monitor available, so you can watch it connect to the network (unless you’re like me and entered the wrong network name at first.)
  • Once it’s up and running, you should see a “Network Ports” section under Ports in the Arduino IDE. If all went well, you should see your board here. Select it and you can now upload to it this way.

That’s it — except for a few details that might not come to mind right away:

  • Make sure your new sketch also has the same OTA functionality (and your SSID and password) baked in, or once it is successfully uploaded, your board will no longer be listening for OTA updates, until you sheepishly ask the museum to borrow your USB cable back.
  • I’ve had the best luck with putting the ArduinoOTA.handle() call at the end of loop(). I suspect it might tend to use up most (or all) of the processor’s time, otherwise.
  • Don’t be a hog with time in your loop — do what you need and pass control back to the OTA handler.
  • If you move your board to a new network with a different SSID and/or password, it will need to be updated before the move. Unless you write the sketch to handle multiple networks, this will disconnect it from the former network.

This kind of functionality suggests some interesting applications — like encasing a system completely in resin, to be programmed wirelessly and powered by light or magnetic couplings.

This entry was posted in Arduino, C, Digital, HOW-TO, Internet, Networking and tagged , , , , , , , , . Bookmark the permalink.

Leave a Reply