forked from BrucesHobbies/radonMaster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwave.py
383 lines (286 loc) · 12.3 KB
/
wave.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#!/usr/bin/env python
# Must be run as super user for bluepy lib to have proper permissions:
# sudo python3 radonMaster.py
# -or- as standalone...
# sudo python3 wave.py
"""
Copyright(C) 2020, BrucesHobbies
All Rights Reserved
AUTHOR: BrucesHobbies
DATE: 12/1/2020
REVISION HISTORY
DATE AUTHOR CHANGES
yyyy/mm/dd --------------- -------------------------------------
2021/03/01 BrucesHobbies Modified for pubScribe.py
OVERVIEW:
Tested with Airthings WavePlus which reports:
- Radon short-term average
- Radon long-term average
- Volatile Organic Compounds (VOC)
- Carbon Dioxide (CO2)
- Temperature
- Relative Humidity (%rH)
- Air pressure
Airthings Wave is slightly different from WavePlus and will require code updates.
See read_wave for SI units flags instead of USA units (pCi/L, deg F, inches Hg).
LICENSE:
This program code and documentation are for personal private use only.
No commercial use of this code is allowed without prior written consent.
This program is free for you to inspect, study, and modify for your
personal private use.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 3 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
import sys
import time
import os
import pubScribe
MCP4725_ENABLED = 0
if MCP4725_ENABLED :
import mcp4725
dac = mcp4725.mcp4725()
# initialize bus
# i2c_ch = 1 # i2c channel
# dac.bus=smbus.SMBus(i2c_ch) # Initialize I2C (SMBus)
SerialNumber = 0
SamplePeriod = 60 # 60 Seconds, only used when run as main()
MODE='terminal'
LOGGING_ENABLED = 1
#
# find_wave and read_waveplus both require bluepy, SerialNumber==0 indicates bluepy lib or wave not found
#
import find_wave2c
if not SerialNumber :
SerialNumber = find_wave2c.findWave()
# If SerialNumber == 0 then don't attempt to read_waveplus
if SerialNumber :
import read_waveplus2c
#
# Alert enables (0 = alert disabled)
#
radonAlertEnabled = 1
vocAlertEnabled = 1
co2AlertEnabled = 1
tempAlertEnabled = 1
humidityAlertEnabled = 1
"""
#
# Airthings brackets with my summary descriptions
#
radonBrackets = ((4.0,"Extreme"),(2.7,"Very high"),(1.4,"High"),(0.0,"Good"))
vocBrackets = ((2000.0,"Extreme"),(250.0,"High"),(0.0,"Good"))
co2Brackets = ((2000.0,"Extreme"),(1500.0,"Very high"),(1000.0,"High"),(400.0,"Average"),(0.0,"Better than average"))
#humidityBrackets are a separate function
tempBrackets = ((77.0,"Red"),(64.0,"Green"),(-99.9,"Blue"))
"""
#
# Airthings updated notification brackets or create your own...
#
radonBrackets = ((4.0,"Red"),(2.7,"Yellow"),(0.0,"Green"))
vocBrackets = ((2000.0,"Red"),(250.0,"Yellow"),(0.0,"Green"))
co2Brackets = ((2000.0,"Red"),(800.0,"Yellow"),(250.0,"Poor"),(0.0,"Good"))
#humidityBrackets are a separate function
tempBrackets = ((77.0,"Red"),(64.0,"Green"),(-99.9,"Blue"))
#
# Alert message throttling variables
#
radonAlertTime = [0] * len(radonBrackets)
vocAlertTime = [0] * len(vocBrackets)
co2AlertTime = [0] * len(co2Brackets)
tempAlertTime = [0] * len(tempBrackets)
humidityAlertTime = [0] * 3
#
THROTTLE_TIME = 24*60*60 # once a day in seconds
def compareValue(value, brackets) :
result = ""
for i in range(len(brackets)) :
if value >= brackets[i][0] :
result = brackets[i][1]
break
return i, result
def compareRH(percentage) :
# percentage is number between 0 and 100
if (percentage>70) or (percentage<25) :
result = "Red"
i=0
elif (percentage>60) or (percentage<30) :
result = "Yellow"
i=1
else :
result = "Good"
i=2
return i, result
def checkAlerts(radonValue, vocValue, co2Value, tempValue, rhValue) :
global radonAlertTime, vocAlertTime, co2AlertTime, tempAlertTime, humidityAlertTime
alert = 0
s = ""
tsec = time.time()
if radonAlertEnabled :
i, result = compareValue(radonValue, radonBrackets)
s = s + "Radon: " + result + "\n"
if i==len(radonAlertTime)-1 :
if radonAlertTime[i]==0 : # send alert once on transition into good bracket
radonAlertTime[i] = tsec
alert = 1
elif (tsec > radonAlertTime[i]+THROTTLE_TIME) :
radonAlertTime[i] = tsec
alert = 1
radonAlertTime[len(radonAlertTime)-1] = 0 # reset
if vocAlertEnabled :
i, result = compareValue(vocValue, vocBrackets)
s = s+ "VOC : " + result + "\n"
if i==len(vocAlertTime)-1 :
if vocAlertTime[i]==0 : # send alert once on transition into good bracket
vocAlertTime[i] = tsec
alert = 1
elif (tsec > vocAlertTime[i]+THROTTLE_TIME) :
vocAlertTime[i] = tsec
alert = 1
vocAlertTime[len(vocAlertTime)-1] = 0 # reset
if co2AlertEnabled :
i, result = compareValue(co2Value, co2Brackets)
s = s + "CO2 : " + result + "\n"
if i==len(co2AlertTime)-1 :
if co2AlertTime[i]==0 : # send alert once on transition into good bracket
co2AlertTime[i] = tsec
alert = 1
elif (tsec > co2AlertTime[i]+THROTTLE_TIME) :
co2AlertTime[i] = tsec
alert = 1
co2AlertTime[len(co2AlertTime)-1] = 0 # reset
if tempAlertEnabled :
i, result = compareValue(tempValue, tempBrackets)
s = s + "Temp : " + result + "\n"
if i==1 :
if tempAlertTime[i]==0 : # send alert once on transition into good bracket
tempAlertTime[i] = tsec
alert = 1
elif (tsec > tempAlertTime[i]+THROTTLE_TIME) :
tempAlertTime[i] = tsec
alert = 1
tempAlertTime[len(tempAlertTime)-1] = 0 # reset
if humidityAlertEnabled :
i, result = compareRH(rhValue)
s = s + "RH : " + result + "\n"
if i==len(humidityAlertTime)-1 :
if humidityAlertTime[i]==0 : # send alert once on transition into good bracket
humidityAlertTime[i] = tsec
alert = 1
elif (tsec > humidityAlertTime[i]+THROTTLE_TIME) :
humidityAlertTime[i] = tsec
alert = 1
humidityAlertTime[len(humidityAlertTime)-1] = 0 # reset
"""
if (MODE=='terminal'):
print(s)
"""
return alert, s
def sensor2StringUnits(sensors) :
s = ""
humidity = str(sensors.getValue(read_waveplus2c.SENSOR_IDX_HUMIDITY)) + " " + str(sensors.getUnit(read_waveplus2c.SENSOR_IDX_HUMIDITY))
radon_st_avg = str(sensors.getValue(read_waveplus2c.SENSOR_IDX_RADON_SHORT_TERM_AVG)) + " " + str(sensors.getUnit(read_waveplus2c.SENSOR_IDX_RADON_SHORT_TERM_AVG))
radon_lt_avg = str(sensors.getValue(read_waveplus2c.SENSOR_IDX_RADON_LONG_TERM_AVG)) + " " + str(sensors.getUnit(read_waveplus2c.SENSOR_IDX_RADON_LONG_TERM_AVG))
temperature = str(sensors.getValue(read_waveplus2c.SENSOR_IDX_TEMPERATURE)) + " " + str(sensors.getUnit(read_waveplus2c.SENSOR_IDX_TEMPERATURE))
pressure = str(sensors.getValue(read_waveplus2c.SENSOR_IDX_REL_ATM_PRESSURE)) + " " + str(sensors.getUnit(read_waveplus2c.SENSOR_IDX_REL_ATM_PRESSURE))
CO2_lvl = str(sensors.getValue(read_waveplus2c.SENSOR_IDX_CO2_LVL)) + " " + str(sensors.getUnit(read_waveplus2c.SENSOR_IDX_CO2_LVL))
VOC_lvl = str(sensors.getValue(read_waveplus2c.SENSOR_IDX_VOC_LVL)) + " " + str(sensors.getUnit(read_waveplus2c.SENSOR_IDX_VOC_LVL))
data = [radon_st_avg, radon_lt_avg, VOC_lvl, CO2_lvl, temperature, humidity, pressure]
return data
hdrRow = ""
#
# Log wave data to csv file
#
def write2Csv(s) :
csvFile = open(wpLogFilename, "a")
csvFile.write(s + "\n")
csvFile.close()
def formatLocalTime() :
# timeStr = str(datetime.datetime.now()) # yyyy-mm-dd hh:mm:ss.ssssss
# return timeStr[:-7]
return time.strftime("%a, %Y-%b-%d, %H:%M:%S", time.localtime()) #%b=abbr mo, %B=mo name, %m=m as decimal
#
# Header row for log file or display
#
def writeHeaders() :
global hdrRow
sensors = read_waveplus2c.Sensors()
hdrRow = "Radon ST avg (" + str(sensors.getUnit(read_waveplus2c.SENSOR_IDX_RADON_SHORT_TERM_AVG)) \
+ "),Radon LT avg (" + str(sensors.getUnit(read_waveplus2c.SENSOR_IDX_RADON_LONG_TERM_AVG)) \
+ "),VOC level (" + str(sensors.getUnit(read_waveplus2c.SENSOR_IDX_VOC_LVL)) \
+ "),CO2 level (" + str(sensors.getUnit(read_waveplus2c.SENSOR_IDX_CO2_LVL)) \
+ "),Temperature (" + str(sensors.getUnit(read_waveplus2c.SENSOR_IDX_TEMPERATURE)) \
+ "),Humidity (" + str(sensors.getUnit(read_waveplus2c.SENSOR_IDX_HUMIDITY)) \
+ "),Pressure (" + str(sensors.getUnit(read_waveplus2c.SENSOR_IDX_REL_ATM_PRESSURE)) + ")"
if MODE=='terminal' :
# print(hdrRow[5:])
print(hdrRow)
#
# Read AirThings Waveplus, log data to csv file
#
msgOnce = 1
def readAirthings() :
global msgOnce
results = ""
alert = 0
s = ""
if not SerialNumber :
if msgOnce :
msgonce = 0
print("Wave not found! Trying manually entering serial number into code instead of scanning.")
return
try:
waveplus = read_waveplus2c.WavePlus(SerialNumber)
waveplus.connect()
sensors = waveplus.read()
waveplus.disconnect()
# extract
humidity = sensors.getValue(read_waveplus2c.SENSOR_IDX_HUMIDITY)
radon_st_avg = sensors.getValue(read_waveplus2c.SENSOR_IDX_RADON_SHORT_TERM_AVG)
radon_lt_avg = sensors.getValue(read_waveplus2c.SENSOR_IDX_RADON_LONG_TERM_AVG)
temperature = sensors.getValue(read_waveplus2c.SENSOR_IDX_TEMPERATURE)
pressure = sensors.getValue(read_waveplus2c.SENSOR_IDX_REL_ATM_PRESSURE)
CO2_lvl = sensors.getValue(read_waveplus2c.SENSOR_IDX_CO2_LVL)
VOC_lvl = sensors.getValue(read_waveplus2c.SENSOR_IDX_VOC_LVL)
if LOGGING_ENABLED :
data = [radon_st_avg, radon_lt_avg, VOC_lvl, CO2_lvl, temperature, humidity, pressure]
topic = "RadonMaster/WavePlus"
pubScribe.pubRecord(pubScribe.CSV_FILE, topic, data, hdrRow)
results = formatLocalTime() + " " + str(sensor2StringUnits(sensors))
alert, s = checkAlerts(radon_st_avg, VOC_lvl, CO2_lvl, temperature, humidity)
if MCP4725_ENABLED :
fanValue = dac.alg(data)
# print("FanValue: ", fanValue)
topic = "RadonMaster/Fan"
hdr = "FanSetting"
pubScribe.pubRecord(pubScribe.CSV_FILE, topic, fanValue, hdr)
except :
print("Exception!!!")
waveplus.disconnect()
return results, alert, s
if __name__ == '__main__':
if (MODE=='terminal'):
print("\nPress ctrl+C to exit program\n")
print("Device serial number: %s" %(SerialNumber))
print("Sample Period: %u seconds" %(SamplePeriod))
print("")
pubScribe.connectPubScribe()
writeHeaders()
try :
while True:
results, alert, s = readAirthings()
if (MODE=='terminal'):
print(results)
if alert :
print("=== ALERT! ===")
print(s)
time.sleep(SamplePeriod)
except KeyboardInterrupt:
print(" Keyboard interrupt caught.")
pubScribe.disconnectPubScribe()