Page 1 of 1

WiFi bottleneck as APSTA when STA can not connected

Posted: Tue Dec 11, 2018 6:18 pm
by rsimpsonbusa
Hi everybody.

Scenario:
I have an ESP32 that will be a "server" that connects to the Internet as an STA and also as a AP, hence it is setup as APSTA. Several clients ESP32s connect to the "server" and send UDP packets.

Problem:
When the Server is able to connect to the external AP there is NO problem. STA is connected to the assigned AP and the ESP32 AP section assigns IPs and receives UDP packets from the clients.

But if the Server(as an STA) can not connect to the external AP, the local AP section takes a very long time to assign an IP to its clients and receives UDP packets after a very long time (if at all), missing many UDP packets sent and. One can see that the client is waiting for the IP with the ESP_LOG at Info level.

Reasoning:
It seems that the STA keeps trying to connect to its configured SSID and causes a SERIOUS bottleneck that eats the wifi's response time and/or cpu (really don't know this). Probably its "scanning" and this is hurting the overall process.

Proof of Reasoning:
(a) When STA connects to the external AP from the start everything flows as expected.
(b) When the STA connects to the external AP some time after start work begins flowing normally. Meanwhile there is serious bottleneck which could imply that the STA is trying to connect to the external AP and eating cycles and hurting everything wifi related.
(c) If I do not configure the STA section,

Code: Select all

ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &configap));
everything works perfectly as a local AP, giving IPs in the usual fast manner.
(c1) This would mean the ESP is NOT trying to connect to the external AP and hence does not eat cpu cycles or wifi process.
(d) This was proved by commenting out the STA config section
The WiFi init section is this

Code: Select all

void initWiFi()
{
	wifi_init_config_t 	cfg=WIFI_INIT_CONFIG_DEFAULT();
	wifi_config_t 		configap;
	u8 				mac[6];
	char 				textl[20];

	tcpip_adapter_init();
	ESP_ERROR_CHECK( esp_event_loop_init(wifi_event_handler, NULL));
	ESP_ERROR_CHECK(esp_wifi_init(&cfg));
	ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
	esp_wifi_set_ps(WIFI_PS_NONE); //otherwise multicast does not work well or at all

	ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_APSTA));
	esp_wifi_get_mac(ESP_IF_WIFI_STA, (uint8_t*)&mac);

	memset(&configap,0,sizeof(configap));

	if(sysConfig.ssid[1][0])!=0) //SSID for external AP and internet access
	{
		strcpy((char *)configap.sta.ssid , sysConfig.ssid[1]);
		strcpy((char *)configap.sta.password, sysConfig.pass[1]);
		ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &configap));
	}
	if(sysConfig.ssid[0][0])!=0) //ssid for local AP
	{
		strcpy((char *)configap.ap.ssid,sysConfig.ssid[0]);
		strcpy((char *)configap.ap.password,sysConfig.pass[0]);
		printf("AP Loading [%s][%s]\n",configap.ap.ssid,configap.ap.password);
	}
	else
	{ //no name given yet, assign one
		sprintf(textl,"LightIoT%02x%02x",mac[4],mac[5]);
		strcpy((char*)configap.ap.ssid,textl);
		strcpy((char*)configap.ap.password,textl);
	}
	configap.ap.ssid_len=strlen((char *)configap.ap.ssid);
	configap.ap.authmode=WIFI_AUTH_WPA_PSK;
	configap.ap.ssid_hidden=false;
	ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_AP, &configap));
	ESP_ERROR_CHECK(esp_wifi_start());
}
Request:
Is this a bug? Is there a way around this problem?
Any help welcomed.

Comments:
I believe this a rather standard situation and unless I've made programming errors, it should not happen AT ALL. During normal operations an internet access can and will go down and what is going to happed is unpredictable. This makes the ESP an unreliable alternative for time critical solutions.

What's been tested:
Changed many of the config options for the Ap and the STA with no change
beacon interval,
scan option,
listen interval,

Regards.

Re: WiFi bottleneck as APSTA when STA can not connected

Posted: Tue Dec 11, 2018 11:03 pm
by WiFive
Do not immediately try to reconnect in your event handler, use some kind of delay/backoff

Re: WiFi bottleneck as APSTA when STA can not connected

Posted: Wed Dec 12, 2018 1:20 am
by rsimpsonbusa
Hi WiFive.

Did try that, no such luck. It really eats up the WiFi logic. The CPU seems ok, its not a busy loop or similar.

What I ended up doing is creating a sort of independent process for the STA and the AP.

Do the AP part as usual, ei,

Code: Select all

ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_AP, &configap));
ESP_ERROR_CHECK(esp_wifi_start());
and the launch a Task that will do a scan

Code: Select all

	int err=esp_wifi_scan_start(&scan_config, true);
looking for the external AP and if it now found retry indefinitely. When found

Code: Select all

ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &configap));
ESP_ERROR_CHECK(esp_wifi_connect());
and kill the task. It actually worked :lol:

Rather primitive for a sophisticated chip wouldn't you say?

Re: WiFi bottleneck as APSTA when STA can not connected

Posted: Sat May 09, 2020 5:32 pm
by rsimpsonbusa
So after some time i found the way to do it.

MUST clear the wifi_config before setting EACH of the adapter, STA and AP. So, do the usual wifi_sta config, then memset or bzero the wifi_config AGAIN and set the other adapter.

Re: WiFi bottleneck as APSTA when STA can not connected

Posted: Mon Jun 15, 2020 3:42 pm
by Pallanez
rsimpsonbusa wrote:
Sat May 09, 2020 5:32 pm
So after some time i found the way to do it.

MUST clear the wifi_config before setting EACH of the adapter, STA and AP. So, do the usual wifi_sta config, then memset or bzero the wifi_config AGAIN and set the other adapter.
Could you elaborate on this please? I'm having a similar problem.

Thanks!