1

I am making a project that uses a web-server and a basic PHP script to turn on a GPIO pin.

Here is my code:

<html>
<head>
<meta name= "viewport" content="width=device-width" />
<title> LED Control </title>
</head>
 <body>
 LED Control:
 <form method= "get" action = "gpio.php">
 <input type="submit" value="ON" name="on">
 <input type="submit" value="OFF" name="off">
 </form>
 <?php
 $setmode17 = shell_exec("/usr/local/bin/gpio -g mode 17 out")
 if (isset($_GET['on'])){
 $gpio_on = shell_exec("/usr/local/bin/gpio -g write 17 1")
 echo "LED is on";
 }
 else if (isset($_GET['off'])){
 $gpio_on = shell_exec("/usr/local/bin/gpio -g write 17 0")
 }
 ?>
 </body>
</html>

The GPIO pin does not turn on. I am also using the wiring pi library. What is the problem?

Here is my update code:

<html>
<head>
 <meta name= "viewport" content="width=device-width" />
 <title> LED Control </title>
 </head>
 <body>
 LED Control:
 <form method= "get" action = "gpio.php">
 <input type="submit" value="ON" name="on">
 <input type="submit" value="OFF" name="off">
 </form>
 <?php
 $setmode17 = shell_exec("/usr/bin/sudo/gpio -g mode 17 out")
 if (isset($_GET['on'])){
 $gpio_on = shell_exec("/usr/bin/sudo/gpio -g write 17 1")
 echo "LED is on";
 }
 else if (isset($_GET['off'])){
 $gpio_on = shell_exec("/usr/bin/sudo/gpio -g write 17 0")
 }
 ?>
 </body>
 </html>

and added this line to visudo: www-data ALL=(ALL) NOPASSWD: /usr/local/bin/gpio

asked Jan 3, 2017 at 19:47

3 Answers 3

1

You may need to allow the www-data user to run that command as root.

Open the sudoers file with sudo visudo and add at the bottom www-data ALL=(ALL) NOPASSWD: /usr/local/bin/gpio, then save.

Now prepend /usr/bin/sudo to the commands in the php files and it should work, like so/usr/bin/sudo /usr/local/bin/gpio -g mode 17 out

answered Jan 3, 2017 at 20:08
2
  • I tried that, but its still not working. Commented Jan 3, 2017 at 21:06
  • Edited the question to make it more clear, try now Commented Jan 3, 2017 at 21:43
0

I was having similar issues, if you are using apache do the following and then reboot your pi:

sudo adduser www-data i2c

sudo adduser www-data spi

sudo adduser www-data gpio

edit - I literally figured this out for myself within the past hour. Luckily I stumbled accross this tidbit while reading the install instructions for WiringPi-PHP

answered Jan 4, 2017 at 6:30
0

Are you positive that you've got your command correct?

If you run /usr/bin/sudo/gpio -g write 17 0 via ssh does it work as expected? I suspect you've got the path slightly incorrect - try removing that slash between sudo and gpio, like this /usr/bin/sudo gpio -g write 17 0

Providing you've added the www-data to the gpio group, I don't see any reason why it wouldn't work as expected.

If you're still stuck, I'll make a shameless plug for a library I've been working on, which allows a simpler OO approach to interfacing with the GPIO. There are also some examples in there for implementing asynchronous communication between the web browser and pi if you chose to go down that road.

answered Jan 6, 2017 at 9:52

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.