Read and change GP pin status of RaspBerry Pi 3

0

I'm doing a little project with a rRspBerry Pi 3 that consists, among other things, of controlling some outputs through a web page. I needed to be able to know and print on the page what the status of these outputs at a given time. I have, for example, a button to turn on an LED and another button to turn off, but I need to print in a text box if the LED is on or off and let or not turn the LED on or off depending on its state.

To read the status I can use the exec("gpio read ".$i, $status); function where $i is the pin I want to read and $status is where it saves the state of the pin, but I do not know how to include it in my web page, how do I know where to go to see an example?

Code:

<!DOCTYPE html>
<html>
    <head>




    </head>
    <body>




        <article>  
            <div id="Janelas">
            <h1>Sistemas de Climatização</h1>

            <p>Soalho radiante</p>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script><scripttype="text/javascript">// <![CDATA[

$(document).ready(function() {

$('#ligar').click(function(){

var a= new XMLHttpRequest();

a.open("GET", "soalholigar.php"); a.onreadystatechange=function(){

if(a.readyState==4){ if(a.status ==200){

 } else alert ("http error"); } }

a.send();

});

});

$(document).ready(function() {

$('#desligar').click(function(){

var a= new XMLHttpRequest();

a.open("GET", "soalhodesligar.php"); a.onreadystatechange=function(){

if(a.readyState==4){ if(a.status ==200){

 } else alert ("http error"); } }

a.send();

});

});



</script>

                <input type="button" id="ligar" value="Ligar">
                <input type="button" id="desligar" value="Desligar">
                <center>


                <input type="text" size="5" name="textgol" value="0">
                </center>


            <p>ZONA 2.</p>

                <input type="button" name="botao-ligar" value="Ligar">
                <input type="button" name="botao-desligar" value="Desligar">

            <p>ZONA 3.</p>

                <input type="button" name="botao-ligar" value="Ligar">
                <input type="button" name="botao-desligar" value="Desligar">

            <p>ZONA 4.</p>

<input type="button" name="botao-ligar" value="Ligar">
                <input type="button" name="botao-desligar" value="Desligar">

            <br/>
            <br/>
            <a href="javascript:window.history.go(-1)">Voltar</a>





    </body>
</html>
    
asked by anonymous 29.11.2017 / 18:51

1 answer

0

We can imagine two PHP files: read.php , which will read the current state of the pin, returning the value in JSON, and write.php , which will change the state of the pin.

read.php

We can imagine that the JSON response of this file is in the {pin: 1, status: 0} format, indicating which pin is read and what its current state. Since we want to obtain the state of our server, this file will treat a GET request, receiving as a parameter the number of the pin to be read. It would look something like this:

<?php

// read.php

header('Content-Type: application/json');

$pin = filter_input(INPUT_GET, 'pin', FILTER_VALIDATE_INT);

exec("gpio read {$pin}", $status);

$status = (int) $status;

echo json_encode(['pin' => $pin, 'status' => $status]);

Thus, when making a request for the URL read.php?pin=1 , for example, it would have the expected return: {pin: 1, status: 0} .

With jQuery, you can make such a request through $.get :

$.get('read.php', {pin: 1}, function (data) {
    console.log("O LED está " + (data.status == 0 ? "desligado" : "ligado"));
}, 'json');

When you run this code, the message will appear in console :

O LED está ligado

If the PHP return was status = 1 , for example.

If you want to, for example, lock the power button if the LED is on, you can set the button to disabled through this code.

function trigger(pin, status) {
    if (status == 0) {
        // LED está desligado
        $("ligar").attr("disabled", false);
        $("desligar").attr("disabled", true);
    } else {
        // LED está ligado
        $("ligar").attr("disabled", true);
        $("desligar").attr("disabled", false);
    }
}

$.get('read.php', {pin: 1}, function (data) {
    trigger(data.pin, data.status);
}, 'json');

So when the LED is on, only the power off button is enabled; and when the LED is off, only the power button is enabled. If you want to check this constantly, just use this logic in conjunction with the setInterval function.

write.php

Since the write file will change the state of the application, it must treat a POST request, receiving as parameters the pin number to be changed and its new state. Since the write command has no returns, we do not need to define the HTTP response body, only the response state code; that is, a 200 response if all goes well and a 500 if something goes wrong. For this example, I will consider that if something went wrong, some error message would be displayed on the terminal and therefore exec would return something other than an empty string .

<?php

// write.php

$pin = filter_input(INPUT_POST, 'pin', FILTER_VALIDATE_INT);
$status = filter_input(INPUT_POST, 'status', FILTER_VALIDATE_INT);

exec("gpio write {$pin} {$status}", $error);

if ($error == '') {
    http_response_code(200);
} else {
    http_response_code(500);
}

With jQuery, you can make this request with $.post :

$.post('write.php', {pin: 1, status: 1});

So you can use this code in conjunction with the click event of the buttons:

$("#ligar").on("click", function (event) {
    $.post('write.php', {pin: 1, status: 1});
});

$("#desligar").on("click", function (event) {
    $.post('write.php', {pin: 1, status: 0});
});

Other implementation details I believe I can do on my own by just changing the logic presented here.

    
30.11.2017 / 14:01