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.
Re: Read pin status using PHP
Which method/library are you using to manipulate the GPIO ?
ghans
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
• Don't like to search the forum BEFORE posting 'cos it's useless ? Try googling : yoursearchtermshere site:raspberrypi.org
Re: Read pin status using PHP
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");
?>
<!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");
?>
- gordon@drogon.net
- Posts: 2023
- Joined: Tue Feb 07, 2012 2:14 pm
- Location: Devon, UK
Re: Read pin status using PHP
It's not the most efficient way, but you can read the status of the outputs in php with: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.
Code: Select all
$button = `gpio -g read 18`;
-Gordon
--
Gordons projects: https://projects.drogon.net/
Gordons projects: https://projects.drogon.net/
Re: Read pin status using PHP
Fastest way to get the current value on a pin in php is via exec
Or you can do javascript post/get queries via jQuery or ajax.
Code: Select all
<?php
$btn = exec('gpio -g read [pin#]');
?>
Re: Read pin status using PHP
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.Heater wrote:jQuery is not a language.
Re: Read pin status using PHP
rottjr wrote:Fastest way to get the current value on a pin in php is via exec
Or you can do javascript post/get queries via jQuery or ajax.Code: Select all
<?php $btn = exec('gpio -g read [pin#]'); ?>
I have been educated, the system method/function works in loo of the exec.
Code: Select all
<?php
$btn = system('gpio -g read [pin#]');
?>