Posted on 4 Comments

#15: Watchdog timers for Arduino home automation

A watchdog timer helps keep your embedded devices running, by making sure they are responding properly and automatically hitting reset or cycling the power if they die. I’ve added a simple hardware watchdog timer to an Arduino compatible board running inside my ceiling, where it’s difficult to access if I need to reset it manually.

View directly on YouTube:

Links for this episode:

4 thoughts on “#15: Watchdog timers for Arduino home automation

  1. Hi Jon,

    I know it was cited as an example, but it seems to me that that software running on the Arduino board is deficient if it only tries the DHCP server once and doesn’t keep doing it until it gets a response. A nitpick, I know, but the hairy-eared programmer in me was twitching. 🙂

    I was also wondering, given you are running most of those board via power over ethernet, is it possible to software control that to trigger a reset instead. Then your watchdogs could just listen to the MQTT and cycle the POE on the corresponding port (as well as report the issue). Just another (probably more complicated) way to attack the problem.

    Great videos.


    1. Hi Evan 🙂

      I’ve had a couple of people say that DHCP requests should be attempted indefinitely so the watchdog isn’t needed, which is correct in general. I should have used a better example. It’s fairly easy to put a check in the main loop so that each time around it checks if it has an IP address, checks how long it was since it was last requested, and tries again if the counter has exceeded a retry time.

      However, that means putting a few extra lines of code in the loop, and allocating another couple of bytes to storing a timestamp. When you’re working in a processor with 2.5k of RAM and 16k of flash, sometimes you need every byte you can get just to buffer part of a packet. Every potential error such as a failed DHCP request can be coded around with checks and safeguards and timestamps, but every one of those extra checks grows the code by another few bytes. At some point you can’t just keep adding tests to recover gracefully from every possible failure.

      Yes, centralised power management would be one way to move the watchdog away from the device and put it in a central location. That would work for some devices, but not for some that are remote. I have some devices running on solar, and I’ve had devices physically located 1000km away that I had no physical access to or control over once they shipped to another site, but I needed to make absolutely sure would attempt to recover themselves if something went wrong. I should probably have used something like that as an example.

      One very interesting idea that came up in the YouTube comments was having some code running somewhere that would watch activity on MQTT, and if it failed to see any particular device for a period of time it would log a failure and send an alert. Not quite going as far as actually power cycling the remote device, but very handy to be notified if something is out of the ordinary. I don’t know of anything like that but I’m going to investigate it, and write something if necessary.

      1. Yes, there are probably many situations where those hardware watchdogs are the correct solution in the never ending list of trade-offs. Sorry if I was sounding negative, didn’t mean to be. They are the hardware equivalent of an exception being thrown all the way to the main-entry function restarting the program, I would say. Certainly done that before.

        P.S. How old is your dog? Our Lab is nine and age is just starting to catch up with him (arthritis). Even still, he would never sit still that long if I needed him to.

        1. Don’t worry about it, you’re not the only person to tell me that it shouldn’t fail hard for something dumb like a lack of DHCP response!

          We think Bonnie is about 5 (maybe going on 6) but we don’t know for sure. We got her from Animal Aid so we only have a best guess of her age at the time she was found. She LOVES attention, so it’s not hard to get her to sit still: all it takes is a pat or a foot against her and she’ll just lie there. Just like a WDT she’ll instantly jump up if you stop paying her attention though, so she was the perfect prop for this video 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *