Go Down

Topic: ESP8266 connecting to IP address fails (Read 3921 times) previous topic - next topic


From sample program WiFiClient

Code: [Select]
  // Use WiFiClient class to create TCP connections
  WiFiClient client;
  const int httpPort = 80;
  if (!client.connect(host, httpPort)) {
    Serial.println("connection failed");

This works fine when
const char* host = "data.sparkfun.com";

const char* host = "";     //webserver on local network

Does not work.

Tried this:
IPAddress server(192,168,0,12);

with this:
if (!client.connect(server, httpPort)) {

Still not working.  I suspect I am making a very basic error.


What doesn't work? What does it do? What is it supposed to do? What does it do when it 'works fine'?


Here is the full program:

I am going to just post a clip, with a little tweak I made:
Code: [Select]
  // We now create a URI for the request
  String url = "/input/";
  url += streamId;
  url += "?private_key=";
  url += privateKey;
  url += "&value=";
  url += value;

  url = "/";
  Serial.print("Requesting 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 !");
  // Read all the lines of the reply from server and print them to Serial
    String line = client.readStringUntil('\r');

All this does is dump the HTTP response out to serial.

It connects fine when host is NOT an IP address.

But when I give it an IP address in the host string it does not connect. 


Are you sure your local IP address is correct?  Can you access from your web browser? Most home  networks use 192.168.1.xxx by default...

Have you tried ip addresses outside your local network eg tried to connect using the IP address of data.sparkfun.com?


Yes, server is there and alive.
I think the problem is that the function is trying to do a DNS lookup on "".

I have to give it a variable of type IPAddress.
  How do I do that?

Here is a snippit of the library function from

Code: [Select]
int WiFiClient::connect(const char* host, uint16_t port)
    IPAddress remote_addr;
    if (WiFi.hostByName(host, remote_addr))
        return connect(remote_addr, port);
    return 0;

int WiFiClient::connect(IPAddress ip, uint16_t port)
    ip_addr_t addr;
    addr.addr = ip;

    if (_client)

    // if the default interface is down, tcp_connect exits early without
    // ever calling tcp_err
    // http://lists.gnu.org/archive/html/lwip-devel/2010-05/msg00001.html
    netif* interface = ip_route(&addr);
    if (!interface) {
        DEBUGV("no route to host\r\n");
        return 0;

    tcp_pcb* pcb = tcp_new();
    if (!pcb)
        return 0;

    if (_localPort > 0) {
        pcb->local_port = _localPort++;

    tcp_arg(pcb, this);
    tcp_err(pcb, &WiFiClient::_s_err);
    tcp_connect(pcb, &addr, port, reinterpret_cast<tcp_connected_fn>(&WiFiClient::_s_connected));

    if (_client)
        return 1;

    //  if tcp_error was called, pcb has already been destroyed.
    // tcp_abort(pcb);
    return 0;


Was going to suggest
Code: [Select]
IPAddress server(192,168,0,12);

But you said you had already tried it in your first post..


All this does is dump the HTTP response out to serial.

So the ESP8266 is a client, you have a separate webserver, what is the ESP8266 supposed to do? It's getting the response, so what makes you say it isn't working?


I think they are saying.. "All this does is dump the HTTP response out to serial".,.....  WHEN USING A HOST NAME....  but dumps nothing when an IP ADDRESS is used.


@Daenerys - did you ever resolve this?

I have the same issue:

WifiClient.connect() returns False if I try to connect to a LAN address (e.g. ""), but returns True for a WAN address (e.g. "arduino.cc").

Like @Daenerys, I have tried using WiFiClient::connect(IPAddress ip, uint16_t port) instead of WiFiClient::connect(const char* host, uint16_t port) (in order to bypass the call to WiFi.hostByName()), but it still returns False.

I have verified that my web server running on is responding when contacted via a browser.

Can anyone advise why WifiClient.connect() can't connect to a local address?


Can you connect to a local hostname, especially the one that resolves to this local IP address? You're sort of comparing apples to oranges by using an internet hostname and a local IP address. Plus, did you open up port 80 on this local IP address on your router so it can receive requests from local devices? Also, is there a web server on the local machine that uses this address? You can't just "connect" to a computer. It has to have something on it capable of understanding and responding to web requests, e.g. a TCP connection.


Try set "Debug Port: Serial" and "Debug Level: WiFi" in Arduino Tools menu, and add Serial.setDebugOutput(true); line after Serial.begin(115200);. This will print some info from WiFi stack.

May be you can see answer in messages from stack.

Go Up