简体   繁体   English

JavaScript与PHP的通讯问题

[英]javascript to PHP communication issue

Ok, so i have a website that is designed to be able to remotely control the GPIO (general purpose in/out) pins on the Raspberry Pi it is running on using the library WiringPi. 好的,所以我有一个网站,旨在使用库WiringPi远程控制正在运行的Raspberry Pi上的GPIO(通用输入/输出)引脚。 the problem is, between a javascript and a php code, the HTTPrequest.responseText always returns undefined. 问题是,在JavaScript和php代码之间,HTTPrequest.responseText始终返回未定义。 index.php reads the pins and prints out their states: index.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>

that works fine--then theres the javascript, which adds the click action event listener to all the images, except for number 5: this is one i want to update with the status of the Pi pin when it is clicked, instead of writing it to the opposite state (like the others). 可以正常工作-然后有一个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;
        }
    }
} );

all of the other event listener functions look the same as the one above, except for this one: 其他所有事件侦听器函数均与上述函数相同,除了以下内容:

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);   
}
} );

i had the php code for this return a string representing the status (that i made up), to eliminate the possiblity of type issues: it didnt work. 我为此的php代码返回了一个表示状态(我组成)的字符串,以消除类型问题的可能性:它没有用。 here is the gpio.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");
}
?>

i even created a redundant for loop that only runs once to make sure the variable $status was returned correctly. 我什至创建了一个冗余的for循环,该循环仅运行一次以确保正确返回变量$ status。 i have exhausted what, according to others with this issue, seems to be the only possibilities. 根据其他有此问题的人的说法,我已经穷光了,这似乎是唯一的可能性。 any criticism is helpful at this point, thanks in advance (and for putting up with so much code). 在此,任何批评都是有帮助的,在此先感谢(并感谢您提供这么多代码)。

exec ("gpio read ".$i, $status[$i], $return ); exec(“ gpio读取”。$ i,$ status [$ i],$ return);

Should not the comma after .$i, be a period as below? 。$ i之后的逗号是否应为以下句点?

exec ("gpio read ".$i. $status[$i], $return ); exec(“ gpio读取为”。$ i。$ status [$ i],$ return);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM