How to chnage wifi mode from SoftAP to STA at runtime

listout
Posts: 31
Joined: Wed Mar 16, 2022 10:54 am

How to chnage wifi mode from SoftAP to STA at runtime

Postby listout » Sun May 01, 2022 8:03 am

I want to change the mode of WiFi from softAP to STA at runtime. Everything is running fine, I can connect to wifi access point but I can't get the IP. I've tried turning on the DHCP log but I'm not that familiar with DHCP to understand what's going on

Here is the code for creating a access point

Code: Select all

	ESP_ERROR_CHECK(esp_netif_init());
	ESP_ERROR_CHECK(esp_event_loop_create_default());
	ESP_ERROR_CHECK(esp_wifi_init(&cfg));

	esp_netif_create_default_wifi_ap();
	wifi_config_t wifi_config = {
		.ap = { .ssid = "test", .ssid_len = strlen("test"),
		.channel = 1,
		.max_connection = 4,
		.authmode = WIFI_AUTH_OPEN,
		},
	};
	ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
	ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP));
	ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &wifi_config));
	ESP_ERROR_CHECK(esp_wifi_start());
For using station, I'm using the example code from gettingstarted/station example. Hence I'm just calling the wifi_init_sta() function. However before calling the function I've made sure to delete the old eventloop deiniting netif and wifi and stopping wifi

Relevant Code

Code: Select all

	vTaskDelay(5000 / portTICK_PERIOD_MS);
	esp_netif_deinit();
	esp_event_loop_delete_default();
	esp_wifi_stop();
	esp_wifi_deinit();
	vTaskDelay(5000 / portTICK_PERIOD_MS);
Log from the terminal:

Code: Select all

I (783) wifi:mode : softAP (24:62:ab:c3:ad:d1)
I (783) wifi:Total power save buffer number: 16
I (783) wifi:Init max length of beacon: 752/752
I (783) wifi:Init max length of beacon: 752/752
I (5793) wifi:flush txq
I (5793) wifi:stop sw txq
I (5793) wifi:lmac stop hw txq
I (5793) wifi:Deinit lldesc rx mblock:10
I (10813) wifi:wifi driver task: 3ffbfe3c, prio:23, stack:6656, core=0
I (10833) wifi:wifi firmware version: 63017e0
I (10833) wifi:wifi certification version: v7.0
I (10833) wifi:config NVS flash: enabled
I (10833) wifi:config nano formating: disabled
I (10843) wifi:Init data frame dynamic rx buffer num: 32
I (10843) wifi:Init management frame dynamic rx buffer num: 32
I (10853) wifi:Init management short buffer num: 32
I (10853) wifi:Init dynamic tx buffer num: 32
I (10863) wifi:Init static rx buffer size: 1600
I (10863) wifi:Init static rx buffer num: 10
I (10863) wifi:Init dynamic rx buffer num: 32
I (10873) wifi_init: rx ba win: 6
I (10873) wifi_init: tcpip mbox: 32
I (10883) wifi_init: udp mbox: 6
I (10883) wifi_init: tcp mbox: 6
I (10883) wifi_init: tcp tx win: 5744
I (10893) wifi_init: tcp rx win: 5744
I (10893) wifi_init: tcp mss: 1440
I (10903) wifi_init: WiFi IRAM OP enabled
I (10903) wifi_init: WiFi RX IRAM OP enabled
I (10913) wifi:mode : sta (24:62:ab:c3:ad:d0)
I (10913) wifi:enable tsf
wifi_init_sta finished.
I (10923) wifi:new:<9,0>, old:<1,0>, ap:<255,255>, sta:<9,0>, prof:1
I (10923) wifi:state: init -> auth (b0)
I (10933) wifi:state: auth -> assoc (0)
I (10943) wifi:state: assoc -> run (10)
W (10973) wifi:<ba-add>idx:0 (ifx:0, 62:a4:b7:1a:9b:01), tid:5, ssn:0, winSize:64
I (11063) wifi:connected with test1234, aid = 3, channel 9, BW20, bssid = 62:a4:b7:1a:9b:01
I (11063) wifi:security: WPA2-PSK, phy: bgn, rssi: -69
I (11073) wifi:pm start, type: 1

I (11093) wifi:AP's beacon interval = 102400 us, DTIM period = 1

Who is online

Users browsing this forum: joglz8, Tomatendose and 110 guests