1

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);

techraf
4,35310 gold badges33 silver badges43 bronze badges
asked Jul 24, 2015 at 16:28

1 Answer 1

2

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.

techraf
4,35310 gold badges33 silver badges43 bronze badges
answered Jul 24, 2015 at 17:06
3
  • I only use my pi on my own network... Do you know another method for enabling remote controk Commented 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. Commented Jul 24, 2015 at 17:42
  • Okay I'm gonna take a look at it Commented Jul 24, 2015 at 17:45

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.