CC Blog Projects Research & Design Hub

Building a Wi-Fi Router Watchdog

Written by Dev Gualtieri

With Raspberry Pi Pico, MicroPython and Thonny IDE

Dev created a watchdog for a Wi-Fi extender using a Raspberry Pi Pico. This monitors Wi-Fi connectivity for his smart home lighting system, which would require a reset twice a year due to rapid power interruptions.


  • Why does a Wi-Fi extender in a smart home system sometimes hang after power interruptions?
  • How can a Raspberry Pi Pico be used as a Wi-Fi watchdog device?
  • What is the role of MicroPython and Thonny IDE in programming the Raspberry Pi Pico?
  • How does the watchdog detect Wi-Fi failure and respond by cycling power relays?
  • What are the benefits and limitations of using a Pico-based solution versus commercial options?
  • Raspberry Pi Pico W
  • MicroPython
  • Thonny IDE
  • Raspberry Pi 3B+
  • 1N5819 diode (Schottky)
  • Relays (plug-in, DIP socket-compatible)
  • gc module (MicroPython garbage collection)
  • WDT from machine module (MicroPython watchdog)
  • Cutecom (Linux serial terminal)
  • /dev/tty/ACM0 (Linux serial port interface)
  • Raspberry Pi | www.raspberrypi.com

My smart home lighting system has been operational for more than two years. The system uses a supervisory program installed on a Raspberry Pi computer to control the commonly available Kasa smart plugs, wall switches and dimmer modules in a Wi-Fi network. This smart home program on the Raspberry Pi has been robust against power failures, rebooting into the proper state when power returns. Unfortunately, the Wi-Fi extender I use hangs about twice each year, requiring a manual reset.

I had no idea what could cause this problem until a chance occurrence. While working at my desktop computer, I heard a rapid tick-tick from its uninterruptible power supply (UPS). This must have been caused by the power company doing a rapid switching between its circuits. The resulting brief power interruption was too rapid to affect the several digital clocks in my house, including the one at my microwave oven, and it was too rapid to trigger a reboot of the smart home Raspberry Pi. When I checked the status of the smart home Wi-Fi extender, I found that it had hung and did not respond. Now, I had at least one cause for lapses in my smart home Wi-Fi connectivity, and I needed a solution.

Several years ago, I wanted to buy a timer for the exposure station I use for photosensitive printed circuit boards. The customer comments for these devices showed that a common use was a daily power off-on cycle of routers to correct the same problem. This tactic was used by people who need constant access to the video cameras at their remote vacation homes. While effective, this is not an elegant solution, and it would potentially result in a long outage if the router hung just after the power cycle.

If the momentary power interruption I observed was the only reason why my Wi-Fi extender hung, connecting it to a UPS would be one solution. Aside from the cost of such units, there’s also the maintenance issue of battery replacement every few years. I’m very sensitive to the growing problem of consumer electronic waste. Recycling of used lead-acid batteries is necessary, but it’s an inconvenience.

I found that a newer version of my Wi-Fi extender has a watchdog option that pings a user selected IP address at a selected interval and reboots the device if the ping is unsuccessful. This is probably the best option for people who have an application similar to my smart home system and would rather buy than build. However, there are cases in which a router replacement can’t be done, as when the router is supplied by your internet service provider. I decided against this easy solution of buying the newer model of my Wi-Fi extender, since I had the more grandiose plan of installing my Raspberry Pi smart home server in the same box as a Wi-Fi watchdog. My project box, as shown in Figure 1, incorporates the Raspberry Pi 3B+ that I use as the smart home master along with a Raspberry Pi Pico W as the watchdog. I’ve never had a problem with this Raspberry Pi 3B+, but I decided to add a little more code to make a watchdog for that, also.

FIGURE 1
Interior of the Wi-Fi watchdog, showing the Raspberry Pi Pico W and its associated circuitry on the left, and the Raspberry Pi 3B+ that I use as the server for my smart home application. The Raspberry Pi 3B+ is not required for the Wi-Fi watchdog.
FIGURE 1
Interior of the Wi-Fi watchdog, showing the Raspberry Pi Pico W and its associated circuitry on the left, and the Raspberry Pi 3B+ that I use as the server for my smart home application. The Raspberry Pi 3B+ is not required for the Wi-Fi watchdog.

The Raspberry Pi Pico W that I used as a watchdog for the Wi-Fi extender in my smart home system is an inexpensive Wi-Fi capable microcontroller that can be programmed using the microPython programming language. Unlike a Raspberry Pi, which is a functional Linux computer, the Raspberry Pi Pico is a microcontroller board similar to an Arduino. The Arduino was previously programmable only using C++, and learning this language is a large hurdle for novices to overcome compared with Python. The Arduino can now be programmed in microPython, and I’m sure that Arduino fans will have no trouble porting my software to an Arduino. Programmers proficient in C++ can use that language for the Pico as well.

CIRCUITRY

The circuitry, as shown in Figure 2 is quite simple. Voltage is supplied by a small wall transformer that was repurposed to become an internal power supply by connecting the the AC plug end to an AC receptacle. The 5V power is supplied to the Raspberry Pi Pico through a 1N5819 diode, a Schottky diode, as recommended in the Pico documentation. This diode allows simultaneous connection of the Pico to a host computer, which also provides a five-volt power source through its USB connector, and the internal power supply. Although I haven’t tried it, substitution of a conventional diode such as a 1N4004, should also work, but with the effect that the Pico would be powered by the host computer, not the circuit power, when its USB cable is connected.

FIGURE 2
Schematic diagram of the Wi-Fi watchdog circuit. If the watchdog is not used to monitor an internal server, as in my smart home application, Relay2 and its associated circuity can be eliminated.
FIGURE 2
Schematic diagram of the Wi-Fi watchdog circuit. If the watchdog is not used to monitor an internal server, as in my smart home application, Relay2 and its associated circuity can be eliminated.

A top view of the circuit board layout is shown as Figure 3. The relays are commonly available devices that can be plugged into DIP integrated circuit sockets. All these relays seem to have the same pin connections, but I found one example for which the coil polarity was important. This was likely a consequence of the relay having an internal permanent magnet that allowed a strong actuation at lower current. I designed the circuit board to accommodate both polarized and non-polarized relay coils.

FIGURE 3 
Top view of the printed circuit board layout. Note that AC line voltage appears around Relay1. The circuit board conductors should be isolated by an insulating tape for safety.
FIGURE 3
Top view of the printed circuit board layout. Note that AC line voltage appears around Relay1. The circuit board conductors should be isolated by an insulating tape for safety.
SOFTWARE

Python has become a popular programming language, principally because of its ability to import code libraries for specific functions. MicroPython, which is Python for microcontrollers, is used by the Raspberry Pi Pico. The first step to programming the Raspberry Pi Pico is to install an integrated development environment (IDE) for Python on your host computer. The best choice for this is Thonny, which has versions for Linux, Windows, and MacOS.

Holding the BOOTSEL button on the Raspberry Pi Pico while plugging its cable into a USB port on the host computer establishes a communications link that allows installation of the MicroPython firmware. After that, this button is essentially useless and does not act as a button to reset your application. The Raspberry Pi Pico W will appear as a mass storage device named RPI-RP2 in your file manager. While Thonny on my Linux computer has a menu option to install MicroPython for a Raspberry Pi Pico, this version will only install firmware for the non-wireless version of the Pico. That’s fine if that’s all you need, but our watchdog program needs Wi-Fi. What you need to do is download the Pico W firmware file, a file with a *. uf2 extension, to your host computer from the Raspberry Pi website [1].

You then copy the *.uf2 to the RPI-RP2 mass storage device. The Raspberry Pi Pico W will reboot, and you are now running the proper version of MicroPython. Thonny has an integrated terminal that allows executing commands, such as the help (“modules”) command, which lists all the MicroPython modules available for import. A proper firmware install will list network as one of these modules. Test programs can be saved on the Raspberry Pi Pico W and run from Thonny. Any program named main.py will be automatically executed at each power-up.

The Wi-Fi watchdog program for the Raspberry Pi Pico, named main.py, can be found with other resources on the Circuit Cellar website. When this is installed on the Pico, the following happens at each power cycling or reboot:

  • Required libraries are imported.
  • GPIO pins 14, 15, and 28 are configured as output pins, and their initial values are set at zero. Pin 28 is the LED pin, pin 14 is the AC power relay pin, and pin 15 is the DC power relay pin.
  • A short time delay is done before enabling the watchdog timer. This aids in development using the Thonny IDE, since it allows some file management before the remainder of the program is started.
  • The Pico watchdog timer is enabled with an 8 second timeout. This timeout value is near the maximum possible value.
  • The watchdog is initially fed, and repeats at intervals of less than eight seconds timeout throughout the program.
  • A 45 second time delay is done to allow devices to recover from a power failure.
  • The SSID and password of your Wi-Fi router is defined.
  • The URL of a check file on the optional integrated server board is defined.
  • Functions are created to flash the LED, halt with the LED off, cycle the AC power relay, and cycle the DC power relay.
  • A function is defined to check connectivity to the Wi-Fi router.
  • An infinite loop is entered to twice check the Wi-Fi router connectivity and cycle its AC power if connection is not established. If connection is not established after that, the LED is set to off to signal a major fault, and the program is halted.

If Wi-Fi connection is established, there’s an optional check at to whether a file exists on an integrated server board. If the file is not found, the DC power to the server board is cycled. If another attempt to find the check file fails, then then the LED is set to off to signal a major fault, and the program is halted.

The usual method of checking connectivity to a server is by using the ping command. However, ping is not a native function in microPython, and a ping implementation is an entire page of code. That’s why my connectivity check uses the simpler approach of checking access to a server file using a get request. The file I used is a small text file, but a blank file would work as well. Figure 4 shows the program executing in the Thonny terminal.

FIGURE 4 
A screenshot shows the Wi-Fi watchdog program running in a terminal in the Thonny microPython IDE on a Linux desktop.
FIGURE 4
A screenshot shows the Wi-Fi watchdog program running in a terminal in the Thonny microPython IDE on a Linux desktop.
MICROPYTHON MODULES

A Wi-Fi router watchdog will only be effective if the watchdog circuit itself doesn’t hang. Fortunately, the Raspberry Pi Pico has a hardware watchdog that’s activated by a microPython command when WDT is imported from the machine module. One minor problem is that the Pico watchdog will remain active until the Pico is power cycled or reset. If main.py exists on the Pico, a reset will again execute this program, once again activating the Pico watchdog. I’ve added a slight delay before watchdog activation, and this gives time to do some file management in Thonny before it main.py continues. When doing program development in the Thonny IDE, it’s best to use a different name for the program and rename it main.py at the end.

The Wi-Fi watchdog program operates in an infinite loop, and when I first started development, the program would crash with a system error after successfully completing the first few checks. I decided that this was caused by the Pico running out of memory having been bloated by allocated data that was no longer referenced. Adding garbage collection after each cycle solved this problem. By importing the gc module, I used the gc.collect() function at the end of each loop.

TESTING

I’ve included print statements throughout the microPython watchdog program to indicate its status. These appear when the program is run in the Thonny terminal, but they also appear at the Pico connector when the program is run autonomously as main.py. These can be read by a serial communications program through a USB cable. In my case, I use the Linux communications program, Cutecom, selecting /dev/tty/ACM0 as the serial port with 8 data bits, two stop bits, and no parity at a baud rate of 19,200. Monitoring the watchdog circuit this way will affirm its proper operation.

As everyone knows, strange things happen in computing, and my Linux system drops this serial port after a day’s operation. Pulling the USB cable and reconnecting brings the serial port back to life. During all this, the Pico watchdog circuit still functions; so, continuous operation of the serial port isn’t required, and I decided against an attempt to find the source of this problem. 

Raspberry Pi Pico Tips

Unless you order a Pico with pre-soldered pins, expect to solder 40 connections before you start. One confusion that even I faced when first programming the Raspberry Pi Pico is that the pin numbers in microPython are the GPIO pin numbers and not the physical pins of the circuit board.

The connector on the Pico board is a micro-USB connector, and most Raspberry Pi Pico purchases include a micro-USB to a male USB A cable for connection to computers for power and programming. It’s common to reboot the Pico by disconnecting and reconnecting the cable at the micro-USB connector, but it’s been noted by many that this connector is not mechanically stable, and it may detach from the circuit board after repeated insertions and removals. An alternative reset can be done by temporarily grounding pin 30, the pin designated as RUN.

Inexpensive breakout boards are available in which the Raspberry Pi Pico pins are connected to screw terminals. As one who has purchased such screw connectors for my own boards, I’m amazed at the low price of such boards containing as many as 40 screw connectors. The breakout boards are good for experimentation before you go to your final circuit board build.

A Raspberry Pi Pico W with no power drain from its GPIO pins requires about 150 mA at startup, and about 50mA in its quiescent state. A GPIO pin can source or sink about 15mA, but the total current from all GPIO pins should not exceed about 50mA. A computer’s USB 2 connector can supply 500mA, and a USB 3 connector can supply 900mA; so, there’s no problem powering these from a computer when drawing current from multiple GPIO pins.

REFERENCES
[1] https://www.raspberrypi.com/documentation/microcontrollers/micropython.html

RESOURCES
Raspberry Pi | www.raspberrypi.com

Code and Supporting Files

PUBLISHED IN CIRCUIT CELLAR MAGAZINE • JANUARY 2024 #414 – Get a PDF of the issue

Keep up-to-date with our FREE Weekly Newsletter!

Don't miss out on upcoming issues of Circuit Cellar.


Note: We’ve made the Dec 2022 issue of Circuit Cellar available as a free sample issue. In it, you’ll find a rich variety of the kinds of articles and information that exemplify a typical issue of the current magazine.

Would you like to write for Circuit Cellar? We are always accepting articles/posts from the technical community. Get in touch with us and let's discuss your ideas.

Sponsor this Article
Website |  + posts

Dev Gualtieri received his PhD. in Solid State Science and Technology from Syracuse University in 1974. He had a 30-year career in research and technology at a major aerospace company and is now retired. Dr. Gualtieri writes a science and technology blog at www.tikalon.com/blog/blog.php. He is the author of three science fiction novels, and books about science and mathematics. See www.tikalonpress.com for details.

Supporting Companies

Upcoming Events


Copyright © KCK Media Corp.
All Rights Reserved

Copyright © 2026 KCK Media Corp.

Building a Wi-Fi Router Watchdog

by Dev Gualtieri time to read: 10 min