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>
3 Answers 3
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
byexec
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.
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
.
-
1This 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– Dougie2020年01月25日 20:48:24 +00:00Commented Jan 25, 2020 at 20:48
Try shell scripting inside shell_exec
, as
echo 1 > /sys/class/gpio/gpio4/value
For writing.
/var/log/apache2/error.log
say anything when you go to your web page?sudo usermod -G -a www-data gpio
to allow Apache2/Lighttpd/NGinx to run thatgpio
command.