nodemcu esp8266 loses connection unless wifi is scanned constantly

I am trying to make something with nodemcu esp8266. However, I am having a bit of an issue. When I connect the esp to the network, it connects briefly (WiFi.status() returns WL_CONNECTED). However, even when it is connected, it doesn't respond to pings or http requests. After about 5 seconds it disconnects (WiFi.status() returns WL_DISCONNECTED). Naturally, the esp is still not reachable. After just a little bit longer, WiFi.status() returns WL_NO_SSID_AVAIL. At this point, it just keeps returning that and the status never changes. After tinkering around for a while I checked what would happen if I scanned the available networks. I did so via WiFi.scanNetworks() I started scanning the networks periodically (about every second, which is also the period at which I am checking the WiFi status) after I started doing that, the esp no longer lost connectivity and was perfectly reachable. Here is how I am connecting to the network:
void wifiConnect(const char* ssid, const char* passwd) {
DBG("Connecting to");
DPRINTLN(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, passwd);

while (WiFi.status() != WL_CONNECTED) {
delay(500);
INFO(".");
}
WiFi.persistent(false);
WiFi.setAutoReconnect(true);
WiFi.setSleepMode(WIFI_NONE_SLEEP);
IPRINTLN("");
IPRINTLN("WiFi connected.");
INFO("Use this URL to connect: ");
INFO("http://");
INFO(WiFi.localIP());
IPRINTLN("/");
}
void wifiConnect(const char* ssid, const char* passwd) {
DBG("Connecting to");
DPRINTLN(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, passwd);

while (WiFi.status() != WL_CONNECTED) {
delay(500);
INFO(".");
}
WiFi.persistent(false);
WiFi.setAutoReconnect(true);
WiFi.setSleepMode(WIFI_NONE_SLEEP);
IPRINTLN("");
IPRINTLN("WiFi connected.");
INFO("Use this URL to connect: ");
INFO("http://");
INFO(WiFi.localIP());
IPRINTLN("/");
}
And here is the code for scanning the wifi networks:
void loop(){
WiFi.scanNetworks();
delay(1000);
}
void loop(){
WiFi.scanNetworks();
delay(1000);
}
Another thing that I noticed is that when I print out WiFi.RSSI() if I am not doing the scanning it remains constant across each call (while connected anyway. After it disconnects it just changes to 31, which is probably some error value). It's like the antenna falls asleep after establishing the connection initially or something.
8 Replies
pseud0
pseud03mo ago
Does this happen on other boards as well? Does this happen in the simplest possible sketch where you only connect to WiFi in setup() and then blink an LED in loop()?
nikcho-kouhai
nikcho-kouhaiOP3mo ago
yeah, same thing happens and I am pretty new to arduino stuff so I can't say anything about other boards as I don't actually have any. however, this behavior certainly seems abnormal
pseud0
pseud03mo ago
Are there many WiFis in the vicinity of the ESP8266? How many networks does it see during a scan? I have seen in the past that in high-traffic environments, the WiFi task can't keep up and crashes or loses connection
nikcho-kouhai
nikcho-kouhaiOP3mo ago
that's very unlikely to be the case. I am in a village at the moment and there are only two networks here. One in my house and one in the neighbors (which is on and off as it's pretty far). It's also not happening because the signal is bad, because I tried connecting to the hotspot on my phone while shoving it basically on top of the esp and the same thing occurred the odd thing to me really is that it's fine as long as I am continously scanning available networks. It really feels like some sort of keep-alive thing for the receiver but that just kind of feels like a hack
pseud0
pseud03mo ago
Try to erase the entire flash of the ESP8266 to force it into a RF recalibration https://www.youtube.com/watch?v=2ah2nFwzAyo Too much RF signal coupling into the antenna is also not good, when it's that close
nikcho-kouhai
nikcho-kouhaiOP3mo ago
well I tried it when it's in a relatively normal distance too and that also didn't help ill check it out in a second For some reason my internet absolutely dunked And I am downloading the flasher at 20KiB/s so this is gonna take a while... alright, so the good news is that I didn't brick my microcontroller the bad news is the same thing is happening
pseud0
pseud03mo ago
Can you use this as your wifiConnect()? The ordering of the calls seem to matter!
void wifiConnect(const char* ssid, const char* passwd) {
DBG("Connecting to");
DPRINTLN(ssid);

WiFi.mode(WIFI_STA);
WiFi.persistent(false); // Set before connecting
WiFi.setAutoReconnect(true); // Set before connecting
WiFi.setSleepMode(WIFI_NONE_SLEEP); // Disable sleep before connecting
WiFi.begin(ssid, passwd); // Now start connection

while (WiFi.status() != WL_CONNECTED) {
delay(500);
INFO(".");
}

IPRINTLN("");
IPRINTLN("WiFi connected.");
INFO("Use this URL to connect: ");
INFO("http://");
INFO(WiFi.localIP());
IPRINTLN("/");
}
void wifiConnect(const char* ssid, const char* passwd) {
DBG("Connecting to");
DPRINTLN(ssid);

WiFi.mode(WIFI_STA);
WiFi.persistent(false); // Set before connecting
WiFi.setAutoReconnect(true); // Set before connecting
WiFi.setSleepMode(WIFI_NONE_SLEEP); // Disable sleep before connecting
WiFi.begin(ssid, passwd); // Now start connection

while (WiFi.status() != WL_CONNECTED) {
delay(500);
INFO(".");
}

IPRINTLN("");
IPRINTLN("WiFi connected.");
INFO("Use this URL to connect: ");
INFO("http://");
INFO(WiFi.localIP());
IPRINTLN("/");
}
You can also add
WiFi.onStationModeDisconnected([](const WiFiEventStationModeDisconnected& event) {
Serial.print("Disconnected from WiFi. Reason: ");
Serial.println(event.reason);
});
WiFi.onStationModeDisconnected([](const WiFiEventStationModeDisconnected& event) {
Serial.print("Disconnected from WiFi. Reason: ");
Serial.println(event.reason);
});
to get more disconnect reasons info
nikcho-kouhai
nikcho-kouhaiOP3mo ago
The same thing happens. Also for some reason the onStationModeDisconnected callback never fires. I tried placing it before WiFi.begin and after, but it never fired.

Did you find this page helpful?