4

I am seeing some very strange behavior with my ESP8266. When I connect it to the WiFi for the first couple of times I am not seeing any issues and the ESP8266 connects just fine.

But after an arbitrary amount of times (like when I restart the unit) it tries to connect for AGES (like multiple minutes). Eventually it says "Connected" but the IP is completely wrong and it is not connected to the internet (GET request fails).

Here are the odd things:

  • If I assign a static IP then it ALWAYS works and connects within seconds
  • If I factory reset my router then it works again for a few times
  • I have not seen this with any other device (like laptops or cellphones)

It is almost as if the DHCP is failing to assign an IP and then returns some random default.

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>

// Replace these with your WiFi network settings
const char* ssid = "MYSSID"; //replace this with your WiFi network name
const char* password = "MYPASSWORD"; //replace this with your WiFi network password

void setup() {
  Serial.begin(115200);
  delay(1000);
  
  IPAddress ip(192,168,1,77);
  IPAddress gateway(192,168,1,1);
  IPAddress subnet(255,255,255,0);
  
// If I uncomment this line then it works fine
//  WiFi.config(ip, gateway, subnet);

  WiFi.begin(ssid, password);

  Serial.println();
  Serial.print("Connecting");
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("success!");
  Serial.print("IP Address is: ");
  Serial.println(WiFi.localIP());
}

void loop() {
  HTTPClient http;

  String serverPath = "http://www.google.com";
  http.begin(serverPath.c_str());
  
  // Send HTTP GET request
  int httpResponseCode = http.GET();
  Serial.print("HTTP Response code: ");
  Serial.println(httpResponseCode);
  String payload = http.getString();
  Serial.println(payload);
  // Free resources
  http.end();

  delay(1000);
}

When DHCP magically works it assigns: 192.168.1.159

When DHCP does not work I get: 169.254.104.241

When I use a static IP then the static IP is assigned.

So the question is: Has anyone seen this behavior and maybe give some hints about how I can get the internet connection to be consistent without needing a static IP?

Update: If I run a ping to 169.254.104.241 I get a reply. But if I try to get anything from the internet I get HTTP Response Code = -1.

2
  • Does the esp connect to wifi either case? That ip 169.254.104.241 is not valid, I guess, but does it actually get this ip (did you scan the network in that case)? – Sim Son Oct 14 '20 at 21:13
  • @SimSon I just ran a ping to that IP and it is responding and if I do "arp -a" then the IP shows up. I did another test and it seems like the device doesn't have an internet connection (I think... I get HTTP Response Code -1). I updated the question. – Armand Maree Oct 14 '20 at 21:37
3

This is normal behavior. well not really, that address is called a apipa address.

It's normal behavior for alot of O.S's to give itself an address in the 169.254.x.x range when they fail to get DHCP responses.

My guess is your library is doing this for you.

It's kind of like the ad-hoc of addressing, if all devices fail to get a dhcp response they will all be assigned to the same subnet, and can communicate with those apipa addresses.

My suspicion is that it may be your router. You can verify this by downloading a free DHCP server application for your OS. Plug in via Ethernet for the PC that's running the DHCP App, Turn off DHCP in the router. If it stops happening you know it's the router.

Edit: Also, this may be a firmware caveat in the router. Check the router manufacturers website and see if any firmware updates are available. if it hangs with DHCP, most likely every customer with that firmware revision has had this problem. This sort of thing usually gets patched very quickly.

1
  • 1
    This was very useful information. Thanks! After your comment I just left the ESP to keep trying to GET a page from the internet and eventually like after 10 minutes it got a successful connection. I still have no idea why this is happening but for the sake of what I need I am good to leave it like this. I will test that DHCP tool you mentioned when I get a break from work and see what happens. – Armand Maree Oct 16 '20 at 14:20
1

Thanks for posting the question and the solution. I spent hours on google and reading message boards before coming across this. I am not sure if my root cause was the same, but to me the "strange" IP for my ESP8266 (nodemcu) came from them connecting through the Guest Network of my Asus router. This resulted in an identical problem to OP with complete random connecting and long duration of no connections to WiFi, Internet and Blynk

Reverting back to ESP8266 2.5.2 in Arduino Studio seems to have magically solved the issue. Someone should alert the makers of the library.

1
  • Glad I am not the only one that saw this issue! Spent so many hours on this. I thought I was alone. Haha. – Armand Maree Mar 29 at 15:32
0

Finally I figured out a solution. I downgraded the ESP8266 board version in Arduino Studio to 2.5.2 and it worked. enter image description here

1
  • That is interesting. Is there any particular reason why you went to version 2.5.2 (the current version is 2.7.4)? What version was causing the problem? Did you try other versions? – StarCat Nov 3 '20 at 10:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.