Skip to content

How to use without root permission.

Dr. Takeyuki Ueda edited this page May 16, 2020 · 7 revisions

Steps

1. Check the link source of /dev/serial0.

The device file for uart which mh_z19 is connected is a symbolic link. You can check the link source by ls -la /dev/serial0 as follow:

pi@raspberrypi:~/mh-z19 $ ls -la /dev/serial0
lrwxrwxrwx 1 root root 7 May 16 20:24 /dev/serial0 -> ttyAMA0

In the above example, the link source is /dev/ttyAMA0

2. Make sure the group permission of the link source has read permission.

Check group permission of the link source by **ls -la.

pi@raspberrypi:~/mh-z19 $ ls -la /dev/ttyAMA0
crw--w---- 1 root tty 204, 64 May 16 22:44 /dev/ttyAMA0

In the above example, the first crw--w---- indicate permission and 5th - indicate that this file doesn't have the read permission. For detail, refer man ls.

In case the link source doesn't have the read permission, add it by chmod command with sudo as follows:

pi@raspberrypi:~/mh-z19 $ sudo chmod g+r /dev/ttyAMA0

g+r can be red as Group permission + Read. The result must be as follows:

pi@raspberrypi:~/mh-z19 $ ls -la /dev/ttyAMA0
crw-rw---- 1 root tty 204, 64 May 16 22:49 /dev/ttyAMA0

At this time, the 5th r means read group permission is granted.

3. Read sensor with serial_console_untouched option.

By default, mh_z19 library automatically stops the getty to open the serial console for mh_z19 before reading the sensor value and restart getty after reading sensor value. After this stopping and restarting getty make the group read permission of the uart device file lost. To avoid this loss of the permission, use serial_console_untouched to block getty stopping and restarting.

use from console

Use mh_z19 with --serial_console_untouched option.

pi@raspberrypi:~ $ python -m mh_z19 --serial_console_untouched
{"co2": 508}

use from program

Call mh_z19.read with serial_console_untouched keyword parameter as True.

Python 2.7.16 (default, Apr  6 2019, 01:42:57) 
[GCC 8.2.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import mh_z19
>>> mh_z19.read(serial_console_untouched=True)
{'co2': 505}