How to connect to wifi network while in APSTA mode?

lukedukeusa
Posts: 3
Joined: Wed Feb 23, 2022 11:48 pm

How to connect to wifi network while in APSTA mode?

Postby lukedukeusa » Sat Mar 12, 2022 2:23 am

Right now I am starting up my esp32-c3 in APSTA mode using esp-idf. This serves a captive portal where a user can select a wifi network and connect to it, similar to how tasmota and wifimanager work. I am able to connect to a network if I call esp_wifi_stop();, then reconfigure, then start the network, but this closes the captive portal. I am would like to start in APSTA mode, but only configure the AP part, then configure the STA later, confirm the STA is working, then shut down the AP, all without rebooting or shutting off wifi or loosing AP connection.

Is this possible and if so how would I do it?

mschultz
Posts: 8
Joined: Mon Apr 26, 2021 6:41 pm

Re: How to connect to wifi network while in APSTA mode?

Postby mschultz » Tue Mar 15, 2022 6:01 pm

I am doing something similar (captive portal for Wi-Fi provisioning) in my project and I'd also like an answer to this question.

When operating in APSTA mode, I want to be able to bring up or shut down the AP without affecting the STA connection status, or connect/disconnect the STA without affecting AP operation.

Is this possible?

maldus
Posts: 83
Joined: Wed Jun 06, 2018 1:41 pm

Re: How to connect to wifi network while in APSTA mode?

Postby maldus » Tue Jun 20, 2023 7:32 am

This is still relevant. Is it possible?

boarchuz
Posts: 606
Joined: Tue Aug 21, 2018 5:28 am

Re: How to connect to wifi network while in APSTA mode?

Postby boarchuz » Tue Jun 20, 2023 10:37 pm

Yes, you can freely change WiFi mode without needing to completely stop and restart the driver.

eg.

Code: Select all

esp_wifi_start();
esp_wifi_set_mode(AP);
esp_wifi_set_config(AP, &ap_config);
/* ... Receive WiFi credentials from user ... */
esp_wifi_set_mode(APSTA);
esp_wifi_set_config(STA, &user_sta_config);
esp_wifi_connect();
/* ... Connection succeeded, we decide we don't need AP any more ... */
esp_wifi_set_mode(STA);
/* ... Do things, then when done ... */
esp_wifi_disconnect();
esp_wifi_stop();

juan3211
Posts: 3
Joined: Tue Mar 06, 2018 7:33 am

Re: How to connect to wifi network while in APSTA mode?

Postby juan3211 » Wed Feb 28, 2024 9:42 pm

Hi, any one from this post has a good source code about this?

I am also trying to make something "similar" to wifimanager but in esp-idf framework.

Thanks a lot

praveenvaniamkulam
Posts: 3
Joined: Fri Sep 27, 2024 11:03 am

Re: How to connect to wifi network while in APSTA mode?

Postby praveenvaniamkulam » Wed Oct 09, 2024 6:30 am

Hi All,

I am trying to communicate to external IP address in WIFI_MODE_APSTA which is not working. Terminal log mentions that station is already connected and assigned with a IP adderss. Code snippet as given below:

Code: Select all

  //public DNS server address
    struct sockaddr_in public_dns;
    public_dns.sin_addr.s_addr = inet_addr(DEFAULT_DNS);  
    public_dns.sin_family = AF_INET;
    public_dns.sin_port = htons(DNS_PORT);
    address_family = AF_INET;
    ip_protocol = IPPROTO_IP;
     
	
	 // Create socket for UDP
    if ((sockfd = socket(address_family, SOCK_DGRAM,ip_protocol)) < 0) {
        ESP_LOGI(TAG,"Socket creation failed");
        exit(EXIT_FAILURE);
    }
    ESP_LOGI(TAG,"Socket creation success");
    
   int err = bind(sockfd, (struct sockaddr *)&public_dns, sizeof(public_dns));
    if (err < 0) {
        ESP_LOGE(TAG, "Public DNS Socket unable to bind: errno %d", errno);
        shutdown(sockfd, 0);
   		close(sockfd);
        return -1;
    }
    ESP_LOGI(TAG, "Public DNS Socket bound to port %d", DNS_PORT);
    
    // Send the query to the Public DNS server
    socklen_t public_socklen = sizeof(public_dns);
   
    err = sendto(sockfd, payload,50, 0, (struct sockaddr *)&public_dns,sizeof(public_dns));
    if (err < 0) {
        ESP_LOGE(TAG, "Error occurred during sending to query to Public DNS: errno %d", errno);   
        shutdown(sockfd, 0);
    	close(sockfd);
        return -1; 
    }
As provided in the above, I tried changing the mode to WIFI_MODE_STA and tried to communicate. Socket is created successfully and bind to the port. But I am getting an error "Error Code 118" while sending the payload. Do I need to disconnect using esp_wifi_disconnect(); before changing the mode?

Thanks in advance.

Who is online

Users browsing this forum: ESPBoards and 113 guests