Last active
December 7, 2016 09:56
-
-
Save sourceperl/b75a5233d9264c5f4186fbdfa90233b9 to your computer and use it in GitHub Desktop.
Check a thingspeak feed is update
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 | |
| import dateutil.parser | |
| import datetime | |
| import time | |
| import json | |
| 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 | |
| utc = UTC() | |
| while True: | |
| connection = http.client.HTTPSConnection('api.thingspeak.com') | |
| headers = {'Content-type': 'application/json'} | |
| connection.request('GET', '/channels/181289/feeds/last.json', headers=headers) | |
| response = connection.getresponse().read() | |
| d = json.loads(response.decode()) | |
| now = datetime.datetime.now(utc) | |
| update = dateutil.parser.parse(d['created_at']) | |
| delta = now - update | |
| print('refresh %.0f s ago' % delta.total_seconds()) | |
| time.sleep(30.0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment