Page 1 of 1

unstable WIFI connection

Posted: Wed Jul 08, 2020 1:01 pm
by azz-zza
hello,
i most likely doing something incorrect. Appreciate pointing out my mistakes.

Code: Select all

#include <WiFi.h>

const char* ssid = "myssid";
const char* password =  "very_big_secret";


void connectToNetwork() {
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Establishing connection to WiFi..");
  }

  Serial.println("Connected to network");

}

void setup() {

  Serial.begin(115200);
  connectToNetwork();

  Serial.println(WiFi.macAddress());
  Serial.println(WiFi.localIP());
}

void loop() {

static long oldTime = 0;
	uint8_t counter = 0;

	if (( millis() - oldTime ) >= 5000)  { //5 seconds check
		oldTime = millis();
		if (WiFi.getMode() == WIFI_MODE_STA && WiFi.status() != WL_CONNECTED ) {
			Serial.println("Wifi is not connected ");
			Serial.printf("Mode - %d, connection status - %d \n" ,WiFi.getMode(),WiFi.status());
			Serial.println("Reconnecting....");
			connectToNetwork();
		}
	}
}

what i observe is :
1. after a minute or so, i finally start getting reply from the host :

Code: Select all

  From 192.168.1.5 icmp_seq=433 Destination Host Unreachable
From 192.168.1.5 icmp_seq=434 Destination Host Unreachable
From 192.168.1.5 icmp_seq=435 Destination Host Unreachable
From 192.168.1.5 icmp_seq=436 Destination Host Unreachable
From 192.168.1.5 icmp_seq=437 Destination Host Unreachable
64 bytes from 192.168.1.17: icmp_seq=438 ttl=255 time=2097 ms
64 bytes from 192.168.1.17: icmp_seq=439 ttl=255 time=1076 ms
64 bytes from 192.168.1.17: icmp_seq=440 ttl=255 time=54.7 ms
64 bytes from 192.168.1.17: icmp_seq=443 ttl=255 time=730 ms
64 bytes from 192.168.1.17: icmp_seq=452 ttl=255 time=591 ms
64 bytes from 192.168.1.17: icmp_seq=461 ttl=255 time=358 ms
64 bytes from 192.168.1.17: icmp_seq=468 ttl=255 time=1279 ms
64 bytes from 192.168.1.17: icmp_seq=469 ttl=255 time=256 ms
64 bytes from 192.168.1.17: icmp_seq=474 ttl=255 time=1147 ms
64 bytes from 192.168.1.17: icmp_seq=475 ttl=255 time=187 ms
considering the router is right under the table - why such a huge delay in boy reply and time it takes to start replying to ping ?
2. after some time (within 5minutes). the reply becomes unstable and eventually stops:

Code: Select all

From 192.168.1.5 icmp_seq=557 Destination Host Unreachable
From 192.168.1.5 icmp_seq=558 Destination Host Unreachable
From 192.168.1.5 icmp_seq=559 Destination Host Unreachable
64 bytes from 192.168.1.17: icmp_seq=569 ttl=255 time=937 ms
From 192.168.1.5 icmp_seq=570 Destination Host Unreachable
From 192.168.1.5 icmp_seq=571 Destination Host Unreachable
From 192.168.1.5 icmp_seq=572 Destination Host Unreachable
From 192.168.1.5 icmp_seq=573 Destination Host Unreachable
at that time the ESP is still "thinks" it is connected - the check on WL_CONNECTED passes. But then it eventually catches up and attempts to reconnect.

at the time of writing, I got lucky and it did succeed in reconnecting. But all day yesterday it would return WL_DISCONNECTED and WL_IDLE_STATUS during the reconnect attempts.

This is regular home wifi. the laptop esp is connected to does not drop connection at the time of the experiment.


P.S. I have 2 ESp32 ( dev1 Kits ), they both exibit the same exact behaviour .
P.P.S. something new WL_CONNECTION_LOST and after reconnect - stable ping replies.

Re: unstable WIFI connection

Posted: Wed Jul 08, 2020 6:16 pm
by lbernstone
esp32 uses power management features for WiFi, so it turns off the radio in between beacon adverts. TCP packets get held by the access point until the device checks in. Datagrams will be dropped. Turn off modem sleep with `WiFi.setSleep(false);`

Re: unstable WIFI connection

Posted: Wed Jul 08, 2020 6:36 pm
by azz-zza
lbernstone wrote:
Wed Jul 08, 2020 6:16 pm
esp32 uses power management features for WiFi, so it turns off the radio in between beacon adverts. TCP packets get held by the access point until the device checks in. Datagrams will be dropped. Turn off modem sleep with `WiFi.setSleep(false);`
Thank you. I gave it a try. Will update the thread after 2 days.

At the same time, the typical "doctor syndrome" - after yet another reboot, it has been running stable for 3 hours. ( even without WIFI.sleep(false) ).

Re: unstable WIFI connection

Posted: Wed Jul 15, 2020 5:14 pm
by azz-zza
not sure if "sleep" helped or not, but the chip has been running fine for a week. Must be some kind of issue with the router.
thank you for the help!