From e00f3a9ede1b8e46b480bd68daf48da0bb08acae Mon Sep 17 00:00:00 2001 From: Filip Wandzio Date: Thu, 4 Sep 2025 01:11:11 +0200 Subject: Initial --- src/wifi_scan.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 src/wifi_scan.c (limited to 'src/wifi_scan.c') 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 @@ +#include "esp_log.h" +#include "esp_wifi.h" +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" + +#define WIFI_SCAN_TASK_STACK_SIZE 4096 +#define WIFI_SCAN_TASK_PRIORITY 5 +#define WIFI_SCAN_MAX_APS 20 +#define WIFI_SCAN_POLL_INTERVAL_MS 500 +#define WIFI_SCAN_DEFAULT_CHANNEL 0 +#define WIFI_SCAN_SHOW_HIDDEN true +#define WIFI_SCAN_TASK_NAME "wifi_scan_task" + +/** + * @brief Task to perform WiFi scanning asynchronously. + * + * Starts a WiFi scan, polls for scan completion, + * retrieves and logs the found access points, then deletes itself. + * + * @param pvParameter Task parameter (unused). + */ +static void wifi_scan_task(void *pvParameter) { + uint16_t ap_count = WIFI_SCAN_MAX_APS; + static wifi_ap_record_t ap_info[WIFI_SCAN_MAX_APS]; + wifi_scan_config_t scan_config = { + .ssid = NULL, + .bssid = NULL, + .channel = WIFI_SCAN_DEFAULT_CHANNEL, + .show_hidden = WIFI_SCAN_SHOW_HIDDEN, + }; + + ESP_LOGI("wifi_scan", "Starting Wi-Fi scan..."); + ESP_ERROR_CHECK(esp_wifi_scan_start(&scan_config, false)); + + while (true) { + uint16_t finished_ap_count = 0; + ESP_ERROR_CHECK(esp_wifi_scan_get_ap_num(&finished_ap_count)); + if (finished_ap_count > 0) + break; + vTaskDelay(pdMS_TO_TICKS(WIFI_SCAN_POLL_INTERVAL_MS)); + } + + ESP_ERROR_CHECK(esp_wifi_scan_get_ap_records(&ap_count, ap_info)); + ESP_LOGI("wifi_scan", "Found %d access points:", ap_count); + for (uint16_t ap_index = 0; ap_index < ap_count; ++ap_index) { + ESP_LOGI("wifi_scan", "%d: SSID: %s, RSSI: %d", ap_index + 1, + ap_info[ap_index].ssid, ap_info[ap_index].rssi); + } + + vTaskDelete(NULL); +} + +/** + * @brief Starts the WiFi scan task. + * + * Creates a FreeRTOS task that runs the WiFi scan asynchronously. + */ +void wifi_scan_start(void) { + xTaskCreate(wifi_scan_task, WIFI_SCAN_TASK_NAME, WIFI_SCAN_TASK_STACK_SIZE, + NULL, WIFI_SCAN_TASK_PRIORITY, NULL); +} -- cgit v1.2.3