Skip to content

Connection to static IP fails #3489

Closed
Closed
@bbkiwi

Description

@bbkiwi

Description

In the attached sketch, having a small delay at the beginning of setup() seems to cause connection to a static IP to fail.

The code below produces the error: it fails to connect. I expected it connect quickly using static IP, but it usually fails to connect at all or takes a very long time. If I try a new static IP, sometimes it seems to connect once, but after resetting or even recompiling and loading fails from then on.

In the example, If either the delay(500); or WiFi.config(staticIP, gateway, subnet); is commented out
the problem goes away. Connection to the static IP is very fast or connection to the assigned IP as usual. Upon reset or power on, connects each time.

(I looked at issue #128 but the problem is there with either order of config and begin)
Of course the solution is not to put a delay, but it is a puzzle why this happens :-)

Settings in IDE

Module: NodeMCUv1.0 (ESP 12E module)
Flash Size: 4M(3M spiff)
CPU Frequency: 80Mhz
Upload Using: SERIAL 256000

// Bug with connecting with static IP
// Fails (except perhaps on first use of specified IP) to connects
//       (rarely will connect after very long time 30 secs)
// Removing delay fixes and connection to specified IP is very fast (1 dot)
// If comment out WiFi.config works fine (the delay has not effect), however
// Connection time is longer as connects via DHCP
// Tested on nodemcu v1.0 IDE 1.8.1 ESP8266 2.3.0

#include <ESP8266WiFi.h>

const char* ssid = "Test Station";
const char* password = "testpassword";
IPAddress staticIP(192,168,1,229);
IPAddress gateway(192,168,1,1);
IPAddress subnet(255,255,255,0);

void setup() {
 Serial.begin(115200);
 //BUG? with delay below, fails to connect to specified IP (except perhaps on first use)
 delay(500); //problem happens with delays of 150 or more
 WiFi.config(staticIP, gateway, subnet);
 WiFi.begin(ssid, password);

 Serial.println();
 Serial.print("Connecting");
 while(WiFi.status() != WL_CONNECTED)
 {
   Serial.print(".");
   delay(500);
 }
 Serial.println();
 Serial.print("WiFi Connected, IP:");
 Serial.println(WiFi.localIP());
}

void loop() {
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    waiting for feedbackWaiting on additional info. If it's not received, the issue may be closed.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions