NodeMCU client.connect returns 0 用于本地托管解析 api?

NodeMCU client.connect returns 0 for locally hosted parse api?

下面是 link 休息 api 使用解析的指南:

https://docs.parseplatform.org/rest/guide/#your-configuration

My hosted remote server configuration

#include <ESP8266WiFi.h> 
const char* ssid = "your-ssid"; //replace with your own wifi ssid 
const char* password = "your-password"; //replace with your own //wifi ssid password 
const char* host = "192.168.1.102:1337";
void setup() { 
Serial.begin(115200); 
delay(10); // We start by connecting to a WiFi network Serial.println();
Serial.println(); 
Serial.print("Connecting to ");
Serial.println(ssid);
/* Explicitly set the ESP8266 to be a WiFi-client, otherwise, it by default, would try to act as both a client and an access-point and could cause network-issues with your other WiFi-devices on your WiFi-network. */
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED){
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected"); 
Serial.println("IP address: "); 
Serial.println(WiFi.localIP()); 
} 
int value = 0; 
void loop() { 
delay(5000); 
++value; 
Serial.print("connecting to ");
Serial.println(host); // Use WiFiClient class to create TCP connections
WiFiClient client;
const int httpPort = 80;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed"); 
return;
}
// We now create a URI for the request
//this url contains the informtation we want to send to the server
//if esp8266 only requests the website, the url is empty
String url = "/";
/* url += "?param1=";
url += param1;
url += "?param2=";
url += param2;
*/
Serial.print("Requesting URL: ");
Serial.println(url); // This will send the request to the server
client.print(String("GET ") + url + " 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 > 5000){ 
Serial.println(">>> Client Timeout !");
client.stop(); 
return; 
} 
} // Read all the lines of the reply from server and print them to Serial
while (client.available()){ 
String line = client.readStringUntil('\r'); Serial.print(line);
}
Serial.println();
Serial.println("closing connection"); 
}

总是返回“连接失败”,因为“client.connect(host, httpPort)”为 0。不确定主机未连接的原因。我和邮递员一起测试了 api 是否正常工作,没问题。

您在主机字符串中嵌入了端口号。主机字符串应该只是名称或 IP 地址。

const char* host = "192.168.1.102:1337";

应该是

const char* host = "192.168.1.102";

及以后您定义的地方

const int httpPort = 80;

您需要为您的设置选择正确的端口,无论是 80 还是 1337。