I can read a GPIO pin's status as such:
<?php
exec ( "gpio read 7", $status );
print_r ( $status );
?>
this works fine. But I want to read the status from within a php if statement, eg:
<?php
if (isset($_GET['trigger']) && $_GET['trigger'] == 1) {
exec ( "gpio read 7", $status );
print_r ( $status );
exec ( "gpio write 7 1" );
}
?>
Though exec ('gpio write 7 1');
works, the read and print does not.
Would anyone know what I am doing wrong?
2 Answers 2
exec adds the returned value to the end of the array $status. So first time, required value is in $status(0). Next time, old value is still in $status(0), but new value is in $status(1), etc for further loops. Fix is to use unset($status) each time round the loop so that exec always adds new value in $status(0).
Here's what I came up with when I had this same problem. This post helped me figure out what the issue was.
function closevalve ($valve) {
$status[0] = 2;
while ($status[0] != 1) {
system ( "gpio write ".$valve." 1" );
exec ( "gpio read ".$valve, $statusa );
$status[0] = $statusa[0];
};
unset($status);
unset($statusa);
echo date("h:i:s A")." - ".whichpin($valve)." Off<br>";
ob_flush();flush();
sleep(5);
};
gpio mode <pin> in
gpio read <pin>
works on an out pin.