Self implemented co2 again
This commit is contained in:
parent
3f083c0aed
commit
5e673e0c0e
1 changed files with 36 additions and 4 deletions
|
@ -1,16 +1,48 @@
|
||||||
import mh_z19
|
import serial
|
||||||
import logging
|
|
||||||
|
|
||||||
|
|
||||||
class Mhz19Co2:
|
class Mhz19Co2:
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.last_read = None
|
self.last_read = None
|
||||||
|
|
||||||
|
self.serial_port = "/dev/serial0"
|
||||||
|
self.baud_rate = 9600
|
||||||
|
self.byte_size = 8
|
||||||
|
self.parity = "N"
|
||||||
|
self.stop_bits = 1
|
||||||
|
self.timeout = None
|
||||||
|
|
||||||
def get_last_read(self) -> int | None:
|
def get_last_read(self) -> int | None:
|
||||||
if self.last_read is None:
|
if self.last_read is None:
|
||||||
return self.read()
|
return self.read()
|
||||||
return self.last_read
|
return self.last_read
|
||||||
|
|
||||||
def read(self) -> int | None:
|
def read(self) -> int | None:
|
||||||
return mh_z19.read()["co2"]
|
ser = None
|
||||||
|
try:
|
||||||
|
ser = serial.Serial(
|
||||||
|
port=self.serial_port,
|
||||||
|
baudrate=self.baud_rate,
|
||||||
|
bytesize=self.byte_size,
|
||||||
|
parity=self.parity,
|
||||||
|
stopbits=self.stop_bits,
|
||||||
|
timeout=self.timeout,
|
||||||
|
)
|
||||||
|
|
||||||
|
# send "Read CO2" command
|
||||||
|
command_data = bytes([0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79])
|
||||||
|
ser.write(command_data)
|
||||||
|
|
||||||
|
# read "Return Value (CO2 concentration)"
|
||||||
|
data = ser.read(9)
|
||||||
|
concentration = data[2] * 256 + data[3]
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error reading data: {e}")
|
||||||
|
finally:
|
||||||
|
if ser:
|
||||||
|
ser.close()
|
||||||
|
ser = None
|
||||||
|
|
||||||
|
self.last_read = concentration
|
||||||
|
return concentration
|
||||||
|
|
Loading…
Reference in a new issue