Arduino bootloader with OTA (over the air) support over nRF24L01
As I was building my SmartHomeDIY platform (OpenSource) one of the first things I was looking for (except of debug tools and board), was a bootloader for Arduino which supports over the air flashing of the internal sketch.
I needed this because I was about to mount all the Arduino board inside my wall, and I did not wanted to take them apart every time I had a bug.
So the way to go, is over the air (OTA), which Arduino does not supports out of the box, so lets learn how to do exactly that.
In order to get OTA working on your Arduino You will need to flash on it a special bootloader that support OTA, but not just any boatloader, you will need one specific to the type of your communication board - in my case, RF24,
If you do not know what is a bootloader please read here.
I wanted this bootloader with work with the rf24 [1] radio board - which you can buy [here]
You will need an Arduino library which support this board which you can find here
I am using Mirf and you will probably need to use it to if you will take my code
So, to start with I needed an open source code that has a modified version of the original Arduino bootloader or a custom basic bootloader like this one.
And I found one on github that does just that and it is called optiboot-nrf24l01
which is a fork of this git
What this code does is not too complicated.
When you flash a program using the Arduino IDE what that is actually happening is that the USB driver (you Arduino board is probably connected via USB) is resting the Arduino in to a bootloader state (think of the bootloader as an operating system or a bios)
Then the bootloader waits for couple of fractions on the UART to receive a spesific data (bytes) which tells it to start the flashing process,
If it gets it then he will flash and override the sketch that is already there.
if it does not get anything (timeout) it will just start the sketch that is already there and run it in a loop.
So what we need is to:
The code over optiboot-nrf24l01 is the one responsible of capturing the bytes on the remote station and overriding the sketch there.
But we still need a local flasher program which was also forked and modified by the same guy here.
This code is responsible to get the bytes (sketch data) from the computer and forward theme to the remote station.
The problem that I had is that every time I was trying to flash the remote station I was eventually flashing the local station.
This is where point 1 is getting into the picture and it took me some time figure it out and find the right solution
what you will have to do is put a 10uF capacitor in between the RESET pin on the Arduino and the GND pin
This way when ever the USB driver is trying to reset the board it will fail but will continue with the flashing process.
- Find a way NOT TO BE RESETED BY THE USB PORT.
- Get the bytes (sketch data) from the computer
- Forward the bytes to the remote station
- Capture the bytes on the remote station
- Override the sketch on the remote station.
The above is the hardware setup as I was using it, note the 10uF capacitor.
The led here is just for debugging.
I used it to make sure I am getting the bytes and was really thing to forward them to the remote station (write the to the rf24 registers over SPI).
If you need the debug code you can get it here
Using AVRDude to flash the bootloader
Once you have the above local station connected you will need to flash the remote station with the custom bootloader who has the OTA support (note that the remote station hardware configuration configuration is actually the same as the above except the capacitor)
First this is to compile the bootloader once you have that you can run avrdude commands against it in order to flash the remote station. by the end of this process you should have an bootloader.hex file.
To flash the ota-bootloader hex file you will need 2 commands and an AVR Programmer,
You can probably use another Arduino to burn the bootloader but using the programmer is much more convenient
Just connect the AVR Programmer to the computer and to the Arduino header like this
Flashing commands for avrdud
Code Editor
Note the 0xDA on the first command - it will not work without it.
optiboot_atmega328_without_watchdog.hex is the compiled bootloader hex file.
Btw, I compiled my bootloader without the watchdog option (by default it will reset into bootloader every 10 sec or so to allow remote bootloading automatically, in my code I just added to my sketch the option to reset the Arduino when it gets a specific command) - read more here
When this is done you should see something like that:
Great! now our remote station is ruining our new bootloader!
All that is left out is to run an avrdude command to flash the local station as if it the remote one - we will just flash our hex file (the same way Arduino IDE does behind the scenes).
When you compile you sketch on Arduino IDE it just puts it on a directory on your computer
I usually just copy it to my avrdude directory and flash it from there.
To read more about getting the sketch hex file read here.
If you need more here with flashing the boot loader check this.
The most important things to remember are:
- The capacitor for the local station - more info about disabling reset from USB here
- Flashing commands - the 0xDA flag
- use my code for debug with LEDs for the bootloader and the flasher if it does not work for you - it will light-up some LEDs to let you know packets are flowing over the air. if need to learn how to turn led on and off in bootloader mode, read this reference about Port Registers manipulations.
- In my case the sketch that I needed was using RF24 communications as well, so I needed the boot loader and the sketch to use the same RF24 library so I used mirf24 lib . that way once the sketch is loaded so it will be able to reset in to boot loader and flash (registers state on the rf24 board stays the same and this make the process work better).
- Note that timing here is a problem so you might need to rest the remote station manually for trail and error with the reset button on the remote Arduino station.
Finally check these links to get the Hardware and the tools needed to get the job done:
Arduino board: http://s.click.aliexpress.com/e/ZBeuJey
USBtinyISP AVR ISP programmer bootloader: http://s.click.aliexpress.com/e/aUvjA2F
All the Arduino schemes attached here where generated with the cool tool:
http://fritzing.org/home/
Hope it will serve you all well,
Fill free to comment or email me with questions.
Please subscribe my Youtube channel.
And my facebook page.
AliExpress.com Product - USBTiny USBtinyISP AVR ISP programmer bootloader Meag2560