最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Raspberry Pi PHP GPIO read - Stack Overflow

programmeradmin6浏览0评论

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 pleted 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 mandline on the Raspberry Pi it gives me 1.... But the PHP script I think is not working...

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 pleted 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 mandline on the Raspberry Pi it gives me 1.... But the PHP script I think is not working...

Share Improve this question edited Nov 4, 2015 at 19:16 Peter Mortensen 31.6k22 gold badges110 silver badges133 bronze badges asked Aug 3, 2014 at 9:36 troystanwaytroystanway 212 silver badges5 bronze badges 2
  • I assume you have gpio working projects.drogon/raspberry-pi/wiringpi/the-gpio-utility – user2764108 Commented Aug 3, 2014 at 9:44
  • Yep, when I run 'code' gpio read 1 'code' at mand line, it returns 1 but the html page is still parsing closed - which is the 0 – troystanway Commented Aug 3, 2014 at 9:54
Add a ment  | 

4 Answers 4

Reset to default 3

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);
?>

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

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

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./raspberry/19_servidor_web.htm">kio4./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>
发布评论

评论列表(0)

  1. 暂无评论