2014-12-07 124 views
0

好吧,所以我有一個網站,它可以遠程控制使用庫WiringPi運行的Raspberry Pi上的GPIO(通用輸入/輸出)引腳。問題是,在javascript和php代碼之間,HTTPrequest.responseText總是返回undefined。 index.php文件讀出相應引腳,並打印出自己的狀態:JavaScript到PHP通信問題

<!DOCTYPE html> 
<html> 
<head> 
    <meta charset="utf-8" /> 
    <title>Raspberry Pi Home Automation</title> 
</head> 

<body style="background-color: black;"> 
<?php 

// Define your username and password 
$username = "[not giving you my username]"; 
$password = "[or password]"; 

if ($_POST['txtUsername'] != $username || $_POST['txtPassword'] != $password) { 

?> 

<h1 style="color: white">Login</h1> 

<form name="form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> 
<p style="color: white"><label for="txtUsername">Username:</label> 
<br /><input type="text" title="Enter your Username" name="txtUsername" /></p> 

<p style="color: white"><label for="txtpassword">Password:</label> 
<br /><input type="password" title="Enter your password" name="txtPassword" /></p> 

<p><input type="submit" name="Submit" value="Login" /></p> 

</form> 

<?php 

} 
else { 

?> 
<?php 
//this php script generate the first page in function of the gpio's status 
$status = array (0, 0, 0, 0, 0, 0, 0, 0); 
for ($i = 0; $i < count($status); $i++) { 
    //set the pin's mode to output and read them 
    if ($i != 5){ 
     system("gpio mode ".$i." out"); 
    } else { 
     system("gpio mode ".$i." in"); 
    } 
    exec ("gpio read ".$i, $status[$i], $return); 
    //if off 
    if ($status[$i][0] == 0) { 
    echo ("<img id='button_".$i."' src='data/img/off/off_".$i.".jpg' alt='off'/>"); 
    } 
    //if on 
    if ($status[$i][0] == 1) { 
    echo ("<img id='button_".$i."' src='data/img/on/on_".$i.".jpg' alt='on'/>"); 
    }  
} 
?> 
<?php 


} 

?> 

<!-- javascript --> 
<script src="script.js"></script> 
</body> 

工作正常 - 然後theres的JavaScript,它增加了點擊動作事件監聽到所有圖像,除了5號:這是一個我想要點擊時更新Pi引腳的狀態,而不是將其寫入相反狀態(與其他狀態一樣)。

//JavaScript, use pictures as buttons, sends and receives values to/from the Rpi 
//These are all the buttons 
var button_0 = document.getElementById("button_0"); 
var button_1 = document.getElementById("button_1"); 
var button_2 = document.getElementById("button_2"); 
var button_3 = document.getElementById("button_3"); 
var button_4 = document.getElementById("button_4"); 
var button_5 = document.getElementById("button_5"); 
var button_6 = document.getElementById("button_6"); 
var button_7 = document.getElementById("button_7"); 


//this function sends and receives the pin's status 
function change_pin (pin, status) { 
//this is the http request 
var request = new XMLHttpRequest(); 
request.open("GET" , "gpio.php?pin=" + pin + "&status=" + status); 
request.send(null); 
//receiving information 
request.onreadystatechange = function() { 
    if (request.readyState == 4 && request.status == 200) { 
     return (request.responseText); 
    } 
//test if fail 
    else if (request.readyState == 4 && request.status == 500) { 
     alert ("server error"); 
     return ("fail"); 
    } 
//else 
    else { return ("fail"); } 
} 
} 

//these are all the button's events, it just calls the change_pin function and updates the page   in function of the return of it. 
button_0.addEventListener("click", function() { 
//if off 
if (button_0.alt === "off") { 
    //use the function 
    var new_status = change_pin (0, 0); 
    if (new_status !== "fail") { 
     button_0.alt = "on" 
     button_0.src = "data/img/on/on_0.jpg"; 
     return 0; 
     } 
    } 
//if on 
if (button_0.alt === "on") { 
    //use the function 
    var new_status = change_pin (0, 1); 
    if (new_status !== "fail") { 
     button_0.alt = "off" 
     button_0.src = "data/img/off/off_0.jpg"; 
     return 0; 
     } 
    } 
}); 

所有的其他事件偵聽器功能看起來是一樣的,如上圖所示,除了這一個:

button_5.addEventListener("click", function() { 

var new_status = change_pin (5, 0); 

if (new_status == "yes") 
{ 
    button_5.alt = "on" 
    button_5.src = "data/img/on/on_5.jpg"; 
    return 0; 
} 
else if (new_status == "no") 
{ 
    button_5.alt = "off" 
    button_5.src = "data/img/off/off_5.jpg"; 
    return 0; 
} 
else if (new_status == "fail"){ 
    return 0; 
} else { 
alert("return fail " + new_status); 
} 
}); 

我不得不這樣做的PHP代碼返回表示狀態的字符串(即我彌補),以消除類型問題的可能性:它沒有工作。這裏是gpio.php:

<!-- This page is requested by the JavaScript, it updates the pin's status and then print it --> 
<?php 
//Getting and using values 
if (isset ($_GET["pin"]) && isset($_GET["status"])) { 

$pin = strip_tags($_GET["pin"]); 
$status = strip_tags($_GET["status"]); 

//Testing if values are numbers 
if ((is_numeric($pin)) && (is_numeric($status)) && ($pin <= 7) && ($pin >= 0) && ($status ==  "0") || ($status == "1")) { 
    if ($pin != 5){ 
     //set the gpio's mode to output  
     system("gpio mode ".$pin." out"); 

     //set the gpio to high/low 
     if ($status == "0") { 
      $status = "1"; 
     } 
     else if ($status == "1") { 
      $status = "0"; 
     } 
     system("gpio write ".$pin." ".$status); 
    } 

    //reading pin's status 
    $status = array (0, 0, 0, 0, 0, 0, 0, 0); 
    for ($i = $pin; $i == $pin; $i++) { 
     exec ("gpio read ".$i, $status[$i], $return); 

     if ($status[$i][0] == 0) { 
      echo ("no"); 
     } else if ($status[$i][0] == 1) { 
      echo ("yes"); 
     } else { 
      echo ("fail"); 
     } 
    } 

} 
else { 
    echo ("fail"); 
} 
} //print fail if cannot use values 
else { 
echo ("fail"); 
} 
?> 

我甚至創造了一個多餘的for循環只運行一次,以確保變量$狀態正確返回。根據其他人對這個問題的看法,我已經窮盡了,似乎是唯一的可能性。任何批評在這一點上都是有幫助的,因爲事先要感謝(並且承認這麼多的代碼)。

+0

任何控制檯錯誤? PHP從位置欄而不是腳本運行時返回正確的東西嗎? – mplungjan 2014-12-07 06:30:30

+0

是的,接線庫實際上是開發者所說的:返回一個表示引腳狀態的整數1或0。 – jimga150 2014-12-07 20:28:31

回答

-1

exec(「gpio read」。$ i,$ status [$ i],$ return);

不應該後面的逗號$ i,是否如下?

exec(「gpio read」。$ i。$ status [$ i],$ return);

+0

我的印象是,狀態是發送給exec函數的命令中的第二個參數:第一個是帶有命令的字符串。 – jimga150 2014-12-14 15:49:09