簡體   English   中英

ESP8266 到 000webhost 不獲取數據一個 php 文件

[英]ESP8266 to 000webhost doesn't GET the data one php file

我有關於 ESP8266 到 000webhost 的問題,這是我的 arduino 串行監視器

19:03:38.806 -> closing connection
19:03:54.834 -> connecting to plantdoctortopic.000webhostapp.com
19:03:55.235 -> Client Connected!
19:03:55.235 -> hello
19:03:55.515 -> HTTP/1.1 200 OK
19:03:55.515 -> Date: Wed, 07 Jul 2021 11:03:54 GMT
19:03:55.515 -> Content-Type: text/html; charset=UTF-8
19:03:55.515 -> Transfer-Encoding: chunked
19:03:55.515 -> Connection: close
19:03:55.515 -> refresh: 1
19:03:55.515 -> Server: awex
19:03:55.515 -> X-Xss-Protection: 1; mode=block
19:03:55.562 -> X-Content-Type-Options: nosniff
19:03:55.562 -> X-Request-ID: 5dc8e2d2a64b028ecca11159a852d551
19:03:55.562 -> 
19:03:55.562 -> 5
19:03:55.562 -> hello
19:03:55.562 -> 0
19:03:55.562 -> 
19:03:55.562 -> 
19:03:55.562 -> closing connection

這是我的 php 代碼

<?php header( "refresh:1" );

$servername = "localhost";
$username = "id17187180_root_nkust";
$password = "@Lastcool0628";
$dbname = "id17187180_plantdoctor_db";

// Create connection
$link = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$link) {
  die("Connection failed: " . mysqli_connect_error());
}
$data1 = $_GET["data1"];

$sql = "UPDATE sensor SET humid = ('$data1')";
$result = mysqli_query($link, $sql) or die(mysqli_error($link));

$sql2 = "SELECT humid from sensor";
$result2 = mysqli_query($link, $sql2) or die(mysqli_error($link));

while ($row = mysqli_fetch_assoc($result2)){
    echo $row["humid"];
}
echo $data1;

?>

這是我的 arduino 代碼

#include <ESP8266WiFi.h> // important libraries for wifi
#include <ESP8266HTTPClient.h> //important libraries for wifi


char* ssid = "Tenda_561790"; // your wifi connection 
char* password = "bwujf96613";
char* host = "plantdoctortopic.000webhostapp.com";
String data;
void setup()
{
    Serial.begin(115200);
    
    Serial.println();
    Serial.print("Connecting to ");
    Serial.println(ssid);

    WiFi.begin(ssid, password);
    Serial.println("Starting Wifi");
    while (WiFi.status() != WL_CONNECTED) {
        Serial.print(WiFi.status());
        delay(500);
        Serial.print(".");
    }

    Serial.println("");
    Serial.println("WiFi connected");
    Serial.println("IP address: ");
    Serial.println(WiFi.localIP());
}



void loop()
{
  
    data = "hello";
    // Use WiFiClient class to create TCP connections
    WiFiClient client;
    const int httpPort = 80;
    Serial.print("connecting to ");
    Serial.println(host);
    if (!client.connect(host, httpPort)) {
        //Serial.println(client.connect(host,httpPort));
        Serial.println("connection failed");
        return;
    } 
    Serial.println("Client Connected!");
    Serial.println(data);
    // This will send the request to the server
    client.print(String("GET /sensor/index.php?") + 
                          ("&data1=") + data +
                          " HTTP/1.1\r\n" +
                 "Host: " + host + "\r\n" +
                 "Connection: close\r\n\r\n");
                 
    unsigned long timeout = millis();
    while (client.available() == 0) {
        if (millis() - timeout > 1000) {
            Serial.println(">>> Client Timeout !");
            client.stop();
            return;
        }
    }

    while(client.available()){
   
    char c = client.read();
    Serial.print(c);
    }

    Serial.println();
    Serial.println("closing connection");

    delay(16000);
}

我的問題是為什么主機已連接而我的網絡總是說

注意:未定義索引:第 14 行 /storage/ssd3/180/17187180/public_html/sensor/index.php 中的 data1

我嘗試更改 arduino 和 php 上的 GET 或 POST,但效果不佳

client.print(String("GET /sensor/index.php?") + 
                          ("&data1=") + data +
                          " HTTP/1.1\r\n" +
                 "Host: " + host + "\r\n" +
                 "Connection: close\r\n\r\n");

大家能幫我解決問題嗎?

首先通過直接從瀏覽器使用 url 檢查數據是否存儲在數據庫中 不要在同一頁面上使用顯示數據語法(sql2 行)。 使用其他網頁來顯示數據 .. 就像使用 add.php 來添加/更新 DB 上的數據和 index.php 來查看數據..

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM