Page 1 of 1

WiFi CSA not working (Channel Switch Announcement)

Posted: Sun Oct 24, 2021 8:23 am
by rfleming
Hi All,

ESP-IDF version: v4.2.2 (Though same thing happened with older versions too when ran the same way)

I have an application that requires me to create a hotspot, connect to it and use a webserver to connect to a local, existing wifi network. The webserver hosted on the esp seemed to be the best way to do this.

Once I have created the hotspot and a webserver, everything works quite well. Though since the esp can only run on a single channel at a time, once I try to connect to a different network, my original device, whether it is my pc with a wifi dongle, or my mobile phone, none of these devices switch to the new network.

To create the wifi network I do the following:

Code: Select all

class Hal_WiFi {
	...
	wifi_config_t configAP;
	wifi_config_t configSTA;
	...
}
	
bool Hal_WiFi::CreateAccessPoint(const char *ssid, const char *password) {
	strcpy((char *)this->configAP.ap.ssid, ssid);
	strcpy((char *)this->configAP.ap.password, password);
	this->configAP.ap.ssid_len = strlen(ssid);
	this->configAP.ap.max_connection = 2;	 //max
	this->configAP.ap.authmode = WIFI_AUTH_WPA_WPA2_PSK;
	this->configAP.ap.channel = 1;

	if (strlen(password) == 0) {
		this->configAP.ap.authmode = WIFI_AUTH_OPEN;
	}

	esp_err_t err;
	if (ESP_OK != (err = esp_wifi_set_mode(WIFI_MODE_AP))) {
		LogE("%s err, couldn't set wifi mode, ret 0x%x", __func__, err);
		return false;
	}
	this->wifiType = WIFI_TYPE_AP;

	if (ESP_OK != (err = esp_wifi_set_config(ESP_IF_WIFI_AP, &this->configAP))) {
		return false;
	}

	//////////////
	LogI("Starting Access Point...");
	setEnable(true);

	return true;
}
Then when joining an existing network, but trying to keep the hotspot active I do the following:

Code: Select all

bool Hal_WiFi::ConnectWithSSID(const char *ssid, const char *password) {
	this->wifiConnecting = false;
	this->connectionRetry = 0;
	this->connectionFailReason = -1;

	/* WiFi Configuration Phase */
	strcpy((char *)this->configSTA.sta.ssid, ssid);
	strcpy((char *)this->configSTA.sta.password, password);

	if (ESP_OK != (err = esp_wifi_set_mode(WIFI_MODE_APSTA))) {
		LogE("%s err, couldn't set wifi mode, ret 0x%x", __func__, err);
		return false;
	}
	this->wifiType = WIFI_TYPE_APSTA;

	if (ESP_OK != (err = esp_wifi_set_config(ESP_IF_WIFI_STA, &this->configSTA))) {
		LogE("%s err, couldn't set wifi sta config, ret 0x%x", __func__, err);
		return false;
	}

	/* WiFi Start Phase */
	LogI("Starting Station...");
	setEnable(true);

	if (ESP_OK != (err = esp_wifi_connect())) {
		LogE("%s err, couldn't wifi connect, ret 0x%x", __func__, err);
		return false;
	}

	this->wifiConnecting = true;
	return true;
}
Finally when I join the new network I get the following log messages:

Code: Select all

Starting Station...
{1B}[0;33mW (139293) httpd_txrx: httpd_resp_send_err: 500 Internal Server Error - Server has encountered an unexpected error{1B}[0m
{1B}[0;33mW (139303) httpd_uri: httpd_uri: uri handler execution failed{1B}[0m
{1B}[0;33mW (141373) httpd_txrx: httpd_resp_send_err: 500 Internal Server Error - Server has encountered an unexpected error{1B}[0m
{1B}[0;33mW (141373) httpd_uri: httpd_uri: uri handler execution failed{1B}[0m
I (142943) wifi:switch to channel 13
I (142943) wifi:ap channel adjust o:1,1 n:13,2
I (142943) wifi:new:<13,0>, old:<1,1>, ap:<13,2>, sta:<0,0>, prof:1
I (142943) wifi:new:<13,2>, old:<13,0>, ap:<13,2>, sta:<13,0>, prof:1
I (143313) wifi:state: init -> auth (b0)
I (143323) wifi:state: auth -> assoc (0)
I (143333) wifi:state: assoc -> run (10)
I (143363) wifi:connected with HomeWifi, aid = 1, channel 13, BW20, bssid = 9c:c9:eb:33:89:62
I (143363) wifi:security: WPA2-PSK, phy: bgn, rssi: -48
I (143363) wifi:pm start, type: 0

Connected to AP, received event STA_CONNECTED.
I (143453) wifi:AP's beacon interval = 102400 us, DTIM period = 2
{1B}[0;32mI (146153) esp_netif_handlers: sta ip: 192.168.1.60, mask: 255.255.255.0, gw: 192.168.1.1{1B}[0m
WiFi IP Event - got new ip: 192.168.1.60
How did other people handle this? Or did they have to disconnect and reconnect to network to fix it?