forked from stenjo/waveplus-reader
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathread_waveplus.py
More file actions
139 lines (114 loc) · 5.53 KB
/
read_waveplus.py
File metadata and controls
139 lines (114 loc) · 5.53 KB
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
# MIT License
#
# Copyright (c) 2018 Airthings AS
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
# https://airthings.com
# ===============================
# Module import dependencies
# ===============================
import sys
import time
import tableprint
from bluepy import btle
from Sensors import Sensors
from WavePlus import WavePlus
# ===============================
# Script guards for correct usage
# ===============================
def print_usage():
print ("USAGE: read_waveplus.py SN SAMPLE-PERIOD [pipe > yourfile.txt]")
print (" where SN is the 10-digit serial number found under the magnetic backplate of your Wave Plus.")
print (" where SAMPLE-PERIOD is the time in seconds between reading the current values.")
print (" where [pipe > yourfile.txt] is optional and specifies that you want to pipe your results to yourfile.txt.")
if len(sys.argv) < 3:
print ("ERROR: Missing input argument SN or SAMPLE-PERIOD.")
print_usage()
sys.exit(1)
if sys.argv[1].isdigit() is not True or len(sys.argv[1]) != 10:
print ("ERROR: Invalid SN format.")
print_usage()
sys.exit(1)
if sys.argv[2].isdigit() is not True or int(sys.argv[2]) < 0:
print ("ERROR: Invalid SAMPLE-PERIOD. Must be a numerical value larger than zero.")
print_usage()
sys.exit(1)
if len(sys.argv) > 3:
Mode = sys.argv[3].lower()
if Mode == 'mqtt':
Broker = sys.argv[4]
else:
Broker = None
else:
Mode = 'terminal' # (default) print to terminal
if Mode!='pipe' and Mode!='terminal':
print ("ERROR: Invalid piping method.")
print_usage()
sys.exit(1)
SerialNumber = int(sys.argv[1])
SamplePeriod = int(sys.argv[2])
try:
#---- Initialize ----#
waveplus = WavePlus(SerialNumber)
if (Mode=='terminal'):
print ("\nPress CTRL+C to exit program\n")
header = ['TimeStamp', 'Humidity', 'Radon ST avg', 'Radon LT avg', 'Temperature', 'Pressure', 'CO2 level', 'VOC level', 'Battery']
print ("Device serial number: %s" %(SerialNumber))
if (Mode=='terminal'):
print (tableprint.header(header, width=12))
elif (Mode=='pipe'):
print (header)
while True:
sensors = None
try:
waveplus.connect()
# read values
sensors = waveplus.read()
# extract
timestamp = time.strftime('%m/%d..%H:%M')
humidity = str(sensors.getValue(sensors.SENSOR_IDX_HUMIDITY)) + " " + str(sensors.getUnit(sensors.SENSOR_IDX_HUMIDITY))
radon_st_avg = str(sensors.getValue(sensors.SENSOR_IDX_RADON_SHORT_TERM_AVG)) + " " + str(sensors.getUnit(sensors.SENSOR_IDX_RADON_SHORT_TERM_AVG))
radon_lt_avg = str(sensors.getValue(sensors.SENSOR_IDX_RADON_LONG_TERM_AVG)) + " " + str(sensors.getUnit(sensors.SENSOR_IDX_RADON_LONG_TERM_AVG))
temperature = str(sensors.getValue(sensors.SENSOR_IDX_TEMPERATURE)) + " " + str(sensors.getUnit(sensors.SENSOR_IDX_TEMPERATURE))
pressure = str(sensors.getValue(sensors.SENSOR_IDX_REL_ATM_PRESSURE)) + " " + str(sensors.getUnit(sensors.SENSOR_IDX_REL_ATM_PRESSURE))
CO2_lvl = str(sensors.getValue(sensors.SENSOR_IDX_CO2_LVL)) + " " + str(sensors.getUnit(sensors.SENSOR_IDX_CO2_LVL))
VOC_lvl = str(sensors.getValue(sensors.SENSOR_IDX_VOC_LVL)) + " " + str(sensors.getUnit(sensors.SENSOR_IDX_VOC_LVL))
battery = str(sensors.getValue(sensors.SENSOR_IDX_BATTERY)) + " " + str(sensors.getUnit(sensors.SENSOR_IDX_BATTERY))
# Print data
data = [timestamp, humidity, radon_st_avg, radon_lt_avg, temperature, pressure, CO2_lvl, VOC_lvl, battery]
if (Mode=='terminal'):
print (tableprint.row(data, width=12))
elif (Mode=='pipe'):
print (data)
waveplus.disconnect()
time.sleep(SamplePeriod)
except btle.BTLEException as ex:
timestamp = time.strftime('%m/%d..%H:%M')
errormsg = " ERROR: Failed to connect to the AirThings Wave Plus sensor, will try again on the next cycle..."
if (Mode=='terminal'):
#print (tableprint.bottom(9, width=12))
print (u'\u2502' + " " + timestamp + " " + u'\u2502' + errormsg + " " + u'\u2502')
#print (tableprint.header(header, width=12))
elif (Mode=='pipe'):
print (timestamp + errormsg)
time.sleep(SamplePeriod)
continue
finally:
waveplus.disconnect()