2016-12-16 22 views
0

我是一些PHP代码的残端。RPi GPIO状态更新到PHP按钮和脚本

我目前有一个按钮更改颜色,通过获得树莓pi3上的GPIO状态,当关闭绿色时打开红色,我卡住试图让它执行两个python脚本之一,当按钮更改颜色。如果我使用两个按钮来打开和关闭一个,我可以让脚本执行,当状态颜色时,我宁愿让它一个按钮。

在此先感谢您的帮助。

我试过的所有页面都不会加载。 以下是我的状态更新。

<html> 
<head> 
</head> 
<body> 
<?php $status1 = trim(shell_exec("gpio -g read 12")); ?> 
<?php 
if ($status1 == "1") { 
echo "<button style=\"background-color:#FF0000; width: 400px; height:350px; font-size:60px;\">Relay 1</button>"; 
} else { 
echo "<button style=\"background-color:#009900; width: 400px; height:350px; font-size:60px;\">Relay 1</button>"; 
} 
?> 
<br> 
<br> 
<?php echo date('Y-m-d H:i:s'); ?> 
</body> 
</html> 
+0

所以,你只是想在页面上的不同颜色按钮取决于gpio查询的结果?还是你想让按钮发送$ _POST或$ _GET数据到一个处理脚本,然后调用其中一个Python脚本? – ivanivan

+0

我可以得到gpio的状态,当点击按钮时我不能得到它执行特定的代码。 – issues

回答

0

当要一个按钮来发送它必须是与指定的动作和方法的形式的数据,并且该按钮必须是一个(大于)输入类型=提交...(小于) - 如果你想根据表单数据做一些事情,必须有一个具有值的命名元素,否则PHP将不会在$ _POST中看到它。

<html> 
<head> 
</head> 
<body> 
    <form name="theform" method="post" action="<?php print($_SERVER['PHP_SELF']); ?>"> 
<?php 
//$status1 = trim(shell_exec("gpio -g read 12")); 

$status="1"; 
if(isset($_POST['turnon'])){ 

    // this is where you call your python or whatever when the status is already 1 
    $status="0"; 

    } 

if(isset($_POST['turnoff'])){ 

    // this is where you call your python or whatever when status is already 0 

    $status="1"; 

    } 

if ($status==="1") { 
echo "<input name=\"turnon\" type=\"submit\" style=\"background-color:#FF0000; width: 400px; height:350px; font-size:60px;\" value=\"Turn Relay 1 on\">"; 
} else { 
echo "<input name=\"turnoff\" type=\"submit\" style=\"background-color:#009900; width: 400px; height:350px; font-size:60px;\" value=\"Turn Relay 1 off\">"; 
} 
?> 
</form> 
<br> 
<br> 
<?php echo date('Y-m-d H:i:s'); 

?> 
</body> 
</html> 
+0

明天我会再试一次。谢谢 – issues

+0

感谢您的帮助,让它一切正常。 – issues