Skip to content

Instantly share code, notes, and snippets.

@medeirosT
Last active March 23, 2021 00:29
Show Gist options
  • Save medeirosT/94c93cd4afe038390e1147139161a723 to your computer and use it in GitHub Desktop.
Save medeirosT/94c93cd4afe038390e1147139161a723 to your computer and use it in GitHub Desktop.
Reset your modem on internet outages using a USB Relay
# see this script for the relay lib : https://github.com/jaketeater/Very-Simple-USB-Relay
from relay import Relay
import os
from os import path
import time
import datetime
def check_ping():
if os.system("ping -c 1 -t 12 -i 0.2 8.8.8.8 > /dev/null") == 0:
return True
else:
return False
pidPath="/tmp/relay.pid"
logPath="/tmp/relay.log"
if (path.exists(pidPath)):
exit()
else:
pidObj=open(pidPath, 'a')
pidObj.write(str(os.getpid()))
pidObj.close()
logObj=open(logPath, 'a')
try:
relay = Relay(idVendor=0x16c0, idProduct=0x05df)
except:
logObj.write("[E] Could not communicate with relay. Is it plugged in? attempted at " + datetime.datetime.now().strftime("%H:%M:%S, %d/%m/%Y")+ "\n");
logObj.close()
os.remove(pidPath);
exit()
if (check_ping()==False):
logObj.write("[E] Detected communication failure at " + datetime.datetime.now().strftime("%H:%M:%S, %d/%m/%Y")+ "\n")
#Note : Since I'm using this modem on NC (Normally Closed) instead of NO (Normally Open)
# It seems these booleans are reversed? Either way, this seems to work with the library.
relay.state(0, on=True)
time.sleep(5)
relay.state(0, on=False)
time.sleep(180)
logObj.close()
os.remove(pidPath)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment