0

I have an apache web server installed in my Raspberry Pi. I created an index.php file on the /var/www/html folder. Whenever I access my Pi's IP address on a web browser to control the lights, none of my commands seem to work after I press the buttons.

My index.php file is as follows:

<html>
<head>
<?php

shell_exec("gpio -g  mode 4 out");

$btn1 = exec('gpio -g read 4');


if(isset($_POST['l1'])) {
    if($btn1 == 1) {
        shell_exec("gpio -g write 4 0");
    }
    else {
        shell_exec("gpio -g write 4 1");
    }
}
?>

<title> LIGHT DEMO CONTROLS</title>
</head>

<body style= "background-color: rgb(0,44,165);" align = "center"><br><font size = "10" face = "Segoe UI" color = "white"><center>
LIGHT CONTROLS<br> <LIGHT CONTROLS<br>--</center></font><br><form method = "post">
    <table style = "width:600px; margin-left: auto; 
    margin-right: auto;margin-top: auto; margin-bottom: auto;"
    border = "2" cellpadding = "2" cellspacing = "2">
    <tbody>
<tr style = "text-align: center;">
    <td style = "text-align: center;"><font size = "10" face = "Segoe UI" color = "white"> L I G H T S</font></td>
</tr>
<tr>
<td>
<table style = "text-alight: center; margin-left: auto; margin-right: auto;" 
border = "0" cellpadding = "10" cellspacing = "2">
<tbody>
<tr style = "text-align: center;">
    <td style = "text-align: center;"> <font size = "6" face =  "Segoe UI" color = "white"> Light 1</font></td>
    <td style = "text-align: center;"><button style =   "height:100px;width:150px" name= "l1"><font size ="6" face  = "Segoe UI" color = "black"> On/Off </font></button></td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</form>
</body>

</html>
2
  • 1
    Basics: did you install the php module for apache? Then, does /var/log/apache2/error.log say anything when you go to your web page?
    – eftshift0
    Aug 1 '18 at 16:35
  • 1
    You will need to use sudo usermod -G -a www-data gpio to allow Apache2/Lighttpd/NGinx to run that gpio command.
    – Dougie
    Jan 25 '20 at 20:49
1

This is debug 101: don't bite more than you can chew. Make sure a single command works before debugging the complete app.

  • Check the output of shell_exec for error messages.

  • If there's nothing, add 2>&1 to your commands to also catch the output from STDERR.

  • Still nothing? Replace shell_exec by exec and check the return code.

  • You may also want to run a trivial command for a start (e.g. df) to see if your Apache/PHP setup works at all.

0

I was having the same issue using python, Executing shell script in PHP, etc. So the solution is:
Application Menu > Preferences > Raspberry Pi Configuration > Interfaces > Enable Remote GPIO.

1
  • 1
    This doesn't really answer the question, although using remote gpio is better than trying to get a webserver running the gpio command from wiringPi. But that would need the person asking the question to change his/her code.
    – Dougie
    Jan 25 '20 at 20:48
0

Try shell scripting inside shell_exec, as

echo 1 > /sys/class/gpio/gpio4/value

For writing.

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.