1

I have been searching, and still can't find an answer that will work. I've tried the chmod, chown, visudo, wrapper script -> main script options, and I still cannot manipulate the GPIO pins from a webpage to python script. Basically I'm sending a GET request with a pin number and pin state (on/off) name/value pairing. I'm using the cgi python module to extract the values, then set up and change the state of a gpio pin.

Nothing wrong with the script since it works from a terminal with sudo. I also have cgitb enabled and it clearly shows the (apparently very common) /dev/mem permissions error. I have given apache and www-data and pi god-mode in the sudoers list with nopasswd, chmod'd /dev/mem..and just about every other suggestion I can find. Still no joy!

This is my cgi script:

#!/usr/bin/python
print "Content-Type: text/html\n\n"
import cgi
import cgitb; cgitb.enable() # Optional; for debugging only
import RPi.GPIO as gpio
gpio.setwarnings(False)

pins = [00,2,3,4] //ignore item 1, my button numbering starts at 1, so 0 is never referenced

arguments = cgi.FieldStorage()
pinNum = arguments.getlist("pnum")
pinState = arguments.getlist("pstate")

p = pinNum[0]
s = pinState[0]
gpio.setmode(gpio.BCM)

pin = pins[int(p)]
gpio.setup(int(pin), gpio.OUT)
gpio.output(int(pin), int(s))

This is output of cgitb:

/usr/lib/cgi-bin/main.py in ()
14 s = pinState[0]
15 gpio.setmode(gpio.BCM)
16 pin = pins[int(p)]
=> 17 gpio.setup(int(pin), gpio.OUT)
18 gpio.output(int(pin), int(s))

gpio = , gpio.setup = , builtin int = , pin = 2, gpio.OUT = 0
: No access to /dev/mem. Try running as root!
args = ('No access to /dev/mem. Try running as root!',)
message = 'No access to /dev/mem. Try running as root!'

BTW...Raspian Wheezy (May '15), updated and upgraded, Current RPi.GPIO version = 0.5.12a2, Python 2.7.3

So..any pointers would be greatly appreciated. Cheers.

  • 1
    "chmod'd /dev/mem" -> Giving whatever process the ability to do just about anything to the system. Beware that is an extremely insecure thing to do. I believe because of that there are in fact other mechanisms which restrict access, which may be why it won't work. It shouldn't. – goldilocks Jun 4 '15 at 12:48
  • "I have given apache and www-data and pi god-mode in the sudoers list" -> You might want to provide a link to where you got this idea because having permission to use sudo to do things requires you use sudo to do things, but that's not how your CGI script is run. – goldilocks Jun 4 '15 at 12:58
  • Yep...I just realised what you meant by that. I'm a dick...shot myself in the foot with that one didn't I? Got so focussed on the sudo aspect I didn't see the obvious. Unless I call the script with a proceeding "sudo", I could add God himself and it still wouldn't work. Lesson learned, cheers goldilocks :-) – justcheckinm8 Jun 5 '15 at 6:53
  • Hey none of us are perfect, lol. The difference between subtle and obvious is partly in the number of times you look at the picture. ;) – goldilocks Jun 5 '15 at 8:18
2

The RPi.GPIO module needs to be run as root.

I guess you are running it as the Apache user (I assume www-data from your comments).

People have got around the restriction using sudoers as you seem to have tried.

I don't know what you are doing wrong.

My pigpio library works differently. It has a daemon which runs as root. Python scripts run as an ordinary user.

There is a pigpio CGI example for Apache2 at http://abyz.me.uk/rpi/pigpio/examples.html#Python_code

0

While it wasn't available when the original question was asked, I stumbled upon this answer from google and wanted to comment on more recent developments.

/dev/gpiomem is now available to allow non-root access to gpios. Current versions of linux and RPi.GPIO can use it without modification and using sudo or changing access to /dev/mem is no longer required.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.