0

I want to control a LED using PHP on a Raspberry Pi 3 via web page. I have installed Raspbian on RPi 3. I have installed apache2 php5 libapache2-mod-php5 and the wiring Pi library. I can access my web page from another computer.

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

I am getting this error "http 500 error" What could be wrong ? Do you see any mistake in my code? What could be the issues ?

6
  • If you are getting a 500 error, the very first diagnostic step is to examine your server error log to see if there is any information that might point to the source of the problem. Commented Nov 30, 2017 at 14:30
  • if I put this <?php echo "hello world"; ?> I don't get any error but when I try with code in question then I get 500 error Commented Nov 30, 2017 at 14:39
  • Is this helpful StackOverflow: Execute a shell script in php Commented Nov 30, 2017 at 15:06
  • PHP is probably the wrong tool for this, in addition to whatever specific problem you're having here. PHP runs when the page loads. You usually want this sort of action to happen when the user takes action - in your case clicks a button - from a page that already loaded. Commented Nov 30, 2017 at 15:20
  • Your code works fine for me if I divide it into two files with the form as test.html and the php script as gpio.php as you've named already... Default installation of apache2 and libapache2-mod-php on Raspbian Stretch installed on a RPi Zero. Commented Nov 30, 2017 at 18:06

1 Answer 1

1

You need to read your server error log as larsks say. However, you can avoid having to make system calls. I leave you a link to a library to be able to use the LEDs directly with fwrite() functions instead of shell_exec(). I hope it can serve you: https://github.com/vivesweb/led_raspberry

1
  • Could you please indicate if you are the author of the library? Commented Sep 14, 2021 at 12:09

Your Answer

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.