I managed to put together a library and refactor the code that I used to achieve Moteino wireless programming.
There’s a new WirelessHEX file in the SPIFlash library. There are new WirelessHEX classes for RFM12B and RFM69W/HW transceivers. These files contain helper code that handles the new sketch over-the-air transmission for both the transmitting and receiving ends. It will increase your sketch size by a few KB, that is just an unavoidable tradeoff.
The target node that is to be wirelessly reprogrammed will need to have code to listen for a potential new sketch. I uploaded the example sketch along with everything else to the RFM12B library examples.
The gateway node sketch, target node sketch, python script that passes the HEX file to gateway node are all located here:
The custom DualOptiboot bootloader allowing the reflashing with the sketch stored on the external SPI FLASH memory chip is here: https://github.com/LowPowerLab/DualOptiboot
I managed to keep the size just under 1k.
So in the future Moteinos will probably ship All Moteinos (R2 and R3) are now shipping with this bootloader to allow for potential wireless reprogramming if you also have the FLASH chip onboard the target mote.
I would call this an alpha release even though it works pretty well. Sometimes it’s a hit or miss because of the delays I’ve set in python. Those could be refactored and code improved to be smarter about initial requests for a sketch transmission. But it seems in most cases it works the first try.
Further improvements are:
- increase speed of transmission
- test with larger sketches
- refactor python script, WirelessHEX library and gateway/target node sketches
I’ll try to keep up with changes and updates. Right now I have no idea if anyone will find this useful or not.
It works! I was able to reprogram a remote Moteino over the air with a blinky sketch!
Definitely not a ground breaking achievement, but it really felt like “Hello World” all over again! 🙂
I didn’t expect to overcome this so soon but I managed to get past what I thought would be the biggest hurdle: customizing Optiboot for wireless programming. Actually it’s just reading the new flash image from the external flash chip and transferring it into the internal flash memory. The “wireless” part happens in Step 1. Continue reading
UPDATE: See Step 2 for actual results.
Well, the title may sound somewhat misleading. But read on. After many days/hours of trial and error I was finally able to validate and transmit an intel HEX flash image to a remote Moteino, which stores it on an external SPI flash chip (soldered to the new SPI Flash footprint available on all Moteino R2s). This is the first step towards wireless programming, and probably the easier step.
I really hate to write bits and pieces of an unfinished solution and keep the readers in suspense/anticipation of what’s coming next (I see there’s a trend for this on some blogs, to increase blog hits and keep users “hooked”), but I think the wireless programming endeavor is rather long and complicated. Yesterday I achieved what I long wanted, to get a new sketch wirelessly transmitted and stored on a remote Moteino, for later interception and reprogramming through a custom bootloader.
For those unfamiliar with a HEX file, it’s the file generated by your Arduino sketch, and then uploaded to your Arduino/Moteino when you click “Upload”. It all happens behind the scenes and through the attached FTDI adapter. It’s a complex protocol that involves a handful of moving parts. The most important is the bootloader which lives on your Arduino/Moteino, which must be able to intercept the “Hey here’s a new program for you!” request and start re-programming the internal flash memory. But for all this you need wires!
Wireless programming of your Arduino/Moteino is useful when you have nodes sitting in trees, on top of your house, in your attic, underwater, underground, inside sealed enclosures, generally where it’s very hard to physically reach once you have a node deployed.