ESP8266WiFi – resolving DNS does not work on ESP8266 until reboot!

When manually configuring IP "WiFi.config(ip, dns_ip1, gateway, subnet);" requests the DNS server for an IP-address only once, when loading the microcontroller, then during any reconnections it uses its own cache and does not request the actual IP from the DNS server address. For this reason, me have to go and reboot the devices manually, If the server IP address has changed.
When auto-configuring DHCP (without the WiFi.config(...) line), the problem does not appear, when changing the IP server, during the reconnection process it receives a new IP and connects.
The library is used together with Blynk to strictly specify: IP_Device, IP_Gateway, Subnet_Mask, IP_DNS.

If comment out WiFi.config(...), resolving occurs every time you reconnect, without rebooting the ESP8266
Used options:

WiFi.config(ip, dns_ip1, gateway, subnet);
//and
WiFi.config(ip, gateway, subnet, dns_ip1, dns_ip2);

Test sketch examples:

/* Blynk connect Manual: IP,GW,MASK,DNS*/
#define BLYNK_PRINT Serial //Displays Blynk Connecting to...
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

bool blynk_ok = false, hot = false, wifi_ok = false;
uint32_t timer_reconnect;    //reconnect timer
#define TIMEOUT 20000        // Timeout for connection attempts to the server, ms.
#define TIME_RECONNECT 30000 // connection check/restart timer, ms (TIME_RECONNECT > TIMEOUT)

char blynk_ip[] = "blynk.tk";
int port = 8080;    //HTTP Blynk local port
char auth[] = "fu1wZEelcI8jL-9109S9Ld9xmYa19juu";
char ssid[] = "WiFi_SSID";
char pass[] = "WiFi_Pass";

IPAddress device_ip   (10,0,1,74);
IPAddress dns_ip1     (10,0,0,10);
IPAddress dns_ip2     (8.8.8.8);
IPAddress gateway_ip (10,0,0,10);
IPAddress subnet_mask (255,255,0,0);

void setup() {
  Serial.begin(115200); delay(25); Serial.println();
  WiFi.mode(WIFI_STA); // Client mode is saved after wifi restart.
  Connect();
}
void Connect() {
  //Blynk.disconnect();     //tst
  //delay(250);             //tst
  //WiFi.disconnect();      //tst
  //delay(250);             //tst
  WiFi.mode(WIFI_OFF); Serial.println("\tTurned off the WiFi module");//solves the problem of alternating successful connection to wifi when rebooting
  //delay(250);             //tst
  //WiFi.forceSleepWake();  //tst
  //WiFi.forceSleepBegin(); //tst
  //delay(250);             //tst
//WiFi.config(device_ip, dns_ip1, gateway_ip, subnet_mask);
  WiFi.config(device_ip, gateway_ip, subnet_mask, dns_ip1, dns_ip2);
  Blynk.config(auth, blynk_ip, port);
  Serial.println("\tConnecting to Wi-Fi: " + String(ssid)); //BLYNK_LOG(ssid);
  WiFi.begin(ssid, pass); //Serial.print("IP address: "); Serial.println(WiFi.localIP());
  if (Blynk.connect(TIMEOUT)) Serial.println("Blynk Connected!"); //BLYNK_CONNECTED() => blynk_ok = true;
  else Serial.println("Blynk Not Connected...");
  Serial.println("Device IP address: " + String(WiFi.localIP().toString()));
}

void CheckConnect() {
  if (millis() - timer_reconnect >= TIME_RECONNECT) {
    timer_reconnect = millis();
    if (WiFi.status() == WL_CONNECTED) wifi_ok = true;
    else {wifi_ok = false; WiFi.begin(ssid, pass); Serial.println("\tConnecting to Wi-Fi...");}
    
    blynk_ok = Blynk.connected(); //checking server connection status
    Serial.println("CHECK: Blynk.connected = " + String(blynk_ok) + ", WiFi.status = " + String(WiFi.status()));
    if (!blynk_ok && wifi_ok) Connect();
  }
}

BLYNK_CONNECTED() {  //method that is called when the hardware connects to Blynk
  blynk_ok = true;
}

void loop() {
  if (Blynk.connected()) Blynk.run();
  CheckConnect();
}

if you are setting the IP then how is it changing .LocalIP should stay same as you config .In the check connection part you haven't used the config part. Actually i dont understand the problem that you are facing . As from description i understand that your IP is changing while reconnecting

How can my LAN address change if I entered it HARD in WiFi.config()? The server IP address has changed. This is described in the first sentence. Do you know what the DNS server does? There is a blynk server at blynk.tk and it has the IP address PING blynk.tk (185.135.86.72), due to the move, the address changed to 194.87.238.185, and the ESP8266 continues to hammer at the old 185.135.86.72 until manually reboot the device. But the library doesn't care. She will resolve the server address only once, when first turned on.
This is a long-standing library bug that for some reason no one cares about.

This bug is very easy to check. In the router, in the hosts section, specify the deliberately incorrect address blynk.tk = 185.135.86.72. Then download my example, with your data (ip/gw) and after connecting, change the address in hosts to the correct one or delete the line. The device will continue to hit the old address because resolving does not work when WiFi.config(); is enabled! If comment it out // WiFi.config(); – then resolving will occur with each connection attempt and the connection will be established with the current IP address of the server.

Ok ,Can you try declaring

inside void Connect() instead of global variable. If that dosen't work you can use ESP.restart() for restart options. For the bug you said .I am not sure making it local could solve the issue . May be it is due to making the IP static or you can try instead of updating the IP inside code by checking its IP using dns name and giving it blynk.config

If you are trying to spot out the bug .When we use wifi config to set static IP is set for communication to specific port and to not change it frequently so we can identify the device and give specific access to the device using its IP/mac address .In DHCP mode every time it is gonna get the IP DHCP server allocated may change according to network traffic . So i don't know it is a bug or it is made that way.

The variable name does not affect its contents. The char variable stores the domain name. And resolving a domain name into an IP address is the work of the library. Otherwise, the option without the WiFi.config() line would be the same as with the line.
Still, this is a library error. Because specifying static local settings inside WiFi.config() should not affect the determination of the IP address of the domain name. Why then write a domain name at all if the library does not know how to resolve it into an IP address? =)

To be sure, I declared the variable ONLY inside
void Connect() {char blynk_ip[] = "blynk.tk";}
– there is no connection, which is obvious, because error in the library. After all, the logic of work has not changed.

ESP.restart() is not suitable. Many projects consider the operating time or processing of data arrays accumulated over more than a day, and a reboot equals a loss of relevance. Otherwise, I would not have raised this error question if everything was as simple as rebooting =)

PS: I got the impression that you still didn’t understand what the problem was. The server address tends to change the IP address, for example: dyndns or moving server hosting...

Hmm.. i don't know what to say about it .Is there a way i can test this case.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.