forked from ayeks/bme680_to_influxdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
senddata.py
209 lines (175 loc) · 6.68 KB
/
senddata.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#!/usr/bin/env python
import time # for time and delay
import sys # for arguments
import datetime # for time and delay
from influxdb import InfluxDBClient # for collecting data
import socket # for hostname
import bme680 # for sensor data
import configparser # for parsing config.ini file
def get_raspid():
# Extract serial from cpuinfo file
cpuserial = "0000000000000000"
with open('/proc/cpuinfo', 'r') as f:
for line in f:
if line[0:6] == 'Serial':
cpuserial = line[10:26]
return cpuserial
# Allow user to set session and runno via args otherwise auto-generate
if len(sys.argv) is 2:
configpath = sys.argv[1]
else:
print("ParameterError: You must define the path to the config.ini!")
sys.exit()
# Parsing the config parameters from config.ini
config = configparser.ConfigParser()
try:
config.read(configpath)
influxserver = config['influxserver']
host = influxserver.get('host')
port = influxserver.get('port')
user = influxserver.get('user')
password = influxserver.get('password')
dbname = influxserver.get('dbname')
sensor = config['sensor']
enable_gas = sensor.getboolean('enable_gas')
session = sensor.get('session')
location = sensor.get('location')
temp_offset = float(sensor['temp_offset'])
interval = int(sensor['interval'])
burn_in_time = float(sensor['burn_in_time'])
except TypeError:
print("TypeError parsing config.ini file. Check boolean datatypes!")
sys.exit()
except KeyError:
print("KeyError parsing config.ini file. Check file and its structure!")
sys.exit()
except ValueError:
print("ValueError parsing config.ini file. Check number datatypes!")
sys.exit()
sensor = bme680.BME680()
raspid = get_raspid()
now = datetime.datetime.now()
runNo = now.strftime("%Y%m%d%H%M")
hostname = socket.gethostname()
print("Session: ", session)
print("runNo: ", runNo)
print("raspid: ", raspid)
print("hostname: ", hostname)
print("location: ", location)
# Create the InfluxDB object
client = InfluxDBClient(host, port, user, password, dbname)
# BME680 configuration
# Set sensor configs
sensor.set_humidity_oversample(bme680.OS_2X)
sensor.set_pressure_oversample(bme680.OS_4X)
sensor.set_temperature_oversample(bme680.OS_8X)
sensor.set_filter(bme680.FILTER_SIZE_3)
sensor.set_temp_offset(temp_offset)
if enable_gas:
sensor.set_gas_status(bme680.ENABLE_GAS_MEAS)
sensor.set_gas_heater_temperature(320)
sensor.set_gas_heater_duration(150)
sensor.select_gas_heater_profile(0)
else:
sensor.set_gas_status(bme680.DISABLE_GAS_MEAS)
# start_time and curr_time ensure that the
# burn_in_time (in seconds) is kept track of.
start_time = time.time()
curr_time = time.time()
burn_in_data = []
# Run until keyboard out
try:
# Collect gas resistance burn-in values, then use the average
# of the last 50 values to set the upper limit for calculating
# gas_baseline.
if enable_gas:
print("Collecting gas resistance burn-in data\n")
while curr_time - start_time < burn_in_time:
curr_time = time.time()
if sensor.get_sensor_data() and sensor.data.heat_stable:
gas = sensor.data.gas_resistance
burn_in_data.append(gas)
print("Gas: {0} Ohms".format(gas))
time.sleep(1)
gas_baseline = int(sum(burn_in_data[-50:]) / 50.0)
# Set the humidity baseline to 40%, an optimal indoor humidity.
hum_baseline = 40.0
# This sets the balance between humidity and gas reading in the
# calculation of air_quality_score (25:75, humidity:gas)
hum_weighting = 0.25
print("Gas baseline: {0} Ohms, humidity baseline: {1:.2f} %RH\n".format(gas_baseline, hum_baseline))
# Sensor read loop
while True:
if sensor.get_sensor_data():
hum = sensor.data.humidity
temp = sensor.data.temperature
press = sensor.data.pressure
iso = time.ctime()
if enable_gas:
hum_offset = hum - hum_baseline
gas = int(sensor.data.gas_resistance)
gas_offset = gas_baseline - gas
# Calculate hum_score as the distance from the hum_baseline.
if hum_offset > 0:
hum_score = (100 - hum_baseline - hum_offset) / (100 - hum_baseline) * (hum_weighting * 100)
else:
hum_score = (hum_baseline + hum_offset) / hum_baseline * (hum_weighting * 100)
# Calculate gas_score as the distance from the gas_baseline.
if gas_offset > 0:
gas_score = (gas / gas_baseline) * (100 - (hum_weighting * 100))
else:
gas_score = 100 - (hum_weighting * 100)
# Calculate air_quality_score.
air_quality_score = hum_score + gas_score
# Round to full
air_quality_score = round(air_quality_score, 0)
json_body = [
{
"measurement": session,
"tags": {
"run": runNo,
"raspid": raspid,
"hostname": hostname,
"location": location
},
"time": iso,
"fields": {
"temp": temp,
"press": press,
"humi": hum,
"gas": gas,
"iaq": air_quality_score,
"gasbaseline": gas_baseline,
"humbaseline": hum_baseline
}
}
]
else:
json_body = [
{
"measurement": session,
"tags": {
"run": runNo,
"raspid": raspid,
"hostname": hostname,
"location": location
},
"time": iso,
"fields": {
"temp": temp,
"press": press,
"humi": hum,
}
}
]
print(json_body)
# Write JSON to InfluxDB
res = client.write_points(json_body)
print(res)
else:
print("Error: .get_sensor_data() or heat_stable failed.")
break
# Wait for next sample
time.sleep(interval)
except KeyboardInterrupt:
pass