Reputation: 21
I've got this script to check the GPIO pin status:
<script type="text/javascript">
$(document).ready(function () {
// This is the init function
// Runs when the page has completed loading
$('#statusCheck').click(function() {
//console.log('checking status');
$.ajax({
url: "check.php",
success: function (data) {
if(data != 1 )
{
// Door is closed
$('#sttext').html('<span style= color:green;>Closed</span>');
}
else if(data == 1)
{
// Door is open
$('#sttext').html('<span style= color:green;>Open</span>Open');
}
//$('#debug').html(''); // Print null string to clear message
//$('#debug').html(data); // Debug message, printing out read back status.
}
});
});
});
</script>
That connects to a button and span:
<strong>Status: <span id="sttext"></span></strong></p>
<button id="statusCheck" class="green-btn">Check Status </button>
The check PHP code is:
<?php
system(exec ( "GPIO read 1", $status ));
system(print_r ( $status ));
?>
I keeps outputing Closed, though the pin is set at 1... When I run the read from the commandline on the Raspberry Pi it gives me 1.... But the PHP script I think is not working...
Upvotes: 1
Views: 9500
Reputation: 81
With this code you can read status of a Pushbutton in GPIO15, when click a button in a web page.
LED in GPIO26 is on/off when push/not-push Pushbutton and then click button web page.
Pushbutton in GPIO15 and 3,3V
Library WiringPi
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Juan A. Villalpando - KIO4.COM</title>
</head>
<body>
<center><h1>Consulta el estado de un Botón mediante página web</h1>
<a href="http://kio4.com/raspberry/19_servidor_web.htm">kio4.com/raspberry</a><br><br>
<form method="get" action="<?php print($_SERVER['PHP_SELF']); ?>">
<input type="submit" style = "font-size: 16 pt" value="Consulta">
</form></center>
<?php
shell_exec("/usr/local/bin/gpio -g mode 26 out");
shell_exec("/usr/local/bin/gpio -g mode 15 in");
shell_exec("/usr/local/bin/gpio -g mode 15 down");
$boton = shell_exec("/usr/local/bin/gpio -g read 15");
$boton = trim($boton);
echo $boton;
echo "<br>";
if($boton == "1")
{
echo "Pulsado";
shell_exec("/usr/local/bin/gpio -g write 26 1");
}
else
{
echo "No Pulsado";
shell_exec("/usr/local/bin/gpio -g write 26 0");
}
?>
</body>
</html>
Upvotes: 0
Reputation: 21
Originally I must have made a mistake...
Because by using this PHP script:
<?php
system ("gpio read 1");
?>
it's parsing the single 0/1 value to the JavaScript code which then runs the if
/else
, and it is working. Additionally, I changed the way the relay/wire spoof was connected to the GPIO of the Raspberry Pi, changing to 3.3 V outputs to GPIO. I think the GPIO to grounds were not the right way...
Upvotes: 1
Reputation: 32310
Most likely it's because the webserver's user (www-data
, httpd
or apache
or so) is maybe allowed to execute gpio
, but not allowed to read the state from /sys/class/gpio
:
dan@nsa / $ cat /sys/class/gpio/
cat: /sys/class/gpio/: Permission denied
I admit it's confusing with PHP's many different commands to execute in a shell context. Your best bet is I guess:
echo system('gpio ...');
You should use the full path to gpio
(like /usr/bin/gpio), to find out where it is you can use locate gpio
(it needs updatedb
, but I am not sure).
Upvotes: 1
Reputation: 4906
I think the problem is with your PHP script. Try this instead:
<?php
exec("gpio read 1", $status);
print_r($status); //or var_dump($status);
?>
Upvotes: 3