Last active
May 11, 2022 09:40
-
-
Save Prodge/9c648c205b974d6331a6f107bcad76d0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
Restart router when it goes offline | |
Runs on orange pi zero v1 | |
""" | |
import os | |
import sys | |
if not os.getegid() == 0: | |
sys.exit('Script must be run as root') | |
import pyping | |
from time import sleep | |
from pyA20.gpio import gpio | |
from pyA20.gpio import port | |
gpio.init() | |
gpio.setcfg(port.PA12, gpio.OUTPUT) | |
gpio.setcfg(port.STATUS_LED, gpio.OUTPUT) | |
def flash_status_led(times): | |
for i in range(times): | |
gpio.output(port.STATUS_LED, gpio.HIGH) | |
sleep(0.1) | |
gpio.output(port.STATUS_LED, gpio.LOW) | |
sleep(0.1) | |
def is_online(): | |
r = pyping.ping('192.168.1.1') | |
online = r.ret_code == 0 | |
print ("Online: {}".format(online)) | |
return online | |
def trigger_relay(): | |
gpio.output(port.PA12, gpio.HIGH) | |
sleep(0.3) | |
gpio.output(port.PA12, gpio.LOW) | |
sleep(0.6) | |
try: | |
print ("Press CTRL+C to exit") | |
while True: | |
flash_status_led(3) | |
if is_online(): | |
sleep(300) | |
else: | |
flash_status_led(5) | |
trigger_relay() | |
sleep(180) | |
except KeyboardInterrupt: | |
print ("Goodbye.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment