简体   繁体   English

Nodemcu不响应http GET请求

[英]Nodemcu does not respond to http GET request

I want to send an HTTP GET request from my nodemcu to a localhost server. 我想从我的nodemcu向本地服务器发送HTTP GET请求。 Both the nodemcu and my laptop are connected to the same Wifi network. nodemcu和我的笔记本电脑都连接到同一Wifi网络。 Though nodemcu connects to the network, it does not send the request. 尽管nodemcu连接到网络,但它不发送请求。 I tried sending the request manually and using "postman" and then it works. 我尝试手动发送请求并使用“邮递员”,然后它可以工作。 So I think the problem is with nodemcu code or something with device. 所以我认为问题出在nodemcu代码或设备上。 Any idea is welcome. 任何想法都欢迎。


#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
}
//=======================================================================

The php code of localhost site is shown here. 本地站点的php代码如下所示。

<html>
<body>

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

</body>
</html>

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

localhost is a shorthand meaning “self”. 本地主机是一个缩写,意为“自我”。 You're telling the NodeMCU to send the request to itself, though it probably doesn't even understand localhost. 您正在告诉NodeMCU将请求发送给自身,尽管它甚至可能不了解本地主机。 You need to use the actual name or IP address of the computer you're trying to send the request to. 您需要使用您要向其发送请求的计算机的实际名称或IP地址。 Localhost will never work the way you're trying to use it here (sending a request from one computer to another). Localhost永远不会以您尝试在此处使用它的方式工作(将请求从一台计算机发送到另一台计算机)。

Try To This : 尝试这样做:

<?php 

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

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM