aboutsummaryrefslogtreecommitdiffstats
path: root/src/wifi_scan.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/wifi_scan.c')
-rw-r--r--src/wifi_scan.c61
1 files changed, 61 insertions, 0 deletions
diff --git a/src/wifi_scan.c b/src/wifi_scan.c
new file mode 100644
index 0000000..e588cfc
--- /dev/null
+++ b/src/wifi_scan.c
@@ -0,0 +1,61 @@
1#include "esp_log.h"
2#include "esp_wifi.h"
3#include "freertos/FreeRTOS.h"
4#include "freertos/task.h"
5
6#define WIFI_SCAN_TASK_STACK_SIZE 4096
7#define WIFI_SCAN_TASK_PRIORITY 5
8#define WIFI_SCAN_MAX_APS 20
9#define WIFI_SCAN_POLL_INTERVAL_MS 500
10#define WIFI_SCAN_DEFAULT_CHANNEL 0
11#define WIFI_SCAN_SHOW_HIDDEN true
12#define WIFI_SCAN_TASK_NAME "wifi_scan_task"
13
14/**
15 * @brief Task to perform WiFi scanning asynchronously.
16 *
17 * Starts a WiFi scan, polls for scan completion,
18 * retrieves and logs the found access points, then deletes itself.
19 *
20 * @param pvParameter Task parameter (unused).
21 */
22static void wifi_scan_task(void *pvParameter) {
23 uint16_t ap_count = WIFI_SCAN_MAX_APS;
24 static wifi_ap_record_t ap_info[WIFI_SCAN_MAX_APS];
25 wifi_scan_config_t scan_config = {
26 .ssid = NULL,
27 .bssid = NULL,
28 .channel = WIFI_SCAN_DEFAULT_CHANNEL,
29 .show_hidden = WIFI_SCAN_SHOW_HIDDEN,
30 };
31
32 ESP_LOGI("wifi_scan", "Starting Wi-Fi scan...");
33 ESP_ERROR_CHECK(esp_wifi_scan_start(&scan_config, false));
34
35 while (true) {
36 uint16_t finished_ap_count = 0;
37 ESP_ERROR_CHECK(esp_wifi_scan_get_ap_num(&finished_ap_count));
38 if (finished_ap_count > 0)
39 break;
40 vTaskDelay(pdMS_TO_TICKS(WIFI_SCAN_POLL_INTERVAL_MS));
41 }
42
43 ESP_ERROR_CHECK(esp_wifi_scan_get_ap_records(&ap_count, ap_info));
44 ESP_LOGI("wifi_scan", "Found %d access points:", ap_count);
45 for (uint16_t ap_index = 0; ap_index < ap_count; ++ap_index) {
46 ESP_LOGI("wifi_scan", "%d: SSID: %s, RSSI: %d", ap_index + 1,
47 ap_info[ap_index].ssid, ap_info[ap_index].rssi);
48 }
49
50 vTaskDelete(NULL);
51}
52
53/**
54 * @brief Starts the WiFi scan task.
55 *
56 * Creates a FreeRTOS task that runs the WiFi scan asynchronously.
57 */
58void wifi_scan_start(void) {
59 xTaskCreate(wifi_scan_task, WIFI_SCAN_TASK_NAME, WIFI_SCAN_TASK_STACK_SIZE,
60 NULL, WIFI_SCAN_TASK_PRIORITY, NULL);
61}