-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmh_z19.py
29 lines (24 loc) · 850 Bytes
/
mh_z19.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
# mh_z19.py
import struct
import utime
from machine import UART
class MHZ19:
"""
MH-Z19C CO2 Sensor Driver
Args:
uart_id (int): UART identifier (e.g., 1 for UART1)
uart (UART): UART object
"""
def __init__(self, uart_id=1, uart=None):
self.uart = uart or UART(uart_id, baudrate=9600, tx=4, rx=5)
utime.sleep(1) # Allow sensor to initialize
def read(self):
# Example implementation; modify based on your sensor's protocol
self.uart.write(b'\xff\x01\x86\x00\x00\x00\x00\x00\x79')
utime.sleep(0.1)
response = self.uart.read()
if response and len(response) >= 9:
co2 = response[2] * 256 + response[3]
temp = response[4] - 40
return {'co2': co2, 'temperature': temp}
return {'co2': None, 'temperature': None}