MH-Z19B Reading problem

Hi,

I tried multiple way to connect the MH-Z19B using Mycodo interface. First of all, using the mh-z19 3.1.1 project, I can easily retrieve data from the /dev/ttyS0 over a SSH connection to the pi with sudo python -m mh_z19 --all. That means, the sensor is working.

Now when I configure Mycodo mh-z19b input, there is nothing happening when trying to read the values. I had configured it to use /dev/ttyS0 and I also tried using a UART-USB converter instead of the TX-RX GPIO pins, not much success.

I don’t know what’s happening, but here is the log about the moment when mycodo tries to acquire data.

2022-01-29 10:40:31,407 - INFO - mycodo.controllers.controller_input_bd84bcbf - Activated in 403.7 ms
2022-01-29 10:40:31,407 - DEBUG - mycodo.daemon - Input controller with ID bd84bcbf-2890-45c0-bd69-9d9f0e392c39 activated.
2022-01-29 10:40:32,420 - DEBUG - mycodo.inputs.mh_z19b_bd84bcbf - No response
2022-01-29 10:40:32,456 - DEBUG - mycodo.controllers.controller_input_bd84bcbf - Adding measurements to InfluxDB with ID bd84bcbf-2890-45c0-bd69-9d9f0e392c39: {}
2022-01-29 10:40:37,407 - ERROR - mycodo.controllers.controller_input_670a2d3b - StopIteration raised 3 times. Possibly could not read input. Ensure it’s connected properly and detected.
2022-01-29 10:40:40,998 - ERROR - mycodo.controllers.controller_input_8a9cea18 - Mycodo is attempting to acquire measurement(s) from an Input that has already critically errored. Review the log lines following Input Activation to investigate why this happened.

Also, I did use a 5k resistor between my VCC and Tx, I really don’t know what to do from know since I can get reading through mh_z19.read() over ssh.

Thanks for you help.

Are you sure you configured the Input correctly?

i have (hopefully) the same problem with the mh-z19b. or at least one aswell.

mycodo gives this error:

2022-05-13 14:44:23,172 - ERROR - mycodo.inputs.mh_z19b_a7e10268 - Error initializing, trying again in 5 seconds: Write timeout
Traceback (most recent call last):
  File "/var/mycodo-root/mycodo/abstract_base_controller.py", line 54, in try_initialize
    self.initialize()
  File "/home/pi/Mycodo/mycodo/inputs/mh_z19b.py", line 153, in initialize
    self.abcoff()
  File "/home/pi/Mycodo/mycodo/inputs/mh_z19b.py", line 201, in abcoff
    self.ser.write(bytearray([0xff, 0x01, 0x79, 0x00, 0x00, 0x00, 0x00, 0x00, 0x86]))
  File "/var/mycodo-root/env/lib/python3.9/site-packages/serial/serialposix.py", line 636, in write
    raise SerialTimeoutException('Write timeout')
serial.serialutil.SerialTimeoutException: Write timeout

i also tried using this one here directly, which worked out of the box

git clone https://github.com/UedaTakeyuki/mh-z19
./setup.sh
sudo python3 -m mh_z19
~/mh-z19 $ sudo python3 -m mh_z19
{"co2": 537}

thank you

Providing just an error is not enough information to replicate your environment or diagnose the issue. You need to provide details about the physical connections as well as the software settings used to be able to investigate the issue.

Also, are you sure you have a MH-Z19B and not a MH-Z19? It looks like it’s failing on ABC, which is only present in the B version.

the label says MH-Z19B.
just tried the MH-Z19 (without B) input, but still the same

2022-05-13 17:23:21,402 - ERROR - mycodo.inputs.mh_z19_3a5b0e1b - Error initializing, trying again in 5 seconds: Write timeout
Traceback (most recent call last):
File “/var/mycodo-root/mycodo/abstract_base_controller.py”, line 54, in try_initialize
self.initialize()
File “/home/pi/Mycodo/mycodo/inputs/mh_z19.py”, line 141, in initialize
self.set_measure_range(self.measure_range)
File “/home/pi/Mycodo/mycodo/inputs/mh_z19.py”, line 194, in set_measure_range
self.ser.write(bytearray([0xff, 0x01, 0x99, 0x00, 0x00, 0x00, 0x13, 0x88, 0xcb]))
File “/var/mycodo-root/env/lib/python3.9/site-packages/serial/serialposix.py”, line 636, in write
raise SerialTimeoutException(‘Write timeout’)
serial.serialutil.SerialTimeoutException: Write timeout

