[英]Raspberry Pi PHP GPIO read
I've got this script to check the GPIO pin status: 我有以下脚本来检查GPIO引脚状态:
<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代码为:
<?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... 我一直输出Closed,尽管该引脚设置为1 ...当我在Raspberry Pi上从命令行运行读取操作时,它却显示1 ....但是我认为PHP脚本不起作用...
I think the problem is with your PHP script. 我认为问题出在您的PHP脚本上。 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
: 这很可能是因为可能允许Web服务器的用户(
www-data
, httpd
或apache
左右)执行gpio
,但不允许从/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 commands to execute in a shell context. 我承认它与PHP的许多不同命令在shell上下文中执行混淆。 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). 您应该使用
gpio
的完整路径(例如/ usr / bin / gpio),以找出它的位置,可以使用locate gpio
(它需要updatedb
,但我不确定)。
Originally I must have made a mistake... 本来我一定是犯了一个错误...
Because by using this PHP script: 因为使用此PHP脚本:
<?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. 它将单个0/1值解析为JavaScript代码,然后运行
if
/ else
,它可以正常工作。 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. 此外,我更改了将继电器/电线欺骗连接到Raspberry Pi的GPIO的方式,将GPIO的输出更改为3.3V。 I think the GPIO to grounds were not the right way...
我认为GPIO接地不是正确的方法...
With this code you can read status of a Pushbutton in GPIO15, when click a button in a web page. 使用此代码,您可以在单击网页中的按钮时读取GPIO15中按钮的状态。
LED in GPIO26 is on/off when push/not-push Pushbutton and then click button web page. 按下/不按下“ Pushbutton” 然后单击按钮的网页时,GPIO26中的LED点亮/熄灭。
Pushbutton in GPIO15 and 3,3V GPIO15和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.com/raspberry/19_servidor_web.htm">kio4.com/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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.