Skip to content

Client.connect fails with static IP Address #2182

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
stavBodik opened this issue Jun 23, 2016 · 10 comments
Closed

Client.connect fails with static IP Address #2182

stavBodik opened this issue Jun 23, 2016 · 10 comments

Comments

@stavBodik
Copy link

stavBodik commented Jun 23, 2016

Hardware

Hardware: ?ESP-12E?
Core Version: ? How do i find it (^^) ? ?

Description

Connecting with WiFiClient.connect fails when connected to router with static ip address,
When connected trough DHCP works fine .
Port 80 forward configured .

Settings in IDE

Module: ?Generic ESP8266 Module?
Flash Size: ?512K (64K SPIFFS)?
CPU Frequency: ?80Mhz?
Flash Mode: ?dio?
Flash Frequency: ?40Mhz?
Upload Using: ?SERIAL?
Reset Method: ?nodemcu?

Sketch

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

.....
} 

Debug Messages

Connecting to ********
wifi evt: 0
wifi evt: 3
WL_CONNECTED=3
WiFi connected
connecting to ********
[hostByName] request IP for: ********
[hostByName] Host: ******** lookup error: -5!
connection failed error
[hostByName] request IP for: ********
[hostByName] Host: ******** lookup error: -5!
0
@chaeplin
Copy link
Contributor

chaeplin commented Jun 23, 2016

Edited: Old info.

@stavBodik
Copy link
Author

I saw this post but i need the config to work before begin all the idia is to tell the WiFi class not to wait for ip address from the router .

Calling WiFi.config() before WiFi.begin() forces begin() to configure the WiFi shield with the network addresses specified in config().

But i will try for knowing if its works .

@chaeplin
Copy link
Contributor

chaeplin commented Jun 23, 2016

@stavBodik

I have tested static ip config. config before begin works.
To use dns, config should be like this.

WiFi.config(ip_static, ip_gateway, ip_subnet, ip_dns); 
// WiFi.config(ip_static, ip_gateway, ip_subnet, ip_dns1, ip_dns2); 
WiFi.begin(ssid, password);

https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.h#L42

@stavBodik
Copy link
Author

stavBodik commented Jun 23, 2016

So i have tryd with the dns thing , not working same -5 look up error .
Maybe it has something to do with my router ? please take look at the next screen shoot :

image

In green is the input to .config

in red , this is wan right ? we are not connecting from output global internet its ok that its DHCP there ?
Also i have tryd after begin with and w/o dns both/single not works .

@chaeplin
Copy link
Contributor

chaeplin commented Jun 23, 2016

@stavBodik
ip and gateway is not on a same subnet.
IPAddress ip(192, 168, 0, 20); and IPAddress gateway(192,168,1,1);

IPAddress ip(192, 168, 0, 20);    
IPAddress gateway(192,168,1,1); 
IPAddress subnet(255,255,255,0); 

If the router runs local dns cache or dns forwarder,

IPAddress ip_static(192, 168, 1, 20);    
IPAddress ip_gateway(192,168,1,1); 
IPAddress ip_subnet(255,255,255,0); 
IPAddress ip_dns(192,168,1,1);
WiFi.config(ip_static, ip_gateway, ip_subnet, ip_dns); 
WiFi.begin(ssid, password);

else

IPAddress ip_static(192, 168, 1, 20);    
IPAddress ip_gateway(192,168,1,1); 
IPAddress ip_subnet(255,255,255,0); 
IPAddress ip_dns1(213,57,2,5);
IPAddress ip_dns1(213,57,22,5);
WiFi.config(ip_static, ip_gateway, ip_subnet, ip_dns1, ip_dns2); 
WiFi.begin(ssid, password);

WAN is connected to ISP using DHCP(ip : 5.xx.xx.184), it's not related to local LAN.
Check router config whether router only permits connection from DHCP client.

@Ciriaco08
Copy link

Change the order of this:

WiFi.config(ip, gateway, subnet);
WiFi.begin(ssid, password);

for this:
WiFi.begin(ssid, password);
WiFi.config(ip, gateway, subnet);

This is solved here: #128

@stavBodik
Copy link
Author

stavBodik commented Jun 23, 2016

chaeplin Amazing option 2 works !!! seems like i had connection problem (rst-16) was disconnected , Thanks !!!! Now with my (dutyCycle==0.00721) && (weak_up +connection+sendingTime) ~=0.2S
My esp can run for years !!!

@drmpf
Copy link

drmpf commented Sep 22, 2016

WiFi.status() != WL_CONNECTED fails for static IP in V2.3.0 but works in V2.2.0

test code

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>

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

ESP8266WebServer server(80);

void setup(void) {
  Serial.begin(115200);
  for (int i = 10; i > 0; i--) { // wait 10sec with fan on at start up to indicate sucessful reset on powerup
    Serial.print(i);
    Serial.print(' ');
    delay(1000);
  }
  WiFi.begin(ssid, password);
  Serial.println("WiFi.begin called waiting for connected");

  IPAddress ip(10, 1, 1, 222);
  IPAddress gateway(ip[0], ip[1], ip[2], 1); // set gatway to ... 1
  IPAddress subnet(255, 255, 255, 0);
  Serial.print(F("Setting ip to: "));
  Serial.println(ip);
  Serial.print(F("Setting gateway to: "));
  Serial.println(gateway);
  Serial.print(F("Setting subnet to: "));
  Serial.println(subnet);
  if (!WiFi.config(ip, gateway, subnet, gateway)) {
    Serial.println("WiFi.config() failed");
  }
  Serial.println("WiFi.config() succeeded");

  // Wait for connection for 15 sec and then just continue
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

}

void loop(void) {
}

Results V2.3.0
5 4 3 2 1 WiFi.begin called waiting for connected
Setting ip to: 10.1.1.222
Setting gateway to: 10.1.1.1
Setting subnet to: 255.255.255.0
WiFi.config() succeeded
............................................................................................

BUT fing, shows 10.1.1.222 is on the network

Results after rolling back to V2.2.0
7 6 5 4 3 2 1 WiFi.begin called waiting for connected
Setting ip to: 10.1.1.222
Setting gateway to: 10.1.1.1
Setting subnet to: 255.255.255.0
WiFi.config() succeeded

Connected to FCC-Wfi
IP address: 10.1.1.222

Also on V2.2.0 also works calling config() BEFORE calling begin (as it should be)

@devyte
Copy link
Collaborator

devyte commented Oct 17, 2017

Closing as resolved.

@devyte devyte closed this as completed Oct 17, 2017
@justin8
Copy link

justin8 commented Mar 24, 2018

For anyone having this issue who ends up here in the future, as ridiculous as it sounds, slowing your upload speed to 57600 baud fixed this issue for me completely. I couldn't get it working on 2.3.0 or 2.2.0 at all beforehand, now both work flawlessly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants