-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogtemperatures.py
executable file
·83 lines (67 loc) · 1.76 KB
/
logtemperatures.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
82
#!/usr/bin/env python
import datetime
import time
import requests
from influxdb import InfluxDBClient
influx_host = '127.0.0.1'
influx_port = 8086
influx_user = 'root'
influx_passwd = 'root'
influx_dbname = 'temperatures'
log_interval = 1
influx_client = InfluxDBClient(influx_host, influx_port, influx_user, influx_passwd, influx_dbname)
try_start=time.time()
while True:
try:
influx_client.create_database(influx_dbname)
break
except:
if time.time() - try_start > 30:
raise
ow_url = 'http://localhost:2121/text/uncached/'
def get_sensors(sensor_type='DS18B20'):
device_list = requests.get(ow_url).content.split('\r\n')
device_dict = {}
for device in device_list:
if not device:
continue
name, name2, entry_type = device.split()
device_details = requests.get(ow_url+name).content.split('\r\n')
sensor_dict = {}
for dd in device_details:
if not dd:
continue
l = dd.split()
if len(l) == 2:
sensor_dict[l[0]] = l[1]
elif len(l) == 1:
sensor_dict[l[0]] = ""
else:
pass
sensor_dict["time"] = datetime.datetime.utcnow().isoformat() + 'Z'
if 'type' in sensor_dict and sensor_dict['type'] == sensor_type:
device_dict[name] = sensor_dict
return device_dict
try:
while True:
json_body = []
sensors = get_sensors()
for name, sensor in sensors.items():
json_body.append(
{
"measurement": "temperature",
"tags": {
"sensorid": sensor['id'],
"alias": sensor['alias'] if sensor['alias'] else sensor['id']
},
"time": sensor['time'],
"fields": {
"value": float(sensor['temperature'])
}
}
)
print(json_body)
influx_client.write_points(json_body)
time.sleep(log_interval)
except KeyboardInterrupt:
print('Exiting')