您好,当我想比较从 esp8266 发送的值是否与 php 中的值相同或仅检查此值是否已设置时,它总是返回 false
我试图打印回这些数据,虽然 isset 说它没有设置,但这是有效的。但是当我用电脑尝试这个时,我的代码正常
if(isset($_POST['key'])){
if($_POST['key'] == "1A2B3C4D"){
$POWER = "";
if($_POST['pressed'] == "1"){
$sql = "SELECT `POWER` FROM `VolleyTest` WHERE ID = 1";
$result = mysqli_query($conn,$sql);
while($row = mysqli_fetch_row($result)){
$POWER = $row[0];
}
if($POWER == "OFF"){
$POWER = "ON";
}else{
$POWER = "OFF";
}
$sql = 'UPDATE `VolleyTest` SET `POWER`="'.$POWER.'" WHERE ID = 1';
$conn->query($sql);
}
$sql = "SELECT `POWER` FROM `VolleyTest` WHERE ID = 1";
$result = mysqli_query($conn,$sql);
while($row = mysqli_fetch_row($result)){
$POWER = $row[0];
}
echo $POWER;//. " ". $_POST['pressed'] . " " . $_POST['key'];
}
}
ESP8266:
btn.Check();
String state = "0";
if(btn.Get()){
state = "1";
}
HTTPClient http; //Declare object of class HTTPClient
btn.Check();
String postData = "key=1A2B3C4D &pressed=" + state;//Post Data
Serial.println(postData);
btn.Check();
http.begin("http://"+host+":38180/read_from_nodemcu.php");//Specify request destination
btn.Check();
http.addHeader("Content-Type", "application/x-www-form-urlencoded"); //Specify content-type header
btn.Check();
int httpCode = http.POST(postData); //Send the request
btn.Check();
String payload = http.getString(); //Get the response payload
btn.Check();
http.end(); //Close connection
btn.Check();
Serial.println(httpCode); //Print HTTP return code
Serial.println(payload); //Print request response payload
btn.Check();
if(httpCode == 200){
if(payload == "ON"){
digitalWrite(D0,HIGH);
}else if(payload == "OFF"){
digitalWrite(D0,LOW);
}else {
Serial.println("Error");
}
}
btn.Check();
杨魅力