ESP8266WiFi strange DHCP behaviour

Hi,
I am using a bunch of Adafruit Feather Huzzah 8266's to collect measurements and publish them to MQTT.

Problem :
When IP adresses are obtained via DHCP the devices loose connection exactly every 60 seconds. They reconnect quickly but connection to the MQTT is also lost, and reconnecting takes some more time. It is not related to lease time which is 24 hours.
At first I overcame the problem by assigning static IP's and that went well until one device needed access to a DNS server.

//Static IP address configuration
IPAddress staticIP(192, 168, 0, 145); //ESP static ip
IPAddress gateway(192, 168, 0, 1); //IP Address of your WiFi Router (Gateway)
IPAddress subnet(255, 255, 255, 0); //Subnet mask
IPAddress dns(0, 0, 0, 0); //DNS

WiFi.config(staticIP, subnet, gateway, dns);
delay(100);
WiFi.begin(ssid, password);

works fine as long as the DNS IP is something invalid, but as soon as the DNS IP is a valid IP, the 8266 issues a DHCP request and I get a DHCP address instead of a static IP. With that I am back to the disconnect problem.

I suppose the reason for this strange behaviour is to be found in the ESP8266WiFi library, but I can't find any logic in it.

Does anyone have a solution to this ???

Best regards
Ole

Do you use version 2.5.2 of the ESP8266 core? For that version I have no explanation for the described behavior. The relevant code is in ESP8266WiFiSTA.cpp.

Thank you for your answer.

Yes the core version is 2.5.2 and I have compiled the code in Platformio as well as in Arduino IDE.
I have been looking into ESP8266WiFiSTA.CC, but I am afraid it is a bit too complicated for me.

There may be another way around this problem: Is it possible to add a DNS after the connection has been established with a static IP ? and if so, what would the code for that be ?

There may be another way around this problem: Is it possible to add a DNS after the connection has been established with a static IP ? and if so, what would the code for that be ?

Not by a high level call. Have you tried to set a second DNS server, maybe to the same address? Does that work?

Yes, tried that.

When setting first DNS to gateway IP and second DNS to valid DNS i get my static IP but DNS is ignored (or at least not connected to).

When setting fírst DNS to a valid DNS and second DNS to gateway IP I get a DHCP IP and connection to DNS.

Setting both DNS's to the same valid DNS IP gives me a DHCP IP.

The relevant code in my eyes is this

  //first, check whether dhcp should be used, which is when ip == 0 && gateway == 0 && subnet == 0.
  bool espOrderUseDHCP = (local_ip == 0U && arg1 == 0U && arg2 == 0U);
  bool arduinoOrderUseDHCP = (local_ip == 0U && arg2 == 0U && arg3 == 0U);
  if (espOrderUseDHCP || arduinoOrderUseDHCP) {
      _useStaticIp = false;
      wifi_station_dhcpc_start();
      return true;
  }

The ESP8266 core uses another implementation of IPAddress which is not very readable but I cannot find an error that would explain why the comparison to 0U can be true although a valid address is set.

Does the call to the config() method return true or false?

I'm embarrassed….

Turns out that I have switched positions for gateway and subnet in the configure call. Actually I took it directly out of an example - that is my only excuse…

Thank you for trying to help me.

Best regards, Ole

By the way.. the periodic 60 sec, dis- and reconnect when DHCP address is used seems to be router specific. Another make of router does not give that problem.