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
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.
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.
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?
//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?
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.