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

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

  • I tried that, but its still not working. – Santi GS Jan 3 '17 at 21:06
  • Edited the question to make it more clear, try now – Matteo Jan 3 '17 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

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.