WifiClient DNS lookup error after Wifi router is restarted
- Dominant language
- C++
- Stars
- 16.7k
- Forks
- 13.1k
- PR merge metrics
- No merged PRs in 30d
Description
### Basic Infos
#### Hardware
Hardware: NodeMCU v1.0 with ESP-12E
Core Version: 2.3.0-rc2
Wifi router: D-Link DIR-635
### Description
I'm using WifiClient to make HTTP GET requests at regular intervals. After a power outage I discovered that the ESP8266 had reconnected to the Wifi and was responding to connections, but no connections from the ESP8266 was possible. I have reduced the code to a minimum and can now reproduce the error by restarting the Wifi router.
After the Wifi router has been restarted and the ESP8266 is failing to connect, it still responds to pings. When millis() reaches 60000, ESP.restart() is executed and after booting it connects to the Wifi but still fails to connect to the web server. Removing power for 10 seconds is not enough, but removing power for ~15 minutes will make it connect again. Setting a static IP address instead of using DHCP will also make the problem go away.
### Settings in IDE
Module: NodeMCU v1.0 (ESP-12E Module), or Generic ESP8266 Module (for debugging)
Flash Size: 4MB (1MB SPIFFS)
CPU Frequency: 80Mhz
Flash Mode: DIO (setting not available for NodeMCU)
Flash Frequency: 40Mhz (setting not available for NodeMCU)
Upload Using: SERIAL
Reset Method: nodemcu (setting not available for NodeMCU)
### Sketch
```cpp
#include
unsigned long _lastHttpGet;
void setup()
{
Serial.begin(115200);
Serial.println("\r\n\r\nBoot");
InitWifi();
}
void loop()
{
if(WiFi.status() != WL_CONNECTED)
{
WiFi.disconnect();
Serial.println("Wifi disconnected");
InitWifi();
}
if(millis() - _lastHttpGet > 15000)
{
HttpGet();
_lastHttpGet = millis();
}
if(millis() > 60000)
{
Serial.println("Restart");
ESP.restart();
}
}
void InitWifi()
{
const char* ssid = "Silence_of_the_LANs";
const char* password = "---";
Serial.printf("Connecting to: %s", ssid);
WiFi.persistent(false);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while(WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.print("\r\nWifi connected: ");
Serial.println(WiFi.localIP());
}
bool HttpGet()
{
const char *url = "http://date.jsontest.com/";
const char *hostname = "date.jsontest.com";
const int port = 80;
const int buflen = 1024;
char buffer[1024];
Serial.printf("Connecting to: %s:%d\r\n", hostname, port);
WiFiClient client;
if(!client.connect(hostname, port))
{
Serial.println("Connection failed");
return false;
}
snprintf(buffer, buflen, "GET %s HTTP/1.1", url);
Serial.println(buffer);
client.println(buffer);
client.println("Host: ESP8266");
client.println("Connection: close");
client.println();
while(true)
{
if(client.available())
{
int n = client.readBytesUntil('\n', buffer, buflen - 1);
buffer[n] = '\0';
if(n > 0)
{
Serial.print("> ");
Serial.println(buffer);
}
}
if(!client.connected())
{
Serial.printf("Disconnected from %s\r\n", hostname);
client.stop();
return true;
}
}
}
```
### Debug Messages
#### Normal connect:
```
Boot
Connecting to: Silence_of_the_LANs.......
Wifi connected: 192.168.0.199
Connecting to: date.jsontest.com:80
GET http://date.jsontest.com/ HTTP/1.1
> HTTP/1.1 200 OK
> Access-Control-Allow-Origin: *
> Content-Type: application/json; charset=ISO-8859-1
> X-Cloud-Trace-Context: 37068d73f001c4f8aa64cdd0b1a63591
> Date: Wed, 19 Apr 2017 19:09:03 GMT
> Server: Google Frontend
> Content-Length: 100
> Connection: close
>
> {
> "time": "07:09:03 PM",
> "milliseconds_since_epoch": 1492628943567,
> "date": "04-19-2017"
> }
Disconnected from date.jsontest.com
Connecting to: date.jsontest.com:80
GET http://date.jsontest.com/ HTTP/1.1
> HTTP/1.1 200 OK
> Access-Control-Allow-Origin: *
> Content-Type: application/json; charset=ISO-8859-1
> X-Cloud-Trace-Context: d42447f97978b65b8089ac96a8cb549d
> Date: Wed, 19 Apr 2017 19:09:19 GMT
> Server: Google Frontend
> Content-Length: 100
> Connection: close
>
> {
> "time": "07:09:19 PM",
> "milliseconds_since_epoch": 1492628959260,
> "date": "04-19-2017"
> }
Disconnected from date.jsontest.com
```
#### Wifi router restart:
```
Wifi disconnected
Connecting to: Silence_of_the_LANs.............................
Wifi connected: 192.168.0.199
Connecting to: date.jsontest.com:80
Connection failed
Connecting to: date.jsontest.com:80
Connection failed
Restart
ets Jan 8 2013,rst cause:2, boot mode:(3,6)
load 0x4010f000, len 1384, room 16
tail 8
chksum 0x2d
csum 0x2d
v09f0c112
~ld
▒
Boot
Connecting to: Silence_of_the_LANs.......
Wifi connected: 192.168.0.199
Connecting to: date.jsontest.com:80
Connection failed
Connecting to: date.jsontest.com:80
Connection failed
Connecting to: date.jsontest.com:80
Connection failed
Restart
```
#### With Core+Wifi debugging enabled, normal connect:
```
Connecting to: date.jsontest.com:80
[hostByName] request IP for: date.jsontest.com
[hostByName] Host: date.jsontest.com IP: 173.194.221.121
:ref 1
GET http://date.jsontest.com/ HTTP/1.1
:wr
:sent 38
:ww
:wr
:sent 2
```
#### With Core+Wifi debugging enabled, after WIfi router restart:
```
Connecting to: date.jsontest.com:80
[hostByName] request IP for: date.jsontest.com
[hostByName] Host: date.jsontest.com lookup error: -5!
Connection failed
```
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.