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>
Greenonline
2,9725 gold badges27 silver badges38 bronze badges
asked Aug 1, 2018 at 16:24
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? Commented Aug 1, 2018 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. Commented Jan 25, 2020 at 20:49

3 Answers 3

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.

answered Mar 23, 2021 at 9:54
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.

Mohi Rostami
4,4321 gold badge20 silver badges39 bronze badges
answered Jan 25, 2020 at 4:52
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. Commented Jan 25, 2020 at 20:48
0

Try shell scripting inside shell_exec, as

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

For writing.

Greenonline
2,9725 gold badges27 silver badges38 bronze badges
answered Feb 19, 2021 at 13:16

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.