@@ -108,13 +108,28 @@ class WifiSta : public WifiBase {
108108 }
109109
110110 // NOTE: Deinit phase
111- // stop the wifi
112- logger_.debug (" Stopping WiFi" );
113- esp_err_t err = esp_wifi_stop ();
114- if (err != ESP_OK) {
115- logger_.error (" Could not stop WiFiSta: {}" , esp_err_to_name (err));
111+ // Check if we're in APSTA mode - if so, just disconnect STA, don't stop WiFi entirely
112+ wifi_mode_t mode;
113+ esp_err_t err = esp_wifi_get_mode (&mode);
114+ if (err == ESP_OK && mode == WIFI_MODE_APSTA) {
115+ // In APSTA mode, just disconnect the STA interface
116+ logger_.debug (" In APSTA mode, disconnecting STA interface" );
117+ esp_wifi_disconnect ();
118+ // Switch to AP-only mode
119+ err = esp_wifi_set_mode (WIFI_MODE_AP);
120+ if (err != ESP_OK) {
121+ logger_.error (" Could not switch to AP mode: {}" , esp_err_to_name (err));
122+ }
123+ logger_.info (" WiFi STA disconnected, AP still running" );
124+ } else {
125+ // In STA-only mode, stop WiFi entirely
126+ logger_.debug (" Stopping WiFi" );
127+ err = esp_wifi_stop ();
128+ if (err != ESP_OK) {
129+ logger_.error (" Could not stop WiFiSta: {}" , esp_err_to_name (err));
130+ }
131+ logger_.info (" WiFi STA stopped" );
116132 }
117- logger_.info (" WiFi STA stopped" );
118133 // Note: WiFi deinit and netif destruction are handled by Wifi singleton
119134 }
120135
@@ -364,8 +379,22 @@ class WifiSta : public WifiBase {
364379 memcpy (wifi_config.sta .bssid , config.ap_mac , 6 );
365380 }
366381
367- logger_.debug (" Setting WiFi mode to STA" );
368- esp_err_t err = esp_wifi_set_mode (WIFI_MODE_STA);
382+ // Get current mode and add STA to it if needed
383+ wifi_mode_t current_mode;
384+ esp_err_t err = esp_wifi_get_mode (¤t_mode);
385+ wifi_mode_t new_mode = WIFI_MODE_STA;
386+ if (err == ESP_OK) {
387+ if (current_mode == WIFI_MODE_AP) {
388+ new_mode = WIFI_MODE_APSTA;
389+ logger_.debug (" AP mode already active, setting mode to APSTA" );
390+ } else if (current_mode == WIFI_MODE_APSTA) {
391+ new_mode = WIFI_MODE_APSTA;
392+ logger_.debug (" APSTA mode already set" );
393+ }
394+ }
395+
396+ logger_.debug (" Setting WiFi mode to {}" , new_mode);
397+ err = esp_wifi_set_mode (new_mode);
369398 if (err != ESP_OK) {
370399 logger_.error (" Could not set WiFi mode STA: {}" , err);
371400 return false ;
0 commit comments