Python async library to interface with the MH-Z19* sensors for CO2.
Project description
mhz19_async
Python library for communicating with the MH-Z19x sensors for CO2 with asyncio.
This sensor is very badly documented and there are conflicting reports on its working. To make things worse, several hardware versions exist (including knockoffs), and there are also different firmware versions. Testing was done on an MH-Z19B sensor (green PCB) that reports firmware version 0502. Only the commands that I could test and verify personally are implemented. Several other commands were tried (including the unlimited/raw CO2 readings, "background" and calibration data readings), but results are inconsistent with docs or other implementations, and they are left out for now.
Here is a list of references used to implement the protocol:
- some datasheets, include operational limits
- reverse engineering of the MH-Z19 and MH-Z19B sensors, quite thorough
- https://github.com/WifWaf/MH-Z19: reference Arduino library, several inconsistencies with the above reverse engineering
- raw braindump of the author of the Arduino library
- another blog post in Russian that sheds some light on CO2 readings and the MODBUS protocol.
Please note that it is unclear whether you can "brick" the sensor, or whether you can fix calibration values once they are messed up.
Features
This library provides
- a
class MHZ19Protocol(asyncio.Protocol)
to speak the protocol of the sensor - an executable that reads commands and prints results in json format.
Implemented commands documented in source, as well as response parsing.
__main__.py
is your reference for
using the library, it is very simple.
Contrary to most implementations, this library makes no attempt to match input commands to responses from the sensor. This is because the input and output dataframes do not have identifiers that allow a reliable match between requests and responses. The intended use case is to send periodically CO2 read requests, in order to get readings at a user-defined time interval.
Command line operation with json
The module can be run as an executable. It reads commands from stdin and prints data from the sensor, one json per line.
export DEVICE=/dev/ttyUSB0
python -m mhz19_async "${DEVICE}"
# pipe commands to stdin, receive outputs in stdout
Input json fields:
command
(required): enum name of the command, or raw intargs
: scalar or list of scalars (depends on command)raw_args
: list of 5 byte arguments to be appended as-is to the command (for debugging and hacking).
Output json fields:
command
: enum name of the received command, or raw int if not recognizedraw
: raw hex string of incoming arguments (6 bytes)- if parsing is implemented, fields are extracted from the raw data and made available in additional fields
error
: error message generated during parsing.
Protocol examples (input json followed by output json):
{"command": "GET_CO2"}
{"command": "GET_CO2", "raw": "06DC01A905E6", "CO2": 425}
{"command": "GET_CLAMPED_CO2_TEMPERATURE"}
{"command": "GET_CLAMPED_CO2_TEMPERATURE", "raw": "03553B000000", "CO2": 853, "temperature": 19}
{"command": "SET_ABC", "args": false}
{"command": "SET_ABC", "raw": "010000000000", "ack": true}
{"command": "GET_FIRMWARE_VERSION"}
{"command": "GET_FIRMWARE_VERSION", "raw": "303530320000", "version": "0502"}
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file pymhz19_async-0.2.0.tar.gz
.
File metadata
- Download URL: pymhz19_async-0.2.0.tar.gz
- Upload date:
- Size: 7.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.11.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a0756ca26dc10e424b07f257762a7739171cae9dab015f08024d03fede1e5ea6 |
|
MD5 | e068ffebc2b0398170d96acde487f24b |
|
BLAKE2b-256 | 4e0c77367d123139f7d4df27b42f15ce411bea4a95e6bb722e44b71e0c832461 |
File details
Details for the file pymhz19_async-0.2.0-py3-none-any.whl
.
File metadata
- Download URL: pymhz19_async-0.2.0-py3-none-any.whl
- Upload date:
- Size: 7.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.11.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8d4f69f861a5a83dd516ade70db9eae6b3ca43df63127295d00598e9cc3192e6 |
|
MD5 | 934958292b5a1acf30776e12aaf49e61 |
|
BLAKE2b-256 | 024e091812140e448097fc2399f3a356d422ad6fbaa0037395c0d782193f344d |