@@ -72,7 +72,7 @@ void common_hal_wifi_radio_set_enabled(wifi_radio_obj_t *self, bool enabled) {
72
72
}
73
73
if (!self -> started && enabled ) {
74
74
// esp_wifi_start() would default to soft-AP, thus setting it to station
75
- ESP_ERROR_CHECK ( esp_wifi_set_mode ( WIFI_MODE_STA ) );
75
+ start_station ( self );
76
76
ESP_ERROR_CHECK (esp_wifi_start ());
77
77
self -> started = true;
78
78
return ;
@@ -89,7 +89,9 @@ mp_obj_t common_hal_wifi_radio_start_scanning_networks(wifi_radio_obj_t *self) {
89
89
if (self -> current_scan != NULL ) {
90
90
mp_raise_RuntimeError (translate ("Already scanning for wifi networks" ));
91
91
}
92
- // check enabled
92
+ if (!common_hal_wifi_radio_get_enabled (self )) {
93
+ mp_raise_RuntimeError (translate ("wifi is not enabled" ));
94
+ }
93
95
start_station (self );
94
96
95
97
wifi_scannednetworks_obj_t * scan = m_new_obj (wifi_scannednetworks_obj_t );
@@ -126,7 +128,25 @@ void common_hal_wifi_radio_set_hostname(wifi_radio_obj_t *self, const char *host
126
128
}
127
129
128
130
wifi_radio_error_t common_hal_wifi_radio_connect (wifi_radio_obj_t * self , uint8_t * ssid , size_t ssid_len , uint8_t * password , size_t password_len , uint8_t channel , mp_float_t timeout , uint8_t * bssid , size_t bssid_len ) {
129
- // check enabled
131
+ if (!common_hal_wifi_radio_get_enabled (self )) {
132
+ mp_raise_RuntimeError (translate ("wifi is not enabled" ));
133
+ }
134
+
135
+ EventBits_t bits ;
136
+ // can't block since both bits are false after wifi_init
137
+ // both bits are true after an existing connection stops
138
+ bits = xEventGroupWaitBits (self -> event_group_handle ,
139
+ WIFI_CONNECTED_BIT | WIFI_DISCONNECTED_BIT ,
140
+ pdTRUE ,
141
+ pdTRUE ,
142
+ 0 );
143
+ if (((bits & WIFI_CONNECTED_BIT ) != 0 ) &&
144
+ !((bits & WIFI_DISCONNECTED_BIT ) != 0 )) {
145
+ return WIFI_RADIO_ERROR_NONE ;
146
+ }
147
+ // explicitly clear bits since xEventGroupWaitBits may have timed out
148
+ xEventGroupClearBits (self -> event_group_handle , WIFI_CONNECTED_BIT );
149
+ xEventGroupClearBits (self -> event_group_handle , WIFI_DISCONNECTED_BIT );
130
150
start_station (self );
131
151
132
152
wifi_config_t * config = & self -> sta_config ;
@@ -157,7 +177,6 @@ wifi_radio_error_t common_hal_wifi_radio_connect(wifi_radio_obj_t *self, uint8_t
157
177
self -> retries_left = 5 ;
158
178
esp_wifi_connect ();
159
179
160
- EventBits_t bits ;
161
180
do {
162
181
RUN_BACKGROUND_TASKS ;
163
182
bits = xEventGroupWaitBits (self -> event_group_handle ,
0 commit comments