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

Got no valid response #22

Closed monojk closed 3 years ago

monojk commented 3 years ago

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?

UedaTakeyuki commented 3 years ago

Hi, monojk!

I'm slightly confused about how to connected mh_z19 with Raspberry Pi. So, would you please tell me more detail on how to connect the mh_z19 sensor and RPi? Is the mh_z19 sensor module connected with RPi directory by UART on RPi? Or connected by something USB-Serial converter?

If possible, could you please attach a photo of how to cabling? Thank you.

monojk commented 3 years ago

The connection is according to the cabling in your README.md 2020-08-29 16 12 20

UedaTakeyuki commented 3 years ago

I'm now clear. The connection seems no problem.

The serial is /dev/serial1 -> ttyAMA0

Would you please attach here how was "ls -la /dev"? thank you for your kindly cooperation for the debugging!

nincube8 commented 3 years ago

Make sure that you put RxD to TxD, and TxD to RxD on the Pi. Then you need to make sure your Pi has created the symbolic links in the system. Reboot, and check you MH-z19 for the Orange light. Make sure I2C is enabled as well.

On Sat, Aug 29, 2020 at 8:18 AM monojk notifications@github.com wrote:

The connection is according to the cabling in your README.md [image: 2020-08-29 16 12 20] https://user-images.githubusercontent.com/25121142/91638914-1b35a480-ea13-11ea-864a-d4e09e04b979.jpg

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/UedaTakeyuki/mh-z19/issues/22#issuecomment-683296932, or unsubscribe https://github.com/notifications/unsubscribe-auth/AEBLRRNY5D2HDRVOIU5N62LSDEE2TANCNFSM4QO7BF3Q .

monojk commented 3 years ago

I'm now clear. The connection seems no problem.

The serial is /dev/serial1 -> ttyAMA0

Would you please attach here how was "ls -la /dev"? thank you for your kindly cooperation for the debugging!

