The cable modem at our Makerspace is super flaky and likes to not give out IP addresses. We can VPN into the space to take care of the odd network issue, but if the internet is down, someone has to drive in and 99% of the time it’s fixed by rebooting the modem. Using a Pi 2, a piface Digital 2 and a little Python, I’ve cobbled together a box that will monitor if the internet is accessible and bounce the cable modem if it’s not. Here is a video of it in action
When it comes back online it will shoot a message out to slack letting us know that the modem had a problem. I need to update the notifciation code though to tell it to hold off on trying to send a message until the modem is actually back online. The code is pretty simple and is below.
#!/usr/bin/python3 import subprocess as sp import pifacedigitalio from time import sleep from slacker import Slacker slack = Slacker('xxxxxxxxxx') pfd = pifacedigitalio.PiFaceDigital() fails=0 maxfails=6 host = "220.127.116.11" def ipcheck(): status,result = sp.getstatusoutput("ping -c1 -w2 " + str(host)) if status == 0: print("System " + str(host) + " is UP !") return 1 else: print("System " + str(host) + " is DOWN ! ") return 0 def reboot(): pfd.output_pins.value = 0 pfd.output_pins.value = 1 print("Rebooting...") sleep(5) pfd.output_pins.value = 0 light_led(0) slack.chat.post_message('#tech_team', 'Ahhh! The cable modem is dead! I\'m gonna kick it') def light_led(fail_count): MAX_LED_COUNT = 6 for x in range(0, MAX_LED_COUNT): if fail_count - x > 0: # Turn On LEDs for fail pfd.output_pins[x +2].value = 1 sleep(.1) else: # Turn on LED for blink start pfd.output_pins[x +2].value = 1 sleep(.1) # Turn off LED for blink end pfd.output_pins[x +2].value = 0 sleep(.1) while True: maxattempts=60 #number of seconds between attempts attempts=0 if ipcheck(): pfd.output_pins.value = 0 fails=0 else: if fails > maxfails: fails=0 reboot() else: fails +=1 while attempts < maxattempts: light_led(fails) attempts +=1