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