pi@rp3b:~ $ ls -la /dev
insgesamt 4
drwxr-xr-x  16 root root        3680 Aug 29 16:11 .
drwxr-xr-x  21 root root        4096 Apr  8  2019 ..
crw-r--r--   1 root root     10, 235 Aug 28 10:22 autofs
drwxr-xr-x   2 root root         640 Aug 28 10:22 block
crw-------   1 root root     10, 234 Aug 28 10:22 btrfs-control
drwxr-xr-x   3 root root          60 Jan  1  1970 bus
crw-------   1 root root     10,  63 Aug 28 10:22 cachefiles
drwxr-xr-x   2 root root        2780 Aug 29 16:11 char
crw-------   1 root root      5,   1 Aug 28 10:23 console
crw-------   1 root root     10,  62 Aug 28 10:22 cpu_dma_latency
crw-------   1 root root     10, 203 Aug 28 10:22 cuse
drwxr-xr-x   7 root root         140 Aug 28 10:22 disk
crw-rw----   1 root video    29,   0 Aug 28 10:22 fb0
lrwxrwxrwx   1 root root          13 Nov  3  2016 fd -> /proc/self/fd
crw-rw-rw-   1 root root      1,   7 Aug 28 10:22 full
crw-rw-rw-   1 root root     10, 229 Aug 28 10:23 fuse
crw-rw----   1 root gpio    254,   0 Aug 28 10:22 gpiochip0
crw-rw----   1 root gpio    254,   1 Aug 28 10:22 gpiochip1
crw-rw----   1 root gpio    247,   0 Aug 28 10:22 gpiomem
crw-------   1 root root     10, 183 Aug 28 10:22 hwrng
lrwxrwxrwx   1 root root          25 Nov  3  2016 initctl -> /run/systemd/initctl/fifo
drwxr-xr-x   2 root root          60 Jan  1  1970 input
crw-r--r--   1 root root      1,  11 Aug 28 10:22 kmsg
lrwxrwxrwx   1 root root          28 Nov  3  2016 log -> /run/systemd/journal/dev-log
brw-rw----   1 root disk      7,   0 Aug 28 10:22 loop0
brw-rw----   1 root disk      7,   1 Aug 28 10:22 loop1
brw-rw----   1 root disk      7,   2 Aug 28 10:22 loop2
brw-rw----   1 root disk      7,   3 Aug 28 10:22 loop3
brw-rw----   1 root disk      7,   4 Aug 28 10:22 loop4
brw-rw----   1 root disk      7,   5 Aug 28 10:22 loop5
brw-rw----   1 root disk      7,   6 Aug 28 10:22 loop6
brw-rw----   1 root disk      7,   7 Aug 28 10:22 loop7
crw-rw----   1 root disk     10, 237 Aug 28 10:22 loop-control
drwxr-xr-x   2 root root          60 Aug 28 10:22 mapper
crw-r-----   1 root kmem      1,   1 Aug 28 10:22 mem
crw-------   1 root root     10,  59 Aug 28 10:22 memory_bandwidth
brw-rw----   1 root disk    179,   0 Aug 28 10:22 mmcblk0
brw-rw----   1 root disk    179,   1 Aug 28 10:22 mmcblk0p1
brw-rw----   1 root disk    179,   2 Aug 28 10:22 mmcblk0p2
brw-rw----   1 root disk    179,   5 Aug 28 10:22 mmcblk0p5
brw-rw----   1 root disk    179,   6 Aug 28 10:22 mmcblk0p6
brw-rw----   1 root disk    179,   7 Aug 28 10:22 mmcblk0p7
drwxrwxrwt   2 root root          40 Jan  1  1970 mqueue
drwxr-xr-x   2 root root          60 Aug 28 10:22 net
crw-------   1 root root     10,  61 Aug 28 10:22 network_latency
crw-------   1 root root     10,  60 Aug 28 10:22 network_throughput
crw-rw-rw-   1 root root      1,   3 Aug 28 10:22 null
crw-------   1 root root    108,   0 Aug 28 10:22 ppp
crw-rw-rw-   1 root tty       5,   2 Aug 29 17:23 ptmx
drwxr-xr-x   2 root root           0 Nov  3  2016 pts
brw-rw----   1 root disk      1,   0 Aug 28 10:22 ram0
brw-rw----   1 root disk      1,   1 Aug 28 10:22 ram1
brw-rw----   1 root disk      1,  10 Aug 28 10:22 ram10
brw-rw----   1 root disk      1,  11 Aug 28 10:22 ram11
brw-rw----   1 root disk      1,  12 Aug 28 10:22 ram12
brw-rw----   1 root disk      1,  13 Aug 28 10:22 ram13
brw-rw----   1 root disk      1,  14 Aug 28 10:22 ram14
brw-rw----   1 root disk      1,  15 Aug 28 10:22 ram15
brw-rw----   1 root disk      1,   2 Aug 28 10:22 ram2
brw-rw----   1 root disk      1,   3 Aug 28 10:22 ram3
brw-rw----   1 root disk      1,   4 Aug 28 10:22 ram4
brw-rw----   1 root disk      1,   5 Aug 28 10:22 ram5
brw-rw----   1 root disk      1,   6 Aug 28 10:22 ram6
brw-rw----   1 root disk      1,   7 Aug 28 10:22 ram7
brw-rw----   1 root disk      1,   8 Aug 28 10:22 ram8
brw-rw----   1 root disk      1,   9 Aug 28 10:22 ram9
crw-rw-rw-   1 root root      1,   8 Aug 28 10:22 random
drwxr-xr-x   2 root root          60 Jan  1  1970 raw
crw-rw-r--   1 root netdev   10,  57 Aug 28 10:22 rfkill
drwxr-xr-x   4 root root          80 Aug 29 16:11 serial
lrwxrwxrwx   1 root root           7 Aug 28 10:22 serial1 -> ttyAMA0
drwxrwxrwt   2 root root          40 Aug 29 17:00 shm
drwxr-xr-x   3 root root         180 Aug 28 10:22 snd
lrwxrwxrwx   1 root root          15 Nov  3  2016 stderr -> /proc/self/fd/2
lrwxrwxrwx   1 root root          15 Nov  3  2016 stdin -> /proc/self/fd/0
lrwxrwxrwx   1 root root          15 Nov  3  2016 stdout -> /proc/self/fd/1
crw-rw-rw-   1 root tty       5,   0 Aug 28 12:13 tty
crw--w----   1 root tty       4,   0 Aug 28 10:22 tty0
crw-------   1 pi   tty       4,   1 Aug 28 10:23 tty1
crw--w----   1 root tty       4,  10 Aug 28 10:22 tty10
crw--w----   1 root tty       4,  11 Aug 28 10:22 tty11
crw--w----   1 root tty       4,  12 Aug 28 10:22 tty12
crw--w----   1 root tty       4,  13 Aug 28 10:22 tty13
crw--w----   1 root tty       4,  14 Aug 28 10:22 tty14
crw--w----   1 root tty       4,  15 Aug 28 10:22 tty15
crw--w----   1 root tty       4,  16 Aug 28 10:22 tty16
crw--w----   1 root tty       4,  17 Aug 28 10:22 tty17
crw--w----   1 root tty       4,  18 Aug 28 10:22 tty18
crw--w----   1 root tty       4,  19 Aug 28 10:22 tty19
crw--w----   1 root tty       4,   2 Aug 28 10:22 tty2
crw--w----   1 root tty       4,  20 Aug 28 10:22 tty20
crw--w----   1 root tty       4,  21 Aug 28 10:22 tty21
crw--w----   1 root tty       4,  22 Aug 28 10:22 tty22
crw--w----   1 root tty       4,  23 Aug 28 10:22 tty23
crw--w----   1 root tty       4,  24 Aug 28 10:22 tty24
crw--w----   1 root tty       4,  25 Aug 28 10:22 tty25
crw--w----   1 root tty       4,  26 Aug 28 10:22 tty26
crw--w----   1 root tty       4,  27 Aug 28 10:22 tty27
crw--w----   1 root tty       4,  28 Aug 28 10:22 tty28
crw--w----   1 root tty       4,  29 Aug 28 10:22 tty29
crw--w----   1 root tty       4,   3 Aug 28 10:22 tty3
crw--w----   1 root tty       4,  30 Aug 28 10:22 tty30
crw--w----   1 root tty       4,  31 Aug 28 10:22 tty31
crw--w----   1 root tty       4,  32 Aug 28 10:22 tty32
crw--w----   1 root tty       4,  33 Aug 28 10:22 tty33
crw--w----   1 root tty       4,  34 Aug 28 10:22 tty34
crw--w----   1 root tty       4,  35 Aug 28 10:22 tty35
crw--w----   1 root tty       4,  36 Aug 28 10:22 tty36
crw--w----   1 root tty       4,  37 Aug 28 10:22 tty37
crw--w----   1 root tty       4,  38 Aug 28 10:22 tty38
crw--w----   1 root tty       4,  39 Aug 28 10:22 tty39
crw--w----   1 root tty       4,   4 Aug 28 10:22 tty4
crw--w----   1 root tty       4,  40 Aug 28 10:22 tty40
crw--w----   1 root tty       4,  41 Aug 28 10:22 tty41
crw--w----   1 root tty       4,  42 Aug 28 10:22 tty42
crw--w----   1 root tty       4,  43 Aug 28 10:22 tty43
crw--w----   1 root tty       4,  44 Aug 28 10:22 tty44
crw--w----   1 root tty       4,  45 Aug 28 10:22 tty45
crw--w----   1 root tty       4,  46 Aug 28 10:22 tty46
crw--w----   1 root tty       4,  47 Aug 28 10:22 tty47
crw--w----   1 root tty       4,  48 Aug 28 10:22 tty48
crw--w----   1 root tty       4,  49 Aug 28 10:22 tty49
crw--w----   1 root tty       4,   5 Aug 28 10:22 tty5
crw--w----   1 root tty       4,  50 Aug 28 10:22 tty50
crw--w----   1 root tty       4,  51 Aug 28 10:22 tty51
crw--w----   1 root tty       4,  52 Aug 28 10:22 tty52
crw--w----   1 root tty       4,  53 Aug 28 10:22 tty53
crw--w----   1 root tty       4,  54 Aug 28 10:22 tty54
crw--w----   1 root tty       4,  55 Aug 28 10:22 tty55
crw--w----   1 root tty       4,  56 Aug 28 10:22 tty56
crw--w----   1 root tty       4,  57 Aug 28 10:22 tty57
crw--w----   1 root tty       4,  58 Aug 28 10:22 tty58
crw--w----   1 root tty       4,  59 Aug 28 10:22 tty59
crw--w----   1 root tty       4,   6 Aug 28 10:22 tty6
crw--w----   1 root tty       4,  60 Aug 28 10:22 tty60
crw--w----   1 root tty       4,  61 Aug 28 10:22 tty61
crw--w----   1 root tty       4,  62 Aug 28 10:22 tty62
crw--w----   1 root tty       4,  63 Aug 28 10:22 tty63
crw--w----   1 root tty       4,   7 Aug 28 10:22 tty7
crw--w----   1 root tty       4,   8 Aug 28 10:22 tty8
crw--w----   1 root tty       4,   9 Aug 28 10:22 tty9
crw--w----   1 root tty     204,  64 Aug 29 16:10 ttyAMA0
crw-------   1 root root      5,   3 Aug 28 10:22 ttyprintk
crw-rw----   1 root dialout 188,   1 Aug 29 16:11 ttyUSB1
crw-------   1 root root     10, 239 Aug 28 10:22 uhid
crw-------   1 root root     10, 223 Aug 28 10:22 uinput
crw-rw-rw-   1 root root      1,   9 Aug 28 10:22 urandom
drwxr-xr-x   3 root root          60 Aug 28 10:22 v4l
crw-rw----   1 root video   243,   0 Aug 28 10:22 vchiq
crw-rw----   1 root video   248,   0 Aug 28 10:22 vcio
crw-------   1 root root    249,   0 Aug 28 10:22 vc-mem
crw-rw----   1 root tty       7,   0 Aug 28 10:22 vcs
crw-rw----   1 root tty       7,   1 Aug 28 10:22 vcs1
crw-rw----   1 root tty       7,   2 Aug 28 10:22 vcs2
crw-rw----   1 root tty       7,   3 Aug 28 10:22 vcs3
crw-rw----   1 root tty       7,   4 Aug 28 10:22 vcs4
crw-rw----   1 root tty       7,   5 Aug 28 10:22 vcs5
crw-rw----   1 root tty       7,   6 Aug 28 10:22 vcs6
crw-rw----   1 root tty       7,   7 Aug 28 10:22 vcs7
crw-rw----   1 root tty       7, 128 Aug 28 10:22 vcsa
crw-rw----   1 root tty       7, 129 Aug 28 10:22 vcsa1
crw-rw----   1 root tty       7, 130 Aug 28 10:22 vcsa2
crw-rw----   1 root tty       7, 131 Aug 28 10:22 vcsa3
crw-rw----   1 root tty       7, 132 Aug 28 10:22 vcsa4
crw-rw----   1 root tty       7, 133 Aug 28 10:22 vcsa5
crw-rw----   1 root tty       7, 134 Aug 28 10:22 vcsa6
crw-rw----   1 root tty       7, 135 Aug 28 10:22 vcsa7
crw-rw----   1 root video   244,   0 Aug 28 10:22 vcsm
crw-rw-rw-   1 root root     10,  58 Aug 28 10:22 vcsm-cma
crw-rw----   1 root tty       7,  64 Aug 28 10:22 vcsu
crw-rw----   1 root tty       7,  65 Aug 28 10:22 vcsu1
crw-rw----   1 root tty       7,  66 Aug 28 10:22 vcsu2
crw-rw----   1 root tty       7,  67 Aug 28 10:22 vcsu3
crw-rw----   1 root tty       7,  68 Aug 28 10:22 vcsu4
crw-rw----   1 root tty       7,  69 Aug 28 10:22 vcsu5
crw-rw----   1 root tty       7,  70 Aug 28 10:22 vcsu6
crw-rw----   1 root tty       7,  71 Aug 28 10:22 vcsu7
crw-------   1 root root     10, 137 Aug 28 10:22 vhci
crw-rw----+  1 root video    81,   0 Aug 28 10:22 video10
crw-rw----+  1 root video    81,   1 Aug 28 10:22 video11
crw-rw----+  1 root video    81,   2 Aug 28 10:22 video12
crw-------   1 root root     10, 130 Aug 28 10:22 watchdog
crw-------   1 root root    251,   0 Aug 28 10:22 watchdog0
crw-rw-rw-   1 root root      1,   5 Aug 28 10:22 zero
pi@rp3b:~ $
monojk commented 3 years ago

Make sure that you put RxD to TxD, and TxD to RxD on the Pi. Then you need to make sure your Pi has created the symbolic links in the system. Reboot, and check you MH-z19 for the Orange light. Make sure I2C is enabled as well. On Sat, Aug 29, 2020 at 8:18 AM monojk @.***> wrote: The connection is according to the cabling in your README.md [image: 2020-08-29 16 12 20] https://user-images.githubusercontent.com/25121142/91638914-1b35a480-ea13-11ea-864a-d4e09e04b979.jpg — You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub <#22 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AEBLRRNY5D2HDRVOIU5N62LSDEE2TANCNFSM4QO7BF3Q .

Hm, the orange LED ist off. Should it be on if power is attached and/or flashing during transfer?

UedaTakeyuki commented 3 years ago

Hi, nincube8!

Thank you for your support. I think your point is very good. Indeed, in the result of "ls -la /dev" there are no serial0, it indicates that UART might not be working.

Hi, monojk! As mentioned by nincube8, most doubtful causes are:

Please make sure and tell me the result. By the way, it's midnight in Tokyo (where I live), sorry I'll get to sleep soon and the next reply might be about 8 hours after :-) thank you for your understanding.

monojk commented 3 years ago

All, thanks for the hint. I thought I had enabled serial, but it wasn#T. Now I am a step further:

