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

时间:2019-04-14 03:49:43

标签: post get iot esp8266

我正在尝试使用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);

谢谢!

1 个答案:

答案 0 :(得分:0)

我改变了方法,并使它起作用。我没有尝试将数据从网页返回到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();


相关问题