Created
May 26, 2017 07:43
-
-
Save sourceperl/b82b18b80260de3ade04c0017555036d to your computer and use it in GitHub Desktop.
Display refresh time of a thingspeak feed on a Rpi sense hat 8x8 LED matrix
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 python3 | |
import http.client | |
from sense_hat import SenseHat | |
import dateutil.parser | |
import datetime | |
import schedule | |
import time | |
import threading | |
import json | |
# some const | |
RED = (255, 0, 0) | |
GREEN = (0, 255, 0) | |
BLUE = (0, 0, 255) | |
WHITE = (255, 255, 255) | |
ZERO = datetime.timedelta(0) | |
class UTC(datetime.tzinfo): | |
def utcoffset(self, dt): | |
return ZERO | |
def tzname(self, dt): | |
return "UTC" | |
def dst(self, dt): | |
return ZERO | |
def job_check(): | |
global is_ok, sec | |
connection = http.client.HTTPSConnection('api.thingspeak.com', timeout=5.0) | |
headers = {'Content-type': 'application/json'} | |
try: | |
connection.request('GET', '/channels/181289/feeds/last.json', headers=headers) | |
response = connection.getresponse().read().decode() | |
if response != '-1': | |
d = json.loads(response) | |
now = datetime.datetime.now(utc) | |
update = dateutil.parser.parse(d['created_at']) | |
delta = abs(now - update) | |
sec = int(delta.total_seconds()) | |
is_ok = (sec < 5*60) | |
except (socket.error, HTTPException): | |
print('HTTP error') | |
except: | |
print('Misc error') | |
def th_display(): | |
#init | |
sense = SenseHat() | |
sense.rotation = 0 | |
sense.low_light=True | |
sense.clear() | |
# loop | |
while True: | |
if is_ok: | |
sense.show_message('OK %ss' % str(sec), text_colour=GREEN) | |
else: | |
sense.show_message('ERR %ss' % str(sec), text_colour=RED) | |
time.sleep(.5) | |
# init | |
utc = UTC() | |
is_ok = True | |
sec = 0 | |
threading.Thread(target=th_display, daemon=True).start() | |
schedule.every(15).seconds.do(job_check) | |
# loop | |
while True: | |
schedule.run_pending() | |
time.sleep(0.1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment