Nodemcu ไม่ตอบสนองต่อคำขอ http GET

ฉันต้องการส่งคำขอ HTTP GET จาก nodemcu ของฉันไปยังเซิร์ฟเวอร์ localhost ทั้ง nodemcu และแล็ปท็อปของฉันเชื่อมต่อกับเครือข่าย Wifi เดียวกัน แม้ว่า nodemcu จะเชื่อมต่อกับเครือข่าย แต่ก็ไม่ได้ส่งคำขอ ฉันพยายามส่งคำขอด้วยตนเองและใช้ "บุรุษไปรษณีย์" จากนั้นก็ใช้งานได้ ดังนั้นฉันคิดว่าปัญหาอยู่ที่รหัส nodemcu หรือบางอย่างกับอุปกรณ์ ยินดีต้อนรับความคิดใด ๆ


#include <ESP8266WiFi.h>
#include <WiFiClient.h> 
#include <ESP8266WebServer.h>
#include <ESP8266HTTPClient.h>

/* Set these to your desired credentials. */
const char *ssid = "******";  //ENTER YOUR WIFI SETTINGS
const char *password = "****";

//Web/Server address to read/write from 
//website or IP address of server

//=======================================================================
//                    Power on setup
//=======================================================================

void setup() {
  delay(1000);
  Serial.begin(115200);
  WiFi.mode(WIFI_OFF);        //Prevents reconnection issue (taking too long to connect)
  delay(1000);
  WiFi.mode(WIFI_STA);        //This line hides the viewing of ESP as wifi hotspot

  WiFi.begin(ssid, password);     //Connect to your WiFi router
  Serial.println("");

  Serial.print("Connecting");
  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  //If connection successful show IP address in serial monitor
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());  //IP address assigned to your ESP
}

//=======================================================================
//                    Main Program Loop
//=======================================================================
void loop() {
  HTTPClient http;    //Declare object of class HTTPClient

  String ADCData, station, getData, Link;
  int adcvalue=253;  //Read Analog value of LDR
  ADCData = String(adcvalue);   //String to interger conversion
  station = "B";

  //GET Data
  getData = "?status=" + ADCData + "&station=" + station ;  //Note "?" //added at front
  Link = "http://localhost/welcome.php" + getData;

  http.begin(Link);     //Specify request destination

  int httpCode = http.GET();            //Send the request
  String payload = http.getString();    //Get the response payload

  Serial.println(httpCode);   //Print HTTP return code
  Serial.println(payload);    //Print request response payload

  http.end();  //Close connection

  delay(5000);  //GET Data at every 5 seconds
}
//=======================================================================

รหัส php ของไซต์ localhost แสดงไว้ที่นี่

<html>
<body>

status: <?php echo $_GET["status"]; ?><br>
station: <?php echo $_GET["station"]; ?>

</body>
</html>

กำลังเชื่อมต่อ..... เชื่อมต่อกับ:**** -1 -1


person jack jill    schedule 17.07.2019    source แหล่งที่มา
comment
ฉันเห็นว่าคุณกำหนดตัวแปร getData ซึ่งมีพารามิเตอร์การสืบค้น อย่างไรก็ตาม ฉันไม่เห็นคุณใช้ตัวแปรนั้นเลยจริงๆ เลยใช่ไหม   -  person Magnus Eriksson    schedule 17.07.2019
comment
ฉันใช้มันเมื่อฉันอัปโหลดไปยัง nodemcu พลาดแล้วนี่.. แก้ไขแล้ว   -  person jack jill    schedule 17.07.2019
comment
เมื่อคุณพูดว่า: ไปยังเซิร์ฟเวอร์ localhost คุณหมายความว่าเว็บเซิร์ฟเวอร์ในเครื่องของคุณอยู่ในอุปกรณ์ nodemcu ด้วยหรือไม่ หากอยู่ในอุปกรณ์อื่น (เช่นแล็ปท็อปของคุณ) คุณจะไม่สามารถใช้ localhost ได้เนื่องจากสิ่งนั้นอ้างถึงตัวเอง (nodemcu กำลังเรียกตัวเอง)   -  person Magnus Eriksson    schedule 17.07.2019


คำตอบ (2)


localhost เป็นคำย่อที่มีความหมายว่า "ตนเอง" คุณกำลังบอกให้ NodeMCU ส่งคำขอถึงตัวเอง แม้ว่ามันอาจจะไม่เข้าใจ localhost ด้วยซ้ำ คุณต้องใช้ชื่อจริงหรือที่อยู่ IP ของคอมพิวเตอร์ที่คุณพยายามส่งคำขอไป Localhost จะไม่ทำงานในลักษณะที่คุณพยายามใช้ที่นี่ (ส่งคำขอจากคอมพิวเตอร์เครื่องหนึ่งไปยังอีกเครื่องหนึ่ง)

person romkey    schedule 17.07.2019
comment
127.0.0.1 เหมือนกับ localhost 192.168.*** น่าจะเป็นสิ่งที่คุณต้องการ - person romkey; 17.07.2019

ลองสิ่งนี้:

<?php 

echo "<pre>";
print_r($_REQUEST);

?>
person Kamani Anand    schedule 17.07.2019
comment
ลองสิ่งนี้ ยังคงไม่มีอะไร. จะได้ -1 ในมอนิเตอร์อนุกรมเสมอ - person jack jill; 17.07.2019