29 bool _wifi_ScanAPs = false; ///< Scanning |
29 bool _wifi_ScanAPs = false; ///< Scanning |
30 bool _wifi_ScanDone = false; ///< Scan ready |
30 bool _wifi_ScanDone = false; ///< Scan ready |
31 uint16_t _wifi_Scanned = 0; ///< Total scanned APs. |
31 uint16_t _wifi_Scanned = 0; ///< Total scanned APs. |
32 |
32 |
33 |
33 |
34 const int TASK_WIFI_REQUEST_WIFI_SCAN = BIT0; ///< When set, means a client requested to scan wireless networks. |
34 const int TASK_WIFI_REQUEST_STA_DISCONNECT = BIT0; ///< When set, means a client requested to disconnect from currently connected AP. |
35 const int TASK_WIFI_REQUEST_STA_DISCONNECT = BIT1; ///< When set, means a client requested to disconnect from currently connected AP. |
35 const int TASK_WIFI_REQUEST_STA_CONNECT = BIT1; ///< When set, means a client requested to connect to an access point. |
36 const int TASK_WIFI_REQUEST_STA_CONNECT = BIT2; ///< When set, means a client requested to connect to an access point. |
36 const int TASK_WIFI_REQUEST_STA_STATUS = BIT2; ///< When set, means a client requested to update the connection status. |
37 const int TASK_WIFI_REQUEST_STA_STATUS = BIT8; |
|
38 |
37 |
39 const int TASK_WIFI_HAS_IP = BIT3; ///< Indicate that we have an IP address |
38 const int TASK_WIFI_HAS_IP = BIT3; ///< Indicate that we have an IP address |
40 const int TASK_WIFI_STA_FAILED = BIT4; ///< Indicate that we could not get a connection to AP as station. |
39 const int TASK_WIFI_STA_FAILED = BIT4; ///< Indicate that we could not get a connection to AP as station. |
41 const int TASK_WIFI_STA_DISCONNECTED = BIT5; ///< Indicate that we are disconnected from an ap station. |
40 const int TASK_WIFI_STA_DISCONNECTED = BIT5; ///< Indicate that we are disconnected from an ap station. |
42 const int TASK_WIFI_STA_CONNECTED = BIT6; ///< Indicate that we are connected to AP as station, flip of BIT6. |
41 const int TASK_WIFI_STA_CONNECTED = BIT6; ///< Indicate that we are connected to AP as station, flip of BIT5. |
43 |
42 |
44 |
43 |
45 |
44 |
46 /** |
45 /** |
47 * @brief Array with AP security names |
46 * @brief Array with AP security names |
121 ESP_LOGE(TAG, "wifi_event_handler() lock error WIFI_EVENT_STA_DISCONNECTED"); |
120 ESP_LOGE(TAG, "wifi_event_handler() lock error WIFI_EVENT_STA_DISCONNECTED"); |
122 } |
121 } |
123 connect_mqtt(false); |
122 connect_mqtt(false); |
124 xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_STA_CONNECTED); |
123 xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_STA_CONNECTED); |
125 xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_STA_DISCONNECTED); |
124 xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_STA_DISCONNECTED); |
126 |
|
127 //if (disconnected->reason == WIFI_REASON_NO_AP_FOUND && ! _wifi_ScanAPs) { |
|
128 // ESP_LOGI(TAG, "Request scan for another AP"); |
|
129 // _wifi_ScanAPs = true; |
|
130 // _wifi_ScanDone = false; |
|
131 // ap_num = MAX_AP_NUM; |
|
132 // ESP_ERROR_CHECK(esp_wifi_scan_start(&scan_config, false)); |
|
133 // xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT); // Keep looping active. |
|
134 // break; |
|
135 //} |
|
136 |
|
137 //if (disconnected->reason == WIFI_REASON_NO_AP_FOUND && _wifi_ScanAPs && _wifi_ScanDone) { |
|
138 // ESP_LOGD(TAG, "Scan completed, look for another AP, found:%d", _wifi_Scanned); |
|
139 // _wifi_ScanAPs = false; |
|
140 // _wifi_ScanDone = false; |
|
141 // for (int i = 0; i < _wifi_Scanned; i++) { |
|
142 // ap = accessp_records[i]; |
|
143 // if ((read_station(ap.ssid) != -1)) { |
|
144 // if (wifiStation.hide) { |
|
145 // continue; // Blacklisted. |
|
146 // } |
|
147 /* We know this one */ |
|
148 // wifi_config_t* config = task_wifi_ConfigSTA; |
|
149 // memset(config, 0x00, sizeof(wifi_config_t)); |
|
150 // memcpy(config->sta.ssid, wifiStation.SSID, strlen(wifiStation.SSID)); |
|
151 // memcpy(config->sta.password, wifiStation.Password, strlen(wifiStation.Password)); |
|
152 // ESP_LOGD(TAG, "new AP %s %s", wifiStation.SSID, wifiStation.Password); |
|
153 // xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT); |
|
154 // break; |
|
155 // } |
|
156 // } |
|
157 // break; |
|
158 //} |
|
159 |
|
160 /* |
|
161 * Reconnect previous AP. |
|
162 */ |
|
163 //if (FetchStaConfig()) { |
|
164 // xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT); |
|
165 //} |
|
166 break; |
125 break; |
167 } |
126 } |
168 |
127 |
169 default: |
128 default: |
170 ESP_LOGW(TAG, "Unknown WiFi event %d", event_id); |
129 ESP_LOGW(TAG, "Unknown WiFi event %d", event_id); |
257 ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM)); |
216 ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM)); |
258 ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); |
217 ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); |
259 ESP_ERROR_CHECK(esp_wifi_start()); |
218 ESP_ERROR_CHECK(esp_wifi_start()); |
260 |
219 |
261 xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT); |
220 xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT); |
|
221 xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_STA_CONNECTED); |
262 xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_STA_DISCONNECTED); |
222 xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_STA_DISCONNECTED); |
263 EventBits_t uxBits; |
223 EventBits_t uxBits; |
264 |
224 |
265 for(;;) { |
225 for(;;) { |
266 |
226 |
267 /* actions that can trigger: request a connection, a scan, or a disconnection */ |
227 /* actions that can trigger: request a connection, a scan, or a disconnection */ |
268 uxBits = xEventGroupWaitBits(xEventGroupWifi, |
228 uxBits = xEventGroupWaitBits(xEventGroupWifi, |
269 TASK_WIFI_REQUEST_STA_CONNECT | TASK_WIFI_REQUEST_WIFI_SCAN | TASK_WIFI_REQUEST_STA_DISCONNECT | TASK_WIFI_REQUEST_STA_STATUS, |
229 TASK_WIFI_REQUEST_STA_CONNECT | TASK_WIFI_REQUEST_STA_DISCONNECT | TASK_WIFI_REQUEST_STA_STATUS, |
270 pdFALSE, pdFALSE, portMAX_DELAY ); |
230 pdFALSE, pdFALSE, portMAX_DELAY ); |
271 |
231 |
272 if (uxBits & TASK_WIFI_REQUEST_STA_DISCONNECT) { |
232 if (uxBits & TASK_WIFI_REQUEST_STA_DISCONNECT) { |
273 /* |
233 /* |
274 * user requested a disconnect, this will in effect disconnect the wifi |
234 * user requested a disconnect, this will in effect disconnect the wifi |
334 * Request WiFi update status, refresh the rssi. |
294 * Request WiFi update status, refresh the rssi. |
335 */ |
295 */ |
336 xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_STATUS); |
296 xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_STATUS); |
337 wifi_ap_record_t ap_info; |
297 wifi_ap_record_t ap_info; |
338 esp_wifi_sta_get_ap_info(&ap_info); |
298 esp_wifi_sta_get_ap_info(&ap_info); |
339 ESP_LOGI(TAG, "Event STA status, ssid:%s, bssid:" MACSTR ", rssi: %d", ap_info.ssid, MAC2STR(ap_info.bssid), ap_info.rssi); |
299 ESP_LOGD(TAG, "Event STA status, ssid:%s, bssid:" MACSTR ", rssi: %d", ap_info.ssid, MAC2STR(ap_info.bssid), ap_info.rssi); |
340 if (xSemaphoreTake(xSemaphoreWiFi, 35) == pdTRUE) { |
300 if (xSemaphoreTake(xSemaphoreWiFi, 35) == pdTRUE) { |
341 wifi_state->STA_rssi = ap_info.rssi; |
301 wifi_state->STA_rssi = ap_info.rssi; |
342 xSemaphoreGive(xSemaphoreWiFi); |
302 xSemaphoreGive(xSemaphoreWiFi); |
343 } else { |
303 } else { |
344 ESP_LOGE(TAG, "lock error TASK_WIFI_REQUEST_STA_STATUS"); |
304 ESP_LOGE(TAG, "lock error TASK_WIFI_REQUEST_STA_STATUS"); |