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

Raspberry Pi 5 Support #53

Closed rewse closed 2 months ago

rewse commented 2 months ago

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
UedaTakeyuki commented 2 months ago

Thank you for your information about the deprecation of RPi.GPIO. I will try a little hard work :-)

UedaTakeyuki commented 2 months ago

Hi rewse, I've updated this to use the gpiozero and uploaded it to the testpypi instead of pypi.

Would you please confirm this and tell me the result? Thank you for your contribution.

site of testpypi How to update mh-z19 from testpypi

pip install --index-url https://test.pypi.org/simple/ mh-z19

After it is confirmed, I'll upload this to pypi.

rewse commented 2 months ago

Thank you for the super fast fix!

The following error occurred.

PinFactoryFallback: Falling back from lgpio: No module named 'lgpio' ``` (mhz19) tats@fox:~$ sudo python3 -m mh_z19 --pwm /var/lib/python/mhz19/lib/python3.12/site-packages/gpiozero/devices.py:300: PinFactoryFallback: Falling back from lgpio: No module named 'lgpio' warnings.warn( Traceback (most recent call last): File "/var/lib/python/mhz19/lib/python3.12/site-packages/gpiozero/pins/pi.py", line 411, in pin pin = self.pins[info] ~~~~~~~~~^^^^^^ KeyError: PinInfo(number=32, name='GPIO12', names=frozenset({'J8:32', 'BOARD32', 'WPI26', 'GPIO12', '12', 12, 'BCM12'}), pull='', row=16, col=2, interfaces=frozenset({'', 'i2c', 'dpi', 'spi', 'uart', 'pwm', 'gpio'})) During handling of the above exception, another exception occurred: Traceback (most recent call last): File "", line 198, in _run_module_as_main File "", line 88, in _run_code File "/var/lib/python/mhz19/lib/python3.12/site-packages/mh_z19/__main__.py", line 104, in print (mh_z19.read_from_pwm(gpio=args.pwm_gpio, range=args.pwm_range)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/var/lib/python/mhz19/lib/python3.12/site-packages/mh_z19/__init__.py", line 215, in read_from_pwm return pwm.read_from_pwm_with_gpiozero(gpio=12, range=5000) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/var/lib/python/mhz19/lib/python3.12/site-packages/mh_z19/pwm.py", line 50, in read_from_pwm_with_gpiozero btn = Button(gpio) ^^^^^^^^^^^^ File "/var/lib/python/mhz19/lib/python3.12/site-packages/gpiozero/devices.py", line 108, in __call__ self = super().__call__(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/var/lib/python/mhz19/lib/python3.12/site-packages/gpiozero/input_devices.py", line 412, in __init__ super().__init__( File "/var/lib/python/mhz19/lib/python3.12/site-packages/gpiozero/mixins.py", line 417, in __init__ super().__init__(*args, **kwargs) File "/var/lib/python/mhz19/lib/python3.12/site-packages/gpiozero/input_devices.py", line 162, in __init__ super().__init__( File "/var/lib/python/mhz19/lib/python3.12/site-packages/gpiozero/mixins.py", line 243, in __init__ super().__init__(*args, **kwargs) File "/var/lib/python/mhz19/lib/python3.12/site-packages/gpiozero/input_devices.py", line 79, in __init__ super().__init__(pin, pin_factory=pin_factory) File "/var/lib/python/mhz19/lib/python3.12/site-packages/gpiozero/devices.py", line 553, in __init__ pin = self.pin_factory.pin(pin) ^^^^^^^^^^^^^^^^^^^^^^^^^ File "/var/lib/python/mhz19/lib/python3.12/site-packages/gpiozero/pins/pi.py", line 413, in pin pin = self.pin_class(self, info) ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/var/lib/python/mhz19/lib/python3.12/site-packages/gpiozero/pins/rpigpio.py", line 101, in __init__ GPIO.setup(self._number, GPIO.IN, self.GPIO_PULL_UPS[self._pull]) RuntimeError: Cannot determine SOC peripheral base address ```

However, I got a correct value after installing lgpio with reference to https://github.com/gpiozero/gpiozero/issues/1120 .

(mhz19) tats@fox:~$ sudo pip3 install --index-url https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple/ lgpio
(mhz19) tats@fox:~$ sudo pip3 list
Package            Version
------------------ --------
certifi            2024.2.2
charset-normalizer 3.3.2
colorzero          2.0
getrpimodel        0.1.24
gpiozero           2.0.1
idna               3.7
lgpio              0.2.2.0
mh_z19             3.1.5
pip                24.0
pyserial           3.5
requests           2.31.0
RPi.GPIO           0.7.1
setuptools         69.5.1
urllib3            2.2.1
(mhz19) tats@fox:~$ sudo python3 -m mh_z19 --pwm
{'co2': 470.0}
UedaTakeyuki commented 2 months ago

Thank you for your continued cooperation in providing useful information. The gpiozero seems to depend on the lgpio, however, gpiozero's setup.py does not have this dependency description. So, I'll add lgpio to mh-z19's setup.py in addition to gpiozero, and upload to the pypi.

UedaTakeyuki commented 2 months ago

I've uploaded my latest package to the PyPI as version 3.1.5.

rewse commented 2 months ago

Confirmed. Thank you!

(mhz19-prod) tats@fox:~$ pip3 install mh_z19
(mhz19-prod) tats@fox:~$ pip list
Package            Version
------------------ --------
certifi            2024.2.2
charset-normalizer 3.3.2
colorzero          2.0
getrpimodel        0.1.24
gpiozero           2.0.1
idna               3.7
lgpio              0.2.2.0
mh_z19             3.1.5
pip                24.0
pyserial           3.5
requests           2.31.0
RPi.GPIO           0.7.1
setuptools         69.5.1
urllib3            2.2.1
(mhz19-prod) tats@fox:~$ sudo python3 -m mh_z19 --pwm
{'co2': 485.0}