-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemperature_maison.py
executable file
·81 lines (68 loc) · 2.19 KB
/
temperature_maison.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/python
import time, sys, signal, json, yaml, os, requests
from influxdb import InfluxDBClient
from influxdb.exceptions import InfluxDBClientError, InfluxDBServerError
from datetime import date, datetime
import subprocess
config = yaml.safe_load(open(os.path.dirname(sys.argv[0]) + "/config.yml"))
base_dir = '/sys/bus/w1/devices/'
sonde1 = base_dir + '28-0319a2790f6a/w1_slave'
sonde2 = base_dir + '28-0319a2792d11/w1_slave'
def read_temp_raw(sonde):
f = open(sonde, 'r')
lines = f.readlines()
f.close()
return lines
def read_temp(sonde):
lines = read_temp_raw(sonde)
while lines[0].strip()[-3:] != 'YES':
time.sleep(0.1)
lines = read_temp_raw(sonde)
equals_pos = lines[1].find('t=')
if equals_pos != -1:
temp_string = lines[1][equals_pos+2:]
temp_c = float(temp_string) / 1000.0
temp_round = round(temp_c,1)
return temp_round
def loop():
while True:
date=datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ')
t_sonde1 = read_temp(sonde1)
t_sonde2 = read_temp(sonde2)
print date
print("Sonde 1 : %.1f" % t_sonde1)
print("Sonde 2 : %.1f" % t_sonde2)
json_body = [
{
"measurement": "temperature",
"tags": {
"host": "sonde1"
},
"time": date,
"fields": { "value": t_sonde1
}
},
{
"measurement": "temperature",
"tags": {
"host": "sonde2"
},
"time": date,
"fields": { "value": t_sonde2
}
}
]
client = InfluxDBClient(config['influxdb']['host'], config['influxdb']['port'], config['influxdb']['user'], config['influxdb']['passwd'], config['influxdb']['db'], config['influxdb']['timeout'])
try:
client.write_points(json_body,time_precision='ms')
except (requests.exceptions.ConnectionError, requests.exceptions.ReadTimeout,
InfluxDBClientError, InfluxDBServerError) as err:
print("Oops ! connextion error !")
#result = client.query('select value from temperature;')
#print("Result: {0}".format(result))
time.sleep(5)
def signal_term_handler(signal, frame):
sys.exit(0)
if __name__ == '__main__':
signal.signal(signal.SIGTERM, signal_term_handler)
loop()