-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery_data.py
executable file
·68 lines (54 loc) · 1.93 KB
/
query_data.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
#!/usr/bin/env python3
from btlewrap.bluepy import BluepyBackend
# from btlewrap import BluepyBackend
from miflora.miflora_poller import MiFloraPoller
from miflora.miflora_poller import (
MI_BATTERY,
MI_CONDUCTIVITY,
MI_LIGHT,
MI_MOISTURE,
MI_TEMPERATURE,
MiFloraPoller,
)
from influxdb import InfluxDBClient
import configparser
config = configparser.ConfigParser()
config.read('config.ini')
def poll(antenna):
"""Poll data from the sensor."""
# backend = _get_backend(args)
poller = MiFloraPoller(antenna[1], BluepyBackend, cache_timeout=60)
# print("Getting data from Mi Flora")
# print(f"FW: {poller.firmware_version()}")
# print(f"Name: {poller.name()}")
# print("Temperature: {}".format(poller.parameter_value(MI_TEMPERATURE)))
# print("Moisture: {}".format(poller.parameter_value(MI_MOISTURE)))
# print("Light: {}".format(poller.parameter_value(MI_LIGHT)))
# print("Conductivity: {}".format(poller.parameter_value(MI_CONDUCTIVITY)))
# print("Battery: {}".format(poller.parameter_value(MI_BATTERY)))
json_body = [
{
"measurement": antenna[0],
"fields":{
"temperature": poller.parameter_value(MI_TEMPERATURE),
"moisture": poller.parameter_value(MI_MOISTURE),
"light": poller.parameter_value(MI_LIGHT),
"conductivity": poller.parameter_value(MI_CONDUCTIVITY),
"battery": poller.parameter_value(MI_BATTERY)
}
}
]
print(json_body)
return json_body
def write_to_influx(json_body):
client = InfluxDBClient(host=config['Influx']['host'], port=int(config['Influx']['port']))
client.write_points(
json_body,
database = "home")
if __name__ == "__main__":
for i in config['Connector']:
try:
body = poll([i, config['Connector'][i]])
write_to_influx(body)
except:
pass