This repository has been archived by the owner on Jun 24, 2024. It is now read-only.
forked from alpacagh/MHZ14-CO2-Logger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CO2Reader.py
137 lines (115 loc) · 4.77 KB
/
CO2Reader.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
import serial
# Default for macOS, Linux is /dev/ttyUSB
defaultPort = '/dev/tty.SLAB_USBtoUART'
class MHZ14Reader:
"""
Simple sensor communication class.
Be _very_ careful using the zero (400ppm) calibration method providedto avoid accidental sensor misconfiguration!
https://www.google.com/#q=MH-Z14+datasheet+pdf
# Possible commands
# mhzCmdReadPPM[9] = [0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79]
# mhzCmdCalibrateZero[9] = [0xFF, 0x01, 0x87, 0x00, 0x00, 0x00, 0x00, 0x00, 0x78] # Run in 400ppm or less for 20mins
# mhzCmdABCEnable[9] = [0xFF, 0x01, 0x79, 0xA0, 0x00, 0x00, 0x00, 0x00, 0xE6]
# mhzCmdABCDisable[9] = [0xFF, 0x01, 0x79, 0x00, 0x00, 0x00, 0x00, 0x00, 0x86]
# mhzCmdReset[9] = [0xFF, 0x01, 0x8d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x72]
# mhzCmdMeasurementRange1000[9] = [0xFF, 0x01, 0x99, 0x00, 0x00, 0x00, 0x03, 0xE8, 0x7B]
# mhzCmdMeasurementRange2000[9] = [0xFF, 0x01, 0x99, 0x00, 0x00, 0x00, 0x07, 0xD0, 0x8F]
# mhzCmdMeasurementRange3000[9] = [0xFF, 0x01, 0x99, 0x00, 0x00, 0x00, 0x0B, 0xB8, 0xA3]
# mhzCmdMeasurementRange5000[9] = [0xFF, 0x01, 0x99, 0x00, 0x00, 0x00, 0x13, 0x88, 0xCB]
# Run Zero Point (400ppm) calibration, run this for 20mins + in <400ppm Environment
#_sensorCommand = [0xFF, 0x01, 0x87, 0x00, 0x00, 0x00, 0x00, 0x00, 0x78]
# Enable ABC, run with --single
#_sensorCommand = [0xFF, 0x01, 0x79, 0xA0, 0x00, 0x00, 0x00, 0x00, 0xE6]
# Disable ABC, run with --single
# _sensorCommand = [0xFF, 0x01, 0x79, 0x00, 0x00, 0x00, 0x00, 0x00, 0x86]
# Default operation, read Co2 levels
# _sensorCommand = [0xff, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79]
"""
def __init__(self, port, _sensorCommand, open_connection=True):
"""
:param string port: path to tty
:param bool open_connection: should port be opened immediately
"""
self._sensorCommand = _sensorCommand
self.port = port
"""TTY name"""
self.link = None
"""Connection with sensor"""
if open_connection:
self.connect()
def connect(self):
"""
Open tty connection to sensor
"""
if self.link is not None:
self.disconnect()
self.link = serial.Serial(self.port, 9600, bytesize=serial.EIGHTBITS,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE, dsrdtr=True,
timeout=5, interCharTimeout=0.1)
def disconnect(self):
"""
Terminate sensor connection
"""
if self.link:
self.link.close()
def _send_data_request(self):
"""
Send data request control sequence
"""
self.link.write(bytearray(self._sensorCommand))
def get_status(self):
"""
Read data from sensor
:return {ppa, t}|None:
"""
self._send_data_request()
response = self.link.read(9)
if len(response) == 9:
return {
"ppa": response[2] * 0xff + response[3],
"t": int((response[4]-32)*5/9), # Internal sensor temperature in Celsius
}
return None
if __name__ == "__main__":
import time
import sys
import argparse
parser = argparse.ArgumentParser(
description='Read data from MH-Z14 CO2 sensor.'
)
parser.add_argument('--tty', default=defaultPort, help='tty port to connect',
type=str, nargs='?')
parser.add_argument('--timeout', default=10, help='timeout between requests',
type=int, nargs='?')
parser.add_argument('--command', default='0xff, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79',
help='See readme, defaults to reading Co2 PPM', type=str, nargs='?')
parser.add_argument('--single', action='store_true', help='single measure')
parser.add_argument('--quit', '-q', action='store_true', help='Check sensor connectivity and quit')
args = parser.parse_args()
port = args.tty
timeout = args.timeout
_sensorCommand = bytearray([int(x, 16) for x in args.command.split(', ')])
if args.single:
timeout = 0
conn = MHZ14Reader(port, _sensorCommand)
if not args.quit:
sys.stderr.write("Connected to {}\n".format(conn.link.name))
while True:
status = conn.get_status()
if status:
print(
"{}\t{}\t{}".format(
time.strftime("%Y-%m-%d %H:%M:%S"),
status["ppa"],
status["t"]
)
)
else:
print("No data received")
sys.stdout.flush()
if timeout != 0:
time.sleep(timeout)
else:
break
conn.disconnect()