Running GPIO fan control script for RPI4 using Manjaro and python 3

Hi all, I’m new to Manjaro.
Just installed on an RPI4 and everything works well apart from…
I have a python 3 script that controls a small fan on my RPI4 using a GPIO pin, which starts like this:

#!/usr/bin/env python3
import time
from gpiozero import OutputDevice

For that to work, it has to import from module gpiozero and despite installing the following packages, verified using “pacman - Qe”, it fails to run:
python-gpiozero 1.6.2-1
python-raspberry-gpio 0.7.0-2

The screen full of errors I get when posting this is printed below:

Traceback (most recent call last):
File “/usr/lib/python3.9/site-packages/gpiozero/pins/pi.py”, line 101, in pin
pin = self.pins[n]
KeyError: 17

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “/home/fat/Documents/pi-fan-controller/RPIfancontrol.py”, line 35, in
fan = OutputDevice(GPIO_PIN)
File “/usr/lib/python3.9/site-packages/gpiozero/devices.py”, line 108, in call
self = super(GPIOMeta, cls).call(*args, **kwargs)
File “/usr/lib/python3.9/site-packages/gpiozero/output_devices.py”, line 83, in init
super(OutputDevice, self).init(pin, pin_factory=pin_factory)
File “/usr/lib/python3.9/site-packages/gpiozero/mixins.py”, line 85, in init
super(SourceMixin, self).init(*args, **kwargs)
File “/usr/lib/python3.9/site-packages/gpiozero/devices.py”, line 549, in init
pin = self.pin_factory.pin(pin)
File “/usr/lib/python3.9/site-packages/gpiozero/pins/pi.py”, line 103, in pin
pin = self.pin_class(self, n)
File “/usr/lib/python3.9/site-packages/gpiozero/pins/rpigpio.py”, line 111, in init
GPIO.setup(self.number, GPIO.IN, self.GPIO_PULL_UPS[self._pull])
RuntimeError: Not running on a RPi!

Any ideas how to make this work or will I have to build an external circuit to drive the fan and forget about GPIO on the RPI4 under Manjaro? it has worked fine on both Lubuntu and Ubuntu so far.

Regards,
AV.

The script uses an unreleased version of the GPIO library.

You need a pre-release version of the python-raspberry-gpio library:

OK I’ve installed RPi.GPIO V 0.7.1a4 (pre-release) and I now get this when I run the script (as SU) - I have no clue what colorzero is and it is not referenced in the script:

[fat@RPI4 pi-fan-controller]$ sudo python3 RPIfancontrol.py
[sudo] password for fat:
Traceback (most recent call last):
File “/home/fat/Documents/pi-fan-controller/RPIfancontrol.py”, line 5, in
from gpiozero import OutputDevice
File “/usr/lib/python3.9/site-packages/gpiozero/init.py”, line 79, in
from .output_devices import (
File “/usr/lib/python3.9/site-packages/gpiozero/output_devices.py”, line 26, in
from colorzero import Color
ModuleNotFoundError: No module named ‘colorzero’

colorzero is another python module, that would be a dependency of your script.

You can find it on pypi, or in the AUR.

OK I’ve installed that too. But still get loads of errors (below).
Its looking like this is a waste of time and I’m going to revert to Ubuntu; I’d hoped that a Manjaro distro specifically targeting the RPI4 would include the basics needed to access the pins on it…

[fat@RPI4 pi-fan-controller]$ sudo python3 RPIfancontrol.py
/usr/lib/python3.9/site-packages/gpiozero/devices.py:288: PinFactoryFallback: Falling back from rpigpio: No module named ‘RPi’
warnings.warn(
/usr/lib/python3.9/site-packages/gpiozero/devices.py:288: PinFactoryFallback: Falling back from lgpio: No module named ‘lgpio’
warnings.warn(
/usr/lib/python3.9/site-packages/gpiozero/devices.py:288: PinFactoryFallback: Falling back from rpio: No module named ‘RPIO’
warnings.warn(
/usr/lib/python3.9/site-packages/gpiozero/devices.py:288: PinFactoryFallback: Falling back from pigpio: No module named ‘pigpio’
warnings.warn(
/usr/lib/python3.9/site-packages/gpiozero/devices.py:285: NativePinFactoryFallback: Falling back to the experimental pin factory NativeFactory because no other pin factory could be loaded. For best results, install RPi.GPIO or pigpio.
warnings.warn(NativePinFactoryFallback(native_fallback_message))

The gpiozero package, as you might have noticed can use several different factories for getting control over the GPIO. I would first make sure that you are executing in the correct environment (where RPi.GPIO is installed) and if you can’t get that to work you might consider to test install pigpio. I haven’t used it myself, but it is one of the factories gpiozero is using and it is available at pigpio@AUR.

No, I gave up and coded an executable in sysfs C ( no dependencies = no hassle ) which works fine; it just reads the temperature on wafer and toggles GPIO 14 depending on its value. I now just need to run this on startup but it will only run with SU privileges.

How can I run this executable using Autostart?

If it was me I would create a .service file calling your binary and enable it. You can do other things but another way to remove sudo dependency and call a script from your autostart when you log in. I do not know exactly what you are using but if it involves the gpio pins I would think this would work.

sudo groupadd gpio
sudo usermod -a -G gpio your_user_name
sudo chown root.gpio /dev/gpiomem
sudo chmod g+rw /dev/gpiomem

Test to see if it works with out sudo

Create a .desktop file in ~/.config/autostart calling a script with your command in it. I guess you might be able to do in the Startup GUI.

Reboot

I did all the above but still fails to run without sudo.
I also tried logging out / back in to see if it would fun from the terminal without sudo, but no luck.