Jump to content

OPiZero and GPIO Rootless access from python


jscax

Recommended Posts

Hi all,
I'm trying to access the GPIO from python context and I absolutely need to do it in a rootless way. (WSGI apache module)

I'm a newbie and noticed that this is the library command causing the error inside the python library:

Code: Select all

fd = open("/dev/mem", O_RDWR);

I do have /dev/mem on my armbian distro and clearly the access to /dev/mem is root limited, what's worst I don't have /dev/gpiomem 

Is there a way to create /dev/gpiomem so I can add the apache user to gpio group and solve this?
Do I need to configure and compile a kernel by myself?

please help me, thank you

Link to comment
Share on other sites

I googled the world searching for a way to access GPIO without root in the userspace but I can't find anything useful

 

can anyone help?

I even tried changing permission of /dev/mem just to try with chmod 777, chown but no way to access GPIO without sudo.

Link to comment
Share on other sites

my findings about my Orange Pi Zero so far...

 

create group gpio if you do not have one and then add the users you need to it

 

vim /lib/udev/rules.d/60-python3-gpio.rules:

KERNEL=="spidev*", GROUP="spi", MODE="0660"
SUBSYSTEM=="gpio*", PROGRAM="/bin/sh -c 'chown -R root:gpio /sys/class/gpio && chown -R root:gpio /sys/devices/platform/soc/1c20800.pinctrl && chown -R root:gpio /sys/devices/platform/soc/1f02c00.pinctrl && chmod -R ug+rw /sys/class/gpio && chmod -R ug+rw /sys/devices/platform/soc/1c20800.pinctrl && chmod -R ug+rw /sys/devices/platform/soc/1f02c00.pinctrl'"

Reboot to test it directely from bash.

 

Then this library had my attention: https://github.com/rm-hull/OPi.GPIO

I had to patch it because as it is it was not working and I had once again a permission issue from userspace.

 

Inside OPi.GPIO/OPi/sysfs.py you can import time then add a little bit of sleep:

def direction(pin, dir):
assert dir in [IN, OUT]
#insert sleep
time.sleep(0.1)
path = "/sys/class/gpio/gpio{0}/direction".format(pin)
with open(path, "w") as fp:
if dir == IN:
fp.write("in")
else:
fp.write("out")

In this way the library is working rootless, in the userspace.

 

This solution works for my relays but I don't know if I can read DHT22 sensor with this library (implementing all the stuff eventually).

If I understood correctly this is "slow" way to access GPIO not compatible with DHT22 readings.

A workaround should be I2C sensor (which I have and I plan to integrate in the future) and for which I hope I will have rootless access with no issue.

 

At the moment for the DHT22 readings I've gone the visudo sudoers way so the www-data user can run a bash script from python context as root.

I feel dirty a little bit I have to work this out in another way.

Link to comment
Share on other sites

on Orangepi One you have to add a file like /etc/udev/rules.d/99-gpio.rules

SUBSYSTEM=="gpio*", PROGRAM="/bin/sh -c '\
    chown -R root:gpio /sys/class/gpio && chmod -R 770 /sys/class/gpio;\
    chown -R root:gpio /sys/devices/platform/sunxi-pinctrl/gpio && chmod -R 770 /sys/devices/platform/sunxi-pinctrl/gpio;\
    '"

add a group gpio and add the user to it, then you have rootless access to gpio

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
×
×
  • Create New...

Important Information

Terms of Use - Privacy Policy - Guidelines