Created
November 14, 2016 21:45
-
-
Save devdevgoat/0952203e4abe63def69a36a637c3ac8b to your computer and use it in GitHub Desktop.
This file contains 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/python | |
import os | |
import sys | |
import csv | |
import datetime | |
import time | |
import twitter | |
import socket | |
s = socket.socket() | |
address = '10.0.1.26' | |
port = 5577 | |
def light(r,g,b): | |
keybit = "31".replace(':', '').decode('hex') | |
keybit += chr(r) + chr(g) + chr(b) | |
keybit += "00:f0:0f".replace(':', '').decode('hex') | |
keybit += chr(sum(bytearray(keybit))%256) | |
try: | |
s.connect((address, port)) | |
s.send("81:8a:8b:96".replace(':', '').decode('hex')) | |
s.recv(1000) | |
s.send("10:14:0f:08:0d:05:16:15:04:00:0f:8b".replace(':', '').decode('hex')) | |
s.recv(1000) | |
s.send(keybit) | |
except: | |
print("Could Not Connect (They are finnicy!)") | |
def test(): | |
#run speedtest-cli | |
print 'running test' | |
a = os.popen("python /home/pi/speedtest/speedtest-cli --simple").read() | |
print 'ran' | |
#split the 3 line result (ping,down,up) | |
lines = a.split('\n') | |
print a | |
ts = time.time() | |
date =datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S') | |
#if speedtest could not connect set the speeds to 0 | |
if "Cannot" in a: | |
p = 100 | |
d = 0 | |
u = 0 | |
#extract the values for ping down and up values | |
else: | |
p = lines[0][6:11] | |
d = lines[1][10:14] | |
u = lines[2][8:12] | |
print date,p, d, u | |
#save the data to file for local network plotting | |
out_file = open('speedtest.csv', 'a') | |
writer = csv.writer(out_file) | |
writer.writerow((ts*1000,p,d,u)) | |
out_file.close() | |
#try to tweet if speedtest couldnt even connet. Probably wont work if the internet is down | |
if "Cannot" in a: | |
print "Red" | |
try: | |
light(255,0,0) | |
except: | |
pass | |
if eval(d)<50: | |
print "Yellow" | |
try: | |
# i know there must be a better way than to do (str(int(eval()))) | |
light(255,165,0) | |
except Exception,e: | |
print str(e) | |
pass | |
elif eval(d)>30: | |
print "Green" | |
try: | |
# i know there must be a better way than to do (str(int(eval()))) | |
light(0,255,0) | |
except Exception,e: | |
print str(e) | |
pass | |
return | |
if __name__ == '__main__': | |
test() | |
print 'completed' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment