Image: ZS1I AllStar Hub, 145.550 Echolink Simplex Link and ZS-Link SVXLink Reflector
With the recent loadshedding experienced I decided to consolidate several projects that were left hanging due to other more urgent projects or the fact that loadshedding was suspended and there was not really a need to finish such projects. Well this time around I decided to make a list of unfinished projects and whether loadshedding is suspended early, these projects must be finalized irrespective if there are other more urgent projects. One such project is the Raspberry Pi Router/Modem "Watchdog" Project. I received requests from fellow radio amateurs to provide more information setting up the "Watchdog" and what was the end result in connecting it to an AllStar node and LTE Modem/Router. Part 1 and Part 2 is available by clicking on the specific part.
What is the project all about? In short I needed an automated “watchdog” to tell me if the Internet was up or down and if down to automatically connect to the Internet once it is back up and running again especially during and after loadshedding. (power outages) If it is up then there is no further action to be taken. In Part 3 I will be looking at a few Linux and Setup commands for this project. Once you finished constructing the "Watchdog" it is time to "wake it up" and getting it to watch the Internet for connection outages or confirm that it is up and running.
Let's first look at our connections that must be made for the "Watchdog" to work correctly.
Cables needed:
1. 12 v DC Cable to power the "Watchdog"
2. Ethernet Cable coming from the Router/Modem that you will be monitoring.
3. Relay Control Cable. One end of the cable connects to the Router. The other end connects to 12v DC going to the Router and the third connection is to the "Watchdog" relay connection which is Com - NC wired.
4. Internet "Watchdog" Bridge Plug - This is needed when the "Watchdog" is not connected to the Router/Modem. It is just a wire bridge to give 12 v DC connectivity.
See images for clarity in this regard.
Once you have connected all the cables it is time to switch the unit on. Wait a few minutes and then retrieve the IP address by either looking in the router/modem or by using software to retrieve the IP address.
I use VNC to get access to the Raspberry Pi. Enter the IP address in VNC as well as your username and password which you must have created when installing the OS. Once you have logged into the Raspberry Pi you must install Python 3.11.1 if not already installed. If not installed do a Google search on how to install Python on a Raspberry Pi. We need Python 3 to run the Internet Watchdog program. Once Python is installed and you rebooted we need to update the Raspberry Pi. Open a Terminal window in the Raspberry Pi.
Enter:
$ sudo apt update (enter)
When all the updates are downloaded and installed use clear and enter to clean the terminal window.
We will now write the two script files as shown in Part 2. Keep Part 2 open in another tab of your browser as we will copy the contents of the two script files into two separate files. To create each file you will have to follow the procedure listed underneath.
$ sudo su (enter)
$ cd /home/pi (enter)
To create and write a new Python script with Nano:
In the terminal window use:
$ nano filename.py In our case replace filename with relay_test.py
Copy and paste the relay_test.py in the second browser to relay_test.py Once copied
press CTRL+S to save the file.
press CTRL+X to exit Nano and come back to the terminal.
Your relay_test.py script file is now created. Do the same with the relay_final.py script file. Also open relay_final.txt and relay_test.txt files by using the above nano method. Do not copy and paste anything in the relay_final.txt or relay_test.txt files. Just save them without anything in it.
The four files you created will be listed in the /home/pi directory.
You should still be in root@raspberrypi:/home/pi#
Use ls command to list the files. The files will be listed as:
relay_test.py
relay_final.py
relay_final.txt
relay_test.txt
Now go to the Applications Menu of the Raspberry Pi (far left top of the screen)
Look for Thonny Python under programming.
Open Thonny
The Thonny Python Program will open
Click File - Open - select relay_test.py
Click OK
relay_test.py script file will open in Thonny Python
Again click file
Select Open
Click on relay_final.py
Click OK
relay_final.py script file will open in Thonny Python
Time to test if the Internet is UP or DOWN using relay_test.py script
In the Thonny Python Program select relay_test.py The script has a false IP address to simulate that the Internet is down.
Go to the green round icon with the black triangle pointing to the right
Click on the icon to run the relay_test.py script
Output will be displayed in Shell (Bottom of Thonny)
Output:
Date and Time is: 2025-02-24 15:44:12.382124
Testing Internet connection using Ping
PING 1.51.17.1 56(84) bytes of data
(Note the 1.51.17.1 is a fake IP address) to simulate that the Internet is down)
--- 1.51.17.1 ping statistics ---
5 Packets transmitted, 0 received, 100% packet loss, time 135 ms
1.51.17.1 Internet connection failure
Turning off power to router
Waiting 1 minute to turn router back on
Power up and Reboot router
Nearly there - wait another while! (Till >>> appear)
It is clear from the above that we do not have a working Internet connection. Internet is down!
You can replace the IP address in the above script with 8.8.8.8 and again test if the Internet is UP or DOWN. The output would reflect that the Internet is UP. No need to change the IP. We will use the final script to get the "Watchdog" up and running.
Next we will use relay_final.py script to find out if the Internet is UP or Down.
In the Thonny Python Program select relay_final.py
Go to the green round icon with the black triangle pointing to the right
Click on the icon to run the relay_final.py script
Output will be displayed in Shell (Bottom of Thonny)
Output:
Date and Time is 2025-02-24 15:59:35.420916
Testing Internet Connection using Ping
Ping 1.1.1.1 (1.1.1.1) 56 (84) bytes of data
64 bytes from 1.1.1.1; icmp_seq=1 ttl=57 time=66.0ms
64 bytes from 1.1.1.1; icmp_seq=2 ttl=57 time=48.6ms
64 bytes from 1.1.1.1; icmp_seq=3 ttl=57 time=47.4ms
64 bytes from 1.1.1.1; icmp_seq=4 ttl=57 time=48.5ms
64 bytes from 1.1.1.1; icmp_seq=5 ttl=57 time=45.8ms
---1.1.1.1 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, 12 ms
rtt vnin/avg/mox/mdev=45.826/51.277/66.032/7, 446 ms
1.1.1.1 Internet connection success. No further input needed.
If the Internet was down the output would be similar to the output we received in relay_test.py simulation.
I drilled an inspection hole on the top of the cabinet to view the two LED's on the relay module. Under normal operation there is a green led that indicates that there is power to the relay module. There is another relay that will indicate whether the Internet is up or down. (See videos in
Part 1)
If only the green power led on the relay module is on then the Internet is up and running.
If the green power led and a red led is lit on the relay module then the Internet is down.
You can now easily see if there is any issues by just looking through the inspection hole.
Finally: We now need to setup the crontab -e file to run the final script every 10 or 30 minutes (you choose the time interval)
Open a Terminal window
Type after $:
crontab -e (enter)
Once file open go to the last entry line in the file and enter the following:
#*/10 * * * * python3 /home/pi/relay_final.py
#*/2 * * * * python3 /home/pi/relay_final.py >> /home/pi/relay_final.txt 2&>1
#*/2 * * * * python3 /home/pi/relay_test.py
#*/2 * * * * python3 /home/pi/relay_test/py >> /home/pi/relay_test.txt 2&>1
We have four options/settings:
1. Running relay_find.py script every 10 minutes
2. Running relay_find.py script every 2 minutes with output to relay_final.txt file
3. Running relay_test.py script every 2 minutes
4. Running relay_test.py script every 2 minutes with output to relay_test.txt file
Note: Options 2 and 4 should not be activated to run continuously as output data can quickly fill up your SD Card. Just use it for test purposes only.
The relay_final.py script will run every 10 minutes and should be activated by removing the # in front of */10 ------ etc
If you go to cd /home/pi (enter) you will see the text (txt) files listed. If you activated relay_final.py and relay_text.py while testing crontab you can click on the txt file and you will be able to view output when running either script file. Only run one script txt file at a time. How ever there is another way to check if crontab ran successfully.
In Terminal use:
grep CRON /var/log/syslog
Note: After testing script files and txt files ensure that only relay_final.py is activated. The three other crontab entries should be de-activated by putting an # in front of each entry.
To check crontab content after closing it you can type in Terminal:
$ crontab -l
Crontab content will be displayed in the terminal window.
This final setup will activate the Raspberry Pi Router "Watchdog" and you will now be able to use this setup/project to automatically monitor the Internet continuously.
Enjoy!!
Video:
Images: Click on images for larger view










