Code Monkey home page Code Monkey logo

mh-z19's Introduction

mh-z19

Read CO2 concentration from mh-z19 sensor and handle it.

MH-Z19

cabling

Connect RPi & mh-z19 as:

  • 5V on RPi and Vin on mh-z19
  • GND(0v) on RPi and GND on mh-z19
  • TxD and RxD are connected to cross between RPi and mh-z18

Followings are example of cabling, but you can free to use other 5v and 0v Pin on the RPi.

Cabling

pi@raspberrypi:~/mh-z19 $ gpio readall
 +-----+-----+---------+------+---+---Pi B+--+---+------+---------+-----+-----+
 | BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
 +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
 |     |     |    3.3v |      |   |  1 || 2  |   |      | 5v      |     |     |
 |   2 |   8 |   SDA.1 |   IN | 1 |  3 || 4  |   |      | 5v      |     |     |  <---- Vin
 |   3 |   9 |   SCL.1 |   IN | 1 |  5 || 6  |   |      | 0v      |     |     |  <---- Gnd
 |   4 |   7 | GPIO. 7 |   IN | 1 |  7 || 8  | 1 | ALT0 | TxD     | 15  | 14  |  <---- RxD
 |     |     |      0v |      |   |  9 || 10 | 1 | ALT0 | RxD     | 16  | 15  |  <---- TxD
 |  17 |   0 | GPIO. 0 |   IN | 0 | 11 || 12 | 0 | IN   | GPIO. 1 | 1   | 18  |
 |  27 |   2 | GPIO. 2 |   IN | 0 | 13 || 14 |   |      | 0v      |     |     |
 |  22 |   3 | GPIO. 3 |   IN | 0 | 15 || 16 | 0 | IN   | GPIO. 4 | 4   | 23  |
 |     |     |    3.3v |      |   | 17 || 18 | 0 | IN   | GPIO. 5 | 5   | 24  |
 |  10 |  12 |    MOSI |   IN | 0 | 19 || 20 |   |      | 0v      |     |     |
 |   9 |  13 |    MISO |   IN | 0 | 21 || 22 | 0 | IN   | GPIO. 6 | 6   | 25  |
 |  11 |  14 |    SCLK |   IN | 0 | 23 || 24 | 1 | IN   | CE0     | 10  | 8   |
 |     |     |      0v |      |   | 25 || 26 | 1 | IN   | CE1     | 11  | 7   |
 |   0 |  30 |   SDA.0 |   IN | 1 | 27 || 28 | 1 | IN   | SCL.0   | 31  | 1   |
 |   5 |  21 | GPIO.21 |   IN | 1 | 29 || 30 |   |      | 0v      |     |     |
 |   6 |  22 | GPIO.22 |   IN | 1 | 31 || 32 | 0 | IN   | GPIO.26 | 26  | 12  |
 |  13 |  23 | GPIO.23 |   IN | 0 | 33 || 34 |   |      | 0v      |     |     |
 |  19 |  24 | GPIO.24 |   IN | 0 | 35 || 36 | 0 | IN   | GPIO.27 | 27  | 16  |
 |  26 |  25 | GPIO.25 |   IN | 0 | 37 || 38 | 0 | IN   | GPIO.28 | 28  | 20  |
 |     |     |      0v |      |   | 39 || 40 | 0 | IN   | GPIO.29 | 29  | 21  |
 +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
 | BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
 +-----+-----+---------+------+---+---Pi B+--+---+------+---------+-----+-----+

Install & Setup

Full Set Install

Full Set include followings:

  • Setup script: Install & Setup following useful stuffs as:
    • mh-z19: A python module to read mh-z19 sensor.
    • PondSlider: A multipurpose versatile sensor handler for python, which read mh-z19 value and handle it.
    • autostart.sh: Utility making mh-z19 as system service to act periodically.

Install Full Set, download from release

git clone https://github.com/UedaTakeyuki/mh-z19.git

Then, got to the folder and issue setup.sh*

./setup.sh 

Necessary settings including serial port enabling are taken place in this script.

Install only sensor module

For python 2.x

sudo pip install mh_z19

For python 3.x

sudo pip3 install mh_z19

In case you would use it witout root permission, call pip without sudo as follows:

For python 2.x

pip install mh_z19

For python 3.x

pip3 install mh_z19

The differences of the interface between each Raspberry Pi model are resolved inside this module. For example, serial device name is difference between Raspberry Pi 3 and older model, but mh-z19 module automatically detect the model and read from appropriate serial device.

