0
skematic

GPIO not working for non root

Recommended Posts

I want to use my GPIO pins on my OPi lite. After much research and testing, I have done the following

- Add user pi to group gpio

-  Changed /etc/udev/rules.d/99-gpio.rules to

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/soc/1c20800.pinctrl/gpiochip0/gpio && chmod -R 770 /sys/devices/platform/soc/1c20800.pinctrl/gpiochip0/gpio;?\
    '"????

Now, accessing GPIO pins is working fine via bash:

(env) pi@orangepilite:~$ echo 65 > /sys/class/gpio/export
(env) pi@orangepilite:~$ echo out > /sys/class/gpio/gpio65/direction
(env) pi@orangepilite:~$ echo 1 > /sys/class/gpio/gpio65/value

The LED connected to the corresponding pin lights up nicely.

 

The problem is using Python to control the pin. I'm using the python module OrangePi.GPIO

(env) pi@orangepilite:~$ python
>>> import OPi.GPIO as GPIO
>>> GPIO.setboard (GPIO.PCPCPLUS)
>>> GPIO.setmode (GPIO.BOARD)
>>> GPIO.setup (21, GPIO.OUT)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
RuntimeError: No access to /dev/mem.  Try running as root!

This error is only there when trying to run the script from user pi. On root it's working fine.

 

How do I access gpio pins on a non root user in python?

 

Edited by skematic
Linked to the wrong python module

Share this post


Link to post
Share on other sites
1 hour ago, martinayotte said:

Change permissions of /dev/mem ...

I have changed the permission for group and others to rw (so same permissions as user root)

@orangepilite:~$ ls -l /dev/mem
crw-rw-rw- 1 root kmem 1, 1 Nov  8 14:01 /dev/mem

However I'm stil getting No access to /dev/mem. Also, by opening access to /dev/mem, could there be any safety issues?

Share this post


Link to post
Share on other sites
47 minutes ago, skematic said:

Also, by opening access to /dev/mem, could there be any safety issues?

Right ! I digged the issue by trying out myself, and figured out by searching the web that it would requires to compile kernel with "CONFIG_STRICT_DEVMEM=n"

Maybe using setuid() method inside the python library should fix that ... I will try it this afternoon ...

Share this post


Link to post
Share on other sites
4 hours ago, skematic said:

No access to /dev/mem.

Ok ! I've found the recipe :

chmod 4775 /usr/bin/python

If you wish to restrict the security, you should probably copy /usr/bin/python into /usr/bin/python-as-root and doing "chmod 4775 /usr/bin/python-as-root" and running your GPIO python code with this one.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
0