0

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?

asked Mar 14, 2015 at 5:09
5
  • how do you know the write works and the read and print don't? Commented Mar 14, 2015 at 5:13
  • @SteveRobillard the LED I have attached to that GPIO is triggered. The read probably works but is still not being printed to the the screen like it does normally. Commented Mar 14, 2015 at 5:17
  • you cannot read from an OUT pin, you should use different pin and configure pin you would like to read as input using gpio mode <pin> in Commented Mar 14, 2015 at 9:36
  • @mpromonet I'm going to disagree with that -- you can check the value of an out pin. Commented Mar 14, 2015 at 14:16
  • 1
    @goldilocks : mea maxima culpa. I did not checked, and you're right gpio read <pin> works on an out pin. Commented Mar 14, 2015 at 14:42

2 Answers 2

1

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

answered Jan 14, 2016 at 18:45
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);
 };
answered Mar 23, 2017 at 20:14

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.