Page 1 of 1

Switching from WIFI AP to WiFi STA

Posted: Fri Nov 09, 2018 4:51 am
by brp80000
How can I terminate a local access point and enable access to an external access point?

Code: Select all

static void wifi_init_STA(void)
{
    wifi_event_group = xEventGroupCreate();

    tcpip_adapter_init();
    ESP_ERROR_CHECK( esp_event_loop_init(event_handler2, NULL) );

    wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
    ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
    ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );

    wifi_config_t wifi_config = {
        .sta = {
            .ssid = EXT_SSID,
            .password = EXT_PASS,},
    };

    ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
    ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) );
    ESP_ERROR_CHECK( esp_wifi_start() );

    printf("wifi_init_STA finished.SSID:%s password:%s\n", wifi_config.sta.ssid, wifi_config.sta.password);
}

void wifi_init_softAP()
{
	wifi_event_group = xEventGroupCreate();

	tcpip_adapter_init();
	ESP_ERROR_CHECK(esp_event_loop_init(event_handler1, NULL));

	wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
	ESP_ERROR_CHECK(esp_wifi_init(&cfg));

	wifi_config_t wifi_config = {
		.ap = {
			.ssid = AP_SSID,
			.ssid_len = strlen(AP_SSID),
			.password = AP_PASS,
			.max_connection = 1,
			.authmode = WIFI_AUTH_WPA_WPA2_PSK},
	};

	if (strlen(AP_PASS) == 0){
		wifi_config.ap.authmode = WIFI_AUTH_OPEN; }

	ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP));
	ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_AP, &wifi_config));
	ESP_ERROR_CHECK(esp_wifi_start());

	printf("wifi_init_softAP finished.SSID:%s password:%s \n", AP_SSID, AP_PASS);
}
I've tried these sequences to switch

Code: Select all

static void wifi_DEinit(void)
{
	esp_wifi_stop();
	esp_wifi_deinit();
	tcpip_adapter_stop();
}
But I get guru meditation

Re: Switching from WIFI AP to WiFi STA

Posted: Fri Nov 09, 2018 5:40 am
by brp80000
I found an error, both my WIFI functions contained esp_event_loop_init
Now the local AP switches to external.
I still wanted to ask if there is a need to execute commands when switching:
esp_wifi_stop();
esp_wifi_deinit();
tcpip_adapter_stop(WIFI_MODE_AP);

Re: Switching from WIFI AP to WiFi STA

Posted: Fri Nov 09, 2018 7:14 am
by Ritesh
brp80000 wrote:I found an error, both my WIFI functions contained esp_event_loop_init
Now the local AP switches to external.
I still wanted to ask if there is a need to execute commands when switching:
esp_wifi_stop();
esp_wifi_deinit();
tcpip_adapter_stop(WIFI_MODE_AP);
Hi,

I believe there is no need to deinit WiFi and stop tcpip adpater to switch from AP to STA and vice versa.

You just needs to stop WiFi and reconfigure it again and then start.

You needs to init WiFi and init TCP IP Adapter only one time while starting WiFi for STA or AP mode.

Let me know if you still have any questions or issue regarding that.

Re: Switching from WIFI AP to WiFi STA

Posted: Sun Nov 11, 2018 9:33 am
by brp80000
Ok its work

Re: Switching from WIFI AP to WiFi STA

Posted: Mon Nov 12, 2018 4:42 am
by Ritesh
brp80000 wrote:
Sun Nov 11, 2018 9:33 am
Ok its work
Great. Thanks for update. Let me know if any query regarding that into future as well