簡體   English   中英

Nodemcu不響應http GET請求

[英]Nodemcu does not respond to http GET request

我想從我的nodemcu向本地服務器發送HTTP GET請求。 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代碼如下所示。

<html>
<body>

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

</body>
</html>

連接.....連接到:“ ****” -1 -1

本地主機是一個縮寫,意為“自我”。 您正在告訴NodeMCU將請求發送給自身,盡管它甚至可能不了解本地主機。 您需要使用您要向其發送請求的計算機的實際名稱或IP地址。 Localhost永遠不會以您嘗試在此處使用它的方式工作(將請求從一台計算機發送到另一台計算機)。

嘗試這樣做:

<?php 

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

?>

暫無
暫無

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

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