To use mh-z19, once you need to set up enabling serial port device on the Raspberry Pi. Following Wiki page might be informative. Also, please refer PyPi top page for detail.

read CO2 Sensor value

pi@raspberrypi:~ $ sudo python3 -m mh_z19 
{'co2': 668}

As above, sudo might be necessary because mh-z19 sensor value is read through serial connection and it request root permission in general.

Handle sensor value by PondSlider multipurpose sensor handler.

The installed mh-z19 module is correspond the pondslider which is multiple & versatile sensor handler to save, send and to do other necessary something with the sensor value.

As an example, we introduce following use case:

  • How to save sensor value to SD card as .CSV file.
  • How to send sensor value to free Remote Monitoring Service.

A brief explanation of pondslider

The pondslider read sensorvalue by sensor-handler specified, and pass the values to value-handlers* which do something with it.

PondSlider

Both sensor and value handlers are python module. For more detail of handlers, please refer this.

How to use sensor value to send to server, save to strage, and so on.

This module correspond the pondslider which is multiple & versatile sensor handler to save, send and to do other necessary something with the sensor value.

In case you choiced Full Install mentioned above, in other words, you've done setup.sh, the pondslider and there example handler's are already installed & setup.

How to save CO2 value on a SD card as a .CSV file.

You can do it with save2strage value handler which is in handlers/value/saver/save2strage, and configuration file to read from mh-z19 sensor and save by save2strage is prepared as config.save.toml on the mh-z19 installed folder.

On the mh-z19 folder By calling pondslider as follows;

sudo python -m pondslider --config config.save.toml

Then, /home/pi/DATA/co2.csv should be created and new line will be added for each call.

How to send CO2 Value to the MONITOR™ Service

The MONITOR™ is a free Remote Monitoring Service to show latest data on the web.

Please refer an introduction to grasp birds-eye understanding about MONIOR™.

A free account is available as follow. Then, login and get a view_id, which is unique id to point a data area on the MONITOR™ to show latest value and chart, as follow.

After get a value_id at your browser, return back to terminal of your Raspberry Pi, then you should set your this software to send measured value to the value_id. To do this, call setid.sh command in the mh_z19 installed directory. Let's say your value_id is vpgpargj, issue setid.sh command as follows:

./setid.sh vpgpargj

Then, call "pondslider" python script as follows:

pi@raspberrypi:~/mh-z19 $ sudo python -m pondslider
{'co2': 742}
co2
{"ok":false,"reason":"ViewID not valid"}

Your time-series chart on the MONITOR™ display on the browser must be updated by the latest CO2 concentration value.

How to set your Raspberry Pi to send CO2 data to MONITOR™ at 5 minute interval.

You can set it by autostart.sh command in the mh_z19 installed directory as follows:

./autostart.sh --on

You can turn off this as follows:

./autostart.sh --off

Also, You can check current status as follows:

./autostart.sh --status

For more detail, please refer this blog.

How to send CO2 Value to the ATT M2X.

The Pondslider also support ATT M2X. For detail, please refer this document.

Calibration, Detection range settings, and ABC(Automatic Baseline Correction) logic on/off.

Features about calibration (both MH-Z19 & MH-Z19B), detection range change (MH-Z19B) and ABC logic on/off(MH-Z19B) are implemented at version 0.2.1 or later.

I'm afraid I've just only implemented these without test due to lack necessary devices and apparatus for the test, fx: standard concentration CO2 GAS, also MH-Z19B module. If you have these devices or apparatus and try to use these functions generously, I really appreciate your issue report regardless result were positive or negative.

For detail please refer this wiki.

Undocumented response values of 0x86 command.

The Revspace/MHZ19 shows values undocumented on the official datasheets (MH-Z19, MH-Z19B). In accordance with this, --all option add these values in the return json value as follows:

sudo python -m mh_z19 --all
{"SS": 232, "UhUl": 10752, "TT": 61, "co2": 818, "temperature": 21}

sudo python3 -m mh_z19 --all
{"TT": 61, "co2": 807, "SS": 232, "temperature": 21, "UhUl": 10752}

or call read_all() function as follows:

>>> import mh_z19
>>> mh_z19.read_all()
{'SS': 232, 'UhUl': 10738, 'TT': 61, 'co2': 734, 'temperature': 21}
>>> 

Use specific serial device.

In case you should use specific serial device instead of Raspberry Pi default serial device which this library automatically select, for example in case to need to use /dev/ttyUSB0 for FT232 usb-serial converter as issue#12, you can specify serial device by --serial_device option as follows:

sudo python -m mh_z19 --serial_device /dev/ttyUSB0

How to use without root permission.

See this wiki.

How to use in your program.

See this wiki.

PWM support.

See this wiki.

In case you can't get the value.

Even if cabling seems no problem and uart seems to be prepateted well but you can't get sensor value. As nincube8 suggested that the pull up by 1-5kΩ register can be working solution. Thank you nincube8!

Q&A

The forum is avai at here. Any questions, suggestions, reports are welcome!

Blog

References

history

  • 0.1.0 2018.09.13 first version self-forked from slider.
  • 0.2.2 2018.11.19 introduce pondslider and separate this PyPi package.
  • 2.0.0 2019.01.18 Add Calibration, ABC on/off requested by this issue. Please refer this wiki.
  • 0.3.5 2019.01.22 Both Python2 & Python3 support
  • 2.3.6 2019.01.22 Merge Pull Request #3 & Pull Request #4. Thanks David!
  • 2.3.7 2019.02.25 Add --all option which requested as issue#5, thanks Rafał!
  • 2.3.8 2019.04.16 Merge Pull Request #7. Thanks Alexander!
  • 2.3.8.1 2019.04.20 Merge Pull Request #8. Thanks WO15!
  • 2.3.8.5 2019.04.21 Merge Pull Request #9. Thanks WO15!
  • 2.3.8.6 2019.04.22 Merge Pull Request #10. Thanks WO15!
  • 2.3.9 2019.05.06 Revise the serial port selection logic. Support using PL011 uart on Raspberry Pi Model 3 and Zero W which is selected by setting dtoverlay=pi3-miniuart-bt or dtoverlay=pi3-disable-bt. Thanks 片岡さん for your kindly report including this issue!
  • 2.4.1 2019.08.11 Add --serial_device option as solution of issue#12. Thanks Actpohomoc and TBR-BRD!
  • 2.4.2 2019.12.12 Merge Pull Request #15. Thanks WO15!
  • 2.5.1 2020.05.16 Add --serial_console_untouched option to support execution without sudo asked as issue#17. Thanks ralphbe91!
  • 2.5.2 2020.06.30 Update the link for datasheet of MH-Z19B from version 1.0 to version 1.5 based be pointed it out as issue#18. Thanks WO15!
  • 2.6.1 2020.07.07 Add --detection_range_10000 option to support Set 0~10000ppm detection range asked as issue#19. Thanks WO15!
  • 2.6.3 2020.08.27 Fix issue#21. Thanks idegre!
  • 3.0.0 2021.02.05 PWM support.
  • 3.0.1 2021.02.17 Fix a degradation of not running with python3. Thank you Masahiko OHKUBO san for your report.
  • 3.0.2 2021.03.25 Fix to support RPi4 correctly as issue#29. Thanks iperniaf!
  • 3.0.3 2021.11.08 Fix issue#35. Thanks false!
  • 3.0.4 2011.11.11 Fix issue#36. Thanks David Bock
  • 3.0.5 2022.01.01 Fix issue#38. Thanks kzehnter!
  • 3.1.0 2022.01.22 add --co2valueonly option by merging issue#39 and issue#40. Thanks jonesthefox!
  • 3.1.1 2022.01.23 remove code clone.
  • 3.1.2 2022.01.31 Fix issue#41. Thanks Christopher M. Pierce!
  • 3.1.3 2022.02.25 Fix issue#43. Thanks Jannis Möller!
  • 3.1.4 2023.11.14 Don't install python2-pip if OS version is BullsEye or later.
  • 3.1.5 2024.05.06 FIx issue#53. Thanks Tats Shibata
  • 3.1.6 2024.08.29 FIx issue#54. Thanks 80kpc

mh-z19's People

Contributors

belibak avatar electronsandstuff avatar false-git avatar iperniaf avatar jannismoeller avatar jonesthefox avatar profi248 avatar uedatakeyuki avatar wo15 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

mh-z19's Issues

Use different serial port

I have used already built-in serial connection for RFID reader. Is there way to use USB serial device to get information from CO2 sensor?
Or implement software serial like we have it on Arduino.

CO2 measurement from script

Dear Sir

I am trying to read out CO2 values by running a python script with no success.
I have successfully been able to read CO2 values using this command from terminal:
sudo python -m mh_z19
And by opening python within the terminal and running the two lines:
import mh_z19
mh_z19.read()

If I make a script with the two lines of code mentioned and run it in terminal ore IDE, however, I am not able to read out CO2 values by get the following error message:
image

