-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiotsender.py
43 lines (34 loc) · 1.1 KB
/
iotsender.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
import requests
import json
import os
import time
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
temp_sensor = os.environ.get('SENSORPATH')
apikey = os.environ.get('IOTPLOTTERAPI')
iotplotterfeed = os.environ.get('IOTPLOTTERFEED')
iotplottersensor = os.environ.get('IOTPLOTTERSENSOR')
makewebhook = os.environ.get('MAKEWEBHOOK')
def temp_raw():
f = open(temp_sensor, 'r')
lines = f.readlines()
f.close()
return lines
def read_temp():
lines = temp_raw()
while lines[0].strip()[-3:] != 'YES':
time.sleep(0.2)
lines = temp_raw()
temp_output = lines[1].find('t=')
if temp_output != -1:
temp_string = lines[1].strip()[temp_output+2:]
temp_c = float(temp_string) / 1000.0
temp_f = (temp_c * (9.0/5.0)) + 32.0
return round(temp_f,1)
headers = {'api-key': apikey}
payload = {}
payload["data"] = {}
payload["data"][iotplottersensor] = []
payload["data"][iotplottersensor].append({"value":read_temp()})
requests.post(iotplotterfeed, headers=headers, data=json.dumps(payload))
#requests.post(makewebhook, data=json.dumps(payload))