2016-12-16 21 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