Could you help me figure out what I am doing wrong?

mh-z19.setHumidity()?

Hi,

is it possible to the humidity and the temperature with your library to increase the measurement accuracy and get a better Co2 result?

Add additional value for detection range

Currently there are 2 detection ranges you can select/set 2000ppm and 5000ppm see
https://github.com/UedaTakeyuki/mh-z19/wiki/CALIBRATION-&-detection-range
in the datasheet v1.5 in chapter 5 on page 5 there are 3 ranges indicated 2000, 5000 and 10.000.
Would it be possible to add the 10.000ppm range as a "detection_range_10000()" command to the code?
on page 8 of the datasheet the details are given.
Set 0~10000ppm detection range, send command: FF 01 99 00 00 00 27 10 2F

Output always empty array

Hi,
I've cloned the most recent repo version and connected everything as shown in the Readme. However, it seems no value is received. This is my output and my connections:

sudo python -m mh_z19
{}

photo_2022-03-02_15-01-50

Funcion abc_off()

Hola a todos. Tengo una duda:

Si escribo en idle python3:

import mh_z19
mh_z19.abc_off()

¿Se desactiva el autocalibrado del sensor?

Muchas gracias por su gran trabajo.

after install pondslider : Error No module named _init__

PLEASE DON'T FORGET TO "STAR" THIS REPOSITORY :)

