volt220
Posts: 2
Joined: Wed May 21, 2014 8:30 am

How to access GPIO without root

Wed May 21, 2014 8:40 am

Hi, Just installed NOOBS and now try to use some IO access.
I need to run without sudo as I later will use a web server.

Can't really figure out whats wrong.
A quite common question but the other post didn't really help me.

Export the IO:
> sudo sh -c 'echo 17 > /sys/class/gpio/export'

> ll /sys/class/gpio/
-rwxrwx--- 1 root gpio 4096 May 21 08:10 export
lrwxrwxrwx 1 root gpio 0 May 21 08:04 gpio17 -> ../../devices/virtual/gpio/gpio17
lrwxrwxrwx 1 root gpio 0 Jan 1 1970 gpiochip0 -> ../../devices/virtual/gpio/gpiochip0
-rwxrwx--- 1 root gpio 4096 Jan 1 1970 unexport

> cat /etc/group | grep pi
adm:x:4:pi
dialout:x:20:pi
cdrom:x:24:pi
sudo:x:27:pi
audio:x:29:pi
video:x:44:pi
plugdev:x:46:pi
games:x:60:pi
users:x:100:pi
pi:x:1000:
ssh:x:103:pi
netdev:x:105:pi
input:x:999:pi
spi:x:1002:pi
gpio:x:1003:pi

The python script:

Code: Select all

#!/usr/bin/env python
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.OUT, initial=GPIO.LOW)
I get this stacktrace when running the above script:
> python test.py
Traceback (most recent call last):
File "test.py", line 5, in <module>
GPIO.setup(17, GPIO.OUT, initial=GPIO.LOW)
RuntimeError: No access to /dev/mem. Try running as root!

If I instead tries to setup the pin outside the python script:
> echo in > /sys/class/gpio/gpio17/direction
> cat /sys/class/gpio/gpio11/value
0
> python test.py
Traceback (most recent call last):
File "test.py", line 5, in <module>
print "read 11: " + str(GPIO.input(17))
RuntimeError: You must setup() the GPIO channel first


I appreciate if someone could explain how this works or hint me the correct direction.
Feels like the export stuff have no impact at all on the python lib RPi?

Thanks!


User avatar
georgehudson
Posts: 10
Joined: Tue May 20, 2014 10:41 am
Location: ohio

Re: How to access GPIO without root

Fri May 23, 2014 9:45 am

Either you have to run your application as root, or use a device driver than trying to use /dev/mem

volt220
Posts: 2
Joined: Wed May 21, 2014 8:30 am

Re: How to access GPIO without root

Sat May 24, 2014 6:09 am

Thanks, obviously I mixed it up (export and /dev/mem).
So there are two alternatives to acess the IO (mmap and sysfs).
To access the mmap you always need sudo permission, correct?!
In NOOBS the pi user have permission to access the sysfs (by default).

The RPi.GPIO lib changed to /dev/mem in version 0.3.0a, the lastest version is now 0.5.5.
https://pypi.python.org/pypi/RPi.GPIO

The wiring lib use /dev/mem but should also be able to run in sysfs.
I'm unable to run wiring2 in the sysfs mode (due to Segmentation fault).

> sudo pip install wiringpi2

Setup port:

Code: Select all

#/bin/bash
echo 18 > /sys/class/gpio/export
echo out > /sys/class/gpio/gpio18/direction
echo 1 > /sys/class/gpio/gpio18/active_low
Execute the script:

Code: Select all

#!/usr/bin/env python
import wiringpi2 as wiringpi 
wiringpi.wiringPiSetupSys()
print "write 1 > 18"
wiringpi.digitalWrite(18, 1)
The output:
write 1 > 18
Segmentation fault

Currently I stick to sysfs, have to investigate wiring later.


If someone are interested about benchmark (even if it's quite old)
http://codeandlife.com/2012/07/03/bench ... pio-speed/

If someone have nice lib or solution please post!

User avatar
joan
Posts: 14257
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: How to access GPIO without root

Sat May 24, 2014 6:14 am

Use the pigpio Python module. It does not force you to run Python as root.

Return to “General programming discussion”