-->
Page 1 of 3

Client.connect fails with static IP Address

PostPosted: Wed Jun 22, 2016 4:12 pm
by stavbodik
Hello ,
I am trying to send GET request with static ip address from the esp ,
With client.begin everything works fine the esp gets ip from the DHCP table and the GET request to the server works .

When i use the client.config before client.begin the esp connects to WiFi successfully , and i can see the esp connected to router from the router clients table .

Unfortunately client.connect fails and returns 0 ( port 80 )

What can cause this ? i was thinking the port 80 blocked but its not because its works normally w/o the static ip , i also added port forwarding by trying to open the port from the esp to him self maybe i should do some other forwarding ( i think it has nothing to do with port forwarding ) ?
Any one can help please ?

Code: Select all#include <ESP8266WiFi.h>
#include <SPI.h>

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

IPAddress ip(192, 168, 0, 20);   
IPAddress gateway(192,168,1,1); // this is the gateway ip of the router as i see it when login to the router panel
IPAddress subnet(255,255,255,0);

void setup() {

  String Statuses[] =  { "WL_IDLE_STATUS=0", "WL_NO_SSID_AVAIL=1", "WL_SCAN_COMPLETED=2", "WL_CONNECTED=3", "WL_CONNECT_FAILED=4", "WL_CONNECTION_LOST=5", "WL_DISCONNECTED=6"};

  Serial.begin(115200);
  delay(10);

  // We start by connecting to a WiFi network

  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.config(ip, gateway, subnet);
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(100);
    Serial.println(Statuses[WiFi.status()]);
  }

  Serial.println("WiFi connected"); 
 
}


void loop() {
  delay(5000);

  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 error");
    Serial.println(client.connect(host, httpPort));
    return;
  }

.....
}


I have written question on github developer's support :

https://github.com/esp8266/Arduino/issues/2182

Thanks !

Re: Client.connect fails with static IP Address

PostPosted: Thu Jun 23, 2016 12:36 am
by schufti
shouldn't there be a DNS set, too?
AFAIK: If you want connect to a host via name, not IP, DNS resolution is needed and without setting DNS IP wouldn't be possible.

Re: Client.connect fails with static IP Address

PostPosted: Thu Jun 23, 2016 4:42 am
by stavbodik
Your absolutely right even 2 DNS , so it works (: i cant believe the sending time average is 0.2S :lol: .

Code: Select allWiFi.config(ip_static, ip_gateway, ip_subnet, ip_dns1, ip_dns2);
  WiFi.begin(ssid, password);

Re: Client.connect fails with static IP Address

PostPosted: Thu Jun 23, 2016 4:50 am
by schufti
and this is with DNS lookup.
try using with IP (instead name) from host ...