I am currently working on a project that uses an Arduino Mega 2560 board along with the Arduino GSM shield to connect to the internet and send/receive SMS messages.

As part of my project, I want to be able to update the software that I am running dynamically. At a high level, I want to connect to my server, check if there is an update, if there is, download it and then overwrite the current sketch to run the new downloaded binary if the checksum of the downloaded binary is as expected (to avoid corruption).

Now, I am new to Arduino programming, so I figured I would ask a community of people with experience to determine the best method.

My current ideas are as follows:

  • Writing a custom bootloader - The sketch will have an update routine that downloads the new binary to a known location, and then performs a system reset. The bootloader will then overwrite the current sketch with the downloaded binary and then run the sketch.
  • Overwriting the sketch whilst running - I'm not sure of the viability of this method, but the overall method would be to write an update routine to the end of flash memory, and in this code simply overwrite all of the non-bootloader code with the new sketch.

Are these methods both viable, if so, is there one that would be preferable to another? If not, are there any known solutions to this problem?

  • The second solution looks like a second "bootloader" to me.. It's the best solution if you want to keep your arduino board "arduino compatible", otherwise the first one is the most clean – frarugi87 Nov 26 '15 at 12:41
  • Possible duplicate of Updating the ARDuino code over GPRS – Gerben Nov 26 '15 at 14:14

You won't be able to download the whole binary and test its checksum before flashing because RAM is so much smaller than flash. To do that, you'd need an external storage device to hold the new binary, and a custom bootloader to install it from the device.

Otherwise you'll have to load-and-flash, testing the checksum on the fly, and retry if you need to until you get a good reload. Can your device and its environment tolerate stopping for however long a couple of retry cycles might take? You can't recover if the process is interrupted once you start flashing; you can only move forward. Can it tolerate a conceivably long outage if communication goes down once the reload process has started?

  • Why could I not simply download the entire binary to external memory, calculate the checksum and then, if valid, copy it a word at a time to flash? – Thomas Russell Nov 26 '15 at 16:09
  • Depending on the size of the sketch, the Arduino Mega may have enough resources to hold your sketch in RAM, or else hold an extra copy in flash (perhaps not executable, if not linked for the holding address, but still hold). Otherwise an external spi flash could be used. – Chris Stratton Nov 26 '15 at 16:40
  • @Shaktal - You could. That would be an instance of the example in my first paragraph. If adding an external memory device is acceptable to you, that would be an effective technique. You'd still need to provide a memory-device-to-flash boot-loader, but it solves the problem of getting a bad checksum or of losing communication during the download process, since you will not have to over-write any of the existing executable image until you have a complete and verified new one. – JRobert Nov 27 '15 at 1:47

Your Answer

