-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy paththingspeak.py
29 lines (26 loc) · 1.07 KB
/
thingspeak.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
"""Thingspeak upload."""
from retrier import retry
import requests
from file_logger import File
import watcher
import secrets
import clock
_URL = (
'https://api.thingspeak.com/update.json'
'?api_key={key}&field1={f1}&field2={f2}&field3={f3}&field4={f4}'
'&field5={volts}&field6={status}')
@retry(Exception, tries=5, delay=2, backoff=2.0, logger=File.logger())
def send(rain_data, battery_volts):
"""Send weather and system information to Thingspeak."""
watcher.feed()
data = rain_data.get_data()
url = _URL.format(key=secrets.THINGSPEAK_API_KEY,
f1=data[0], f2=data[1], f3=data[2], f4=data[3],
volts=battery_volts,
status=int(not rain_data.rainfall_occurring()))
File.logger().info('%s - Req to: %s', clock.timestamp(), url)
with requests.get(url) as response:
File.logger().info('%s - HTTP status: %d', clock.timestamp(),
response.status_code)
if response.status_code != 200:
raise ValueError("HTTP status %d" % response.status_code)