pi@rp3b:~ $ cat mh-z19.py
#!/usr/bin/python -u
import mh_z19
print(mh_z19.read())
print(mh_z19.read_all())

and I get the following results. Notice that read() still returns None and sometimes there is an error:

pi@rp3b:~ $ sudo ./mh-z19.py
None
{'TT': 63, 'UhUl': 25856, 'SS': 0, 'temperature': 23, 'co2': 1441}
pi@rp3b:~ $ sudo ./mh-z19.py
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/serial/serialposix.py", line 490, in read
    'device reports readiness to read but returned no data '
serial.serialutil.SerialException: device reports readiness to read but returned no data (device disconnected or multiple access on port?)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/mh_z19/__init__.py", line 63, 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?)
None
{'temperature': 23, 'SS': 0, 'TT': 63, 'UhUl': 25856, 'co2': 1441}
UedaTakeyuki commented 3 years ago

It's a weird occurring serial reading error only in the mh_z19.read(), even not occurring it in mh_z19.read_all() which uses the same logic except for command string.

Indeed, on my side, the issue is not reproduced as follows:

pi@raspberrypi:~ $ cat mh-z19.py
#!/usr/bin/python -u
import mh_z19
print(mh_z19.read())
print(mh_z19.read_all())
pi@raspberrypi:~ $ sudo ./mh-z19.py 
{'co2': 751}
{'SS': 235, 'UhUl': 10356, 'TT': 61, 'co2': 751, 'temperature': 21}