If you rather keep your project secret, feel free
to email the author at <[email protected]>; any
private correspondence is treated as confidential.
pi@raspberrypi:~ $ sudo python -m pondslider --config config.save.toml
Traceback (most recent call last):
File "/usr/lib/python3.9/runpy.py", line 197, in _run_module_as_main
return _run_code(code, main_globals, None,
File "/usr/lib/python3.9/runpy.py", line 87, in _run_code
exec(code, run_globals)
File "/usr/local/lib/python3.9/dist-packages/pondslider/main.py", line 6, in
import init as pondslider
ModuleNotFoundError: No module named 'init'

link to new datasheet of MH-Z19

in README.md file and in the section about CALIBRATION you have several links to the datasheet available on the sensor manufacture website. This datasheet you currently link to is version 1.0 published in 2016, however there is also a version 1.5 published in 2019. Probably it is wise to link to this later version or if the old version is still relevant link to both.
link to version 1.5 on Winsen-sensor website.
https://www.winsen-sensor.com/d/files/MH-Z19B.pdf

No Data & Serial Port problems

Hallo,
I'm very new to all of this, so sorry in advance.

-Raspberry Pi 4 /w MH_Z19C
-I installed it via "pip install".
-I've enabled the serial port hardware in raspi-config as well as I2C which I need for the other part of my project.
-I triple-checked the wiring.
-The red led is blinking.
-I used "chmod 777" (just in case) for both /dev/ttyS0 and /dev/AMA0
-I disabled bluetooth just like one person of the other issues stated that it worked for them.
-After I used mh_z19.read_all() once, it reset all the permissions for ttyS0 back to what it was before. Therefore I get this after using it again:
helplol

Help? ^^°

Still return {} after using pull-up resistor and change freq = 250 and enable_uart = 1

Hallo Mr. Takeyuki and everyone,

I still have a problem with my MH-Z19C sensor, it is still always treturn {}, can someone help me? is it compatible for the MH-Z19C or only for B? I already read many blogs and github and I cannot solve my problem

I did my work on Raspberry Pi 5, and I also tried it on my 3+ but still not working. I already tried the pull-up resistor both for RX or TX pins.

WhatsApp Image 2024-04-06 at 2 18 09 AM
return {}

Thank you so much.

Got no valid response

On my Raspberry 3B+ I get
None from mh_z19.read() and 9 from mhz_19.read_all()
I am using Python3.6
The serial is /dev/serial1 -> ttyAMA0
The mh_z19 is connected RxD <-> TxD and TxD <-> RxD (physical pins 8 and 10)

There is also another module (SDS011) connected to USB.

Any suggestion for debugging?

Another `{}` output issue

Hi all,

I have an Mh-z19c connected to a Pi Zero W with TxD and RxD cross connected to pins 8 (GPIO 14) and 10 (GPIO 15) as per the repo readme and I see an orange light flashing inside indicating power.

The problem is the output from mh_z19 always {}.

I found the serial output is not passing this check:

if len(s) >= 4 and s[0] == 0xff and s[1] == 0x86 and ord(checksum(s[1:-1])) == s[-1]:
  return s[2]*256 + s[3]

Instead I see values like these examples:

ser.read(9): b'BM\x0c\x9c\t\xad\x02\x13\x01' - s[0]=66, s[1]=77, s[-1]=1
ser.read(9): b'(\x00\x00\xe2\x00\x06\x13BM' - s[0]=40, s[1]=0, s[-1]=77
ser.read(9): b'\x0c\x9c\t\xad\x02\x13\x01(\x00' - s[0]=12, s[1]=156, s[-1]=0
ser.read(9): b'\x00\xe2\x00\x06\x13BM\x0c\x9c' - s[0]=0, s[1]=226, s[-1]=156
ser.read(9): b'\t\xad\x02\x13\x01(\x00\x00\xe2' - s[0]=9, s[1]=173, s[-1]=226
ser.read(9): b'BM\x0c\x9c\t\xb2\x02\x15\x01' - s[0]=66, s[1]=77, s[-1]=1
ser.read(9): b'(\x00\x00\xe2\x00\x06\x1aBM' - s[0]=40, s[1]=0, s[-1]=77
ser.read(9): b'\x0c\x9c\t\xb2\x02\x15\x01(\x00' - s[0]=12, s[1]=156, s[-1]=0
ser.read(9): b'\x00\xe2\x00\x06\x1aBM\x0c\x9a' - s[0]=0, s[1]=226, s[-1]=154
ser.read(9): b'\t\xaa\x02\x15\x01(\x00\x00\xe2' - s[0]=9, s[1]=170, s[-1]=226

I've followed the advice of all the existing issues I can find:

  • Enable serial port via raspi-config
  • Ensure enable_uart=1 is in /boot/config.txt
  • Set core_freq=250 in /boot/config.txt
  • Set dtoverlay=disable-bt in /boot/config.txt to disable Bluetooth
  • Add a resistor between TxD and Vin (I used a 2k)

If anyone has any suggestions they would be highly appreicated!

This is the variant of sensor I have:
image

Question about enabling serial port

I was attempting to follow the instructions at https://github.com/UedaTakeyuki/mh-z19/wiki/How-to-Enable-Serial-Port-hardware-on-the-Raspberry-Pi but ran into issues with permissions on the /dev/ttyS0 port on (rpi3). I added the enable_uart=1 in /boot/config.txt and rebooted, the device was set with permissions 620. I could chmod them but it would reset on boot.

The only way I found to fix this issue was to use the raspi-config instructions instead. Is this the intended behavior?

It would be better to not run python as root or with sudo as it wreaks havoc with pipenv and dependencies. I think that by enabling the serial port via raspi-config the resultant permissions should let me run python as a normal user and still access the hardware.

untested zero point calibration command seems to be working as expected

As requested on https://github.com/UedaTakeyuki/mh-z19/blob/master/pypi/README.md, I'd like to report that the zero point calibration function seems to be working as expected. When called e.g. by

sudo python -m mh_z19 --zero_point_calibration

the current CO2 concentration is set to represent 400 ppm CO2. This was checked in the following way:

  1. calling
    sudo python -m mh_z19 --all
    showed a reading of more than 800 ppm.
  2. calling
    sudo python -m mh_z19 --zero_point_calibration
    returned a message saying that ZERO point calibration was carried out.
  3. immediately thereafter, without changing, calling again:
    sudo python -m mh_z19 --all
    returned a reading close to 400 ppm.

Tested using a mh_z19b device connected to a Raspberry Pi Zero W, purchased in the fall of 2020.

Return co2 value as raw string

As my project does not use json, i wanted the module just to return a raw number. so I added an 'co2valueonly' option to it.

I'll try to add an pull request :)

Without root permission won't set

Thanks for this usefull package and the clear documentation! The installation went well on a RPI 3B and I got sensor readings. I want to use the sensor in Python programs and so I want to get rid of the root permission. Unfortunately this doesn't work for me.
I used sudo chmod g+r /dev/ttyAMA0
using ls -la /dev/ttyAMA0 this gave me crw-rw---- 1 root tty 204, 64 Nov 8 20:00 /dev/ttyAMA0
but when I tried python3 -m mh_z19 --serial_console_untouched I got an error:

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/serial/serialposix.py", line 265, in open
    self.fd = os.open(self.portstr, os.O_RDWR | os.O_NOCTTY | os.O_NONBLOCK)
PermissionError: [Errno 13] Permission denied: '/dev/serial0'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/pi/mh-z19/mh_z19.py", line 66, in read_concentration
    ser = connect_serial()
  File "/home/pi/mh-z19/mh_z19.py", line 62, in connect_serial
    timeout=1.0)
  File "/usr/lib/python3/dist-packages/serial/serialutil.py", line 240, in __init__
    self.open()
  File "/usr/lib/python3/dist-packages/serial/serialposix.py", line 268, in open
    raise SerialException(msg.errno, "could not open port {}: {}".format(self._port, msg))
