Search code examples
javascriptphplinuxraspberry-pigpio

Raspberry Pi PHP GPIO read


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


Solution

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