1

I have installed Apache2 and I am running Raspbian Jessie. Then I installed WiringPi and PHP and now I want to access the GPIO headers on Pi using the PHP Web page.

Here is how my page looks. I want to access GPIO 29 ( Pin No. 40 ) .

<?php 
system("gpio -g mode 29 out");
system("gpio -g write 29 1");
?>
7
  • 2
    You know this is a horribly bad idea to let Apache execute shell commands, right?
    – Aloha
    Commented Mar 27, 2016 at 0:44
  • @greenonline . Hey I just want to run the gpio pins using the code above. Any idea ? Commented Mar 30, 2016 at 19:00
  • What is not working? Your question is a little unclear. What errors do you get in the logs? Do you have any more code? If so where is the rest of the HTML/web page? What else have you tried? Do you have something (an LED) connected to the pin to check if it is HIGH or not? Does a simple php page work? Make sure that you can get a simple example (i.e.hello world) working first, before trying to access the GPIO. I will try to post a fuller answer tomorrow... Commented Mar 30, 2016 at 19:44
  • I just connect the GPIO 29 pin and GND to a LED. When I visit the page having <?php system("gpio -g mode 29 out"); system("gpio -g write 29 1"); ?> Then it turns the LED On. For eg. the script is named as on.php . So when I visit 192.168.1.102/on.php , It worked . But now this isint working after I installed the Updated version of raspbian i.e Jessie. Thank You. Commented Mar 30, 2016 at 21:25
  • Only just seen your reply - You need to prefix it with @<username> for the user to get a notification. BTW, it is always better to update your question, rather than put extra details in the comments, as it is then easier for other people to see all of the facts, without having to read through the comments. Anyway, you say that you installed an update Jessie. Maybe that update deleted your previous configuration? Did you do an update or a fresh install? I would double check your config of PHP and apache, to see if it is the same. If you did just an update then it should still be the same. Commented Mar 31, 2016 at 16:01

1 Answer 1

1

Please note

This is a very quick answer (a work in progress if you will), as I have not had time to test it, and it is way past my bed time. I will tidy up this answer tomorrow, once I have time to run it myself.

However...

I think (I may be wrong) that you are missing an export to expose the GPIO pins to PHP. You may need to do

gpio export 29

Thus, your script will become

<?php 
system("gpio export 29");
system("gpio -g mode 29 out");
system("gpio -g write 29 1");
?>

Try that.

I have just found a thread on the Pi forums, PHP GPIO Library, that says the same thing, so maybe I am correct. There are some other interesting points raised in that thread, which you may find useful.

However, the export command is not included in many other examples/tutorials, so maybe it is not the way forward. For example, from PHP on Raspberry Pi, this HTML/PHP script does not include the export:

<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_off = shell_exec("/usr/local/bin/gpio -g write 17 0");
        echo "LED is off";
    }
  ?>
 </body>
</html>

Note: As PandaLion98 says in their comment, getting scripts to run in Apache can be dangerous, if they are publicly facing.

2
  • hello thanks for the help. I'm new here and thanks for the suggestions. I don't get any errors. The only error is that the LED doesn't lightens up. Commented Mar 31, 2016 at 16:05
  • Greenonline I've posted another question. Please see it and help if possible. ThankYou. Commented Mar 31, 2016 at 16:06

Your Answer

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

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