serial.serialutil.SerialException: [Errno 13] could not open port /dev/serial0: [Errno 13] Permission denied: '/dev/serial0'
{}

And then the 'r' on the fifth position is gone and I have to use chmod again. How to solve this?

Wrong temperature and wrong co2 value

Hi everybody,

I am currently trying to get some data using the mh-z19. Considering installation and connection, everything works perfectly fine for me. However when I started to track the data (I am writing it into a .csv file), I would always get the same value for temperature and almost the same values for the co2 measuring:

CO2,Temperature,Time
400,3,31/03/2022 21:00:55
400,3,31/03/2022 21:01:10
400,3,31/03/2022 21:01:25
400,3,31/03/2022 21:01:40
400,3,31/03/2022 21:01:55
400,3,31/03/2022 21:02:10
400,3,31/03/2022 21:02:26
400,3,31/03/2022 21:02:41
400,3,31/03/2022 21:02:56
448,3,31/03/2022 21:04:45
446,3,31/03/2022 21:05:00
442,3,31/03/2022 21:05:15
437,3,31/03/2022 21:05:30
438,3,31/03/2022 21:05:46
434,3,31/03/2022 21:06:01
430,3,31/03/2022 21:06:16
432,3,31/03/2022 21:06:31
436,3,31/03/2022 21:06:46
437,3,31/03/2022 21:07:01
439,3,31/03/2022 21:07:17
435,3,31/03/2022 21:07:32
433,3,31/03/2022 21:07:47
438,3,31/03/2022 21:08:02
444,3,31/03/2022 21:08:17
448,3,31/03/2022 21:08:33
448,3,31/03/2022 21:08:48

Some information about the environment: the sensor is currently placed inside where the temperature is at 18-20 degrees celsius.
Additionally I spent a lot of time in the room the sensor is placed in and I didn't open the windows . So I am sure that the co2 concentration is wrong, same goes for the temperature.

I was thinking that the sensor maybe needs some time to calibrate itself but after running 24 hours and giving me more than 5000 entries with always the same temperature of 3 degrees and nearly always the same co2 value (between 400-500ppm), I don't think its working properly.

Maybe you can help me and know how to fix this Issue or somehow 'calibrate' the sensor again, I am really clueless at the moment.

I would be very grateful if you can help me.

error: ubyte format requires 0 <= number <= 255

