JamieB
Posts: 5
Joined: Mon Nov 05, 2012 1:40 pm

Executing GPIO python scripts through PHP

Tue Nov 27, 2012 8:37 pm

Hi all,

I'm a real noob at this but basically I want to be able to control the GPIO pins through a web interface.

I've written some basic python script that works when run as sudo, from ssh, but when I try and call it from a PHP form, I get this:

Traceback (most recent call last): File "/home/pi/gpio16on.py", line 3, in GPIO.setup(16, GPIO.OUT) File "/usr/local/lib/python2.7/dist-packages/RPi.GPIO-0.2.0-py2.7.egg/RPi/GPIO/__init__.py", line 102, in setup with open('/sys/class/gpio/export', 'w') as f: IOError: [Errno 13] Permission denied: '/sys/class/gpio/export'

I've tried to give myself access to the export file, but no joy.

Any ideas?

thsBavR10
Posts: 233
Joined: Sat Jul 21, 2012 3:11 pm

Re: Executing GPIO python scripts through PHP

Wed Nov 28, 2012 6:43 am

I guess, you have to call the python script as root, eg. with a preceding 'sudo '.

almarlow
Posts: 6
Joined: Thu Aug 02, 2012 12:47 am

Re: Executing GPIO python scripts through PHP

Thu Nov 29, 2012 6:43 am

Hi,

The solution I found to this problem is described in the following post. It may be appropriate in your situation.
http://www.raspberrypi.org/phpBB3/viewt ... 73#p138973

almarlow

Gachl
Posts: 5
Joined: Thu Nov 29, 2012 1:57 pm

Re: Executing GPIO python scripts through PHP

Thu Nov 29, 2012 2:27 pm

I usualy solve this by putting all my scripts in the sudo config file for the php user, so I can call all scripts from php with sudo.

For example (visudo):

Code: Select all

Cmnd_Alias GPIO_CMDS = /path/to/scripts1/*, /path/to/scripts2/*

www     ALL=(root) NOPASSWD: GPIO_CMDS
this way the www user can execute all files in /path/to/scripts1 and /path/to/scripts2 as root from anywhere with no password required.

JamieB
Posts: 5
Joined: Mon Nov 05, 2012 1:40 pm

Re: Executing GPIO python scripts through PHP

Tue Dec 11, 2012 1:29 pm

Thanks Guys.

I found the solution, it was because I was trying to execute using the root account, I gave the www-data access rights to run sudo, and it works great.

tgswilson
Posts: 2
Joined: Sun Dec 30, 2012 11:09 am

Re: Executing GPIO python scripts through PHP

Thu Jan 17, 2013 11:53 pm

>I gave the www-data access rights to run sudo
Can you please explain exactly what you mean by this?
The commands that you ran would be good :-)

willhjlangford
Posts: 1
Joined: Thu Feb 14, 2013 9:03 pm

Re: Executing GPIO python scripts through PHP

Thu Feb 14, 2013 9:11 pm

I got this working, first I ran:

Code: Select all

cd /etc/
sudo nano sudoers
*I added this line to the bottom of the file:*

Code: Select all

www-data ALL=(root) NOPASSWD:ALL
and in my php file I had:

Code: Select all

<html>
<?php
$result = exec("sudo python blink.py", $output");
?>
</html>
also I had to chmod my php and python script to 777.

Return to “Automation, sensing and robotics”