I have made a very small script for my Raspberry Pi where I turn on the LED.
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(21,GPIO.OUT)
GPIO.output(21,1)
time.sleep(5)
GPIO.output(21,0)
GPIO.cleanup()
The script works I checked it with sudo python led.py
I'm using apache server on my rapberry pi. Python version is 2.7.3
index.php:
<?php
if(isset($_GET['on'])){
echo "on"
exec('sudo python /var/www/led.py');
}
else if(isset($_GET['off'])){
echo "off";
}
?>
<html>
<head>
</head>
<body>
<form method="get">
<button name="on">on</button><br>
<button name="off">on</button><br>
</form>
</body>
</html>
I tried with echo exec(whoami);
1 Answer 1
It is all to do with permissions.
The scipt works locally because it is run as the (presumably) pi
user. The pi
user has (passwordless) access to the sudo
command to run programs as root
. The RPi.GPIO module requires to be run as root
.
It fails in your script because it runs as the (Apache) www-data
user. The www-data
user in not a member of sudoers
so can not run sudo
. Thus the RPi.GPIO script fails as you don't have root access.
There are ways around this but all the ones I know of are pretty insecure and will compromise the security of your Pi.
-
I only use my pi on my own network... Do you know another method for enabling remote controkGProduct– GProduct2015年07月24日 17:25:20 +00:00Commented Jul 24, 2015 at 17:25
-
@GProduct Google will reveal dozens of ways depending on how heavyweight a solution you want. Personally I use my own pigpio Python module as scripts run as an ordinary user (no sudo required) and communicate with a daemon which runs as root.joan– joan2015年07月24日 17:42:38 +00:00Commented Jul 24, 2015 at 17:42
-
Okay I'm gonna take a look at itGProduct– GProduct2015年07月24日 17:45:30 +00:00Commented Jul 24, 2015 at 17:45