Peted
Posts: 7
Joined: Fri Feb 15, 2013 9:06 pm

CGI and GPIO /dev/mem error

Fri Feb 15, 2013 9:14 pm

I am trying to turn on and off GPIO ports via CGI scripts. The script I have is:

Code: Select all

#!/usr/bin/env python
import cgi
import cgitb; cgitb.enable()  
import RPi.GPIO as GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(11, GPIO.OUT)
GPIO.output(11, False)
print "Content-type: text/html"
print
print """
<html>
<head><title>light goes on</title></head>
<body>
<h3> light is on</h3>
</body>
</html>
"""
But when run this I get the following error:
<class 'RPi.GPIO.SetupException'> Python 2.7.3: /usr/bin/python
Fri Feb 15 20:58:41 2013

A problem occurred in a Python script. Here is the sequence of function calls leading up to the error, in the order they occurred.

/usr/lib/cgi-bin/test.py in ()
4 import cgitb; cgitb.enable() # for troubleshooting
5
=> 6 import RPi.GPIO as GPIO
7 GPIO.setwarnings(False)
8 GPIO.setmode(GPIO.BOARD)
RPi undefined, GPIO undefined
<class 'RPi.GPIO.SetupException'>: 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!'

Is there any way the www-data user can have access to this resource or is there another way for this to work? I am a beginner at all this so please keep the replies simple!

Any help would be great.

Many thanks

-rst-
Posts: 1316
Joined: Thu Nov 01, 2012 12:12 pm
Location: Dublin, Ireland

Re: CGI and GPIO /dev/mem error

Wed Feb 20, 2013 1:19 pm

You could possibly split your scripts into 'CGI wrapper' and 'GPIO script' - make the CGI script to run the GPIO one using os.system('sudo <scriptname>').

Or you could look at using the wiringPi library: "wiringPi has the ability to use the traditional /sys/class/gpio/ style interface, and if the GPIO pins have been exported and had their ownership changed appropriately, then applications using the wiringPi library, can be run without root privileges" https://projects.drogon.net/raspberry-pi/wiringpi/
http://raspberrycompote.blogspot.com/ - Low-level graphics and 'Coding Gold Dust'

Return to “General programming discussion”