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();
}