Skip to main content

MH-Z19B CO2 sensor communication via serial port

Project description

This package is ment to be used to read data from MH_Z19B CO2 Sensor via serial. Sensor can be bought on AliExpress for around 18€ (Jan 2019)

It is recommended to disable Auto calibration and set measuring range to 5000 to get adequate readings after first day of use.

Requirements:

You need a USB to TTL adapter to read data from the sensor. I use Prolific Technology PL2303 adapter plugged into my router. Router runs a python script with this library that reads metrics periodically and sends it via MQTT.

Python 3.5+

Installation:

pip3 install pymh-z19b-serial

Library Usage examples:

It is possible to use sync and async way to read from the sensor

Sync

Uses pyserial

from mh_z19b.sync import Sensor

sensor = Sensor(port='/dev/cu.usbserial', baudrate=9600)
print(sensor.read_metric())

Async

Uses asyncio and pyserial-asyncio

import asyncio

from mh_z19b.async import Sensor

async def run(loop):
    sensor = Sensor(loop)
    await sensor.open_serial(url='/dev/cu.usbserial', baudrate=9600)
    data = await sensor.read_metric()
    print(data)

loop = asyncio.get_event_loop()
loop.set_debug(True)
loop.run_until_complete(run(loop))
loop.close()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pymh-z19b-serial-1.2.1.tar.gz (4.2 kB view details)

Uploaded Source

File details

Details for the file pymh-z19b-serial-1.2.1.tar.gz.

File metadata

  • Download URL: pymh-z19b-serial-1.2.1.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.29.1 CPython/3.6.3

File hashes

Hashes for pymh-z19b-serial-1.2.1.tar.gz
Algorithm Hash digest
SHA256 80cb2769f56e8701449a456adac64ce36a3e88e845d0e9cefbf89080b4eb93db
MD5 1a30bc47750c4ff5425cc236ccec3f20
BLAKE2b-256 4ab54061497e775d670e79e5f741485df5c1303972de35dcc6d02acf90bd0fdd

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page