Part 1 of this project is available HERE
As explained in Part 1 I needed an automated “watchdog” for the Internet connection especially during loadshedding. (power outages) In this part I will provide the Phyton Script Code, running the script as a Cron Job and the final installation of the project in a cabinet and then to the GRHub Network LTE Router and Power Source.
Lets look at the two Phython Scripts:
The first script is for testing purposes to mimic that the Internet is down.
#Test Internet Connection with Raspberry Pi
#By ZS1I, GRHub Network, Mossel Bay
#18 February 2023
#relay_test.py code written in Python3 (Ver 3.7.3)
import os
import RPi.GPIO as GPIO
import time
#Ignore Warnings
GPIO.setwarnings(False)
#Use Broadcom Numbering
GPIO.setmode(GPIO.BCM)
# Pin Definitons:
relay1Pin = 5 #
# relay pins set as output
GPIO.setup(relay1Pin, GPIO.OUT)
#time.sleep(60) #Time delay when testing using VNC after a Reboot
from datetime import datetime
# Getting the current date and time
dt = datetime.now()
print("Date and Time is:", dt)
print ("Testing Internet connection using Ping.")
hostname = "1.51.17.1" #Fake IP to test Internet Failure
response = os.system ("ping -c 5 " +hostname)
if response ==0:
print(hostname, "Internet connection success. No further input needed.")
else:
print(hostname, "Internet connection failure.")
print("Turning off power to router.")
GPIO.output(relay1Pin,GPIO.HIGH)
#Waiting 1 minutes for modem to reboot.
print("Waiting 1 minutes to turn router back on.")
time.sleep(60) #60 seconds = 1 minutes
#Turning power on to router
GPIO.output(relay1Pin,GPIO.LOW)
print("Power up and Reboot router.")
print("Nearly there - wait another while!")
The second script is the real time "Watchdog" script that monitors (pings) the Internet every 30 minutes. If the Internet is up and running then no further action will be taken and it will keep on pinging the Internet every 30 minutes. If the Internet is down then it will start the sequence as already explained in Part 1.
#Raspberry Pi Test Internet Relay Code Ver 1.1
#By ZS1I, GRHub Network, Mossel Bay
#18 February 2023
#relay_final.py code written in Python3 (Ver 3.7.3)
import os
import RPi.GPIO as GPIO
import time
#Ignore Warnings
GPIO.setwarnings(False)
#Use Broadcom Numbering
GPIO.setmode(GPIO.BCM)
# Pin Definitons:
relay1Pin = 5 #
# relay pins set as output
GPIO.setup(relay1Pin, GPIO.OUT)
#time.sleep(60) #Time delay when testing using VNC after a Reboot
from datetime import datetime
# Getting the current date and time
dt = datetime.now()
print("Date and Time is:", dt)
print ("Testing Internet Connection using Ping.")
hostname = "1.1.1.1" #IP to test Internet
response = os.system ("ping -c 5 " +hostname)
if response ==0:
print(hostname, " Internet connection success. No further input needed.")
else:
print(hostname, "Internet connection failure.")
print("Turning off power to router.")
GPIO.output(relay1Pin,GPIO.HIGH)
#Waiting 1 minutes for modem to reboot.
print("Waiting 1 minutes to turn router back on.")
time.sleep(60) #60 seconds = 1 minutes
#Turning power on to router
GPIO.output(relay1Pin,GPIO.LOW)
print("Power up and Reboot router.")
print("Nearly there - wait another while!")
Now this code is simple but it works great. The pinging time of 30 minutes is set in the Crontab file.
Herewith more information on the Crontab file and how to set it up for this project.
Running a python script every 30 minutes using Crontab?
Suppose I have a python
file relay_test.py
To schedule it to run every 30 minutes, use
crontab -e
Then edit to add
*/30 * * * * python
3
/
home/pi/relay_
test.py
To check if cron ran succesfully
grep CRON /var/log/syslog
Here, you will see in logs, lines like
May 31 14:25:01 shivam-PC CRON[17805]: (shivam) CMD (python /home/shivam/test.py)
Note: statements might not show in logs, so use
*/30 * * * * python
3
/
home/pi
/
relay_
test.py >> /
home/pi
/
relay_test
.txt
and then check relay_test.txt
for print logs.
You may want to capture stderr to the output file as well with */30 * * * * python3 /home/pi/relay_test.py >> /home/pi/relay_test.txt 2&>1
Note that I setup the crontab file in this example to log activity every 30 minutes to a relay_test.txt file. I can now check the output as and when I need to.
How do I connect the Power Supply to the "Watchdog" and the Router?
Very simple. Here is and image of the connections.
That's it!! Nothing to it and you can now relax and not worry if the Internet is online or not. This project will be your Internet "Watchdog".
Images: (Click on images for larger view.)