-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtempclient.py
More file actions
35 lines (31 loc) · 782 Bytes
/
tempclient.py
File metadata and controls
35 lines (31 loc) · 782 Bytes
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
from urllib import request
import re
import time
def getTemp():
try:
tfile = open("/sys/bus/w1/devices/28-0316a0fc60ff/w1_slave")
except Exception as e:
print(e)
else:
text = tfile.read()
finally:
tfile.close()
temperaturedata = re.findall(' t\=(\S+)$', text)
temperature = float(temperaturedata[0]) / 1000
return temperature
apikey = '85b0c7f75ce26bdba4b5d16e64dd31d8'
url = 'http://api.yeelink.net/v1.0/device/355310/sensor/401857/datapoints'
req = request.Request(url)
req.add_header('U-ApiKey',apikey)
while True:
temperature = getTemp()
value = '{"value":%f}' % temperature
values = bytes(value.encode('utf8'))
try:
request.urlopen(req,data=values)
except Exception as e:
print('link error')
print(e)
else:
print('link succeed')
time.sleep(12)