Skip to content
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

Connection to static IP fails #3489

Closed
bbkiwi opened this issue Aug 2, 2017 · 18 comments
Closed

Connection to static IP fails #3489

bbkiwi opened this issue Aug 2, 2017 · 18 comments
Labels
waiting for feedback Waiting on additional info. If it's not received, the issue may be closed.

Comments

@bbkiwi
Copy link

bbkiwi commented Aug 2, 2017

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() {
}
@hazza-ob
Copy link

hazza-ob commented Aug 2, 2017

Have you tried testing this with other micro controllers? May be due to the timers within the chip...

Although this is just theorising

@N0ury
Copy link

N0ury commented Sep 30, 2017

@bbkiwi I have similar problem.
I didn't see that running WiFi.config at the very beginning of setup would work.
As for you, it works fine with dhcp.
Connection is also very fast. I have no dot at all after first run.
I'm using same conf with ESP-12F board. Tried also 1M spiff.
Did you solve the issue ? If so, how ?
Thanks

@bbkiwi
Copy link
Author

bbkiwi commented Oct 1, 2017 via email

@lrmoreno007
Copy link
Contributor

Try this:

 // 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);
  delay(100);
  WiFi.mode(WIFI_STA);
  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() {
 }

Regards

@devyte
Copy link
Collaborator

devyte commented Feb 28, 2018

@bbkiwi @lrmoreno007 is this still valid with latest git?

@devyte devyte added the waiting for feedback Waiting on additional info. If it's not received, the issue may be closed. label Feb 28, 2018
@lrmoreno007
Copy link
Contributor

I don't have problems.

@devyte devyte closed this as completed Mar 1, 2018
@hasenradball
Copy link
Contributor

Hello,

please try to check the reset reason!
Like this:

 void setup() {
  
  if(ESP.getResetReason() != "Power on") {
     WiFi.mode(WIFI_OFF);
  }
  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);
  delay(100);
  WiFi.mode(WIFI_STA);
  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() {
 }

The the problem should never occur.

@hasenradball
Copy link
Contributor

Hello I think I have understood the Problem, see description below:

  1. The ESP has an autoreconnect function with does a reconnect with an previous established WLAN connection.
  2. With inserting an delay(500) the autoreconnect function is able to etablish the connection wih previous settings.
  3. Therefore the WiFi configuration after the delay() is redundant and not possible!
  4. If you want that the WiFi Configuration works fine, you should set a WIFI_OFF before.

@PropsDataCom
Copy link

PropsDataCom commented Jan 15, 2019

Hello I think I have understood the Problem, see description below:

  1. The ESP has an autoreconnect function with does a reconnect with an previous established WLAN connection.
  2. With inserting an delay(500) the autoreconnect function is able to etablish the connection wih previous settings.
  3. Therefore the WiFi configuration after the delay() is redundant and not possible!
  4. If you want that the WiFi Configuration works fine, you should set a WIFI_OFF before.

Your comment about turning off the wifi fixed my issues, thanks. this snippet is what makes all your static ip problems go away (at least for the ESP8266).

WiFi.mode(WIFI_OFF);
WiFi.config(IP, gateway, subnet); @ @

@hasenradball
Copy link
Contributor

I prefer to set at the first line within the setup:

WiFi.setAutoConnect(false);

The you will also have no problems anymore

@N0ury
Copy link

N0ury commented Jan 15, 2019

Hi,

Which solution would be the best, power consuming talking:
1 - always turn off wifi
2 - turn off wifi only when we are not in "Power on" mode
3 - set AutoConnect to false

Thanks

@hasenradball
Copy link
Contributor

When you know you nerd wifi then boot with wifi.
Else boot without wifi.

@d-a-v
Copy link
Collaborator

d-a-v commented Jan 15, 2019

About power saving we now have preinit()

BTW, any good battery-powered example is welcome.

@hasenradball
Copy link
Contributor

I have one good example

@hasenradball
Copy link
Contributor

Take this adapter:
https://www.mikrocontroller-elektronik.de/esp12e-tutorial-einstieg-mit-dem-esp8266-modul/

and use a Voltage controller up to 6 V or up to 13 V.
Then you will be able to use a Photo akku.

@N0ury
Copy link

N0ury commented Jan 15, 2019

@d-a-v I'm not sure this is a good example for you, but it's good enough for me.
I use this app with an esp8266 powered by a very small battery.
Please remove all "oled" instructions, they are power consuming. They are here only for test purpose.
Comments are in french, it was a private app.
It reads temp from a DHT22.
Serial is used only for debugging, it is disabled otherwise.
I've just uploaded source here
I'll add schematic later.

@hasenradball
Copy link
Contributor

Or you can take this one for the small Brother:

http://esp8266-01-adapter.de/

@happytm
Copy link

happytm commented Dec 15, 2019

The lowest power ESP device I have tried is with following code:

https://github.com/happytm/BatteryNode

It is work in progress but I have tested it for about a month and I am hoping to run it for ever without ever changing coin cell battery I use (LIR2450). I use 53x30 mm micro solar panel to charge it using TP4056 charge controller. ESP8266 is bear bone ESP12 chip with BME280. Very inexpensive and most power efficient setup. The most power saving come from code as it requires very little uptime.

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
waiting for feedback Waiting on additional info. If it's not received, the issue may be closed.
Projects
None yet
Development

No branches or pull requests

9 participants