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? ;-)

1 comment:

  1. Sketch Arduino and PIC firmware for Wifly RN-XV modem configuration without Wifly library and with front-end: https://dl.dropboxusercontent.com/u/101922388/WiflySanUSB.zip
    Type in browser address bar: 192.168.1.195/YT to toggle Led and open a HTML page.
    The microcontroller can be, with this firmware and Wifly, a server (switching the led signal through 192.168.1.195/YT in adress bar)
    or also client (the AD converter value is inserted in google databank).

    ReplyDelete