I have a python program which calls mh_z19.read() every few seconds and sometimes (I don't know why it is only sometimes) I get this error:

Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/mh_z19/init.py", line 73, in mh_z19
if len(s) >= 4 and s[0] == "\xff" and s[1] == "\x86" and checksum(s[1:-1]) == s[-1]:
File "/usr/local/lib/python2.7/dist-packages/mh_z19/init.py", line 231, in checksum
return struct.pack('B', 0xff - (sum(array) % 0x100) + 1)
error: ubyte format requires 0 <= number <= 255

Steps to reproduce:
Call mh_z19.read() every 2 seconds and wait.
In an hour of runtime it happened around 4 times.

Usage in Python 3 without sudo

Is it possible to use this library with Python 3 without sudo?

When I use "import mh_z19" and "mh_z19.read()" in my program it doesn't work. Although I have activated the serial port (just as you and others decribed ist), I get the following error: PermissionError: [Errno 13] Permission denied: '/dev/serial0'. The sensor is working, because when I use "$ sudo python -m mh_z19" I get the desired result.

I am a beginner in Python, that has already taken up a lot of my time.

Raspberry 4 Support

Hi,
I'm using your lib with a rpbi 4 and it needs some fixes.
¿Can I contribute to the community fixing them?

sudo python -m mh_z19 alway returns null

I am using MH_Z19B with a Raspberry Pi 3B
I always get null when I use sudo python -m mh_z19
I activated Serial in raspi-config
I wired everything correct but I can't find the problem.
When the sensor is not connected it also shows null
Could you please help me find the problem ?

"null" on screen.

I have the MH-Z19 attached to the ANAVI phat, on a raspberry pi zero w. I installed the python modules and enabled the i2c interface. I found a similar help request, but I tried all of the different codes and nothing happened.

THis is my wiring:
Tx-Rx
Rx-Tx
vin-3.3v
GND-GND
When I type insudo python -m mh_z19 this output is null.
image

--serial_device not working

when attempting to use the --serial_device parameter it always returns an error.
sorry I don't have the exact error as I changed the code on my pi and didn't get any screenshots or the exact error. I found this when trying to use a usb->serial interface on my pi3 so i wouldn't need to deal with the onboard serial
luckily its an easy fix, but i cant create a PR to save you the trouble
https://github.com/UedaTakeyuki/mh-z19/blob/master/pypi/mh_z19/__main__.py#L56 here it should be mh_z19.set_serialdevice(args.serial_device)

Describe the bug
when attempting to use the --serial_device parameter it always returns an error.
sorry I don't have the exact error as I changed the code on my pi and didn't get any screenshots

To Reproduce

  1. call sudo python3 -m mh_z19 --serial_device /dev/ttyUSB0
  2. see set_serialdevice(args.serial_device) is undefined error

Expected behavior
the command should work

Screenshots
If applicable, add screenshots to help explain your problem.

Using along python3

Hi! Like your work on the mh-z19. It works flawless when using as command line (python -m mh_z19) and by downloading the file. I want to use your code with a program i have wrote on python3 (it performs different actions depending on the co2 value) but I always have problems with the import. After solving some of them (as using .encode() ) i cant move foward after this message:

Traceback (most recent call last):
  File "/home/pi/Desktop/soul/mh_z19-0.1.6/mh_z19/__init__.py", line 73, in mh_z19
    s=ser.read(9)
  File "/usr/lib/python3/dist-packages/serial/serialposix.py", line 497, in read
    raise SerialException('read failed: {}'.format(e))
serial.serialutil.SerialException: read failed: device reports readiness to read but returned no data (device disconnected or multiple access on port?)

I think this happens because the file is being imported (import init as mh_z19) from a python3 file.
What can you suggest me?

Raspberry Pi 5 Support

mh_z19 3.1.3 doesn’t work with Raspberry Pi 5 because the dependent RPi.GPIO 0.7.1 doesn’t work with it.

It seems RPi.GPIO is no longer maintained because of no response to the ticket to support RP5. The official RP doc is using gpiozero to access GPIO.

$ python3 -m mh_z19 --pwm
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "/var/lib/python/ansible/lib/python3.12/site-packages/mh_z19/__main__.py", line 104, in <module>
    print (mh_z19.read_from_pwm(gpio=args.pwm_gpio, range=args.pwm_range))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/var/lib/python/ansible/lib/python3.12/site-packages/mh_z19/__init__.py", line 222, in read_from_pwm
    GPIO.setup(gpio,GPIO.IN)
RuntimeError: Cannot determine SOC peripheral base address

sudo python -m mh_z19 alway returns {}

Hi, I ran the following command as written in the readme
git clone https://github.com/UedaTakeyuki/mh-z19.git
. /setup.sh
but when I do sudo python -m mh_z19, I get No such file or directory: ' /dev/ttyS0't
I enabled serial communication in raspi-config and did sudo python -m mh_z19, but it shows {}.
I also tried PWM but it timed out and could not get the value.

Character Encoding Issue When Installing on Windows

Describe the bug
Thanks for the library! I have an issue where pip fails to install it on Windows due to character encoding in README.md read by setup.py. By the way, my use case is that I am developing code on a Windows platform and then pushing it to a Raspberry Pi to take advantage of my existing IDE setup.

Here is a full copy of the pip error:

Collecting mh-z19
  Using cached mh_z19-3.1.1.tar.gz (13 kB)
  Preparing metadata (setup.py): started
  Preparing metadata (setup.py): finished with status 'error'

  error: subprocess-exited-with-error
  
  python setup.py egg_info did not run successfully.
  exit code: 1
  
  [8 lines of output]
  Traceback (most recent call last):
    File "<string>", line 2, in <module>
    File "<pip-setuptools-caller>", line 34, in <module>
    File "C:\Users\Christopher Pierce\AppData\Local\Temp\pip-install-n1zxmenn\mh-z19_8fc0a2b6efaf4a988424c84193cf061c\setup.py", line 4, in <module>
      long_description = f.read()
    File "C:\Users\Christopher Pierce\AppData\Local\Programs\Python\Python39\lib\encodings\cp1252.py", line 23, in decode
      return codecs.charmap_decode(input,self.errors,decoding_table)[0]
  UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 10805: character maps to <undefined>
  [end of output]
  
  note: This error originates from a subprocess, and is likely not a problem with pip.
error: metadata-generation-failed

Encountered error while generating package metadata.

See above for output.

note: This is an issue with the package mentioned above, not pip.
hint: See above for details.

To Reproduce
Steps to reproduce the behavior:

  1. Create a new venv on a Windows computer
  2. Run pip3 install mh_z19

Expected behavior
The library is installed.

Returns null - Need help

Hi there,

first of all thank you for your package. I have recently started playing around with a rpi 4 and wanted to connect a mh-z19b with your python package.

I made sure the serials are available:

serial0 -> ttyS0
serial1 -> ttyAMA0

Enabled i2c
However i2c output is empty

And even made sure uart is enabled (as seen on another blog )

When I run sudo python -m mh_z19 I (most of the time) get back
null

Sometimes, however there is also another message:
IMG_1122

Here is my cabling set up.
IMG_0623

IMG_0625

I should mention, that the soldered pins at the mh-z19b where bent a bit due to delivery. But I don't think (hope) this is a problem.

Do you have any ideas what else I could test to make it work?

Thanks in advance!

function abc_off() work fine but function read() not work

Hello everyone, I come to comment on a positive and a negative result. I have raspberry pi 3 b +.
I have tested the abc_off () function for 7 days continuously and it works fine. On the other hand, when I print(mh_z19.read_from_pwm ()) everything goes well and I get the correct reading, but when I print (mh_z19.read()) I get an error. I attach an image with the error that I get. I have previously made the configuration included in this link but I keep getting the error of the attached image. Somebody could help me?

Thank you

image

Reported co2 level wrong

Hey,

I received my MH-Z19B and connected it to my raspberry.

I ran a pip install mh-z19 and when I execute python -m mh_z19 --all I receive the following output:
{"SS": 0, "UhUl": 0, "TT": 50, "co2": 5000, "temperature": 10}

Temperature is reported more or less correctly (compared against thermometer and DHT22). But co2 value can't be correct. Point of measure was outside.

What else can I do for debugging?

No module named mh_z19

Hello, I'm a newbie to python and it's packages so i blindly followed your instructions to install and use your module from https://pypi.org/project/mh-z19/
I'm connected to my Raspberry over SSH.
Taking the pip install route, which is the easiest but python not finding the module (error in title) led me to try to run it with python3 and even reinstalling it with pip3 left me with no error but also with no success.

So, there are two things I noticed while inspecting the instructions:

Install:
pip install mh-z19

Use:
sudo python -m mh_z19
  1. the use of sudo
  2. the use of - and _

What I've did now was like:

Install:
sudo pip install mh_z19

Use:
sudo python -m mh_z19

And it works.
Seems like it makes no difference if using - or _ for install but the sudo made a difference.
Am I right that something's not proper with that instructions?

Timestamp is wrong when downloading data

PLEASE DON'T FORGET TO "STAR" THIS REPOSITORY :)

If you rather keep your project secret, feel free
to email the author at <[email protected]>; any
private correspondence is treated as confidential.

When reporting a bug please include the following:

Describe the bug
A clear and concise description of what the bug is.

To Reproduce
Steps to reproduce the behavior:

  1. Go to '...'
  2. Click on '....'
  3. Scroll down to '....'
  4. See error

Expected behavior
A clear and concise description of what you expected to happen.

Screenshots
If applicable, add screenshots to help explain your problem.

Serial Device Must Be Specified When Using Raspberry Pi 5

Hi Takeyuki,
I found that when using Raspberry Pi 5, it is necessary to specify the serial device. Is this expected behavior?

When I connect the MH-Z19E to the Raspberry Pi 5 according to the wiring instructions in the README and run the command
sudo python3 -m mh_z19
the output is
{}

However, when I specify the serial device in this command
sudo python3 -m mh_z19 --serial_device /dev/ttyAMA0
I get an output like
{"co2": 1711}

This might be related to a change in the Raspberry Pi 5.
https://www.raspberrypi.com/documentation/computers/configuration.html#primary-and-secondary-uart

/dev/serial0 and /dev/serial1 are symbolic links which point to either /dev/ttyS0 or /dev/ttyAMA0.
On the Raspberry Pi 5, /dev/serial0 is a symbolic link that points to /dev/ttyAMA10.

mh_z19 Version: 3.1.5
Device: MH-Z19E

Not getting data

Hi,

I have the mh-z19b. I have installed the library but I am not getting data.
image

I also tried in a python file with

import mh_z19
mh_z19.read()

I get none back with that.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.