-
Notifications
You must be signed in to change notification settings - Fork 3
/
speedtest.py
executable file
·107 lines (96 loc) · 3.19 KB
/
speedtest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/sudo env/bin/python3
# *-* coding: utf-8 -*-
"""Display internet speed in SSD1306 display"""
from __future__ import print_function
import os
import requests
import math
import time
import Adafruit_GPIO as GPIO
import Adafruit_GPIO.MCP230xx as MCP230xx
PIN_BASE = 0
I2C_ADDR = 0x20
MCP_PINS = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
MCP_PINS_R = sorted(MCP_PINS, reverse=True)
SPEED_LIMIT = 10
NUM_LEDS = len(MCP_PINS)
BLINK_SPEED = 0.03
INTERVAL = 1800 # in seconds
MAKER_BASE_URL = 'https://maker.ifttt.com/trigger/speedtest/with/key/'
with open('/home/pi/.maker_key', 'r') as key_file:
maker_key = key_file.read()
def main():
for i in range(1, 4):
try:
ping, download, upload = get_speedtest_results()
print('Ping: {}'.format(str(ping)))
print('Download: {}'.format(str(download)))
print('Upload: {}'.format(str(upload)))
maker_url = MAKER_BASE_URL + maker_key + '?value1=' \
+ str(ping) + '&value2=' + str(download) \
+ '&value3=' + str(upload)
content = requests.get(maker_url).text
print(content)
speedometer(download)
os.system('./speedoled.py '
+ str(download) + ' ' + str(upload))
print('======= DONE =======')
time.sleep(INTERVAL)
except ValueError as err:
print(err)
print('Try {} - Trying again....'.format(str(i)))
pass
else:
print('Ping: {}'.format(str(ping)))
def speedometer(speed):
if speed > SPEED_LIMIT:
speed = SPEED_LIMIT
speed_percent = (speed * 100) / SPEED_LIMIT
leds_lit = (NUM_LEDS * speed_percent) / 100
leds_lit = int(math.ceil(leds_lit))
print('LEDs lit: {}/{}'.format(str(leds_lit), str(NUM_LEDS)))
mcp = MCP230xx.MCP23017()
for pin in MCP_PINS:
mcp.setup(pin, GPIO.OUT)
for _i in range(0, 10):
for pin in MCP_PINS:
mcp.output(pin, 1)
time.sleep(BLINK_SPEED)
next_pin = pin + 1
mcp.output(next_pin, 1)
mcp.output(pin, 0)
time.sleep(BLINK_SPEED)
for pin in MCP_PINS_R:
mcp.output(pin, 1)
time.sleep(BLINK_SPEED)
prev_pin = pin - 1
if (prev_pin != -1):
mcp.output(prev_pin, 1)
mcp.output(pin, 0)
time.sleep(BLINK_SPEED)
i = 1
for pin in MCP_PINS:
if leds_lit >= i:
mcp.output(pin, 1)
time.sleep(BLINK_SPEED)
else:
mcp.output(pin, 0)
i += 1
def get_speedtest_results():
ping = download = upload = None
with os.popen('env/bin/speedtest --simple') as speedtest_output:
for line in speedtest_output:
label, value, unit = line.split()
if 'Ping' in label:
ping = float(value)
elif 'Download' in label:
download = float(value)
elif 'Upload' in label:
upload = float(value)
if all((ping, download, upload)):
return ping, download, upload
else:
raise ValueError('TEST FAILED')
if __name__ == '__main__':
while True:
main()