You'd run this out of cron, like, every few hours:
#!/usr/bin/python
import RPi.GPIO as GPIO
import sys
import time
import subprocess
import os
import random
hosts = ["1.1.1.1", "8.8.8.8", "9.9.9.9"]
random.shuffle(hosts)
FNULL = open(os.devnull)
for host in hosts:
res = subprocess.call(["ping", "-c", "3", "-W", "5", host], stdout=FNULL, stderr=FNULL)
if (res == 0):
sys.exit(0) # it worked. Bail
print "All hosts unreachable - resetting router"
reset_pin = 18
# Perform the reset operation
GPIO.setmode(GPIO.BCM)
GPIO.setup(reset_pin, GPIO.OUT, initial=GPIO.LOW)
time.sleep(2)
GPIO.cleanup()
sys.exit(1)
This assumes you're using GPIO pin 18, but you can select any free one you like.
It's probably not entirely kosher to just ping those hosts, so to insure that you don't wind up in hot water, you should only run this script VERY sparingly. And it wouldn't be a bad idea to maybe pick different hosts - hosts close enough to be a good test for whether your router is up or not.
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.
Or just do a sleep in the Python script instead of installing a cron job since there's no problem with leaving the script running.
Are you sure? yes | no