A watchdog is a device used to protect a system from specific software or hardware failures that may cause the system to stop responding. The application is first registered with the watchdog device. Once the watchdog is running on your system, the application must periodically send information to the watchdog device. If the device doesn’t receive this signal within the set period of time it would execute the proper keystrokes to reboot the machine or restart the application.
Watchdog is a function that monitors whether the system is running normally or not.
The watchdog board for Raspberry Pi Model B + we’ll be using this time has a built-in program to monitor the GPIO in advance, so it can be installed very easily.
The IC with the built-in countdown timer monitors GPIO of Raspberry Pi, and the software on Raspberry Pi sends pulse signals to GPIO at a fixed interval to reset the timer in the IC.
The countdown timer on the watchdog does not reset when no pulse signals are sent due to an error on the Raspberry Pi. The IC on the board will then stop providing the power to Raspberry Pi and restart providing the power to restart the Raspberry Pi.
We’ll be using this watchdog board. There is “Raspberry Pi Model B +”, but it also works for Raspberry Pi Model A +, as well as Raspberry Pi 2 Model B. In this tutorial, we’re using Raspberry Pi 2 Model B.
As with Sense HAT, you can just plug the watchdog into the GPIO pin of Raspberry Pi to connect it. If you want to fasten the boards so they are securely connected, please feel free to use Raspberry Pi screws and spacers.
As shown in Figure 3, connect the power using the DC jack to the watchdog board. The DC connector will supply power to both the watchdog and the Raspberry Pi body.
The following two points can be confusing so let’s clarify…
1. Supply 5V power from the DC jack on the watchdog board.
2. Do not use the power supply of the Raspberry Pi body
Since the power is taken from the DC jack of the watchdog, the power (microUSB) of the Raspberry Pi main unit is not used. If another power supply is passing, it will not initiate a restart of the Raspberry Pi, even if the watchdog operates fine.
I learned this the hard way and had to juggle around with power supplies so many times to figure this out!
When checking the operation, LED state indicator should be lit up. It is off by default for power saving, but it will be turned on when shorting the LED on/off jumper. I used a jumper pin to short circuit, but in the picture, it’s replaced with a jumper wire.
Lighting up the LED follows the following three states. It’s hard to imagine using the numerical values (seconds), but you’ll notice obvious differences when the LED flashes.
How to light the LED at each state:
When WD_DEFAULT, the state LED blinks once every about 2.56 seconds
When WD_Active, the state LED blinks once every about 0.32 seconds
When WD_ReBoot, the state LED blinks once every 0.08 seconds
Introducing the Watchdog
For this tutorial, we’ll be using Raspberry Pi 2 Model B and Raspbian Jessie with PIXEL released in 2016-11-25.
sudo apt-get update
sudo apt-get upgrade
After writing Raspbian to the SD card, run the above commands as usual.
The watchdog operates as shown in Figure 5. Simply turning on the power does not start up the watchdog. The default state of “State LED blinks once every about 2.56 seconds” continues. For details on how to activate the watchdog, please refer to How to use the watchdog timer for Raspberry (Japanese). They are listed at the bottom of the page.
To prepare Raspberry Pi, run a program that automatically generates a pulse signal to the monitoring port (GPIO 17 by default) once it is ready to boot up.
This is quite important. A source program that performs this processing is also listed:
The operation of the program is “to output a pulse to the GPIO 17 once a second”.
In order to link with the watchdog board, it is necessary to automatically start a program that pulses this GPIO. By doing this, you can reuse the watchdog function automatically even if Raspberry Pi reboots.
Let’s start up the watchdog using this program!
sudo nano blink17.py
import RPi.GPIO asGPIO
First, save the program in a folder and execute it with the following command.
Since the watchdog starts up when it detects five pulses, in this program the state LED will blink once in about 0.32 seconds after 5 seconds of the execution. After that, if there is no pulse for more than 5 seconds, the status LED changes to flashing LED once every 0.08 seconds and the reboot is performed automatically. (In the video, it is forcibly terminated with [Ctrl] + [C]).
Automatic Reboot Test
It’s convenient to set the program for the watchdog to run at startup so that the program runs even after automatic reboot!
Let’s test by using a program adjusted so as to process only for 30 seconds (Since it’s difficult to release cron if it’s too short, at least 30 is recommended).
sudo nano blink17_30sec.py
import RPi.GPIO asGPIO
Next, register it with cron after granting execution privilege.
sudo chmod 755 blink17_30sec.py
Since we want to execute it at “startup,” let’s specify “@ reboot”. I added the following to the last line.
Reload cron and restart Raspberry Pi.
sudo /etc/init.d/cron restart
The state is easy to understand when you look at the blinking LED.
When you are done checking the operation, let’s undo the cron setting. When introducing, it’s OK to change the program to be called with cron to continue sending a pulse without restriction (blink 17.py in this case).
A watchdog comes in very handy for remote access and control. For whatever reason your Raspberry Pi freezes, you no longer need to manually reboot the Pi by unplugging it and plugging it back in.
Nevertheless, it seems necessary to test whether the pulse stops properly when there is error processing, or whether there is any conflicting processing. Be sure not to rely too heavily on the watchdog. It should be used sort of like an “insurance”.