ninamia
Posts: 2
Joined: Mon Sep 14, 2015 5:09 am

Read pin status using PHP

Mon Sep 14, 2015 5:25 am

Hi,
I am new at the Pi family, Python, JQuery and PHP. I build a little home project where I kan switch my lights on and of using the Pi and my website. Languages I use was PHP and JQuery. When I open my browser and run my .php page, everything is workin 100%, but when I closed my .php page and reopen it again all my buttons are resetted although my lights is still on. What I need is some advice/script to first check the status of my GPIO pins so that I can display is on the current buttons. Please if someone can assist me.
I know there is a nice GPIO webinterface already build but that is not what I want. Please assist a beginner.

ghans
Posts: 7850
Joined: Mon Dec 12, 2011 8:30 pm
Location: Germany

Re: Read pin status using PHP

Mon Sep 14, 2015 8:52 am

Which method/library are you using to manipulate the GPIO ?

ghans
• Don't like the board ? Missing features ? Change to the prosilver theme ! You can find it in your settings.
• Don't like to search the forum BEFORE posting 'cos it's useless ? Try googling : yoursearchtermshere site:raspberrypi.org

ninamia
Posts: 2
Joined: Mon Sep 14, 2015 5:09 am

Re: Read pin status using PHP

Tue Sep 15, 2015 4:08 am

Here is my complete code: and the path is /var/www

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">

<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script type = "text/javascript">


$(document).ready(function(){


// ### aON ### \\

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

var a = new XMLHttpRequest();
a.open("GET","aON.php");
a.onreadystatechange=function(){
if(a.readyState==4){
if(a.status == 200){
//alert("Pin On")
}
//else alert("HTTP ErRRORS")$
}
}
a.send();
//TestMe();
document.getElementById('aStatus').innerHTML = '[On]';
document.getElementById('aStatus').style.color="green";
document.getElementById('aStatus').style.fontSize = "35px";
var txtName = document.getElementById('aStatus');

// Change Button Color
document.getElementById('aON').style.backgroundColor = "green";

if(txtName) { txtName.style.fontWeight = 'bold'; }

});

$('#aOFF').click(function(){
var a = new XMLHttpRequest();
a.open("GET","aOFF.php");
a.onreadystatechange=function(){
if(a.readyState==4){
if(a.status == 200){
}
//else alert("HTTP ErRRORS")$
}
}
a.send();
document.getElementById('aStatus').innerHTML = '[Off]';
document.getElementById('aStatus').style.color="red";
document.getElementById('aStatus').style.fontSize = "35px";
var txtName = document.getElementById('aStatus');

// Change Button Color
document.getElementById('aOFF').style.backgroundColor = "red";

if(txtName) { txtName.style.fontWeight = 'bold'; }

});

// ### aON ### \\


});

</script>


<title>Pi Light Controller</title>
</head>
<body>
<form>
<h2 align="center">Web Interface Controller </h2>
<hr align="center" width="30%"> </hr>
<br><br>
<p align="center">288 Panorama Road Web Interface Controller.</p>

<br><br>




</table>

<table align="center" border="1">

<tr>
<td>
<label>**********Action*********</label>
</td>
<td>
<label>* Do *</label>
</td>
<td>
<label>* Do *</label>
</td>
<td>
<label>Status</label>
</td>
<td>
<label">Page</label>
</td>

<br>
<tr>
<td>
<label id="aLB">Standby Lights Inside</label>
</td>
<td>
<button type="button" id="aON">ON</button>
</td>
<td>
<button type="button" id="aOFF">OFF</button>
</td>
<td>
<label id="aStatus" name="aStatus"><font size="14">???</font><br>
</td>
<td>
<label id="" width="10px">aON</label>
</td>
</tr>


</form>

</body>
</html>


-- aOFF.php
<?php
system("gpio -g mode 18 out");
system("gpio -g write 18 0");
?>

-- aON.php
<?php
system("gpio -g mode 18 out");
system("gpio -g write 18 1");
?>

User avatar
[email protected]
Posts: 2014
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: Read pin status using PHP

Thu Sep 24, 2015 12:37 pm

ninamia wrote:Hi,
I am new at the Pi family, Python, JQuery and PHP. I build a little home project where I kan switch my lights on and of using the Pi and my website. Languages I use was PHP and JQuery. When I open my browser and run my .php page, everything is workin 100%, but when I closed my .php page and reopen it again all my buttons are resetted although my lights is still on. What I need is some advice/script to first check the status of my GPIO pins so that I can display is on the current buttons. Please if someone can assist me.
I know there is a nice GPIO webinterface already build but that is not what I want. Please assist a beginner.
It's not the most efficient way, but you can read the status of the outputs in php with:

Code: Select all

$button = `gpio -g read 18`;
and so on. Lookup the backticks operator. This works when the pins is in output mode and returns what it was last set to.

-Gordon
--
Gordons projects: https://projects.drogon.net/

Heater
Posts: 11848
Joined: Tue Jul 17, 2012 3:02 pm

Re: Read pin status using PHP

Thu Sep 24, 2015 5:47 pm

jQuery is not a language.

rottjr
Posts: 11
Joined: Tue Mar 22, 2016 3:47 pm

Re: Read pin status using PHP

Tue Mar 22, 2016 10:37 pm

Fastest way to get the current value on a pin in php is via exec

Code: Select all

<?php
$btn = exec('gpio -g read [pin#]');
?>
Or you can do javascript post/get queries via jQuery or ajax.

rottjr
Posts: 11
Joined: Tue Mar 22, 2016 3:47 pm

Re: Read pin status using PHP

Tue Mar 22, 2016 10:43 pm

Heater wrote:jQuery is not a language.
To elaborate since the above post doesn't explain, jQuery is a javascript library that uses classes to perform complex functions such as ajax posts in a faster easier manner.

rottjr
Posts: 11
Joined: Tue Mar 22, 2016 3:47 pm

Re: Read pin status using PHP

Wed Mar 23, 2016 3:56 pm

rottjr wrote:Fastest way to get the current value on a pin in php is via exec

Code: Select all

<?php
$btn = exec('gpio -g read [pin#]');
?>
Or you can do javascript post/get queries via jQuery or ajax.

I have been educated, the system method/function works in loo of the exec.

Code: Select all

<?php
$btn = system('gpio -g read [pin#]');
?>

Return to “Other programming languages”