i guess the problem is not the physical connection, as i get the correct values outside of mycodo ?

what software settings exactly?

thank you

Physical connections of the hardware, any and all settings you configured in software… all of these are needed to diagnose an issue. None should be assumed.

i have it exactly like this
direct connection, no breadboard or anything

RXD(Sensor)->TXD(rpi)
TXD(Sensor)->RXD(rpi)
GND → GND
VIN → 5V

mh-z19b settings are all to default. /dev/ttyAMA0 - baudrate 9600

what settings are you mean? i have nothing configured yet. only turned everything to “on” in mycodo-rpi-settings

I2C Enabled: True
1-Wire Enabled: True
Serial Enabled: True
SPI Enabled: True
SSH Enabled: True
Pi Camera Enabled: False

The Mycodo Input is configurable as well.

yes. all to default.

Is /dev/ttyAMA0 the location of your serial device connected to the sensor?

Have you searched similar issues on github? Here are some:

Is /dev/ttyAMA0 the location of your serial device connected to the sensor?

i guess so. isn’t AMA0 the default location? because again, it was working via CLI - all set to default. only one UART device connected.
how would i found out if AMA0 was correct?

anyway; i now tried a cp210x usb converter with /dev/ttyUSB0 and was working directly :confused:

1 Like

Hi Guys,

like many others, unfortunately I also have problems with the MH-Z19. Since many days I’ve tried everything that is on my mind, but unfortunately I do not know any further, so I would be very happy about your help.

I have the MH-Z19C-PC connected to the USB-Uart converter CP2102 as follows:
CP2102 MHZ-19
RX TX
TX RX
GND GND
VCC VCC

As input in Mycodo I used the sensor MH-Z19 (without B). Uart Device was set to /dev/ttyUSB0.

I use a Raspberry 4b.
The red LED of the sensor, which is located behind the fleece, is blinking steadily.

2023-02-07 10:56:26,039 - ERROR - mycodo.inputs.mh_z19_2fb1f0f8 - Error 101: Device not set up. See https://kizniche.github.io/Mycodo/Error-Codes#error-101 for more info.
2023-02-07 10:56:26,040 - ERROR - mycodo.controllers.controller_input_2fb1f0f8 - StopIteration raised 3 times. Possibly could not read input. Ensure it's connected properly and detected.
2023-02-07 10:56:36,747 - DEBUG - mycodo.inputs.mh_z19_2fb1f0f8 - No response
2023-02-07 10:56:36,752 - DEBUG - mycodo.controllers.controller_input_2fb1f0f8 - Adding measurements to InfluxDB with ID 2fb1f0f8-2beb-4528-be35-2718dee92226: {}
2023-02-07 10:56:41,002 - ERROR - mycodo.inputs.mh_z19_2fb1f0f8 - Error 101: Device not set up. See https://kizniche.github.io/Mycodo/Error-Codes#error-101 for more info

I’m so thankful for every help.

Regards

Wrong connection

RX RX
TX TX
GND GND
VCC VCC

Connect like this and see if problem is solved

The RX LED should also blink

1 Like

Hi,
i changed the wiring as you said.
But still no progress.

The power led and the rx led are constantly on.

Do you have any further ideas?

Regards

To find out USB Device ID
plug in the usb cable
run following command
lsusb

U can see the device

To find out USB Port No

dmesg | grep ttyUSB

U will see USB port Number

Put this port number in mycodo and check

Thank you for your fast response.
I checked as you said. But it doesn’t work either.

pi@raspberrypi:~ $ lsusb
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 003: ID 10c4:ea60 Silicon Labs CP210x UART Bridge
Bus 001 Device 002: ID 2109:3431 VIA Labs, Inc. Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
pi@raspberrypi:~ $ dmesg | grep ttyUSB
[    6.124111] usb 1-1.1: cp210x converter now attached to ttyUSB0
pi@raspberrypi:~ $

So the converter runs at USB0.
I configured this USB port already in Mycodo (see the screenshot from previous post).

Do you have further ideas?

Oh…I just checked your message.
You are using MH-Z19C
Only MH-Z19 and Mh-Z19B are supported in Mycodo. To use MH-Z19C you need to create custom input and if you find it difficult then replace the sensor with Mh-Z19 or 19B

Oh… :no_mouth:

I will have a look if i found some instructions on how to create a custom input (for beginners).

Anyways: Thank you so much for your help!

Sorry for picking up an old thread, but I my MH-Z19C is on the way to me and I started to prepare the mycodo and probably will face same situation. Were you able to create a custom input for the C version of the sensor?