I wonder there can be something instability of the serial connection, like loose physical cabling, unstable power supply, and so on.

nincube8 commented 3 years ago

Not saying this will work, but it's helped me with sensors in the past. Try using a 5k pull up resistor across the Vcc and TxD, and if that doesn't work, try putting the resistor across the RxD and Vcc lines.

On Sun, Aug 30, 2020 at 2:19 AM Dr. Takeyuki Ueda notifications@github.com wrote:

It's a weird occurring serial reading error only in the mh_z19.read(), even not occurring it in mh_z19.read_all() which uses the same logic except for command string.

Indeed, on my side, the issue is not reproduced as follows:

pi@raspberrypi:~ $ cat mh-z19.py

!/usr/bin/python -u

import mh_z19 print(mh_z19.read()) print(mh_z19.read_all()) pi@raspberrypi:~ $ sudo ./mh-z19.py {'co2': 751} {'SS': 235, 'UhUl': 10356, 'TT': 61, 'co2': 751, 'temperature': 21}

I wonder there can be something instability of the serial connection, like loose physical cabling, unstable power supply, and so on.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/UedaTakeyuki/mh-z19/issues/22#issuecomment-683391377, or unsubscribe https://github.com/notifications/unsubscribe-auth/AEBLRRMZJO456KEP4T4LRXLSDIDPXANCNFSM4QO7BF3Q .

nincube8 commented 3 years ago

Make sure that the MH-z19 is lighting up, it has a little laser inside it and if it's getting good power it will light up orange, that will tell you if the voltage is good or not.

On Sun, Aug 30, 2020 at 2:40 AM CK Gaming Channel cevatkose@gmail.com wrote:

Not saying this will work, but it's helped me with sensors in the past. Try using a 5k pull up resistor across the Vcc and TxD, and if that doesn't work, try putting the resistor across the RxD and Vcc lines.

On Sun, Aug 30, 2020 at 2:19 AM Dr. Takeyuki Ueda < notifications@github.com> wrote:

It's a weird occurring serial reading error only in the mh_z19.read(), even not occurring it in mh_z19.read_all() which uses the same logic except for command string.

Indeed, on my side, the issue is not reproduced as follows:

pi@raspberrypi:~ $ cat mh-z19.py

!/usr/bin/python -u

import mh_z19 print(mh_z19.read()) print(mh_z19.read_all()) pi@raspberrypi:~ $ sudo ./mh-z19.py {'co2': 751} {'SS': 235, 'UhUl': 10356, 'TT': 61, 'co2': 751, 'temperature': 21}

I wonder there can be something instability of the serial connection, like loose physical cabling, unstable power supply, and so on.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/UedaTakeyuki/mh-z19/issues/22#issuecomment-683391377, or unsubscribe https://github.com/notifications/unsubscribe-auth/AEBLRRMZJO456KEP4T4LRXLSDIDPXANCNFSM4QO7BF3Q .

monojk commented 3 years ago

Hi nincube8, the 4.7k pull up resistor across the Vcc and TxD helped. Now I am also getting a result from read(). Thanks.

The orange LED still doesn't light up. I measure a Vcc voltage of 4.8V

Thanks also to UedaTakeyuki for help.