11 static const char *TAG = "task_wifi"; |
11 static const char *TAG = "task_wifi"; |
12 |
12 |
13 |
13 |
14 SemaphoreHandle_t xSemaphoreWiFi = NULL; ///< Semaphore WiFi task. |
14 SemaphoreHandle_t xSemaphoreWiFi = NULL; ///< Semaphore WiFi task. |
15 EventGroupHandle_t xEventGroupWifi; ///< Events WiFi task. |
15 EventGroupHandle_t xEventGroupWifi; ///< Events WiFi task. |
16 uint16_t ap_num = MAX_AP_NUM; ///< Scan counter. |
|
17 wifi_ap_record_t *accessp_records; ///< [MAX_AP_NUM] records array with scan results. |
|
18 wifi_config_t *task_wifi_ConfigSTA = NULL; ///< Current STA configuration. |
16 wifi_config_t *task_wifi_ConfigSTA = NULL; ///< Current STA configuration. |
19 WIFI_State *wifi_state = NULL; ///< Public state for other tasks. |
17 WIFI_State *wifi_state = NULL; ///< Public state for other tasks. |
20 esp_netif_t *sta_netif = NULL; ///< Station interface |
18 esp_netif_t *sta_netif = NULL; ///< Station interface |
21 |
19 |
22 |
20 |
23 wifi_scan_config_t scan_config = { ///< WiFi scanner configuration. |
21 //wifi_scan_config_t scan_config = { ///< WiFi scanner configuration. |
24 .ssid = (uint8_t *)CONFIG_ESP_WIFI_SSID, |
22 // .ssid = (uint8_t *)CONFIG_ESP_WIFI_SSID, |
25 .bssid = 0, |
23 // .bssid = 0, |
26 .channel = 0, |
24 // .channel = 0, |
27 .show_hidden = false |
25 // .show_hidden = false |
28 }; |
26 //}; |
29 |
27 |
30 bool _wifi_ScanDone = false; ///< Scan ready |
28 bool System_TimeOk = false; ///< True if online and sntp sync. |
31 bool _wifi_BetterAP = false; ///< If better AP available. |
|
32 int8_t _wifi_RSSI = -127; ///< Latest RSSI level. |
29 int8_t _wifi_RSSI = -127; ///< Latest RSSI level. |
33 uint16_t _wifi_Scanned = 0; ///< Total scanned APs. |
|
34 |
30 |
35 extern char hostname[]; ///< Generated hostname |
31 extern char hostname[]; ///< Generated hostname |
36 |
32 |
37 |
33 |
38 |
34 |
39 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 = BIT0; ///< When set, means a client requested to disconnect from currently connected AP. |
40 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 = BIT1; ///< When set, means a client requested to connect to an access point. |
41 const int TASK_WIFI_REQUEST_STA_SCAN = BIT2; ///< When set, means a client requested a AP scan. |
|
42 const int TASK_WIFI_REQUEST_STA_STATUS = BIT3; ///< When set, means a client requested to update the connection status. |
|
43 |
37 |
44 const int TASK_WIFI_HAS_IP = BIT4; ///< Indicate that we have an IP address |
38 const int TASK_WIFI_HAS_IP = BIT4; ///< Indicate that we have an IP address |
45 const int TASK_WIFI_STA_FAILED = BIT5; ///< Indicate that we could not get a connection to AP as station. |
39 const int TASK_WIFI_STA_FAILED = BIT5; ///< Indicate that we could not get a connection to AP as station. |
46 const int TASK_WIFI_STA_DISCONNECTED = BIT6; ///< Indicate that we are disconnected from an ap station. |
40 const int TASK_WIFI_STA_DISCONNECTED = BIT6; ///< Indicate that we are disconnected from an ap station. |
47 const int TASK_WIFI_STA_CONNECTED = BIT7; ///< Indicate that we are connected to AP as station, flip of BIT5. |
41 const int TASK_WIFI_STA_CONNECTED = BIT7; ///< Indicate that we are connected to AP as station, flip of BIT5. |
112 |
93 |
113 static void wifi_event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) |
94 static void wifi_event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) |
114 { |
95 { |
115 switch (event_id) { |
96 switch (event_id) { |
116 |
97 |
117 case WIFI_EVENT_SCAN_DONE: |
|
118 { |
|
119 /* Get the results so the memory used is freed. */ |
|
120 ESP_ERROR_CHECK(esp_wifi_scan_get_ap_records(&ap_num, accessp_records)); |
|
121 ESP_LOGI(TAG, "Event wifi Scane done, %d records", ap_num); |
|
122 _wifi_BetterAP = false; |
|
123 for (int i = 0; i < ap_num; i++) { |
|
124 wifi_ap_record_t ap = accessp_records[i]; |
|
125 ESP_LOGI(TAG, "AP:%d bssid:%02x:%02x:%02x:%02x:%02x:%02x ssid:%s ch:%d rssi:%d", |
|
126 i, ap.bssid[0], ap.bssid[1], ap.bssid[2], ap.bssid[3], ap.bssid[4], ap.bssid[5], |
|
127 ap.ssid, ap.primary, ap.rssi); |
|
128 if (ap.rssi > CONFIG_ESP_WIFI_ROAMING_LEVEL && ap.rssi > (_wifi_RSSI + 3)) { |
|
129 _wifi_BetterAP = true; |
|
130 ESP_LOGI(TAG, "AP:%d is a better AP", i); |
|
131 } |
|
132 } |
|
133 _wifi_Scanned = ap_num; |
|
134 _wifi_ScanDone = true; |
|
135 if (_wifi_BetterAP) { |
|
136 ESP_LOGI(TAG, "Disconnect current AP"); |
|
137 disconnect_WiFi(); |
|
138 } |
|
139 break; |
|
140 } |
|
141 |
|
142 case WIFI_EVENT_STA_START: |
98 case WIFI_EVENT_STA_START: |
143 { |
99 { |
144 ESP_LOGI(TAG, "Event wifi START"); |
100 ESP_LOGI(TAG, "Event wifi START"); |
145 // Set the configured hostname for the dhcp client. |
101 // Set the configured hostname for the dhcp client. |
146 ESP_ERROR_CHECK(esp_netif_set_hostname(sta_netif, hostname)); |
102 ESP_ERROR_CHECK(esp_netif_set_hostname(sta_netif, hostname)); |
147 esp_wifi_connect(); |
103 esp_wifi_connect(); |
148 _wifi_BetterAP = false; |
|
149 break; |
104 break; |
150 } |
105 } |
151 |
106 |
152 case WIFI_EVENT_STA_CONNECTED: |
107 case WIFI_EVENT_STA_CONNECTED: |
153 { |
108 { |
166 xSemaphoreGive(xSemaphoreWiFi); |
121 xSemaphoreGive(xSemaphoreWiFi); |
167 } else { |
122 } else { |
168 ESP_LOGE(TAG, "wifi_event_handler() lock error WIFI_EVENT_STA_CONNECTED"); |
123 ESP_LOGE(TAG, "wifi_event_handler() lock error WIFI_EVENT_STA_CONNECTED"); |
169 } |
124 } |
170 print_servers(); |
125 print_servers(); |
171 _wifi_BetterAP = false; |
|
172 xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_STA_CONNECTED); |
126 xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_STA_CONNECTED); |
173 xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_STA_DISCONNECTED); |
127 xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_STA_DISCONNECTED); |
174 break; |
128 break; |
175 } |
129 } |
176 |
130 |
186 wifi_state->STA_rssi = 0; |
140 wifi_state->STA_rssi = 0; |
187 xSemaphoreGive(xSemaphoreWiFi); |
141 xSemaphoreGive(xSemaphoreWiFi); |
188 } else { |
142 } else { |
189 ESP_LOGE(TAG, "wifi_event_handler() lock error WIFI_EVENT_STA_DISCONNECTED"); |
143 ESP_LOGE(TAG, "wifi_event_handler() lock error WIFI_EVENT_STA_DISCONNECTED"); |
190 } |
144 } |
191 _wifi_BetterAP = false; |
145 System_TimeOk = false; |
192 xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_STA_CONNECTED); |
146 xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_STA_CONNECTED); |
193 xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_STA_DISCONNECTED); |
147 xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_STA_DISCONNECTED); |
194 break; |
148 break; |
195 } |
149 } |
196 |
150 |
234 wifi_state->STA_online = false; |
188 wifi_state->STA_online = false; |
235 xSemaphoreGive(xSemaphoreWiFi); |
189 xSemaphoreGive(xSemaphoreWiFi); |
236 } else { |
190 } else { |
237 ESP_LOGE(TAG, "got_ip_event_handler() lock error IP_EVENT_STA_LOST_IP"); |
191 ESP_LOGE(TAG, "got_ip_event_handler() lock error IP_EVENT_STA_LOST_IP"); |
238 } |
192 } |
|
193 System_TimeOk = false; |
239 break; |
194 break; |
240 |
195 |
241 case IP_EVENT_AP_STAIPASSIGNED: |
196 case IP_EVENT_AP_STAIPASSIGNED: |
242 ESP_LOGI(TAG, "IP_EVENT_AP_STAIPASSIGNED"); |
197 ESP_LOGI(TAG, "IP_EVENT_AP_STAIPASSIGNED"); |
243 break; |
198 break; |
259 int rc = sntp_get_sync_status(); |
214 int rc = sntp_get_sync_status(); |
260 |
215 |
261 if (rc == SNTP_SYNC_STATUS_COMPLETED) { |
216 if (rc == SNTP_SYNC_STATUS_COMPLETED) { |
262 time(&now); |
217 time(&now); |
263 localtime_r(&now, &timeinfo); |
218 localtime_r(&now, &timeinfo); |
264 // System_TimeOk = true; |
219 System_TimeOk = true; |
265 strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo); |
220 strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo); |
266 ESP_LOGI(TAG, "NTP time is set: %s", strftime_buf); |
221 ESP_LOGI(TAG, "NTP time is set: %s", strftime_buf); |
267 } else { |
222 } else { |
268 ESP_LOGI(TAG, "NTP unknown time sync event rc=%d", rc); |
223 ESP_LOGI(TAG, "NTP unknown time sync event rc=%d", rc); |
|
224 System_TimeOk = false; |
269 } |
225 } |
270 } |
226 } |
271 |
227 |
272 |
228 |
273 void task_wifi( void * pvParameters ) |
229 void task_wifi( void * pvParameters ) |
329 |
284 |
330 wifi_config_t wifi_config = { |
285 wifi_config_t wifi_config = { |
331 .sta = { |
286 .sta = { |
332 .ssid = CONFIG_ESP_WIFI_SSID, |
287 .ssid = CONFIG_ESP_WIFI_SSID, |
333 .password = CONFIG_ESP_WIFI_PASSWORD, |
288 .password = CONFIG_ESP_WIFI_PASSWORD, |
334 #if CONFIG_WIFI_ALL_CHANNEL_SCAN |
|
335 .scan_method = WIFI_ALL_CHANNEL_SCAN, |
|
336 #elif CONFIG_WIFI_FAST_SCAN |
|
337 .scan_method = WIFI_FAST_SCAN, |
289 .scan_method = WIFI_FAST_SCAN, |
338 #endif |
|
339 .failure_retry_cnt = 3, |
290 .failure_retry_cnt = 3, |
340 .sort_method = WIFI_CONNECT_AP_BY_SIGNAL, |
291 .sort_method = WIFI_CONNECT_AP_BY_SIGNAL, |
341 .threshold.rssi = CONFIG_ESP_FAST_SCAN_MINIMUM_SIGNAL, |
|
342 .threshold.authmode = WIFI_AUTH_WPA2_PSK, |
292 .threshold.authmode = WIFI_AUTH_WPA2_PSK, |
343 }, |
293 }, |
344 }; |
294 }; |
345 ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); |
295 ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); |
346 ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config)); |
296 ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config)); |
352 EventBits_t uxBits; |
302 EventBits_t uxBits; |
353 |
303 |
354 for(;;) { |
304 for(;;) { |
355 |
305 |
356 /* actions that can trigger: request a connection, a scan, or a disconnection */ |
306 /* actions that can trigger: request a connection, a scan, or a disconnection */ |
357 uxBits = xEventGroupWaitBits(xEventGroupWifi, |
307 uxBits = xEventGroupWaitBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT | TASK_WIFI_REQUEST_STA_DISCONNECT, pdFALSE, pdFALSE, portMAX_DELAY ); |
358 TASK_WIFI_REQUEST_STA_CONNECT | TASK_WIFI_REQUEST_STA_DISCONNECT | TASK_WIFI_REQUEST_STA_SCAN | TASK_WIFI_REQUEST_STA_STATUS, |
|
359 pdFALSE, pdFALSE, portMAX_DELAY ); |
|
360 |
308 |
361 if (uxBits & TASK_WIFI_REQUEST_STA_DISCONNECT) { |
309 if (uxBits & TASK_WIFI_REQUEST_STA_DISCONNECT) { |
362 /* |
310 /* |
363 * user requested a disconnect, this will in effect disconnect the wifi |
311 * user requested a disconnect, this will in effect disconnect the wifi |
364 */ |
312 */ |
381 if (wifierror != ESP_OK) { |
329 if (wifierror != ESP_OK) { |
382 ESP_LOGE(TAG, "esp_wifi_connect() `%s' rc=%04x", CONFIG_ESP_WIFI_SSID, (int)wifierror); |
330 ESP_LOGE(TAG, "esp_wifi_connect() `%s' rc=%04x", CONFIG_ESP_WIFI_SSID, (int)wifierror); |
383 xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_STA_FAILED); |
331 xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_STA_FAILED); |
384 } |
332 } |
385 uxBits = xEventGroupWaitBits(xEventGroupWifi, TASK_WIFI_STA_CONNECTED | TASK_WIFI_STA_FAILED, pdFALSE, pdFALSE, 5000 / portTICK_PERIOD_MS); |
333 uxBits = xEventGroupWaitBits(xEventGroupWifi, TASK_WIFI_STA_CONNECTED | TASK_WIFI_STA_FAILED, pdFALSE, pdFALSE, 5000 / portTICK_PERIOD_MS); |
386 |
|
387 } else if (uxBits & TASK_WIFI_REQUEST_STA_STATUS) { |
|
388 /* |
|
389 * Request WiFi update status, refresh the rssi. |
|
390 */ |
|
391 ESP_LOGD(TAG, "Request STA status"); |
|
392 xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_STATUS); |
|
393 wifi_ap_record_t ap_info; |
|
394 esp_wifi_sta_get_ap_info(&ap_info); |
|
395 ESP_LOGI(TAG, "Event STA status, ssid:%s, bssid:" MACSTR ", rssi: %d", ap_info.ssid, MAC2STR(ap_info.bssid), ap_info.rssi); |
|
396 _wifi_RSSI = ap_info.rssi; |
|
397 _wifi_BetterAP = false; |
|
398 if (xSemaphoreTake(xSemaphoreWiFi, 35) == pdTRUE) { |
|
399 wifi_state->STA_rssi = ap_info.rssi; |
|
400 wifi_state->STA_channel = ap_info.primary; |
|
401 snprintf(wifi_state->STA_bssid, 18, "%02x:%02x:%02x:%02x:%02x:%02x", |
|
402 ap_info.bssid[0], ap_info.bssid[1], ap_info.bssid[2], ap_info.bssid[3], ap_info.bssid[4], ap_info.bssid[5]); |
|
403 xSemaphoreGive(xSemaphoreWiFi); |
|
404 } else { |
|
405 ESP_LOGE(TAG, "lock error TASK_WIFI_REQUEST_STA_STATUS"); |
|
406 } |
|
407 |
|
408 } else if (uxBits & TASK_WIFI_REQUEST_STA_SCAN) { |
|
409 |
|
410 ESP_LOGI(TAG, "Request STA scan"); |
|
411 xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_SCAN); |
|
412 /* safe guard against overflow */ |
|
413 ap_num = MAX_AP_NUM; |
|
414 _wifi_ScanDone = false; |
|
415 _wifi_BetterAP = false; |
|
416 ESP_ERROR_CHECK(esp_wifi_scan_start(&scan_config, false)); |
|
417 } |
334 } |
418 |
335 |
419 } /* for(;;) */ |
336 } /* for(;;) */ |
420 } |
337 } |
421 |
338 |