Tutorial: Adding a WatchDog Timer to Your Project
This is a tutorial on adding a WatchDog timer to the SkyWeather Raspberry Pi based kit. However, this information is applicable to any 5.0V computer project
What is SkyWeather?
SkyWeather allows you to build your own Raspberry Pi based weather station with a Sky Cam to take pictures of your Sky and display them the cloud on WeatherSTEM. You might even make it to the WeatherChannel!
All up-to-date information for SkyWeather is here.
The Solar Extender Kit for SkyWeather already includes a WatchDog Timer. Some people place their stations in hard to get places that make it difficult to reboot the computers and would like a more reliable system.
What is a WatchDog Timer?
Computers sometimes lose their way. A power glitch, RFI (Radio Frequency Interference), hanging peripherals, or just plain bad programming can cause your small computer to hang causing your application to fail. It happens all the time. How often do you have to reboot your PC? Not very often, but once in while your Mac or PC will freeze making you have to power cycle the computer. Raspberry Pi’s will sometimes freeze because of a task not freeing up sockets or consuming other system resources and from power supply fluctuations. Arduinos sometimes freeze because of brownouts on the power line or a short power interruption or because of running out of system resources such as RAM and/or stack space, which is a very limited resource in an Arduino. Sometimes even programmers (gasp!) make mistakes.
Solar Power systems are especially susceptible to these kind of problems because of the nature of the power supply. For this reason, SwitchDoc Labs included an external hardware WatchDog timer on our new SunControl solar power charger / controller.
In small computers, you give your device the chance to recover from faults by using what is called a WatchDog Timer (WDT). A WDT is an electronic timer that is used to detect and recover from computer malfunctions. If the computer fails to reset the timer (also called “patting the dog”) on the WDT before the WDT timer expires, the WDT signal is used to initiate either corrective actions or simply to reboot the computer.
Will the use of a WatchDog Timer make your computer project more reliable? The answer is yes. The proper use of a WatchDog timer can make your computer reboot when it gets lost. A known problem with some Python libraries on the Raspberry Pi is that some of those libraries don’t properly release sockets and after a long period of time (days generally – not weeks) the Raspberry Pi will hang or thrash because it is out of resources. A properly designed program could detect this and reboot the computer, but a WatchDog Timer can be used to cover a whole multitude of sins with one fell swoop. In Project Curacao, (and Project Curacao2) we use a WatchDog Timer to reset the Battery Power Watchdog in case of a brownout or an RFI upset event and GroveWeatherPi (Raspberry Pi based Weather Station) we use it to make sure we recover from brownouts in the solar power system.
Why Add a WatchDog Timer to SkyWeather?
The SkyWeather Solar Extender Kit already includes a WatchDog Timer, but what about a non-solar power solar kit? Some people place their stations in hard to get places that make it difficult to reboot the computers (and I’m thinking especially about you John in Belleair Florida with your 35 foot radio tower power by POE – check him out on https://www.weatherstem.com/dashboard-sales) and would like a more reliable system. Another customer is building a SkyWeather kit that he is placing way out in the woods.
The University of Idaho in Coeur d’Alene is putting a SkyWeather up on the top of their building in Coeur d’Alene and it is hard to get to in order to reboot the system if the Raspberry Pi hangs (which is does sometimes, especially the Pi Zero which has so little memory).
Adding a WatchDog Timer under SkyWeather Control
Parts List Links (see product pages for board documentation):
- – SwitchDoc Dual WatchDog Timer
- – SwitchDoc Grove PowerControl
- – USB Type A to USB Micro Cable (short)
- – USB Type A Male to USB Type A Female (Extender Cord)
- – Grove Male Pin Header to Grove Cable
- – Grove 30cm Cable (50cm will work too)
- – Jumper Wires
- – M3 Nylon Pylons
- – Male Pin Headers
How This Circuit Works
A WatchDog timer is a circuit that will trigger if it is not reset (called “patting the dog”) causing something else to happen. In this case we run the trigger signal from the watchdog board into the enable signal on the USB PowerControl board (think of the USB PowerControl board as a solid state relay). We have the PowerControl board turned on by connecting LIPOBATIN to 5V (the board thinks it is hooked up to a fully charge battery) and then by applying the 300ms going high signal from the WatchDog (which if triggered means it hasn’t been petted within about 200 seconds) it shuts off the USB Port power to the Raspberry Pi and reboots the computer. That way if the processor glitches, or the software crashes, the Raspberry Pi will be restarted.
We “pat the dog” with a GPIO line from the Raspberry Pi through the Pi2Grover board and then by Grove cable to the WatchDog board.
Assembly Steps
Step 1) Solder a two pin male header in the the Pins Holes U5OUT Power and GND on the USB PowerControl Board
Step 2) Cut one end of a Female to Female Jumper to about 3 inches. Strip and solder the non header end of the wire to TP2 (which is connected to 5V in) and plug the other end onto the LIPOBATIN Male pin on the USB PowerControl Board.
Step 3) Cut one end of a Female to Female Jumper to about 6 inches and strip and solder the non header end also to TP2 on the USB PowerControl board.
On the Grove USB PowerControl TP2 should now look like below:
Step 4) Take M3 Nylon Pylons and put them in the mounting holes on both the Dual WatchDog Board and the the USB PowerControl Board. You may want to super glue a third pylon to the bottom of the USB PowerControl board for stability.
Step 5) Super Glue the boards to the inside of your box as shown. Leaving room between the two for a USB Plug.
Step 6) Take a Grove Cable to Female Pin header cable and cut off all but the white wire on the cable. Plug it into the Grove connector on the USB PowerControl board.
Step 7) Connect the white wire from the Grove cable plugged into the USB PowerControl to the Pule Trigger male pin header on the WatchDog board on the upper left hand corner as shown.
Step 8) Plug a Female to Female jumper write from GND on the USB PowerControl board (as seen above) to the GND line on the WatchDog Timer board (as seen above). If you don’t do this, nothing will work. Remember Dr. Shovics dictum, “You can always trust your mother, but you can never trust your ground”.
Step 9) Plug the female header end of the wire (you connected to TP2 in Step 2) into the VDD male pin header on the WatchDog board (the RED wire in the picture above).
Step 10) Take a normal 30cm or 50cm Grove Cable and clip the white wire in the cable (this allows you to use D5 for something else) and plug it into the Pi2Grover board into the D4/D5 connector. Take the other end and plug it into the grove connector on the WatchDog board.
The timeout interval for the WatchDog timer is set by turning the respective TM1 single turn potentiometer.It can be set from 30 seconds to 240 seconds.It is set by turning it over a 270 degree range.
The Software
The software supporting the WatchDog timer is already built into SkyWeather. Once you hook this circuit up it will reboot your pi in around 240 seconds so be sure to have SkyWeather set to start up with your computer (see the SkyWeather Configuration and Operations manual).
If you want to work on the SkyWeather software or on the Pi with the SkyWeather software turned off, then you can run the SDL_Pi_SkyWeather/doWatchdogPat.py program like this in a separate terminal window:
sudo python doWatchdogPat.py &
That will keep the WatchDog circuit happy from all the patting this program will do. The code for the doWatchdogPat.py follows, as an example on how to Pat for this circuit for your own projects.
# pats the WatchDog twice times and quits. Allows for startup # Check for user imports try: import conflocal as config except ImportError: import config import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) def patTheDog(): # pat the dog print "------Pre Python Start Patting The Dog------- " GPIO.setup(config.WATCHDOGTRIGGER, GPIO.OUT) GPIO.output(config.WATCHDOGTRIGGER, False) time.sleep(0.2) GPIO.output(config.WATCHDOGTRIGGER, True) GPIO.setup(config.WATCHDOGTRIGGER, GPIO.IN) # pat it twice while True: patTheDog() time.sleep(15) patTheDog()
Completed circuit is below: