Search code examples
cparse-platformarduinoesp8266nodemcu

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


Below is the link for rest api guide using parse:

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"); 
}

Always returning "connection failed", since "client.connect(host, httpPort)" was 0. Not sure why the host is not connecting. I tested with postman to check whether the api was working, and it was fine.


Solution

  • You embedded the port number in the host string. The host string should be just the name or IP address.

    const char* host = "192.168.1.102:1337";
    

    should be

    const char* host = "192.168.1.102";
    

    and later where you define

    const int httpPort = 80;
    

    you'll need to choose the correct port for your setup, whether that's 80 or 1337.