WIFI SSID Scanner Using ESP32 IDF
Contents
Introduction
Using the ESP32 IDF (Espressif IoT Development Framework) to scan for nearby SSIDs (Wi-Fi network names) is a common feature. Through the Wi-Fi module of ESP32, the device can scan surrounding Wi-Fi networks and obtain relevant information, such as SSID, signal strength (RSSI), etc.
Install VSCode & ESP-IDF VSCode Extension
Make sure you have installed and configured the ESP-IDF development environment. You can also refer to the article the ESP32 Tutorial – ESP-IDF With VSCode.
ESP32 DevKit Development Module
Choose an ESP32 module such as ESP32 DevKitC V4.
Create A New ESP32 Project
We can use VSCode's IDF plug-in to create a new ESP32 project. Please refer to ESP32 Tutorial – How To Create An ESP32 Project With VSCode.
Coding
Below is an example program that uses ESP-IDF to scan for Wi-Fi networks. It will scan for surrounding Wi-Fi networks and print out their SSID and signal strength (RSSI).
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_wifi.h"
#include "esp_log.h"
#include "nvs_flash.h"
#define DEFAULT_SCAN_LIST_SIZE 20 // Define the number of APs to store
static const char *TAG = "wifi_scan";
void wifi_scan(void *pvParameters) {
// 1. Initialize Wi-Fi
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
// 2. Set Wi-Fi mode to STA (Station)
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
ESP_ERROR_CHECK(esp_wifi_start());
// 3. Configure scan parameters
wifi_scan_config_t scan_config = {
.ssid = NULL,
.bssid = NULL,
.channel = 0,
.show_hidden = true
};
// 4. Start Wi-Fi scan
ESP_ERROR_CHECK(esp_wifi_scan_start(&scan_config, true));
// 5. Get scan results
uint16_t number = DEFAULT_SCAN_LIST_SIZE;
wifi_ap_record_t ap_info[DEFAULT_SCAN_LIST_SIZE];
ESP_ERROR_CHECK(esp_wifi_scan_get_ap_records(&number, ap_info));
// 6. Get number of scanned networks
uint16_t ap_count = 0;
ESP_ERROR_CHECK(esp_wifi_scan_get_ap_num(&ap_count));
ESP_LOGI(TAG, "Total APs found: %u", ap_count);
// 7. Print all SSIDs and RSSI (signal strength)
for (int i = 0; i < ap_count; i++) {
ESP_LOGI(TAG, "SSID: %s, RSSI: %d", ap_info[i].ssid, ap_info[i].rssi);
}
// Stop the Wi-Fi module
ESP_ERROR_CHECK(esp_wifi_stop());
vTaskDelete(NULL); // Delete task
}
void app_main(void) {
// Initialize NVS
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
}
ESP_ERROR_CHECK(ret);
// Create Wi-Fi scan task
xTaskCreate(&wifi_scan, "wifi_scan", 4096, NULL, 5, NULL);
}
Compile, Flash & Monitor
Find the "ESP32-IDF: Build, Flash and Monitor" ICON in VSCode to execute and burn.
Results
After executing the Wi-Fi scanning code on ESP32, the results will be printed via the serial monitor (such as idf.py monitor). Here is an example of the results you might see on the serial monitor:
I (2894) wifi_scan: Total APs found: 5
I (2894) wifi_scan: SSID: MyWiFi, RSSI: -55
I (2894) wifi_scan: SSID: CafeNetwork, RSSI: -60
I (2894) wifi_scan: SSID: OfficeWiFi, RSSI: -45
I (2894) wifi_scan: SSID: GuestNetwork, RSSI: -70
I (2894) wifi_scan: SSID: HiddenSSID, RSSI: -80
Conclusion
Through the serial monitor you will see the device print out the surrounding Wi-Fi SSID and corresponding signal strength.
This code will scan for all nearby Wi-Fi networks and display their names and signal strengths.