Wednesday, March 5, 2014

How to use the Arduino watchdog to build a reliable IOT WiFi prototype



Problem


Arduino is a convenient but unstable prototyping/DIY platform. When building applications that are supposed to run indefinitely (for example, monitoring), you will need a fault tolerance approach that virtually guarantees that your mote can self-heal.


Solution (ok, it's a hack!)


Use the a call to the set the hardware watchdog. Usually this is used to handle short processes (milliseconds). Locally the Arduino exposes a few time intervals including a gargantuan 5sec timeout. As it turns out, this is extremely useful if you do not quite your Wi-Fi shield like, oh I do not know, the CC3000.


So how does it work? Well you can find out more here (no point in reinventing the wheel) but basically you wanto import the library and then use a pattern like:

...
wdt_enable(WDTO_2S);
critical section here
wdt_reset();
...



The first call tells the watchdog to bump the board (hardware reset) after the specified interval, in this case 2 seconds. The second call after the critical section put the watchdog back to sleep so you can continue your logic and avoid the draconian and unnecessary reboot since your critical section returned succesfully.

I used this on the CC3000 since this board can occasionally take a very long time to return from DHCP and/or DNS resolution. At times, it won't actually return at all thus the need to hack the Arduino code to self heal. Obviously the solution would be to dig into the CC3000 open source code and find out what is causing the application to hang indefinitely but who has that much time while prototyping? ;-)

Restoring Ms Elly

Ms Elly is a 1972 vintage Moto Guzzi. A 850 Eldorado to be precise, thus the name Ms Elly!
I decided to give her her own blog so look for it here.