使用 GET 从我的网页检索数据



我正在尝试使用ESP8266 NodeMCU从网页上的数据库中检索数据以控制LED。我正在努力解决双方的代码,以便ESP8266要求数据和网页返回数据。 我已经建立了网页和数据库。 这是服务器端PHP文件的相关位,它不起作用。

 
 if(!empty($_GET['mode']) && !empty($_GET['brightness']))
    {
        $mode = $_GET['mode'];
        $brightness = $_GET['brightness'];
        $sql = "SELECT * FROM config ORDER BY id DESC LIMIT 1 (mode, brightness) 
    $result = $conn->query($sql);   
SELECT fields FROM table ORDER BY id DESC LIMIT 1;
        VALUES ('".$mode."', '".$brightness."')";
        if ($conn->query($sql) === TRUE) {
            echo "1" . $row["id"]."<br>";
        } else {
            echo "Error: " . $sql . "<br>" . $conn->error;
        }
    }
    $conn->close();
<code>
and on the ESP8266 side....

getData = "?mode=" "&brightness="; Link = "http://***.com/Feed.php" + getData; http.begin(Link); //Specify request destination int httpCode = http.GET(); String payload = http.getString(); Mode = payload.substring(0,3); String Brightness = payload.substring (4,6);

提前感谢!

我改变了我的方法并让它工作。 我没有尝试将数据从网页返回到ESP8266而是将数据显示在一个非常简单的页面上。

<?php
$con=mysqli_connect("localhost","*****","*****","*****");    
if (mysqli_connect_errno())  {  echo "Failed to connect to config: " . mysqli_connect_error();  } 
$result = mysqli_query($con,"SELECT * FROM config ORDER BY id DESC LIMIT 1");
    while ($row = $result->fetch_assoc()) { 
    $mode = $row[mode]; $brightness = $row[brightness]; $speed = $row[speed]; 
    print $mode;
    print $brightness;
    print $speed;
}
    ?>

然后让 ESP 下载整个页面并使用子字符串解析数据,如下所示......


HTTPClient http;    //Declare object of class HTTPClient
Link = "http://*****.com/*****.php";
http.begin(Link);     //Specify request destination
int httpCode = http.GET();            //Send the request
String payload = http.getString();    //Get the response payload
Mode = payload.substring(156,158).toInt();
Speed = payload.substring(158,160).toInt();
Brightness = payload.substring(160,162).toInt();

最新更新