UedaTakeyuki / mh-z19

Read CO2 concentration from mh-z19 sensor on the Raspberry Pi & handle it. Detect Raspberry Pi model automatically and read the value from an appropriate serial device.
MIT License
182 stars 37 forks source link

Another `{}` output issue #52

Closed 2ZZ closed 2 months ago

2ZZ commented 2 months ago

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:

If anyone has any suggestions they would be highly appreicated!

This is the variant of sensor I have: image

UedaTakeyuki commented 2 months ago

Hi 2ZZ,

Inferred from the random response you've got, most possible causes are:

Would you please tell me what is happening when you try again with the option --serial_device /dev/serial1, --serial_device /dev/ttyAMA0 or --serial_device /dev/ttyS0, also try without dtoverlay=disable-bt in your/boot/config.txt, thank you.

2ZZ commented 2 months ago

Hi @UedaTakeyuki,

I don't think it's a preheat issue as I've left it powered on for many hours.

Re-enabling BT and rebooting changed the response from those alternative serial devices, the output is below with some extra debug lines added.

/dev/serial1

BT disabled

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/serial/serialposix.py", line 380, in _reconfigure_port
    orig_attr = termios.tcgetattr(self.fd)
termios.error: (5, 'Input/output error')

BT enabled

ser.read(9): b''
ser.read(9): b''
ser.read(9): b''
ser.read(9): b''
ser.read(9): b''
{}

/dev/ttyAMA0

BT disabled

ser.read(9): b'BM\x0c\x8c\t\xac\x02\xce\x01'
ser.read(9): b'*\x00\x00\xe2\x00\x06\xbfBM'
ser.read(9): b'\x0c\x8c\t\xac\x02\xce\x01*\x00'
ser.read(9): b'\x00\xe2\x00\x06\xbfBM\x0c\x8c'
ser.read(9): b'\t\xac\x02\xce\x01*\x00\x00\xe2'
{}

BT enabled

ser.read(9): b''
ser.read(9): b''
ser.read(9): b''
ser.read(9): b''
ser.read(9): b''
{}

/dev/ttyS0

BT disabled

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/serial/serialposix.py", line 380, in _reconfigure_port
    orig_attr = termios.tcgetattr(self.fd)
termios.error: (5, 'Input/output error')

BT enabled

ser.read(9): b'BM\x0c{\t\xa5\x03/\x01'
ser.read(9): b'*\x00\x00\xe2\x00\x06\tBM'
ser.read(9): b'\x0c{\t\xa5\x03/\x01*\x00'
ser.read(9): b'\x00\xe2\x00\x06\tBM\x0c{'
ser.read(9): b'\t\xa5\x030\x01*\x00\x00\xe2'
{}

I will put an order in for a replacement sensor to rule that out.

UedaTakeyuki commented 2 months ago

It seems as follows:

The response from the sensor module (possibly) seems to repeat the following sequence:

Do you have any idea about the sequence above?

2ZZ commented 2 months ago

I really have no idea on that sequence. I have a spare Pi 2 B here so I tried it on there and the issue is the same. Once the new sensor arrives I will try again.

UedaTakeyuki commented 2 months ago

If you don't mind its rough resolution and re-cabling, PWM interface is also available.

2ZZ commented 2 months ago

PWM is working nicely :raised_hands:

root@raspberrypi:~# while :; do python -m mh_z19 --pwm; done
{'co2': 480.0}
{'co2': 480.0}
{'co2': 480.0}
{'co2': 480.0}
{'co2': 480.0}
{'co2': 480.0}
{'co2': 475.0}
{'co2': 475.0}
{'co2': 475.0}
{'co2': 470.0}
{'co2': 470.0}
{'co2': 470.0}
{'co2': 470.0}
{'co2': 465.0}
{'co2': 460.0}
{'co2': 460.0}
{'co2': 460.0}
{'co2': 460.0}
{'co2': 460.0}

I will still try GPIO again as I already ordered the new sensor.

2ZZ commented 2 months ago

The new sensor arrived and works straight away. This is the one I bought: https://www.aliexpress.com/item/1005002038004611

python3 -m mh_z19
{"co2": 796}

Thanks for the assistance @UedaTakeyuki

GiChoc commented 1 month ago

Hello @2ZZ , I have exactly the same problem as you. PWM works fine but the readings from UART are as yours. They do not pass this check.

What was the problem eventually with your case? Do you think I have a faulty sensor?

2ZZ commented 1 month ago

Hello @2ZZ , I have exactly the same problem as you. PWM works fine but the readings from UART are as yours. They do not pass this check.

What was the problem eventually with your case? Do you think I have a faulty sensor?

Possibly, which variety of sensor do you have? You may find PWM mode works even if UART doesn't.

GiChoc commented 1 month ago

Hello @2ZZ , I have exactly the same problem as you. PWM works fine but the readings from UART are as yours. They do not pass this check. What was the problem eventually with your case? Do you think I have a faulty sensor?

Possibly, which variety of sensor do you have? You may find PWM mode works even if UART doesn't.

I've got the MH-Z19B variety. I have now given up :(