Search code examples
tcparduinoesp8266loopback-address

Cannot connect to LocalIP or Localhost (127.0.0.1) Using Arduino ESP8266 or Compatible Boards


I am trying to connect my Arduino ESP8266 or any of the compatible boards like WeMos mini or NodeMCU to my local server, either localhost(127.0.0.1) or 172.xx.xx.xxx port 80.

I get an httpResponseCode -1 error. But if I have it connected to a remote server I get an httpResponseCode 200 (OK).

I am running an XMPP server (even tested with Coldfusion server). My code is as below. Can anybody help?

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

const char* ssid = "ssid";
const char* password = "password";

//Your Domain name with URL path or IP address with path
String serverName = "http://stackoverflow.com";// returns 200 (ok results)
//String serverName = "127.0.0.1"; // Gives -1 error, Tried IP addresses with 172.xx.xx.xx or even 192.168.xx.xx.

// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastTime = 0;
// Timer set to 10 minutes (600000)
// unsigned long timerDelay = 600000;
// Set timer to 5 seconds (5000)
unsigned long timerDelay = 5000;

void setup() {
  Serial.begin(115200); 
  WiFi.begin(ssid, password);
  Serial.println("Connecting");
  while(WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to WiFi network with IP Address: ");
  Serial.println(WiFi.localIP());
  Serial.println("Timer set to 5 seconds (timerDelay variable), it will take 5 seconds before publishing the first reading.");
}

void loop() {
  //Send an HTTP POST request every 10 minutes
  if ((millis() - lastTime) > timerDelay) {
    //Check WiFi connection status
    if (WiFi.status() == WL_CONNECTED){
      HTTPClient http;
      String serverPath = serverName + "?temperature=24.37";
      // Your Domain name with URL path or IP address with path
      //http.begin(serverPath.c_str());
        http.begin(serverName);
      // Send HTTP GET request
        int httpResponseCode = http.GET();
      
        if (httpResponseCode>0) {
          Serial.print("HTTP Response code: ");
          Serial.println(httpResponseCode);
          String payload = http.getString();
          Serial.println(payload);
        } else {
          Serial.print("Error code: ");
          Serial.println(httpResponseCode);
      }
      // Free resources
      http.end();
    } else {
      Serial.println("WiFi Disconnected");
    }
    lastTime = millis();
  }
}

Solution

  • The address 127.0.0.1 is the Arduino itself, not your local server.

    Every host/computer with an IP address also has an IP address 127.0.0.1; it is the address of the loopback interface.

    This localhost address always refers to the current computer. The Arduino also has this address, and you are trying to connect to it.

    Use the IP address of your XMPP server; find out what the address is first, that works much better than guessing and trying.