文章目錄
- 一、快速運行
- 二、運行效果
- 三、程式流程
- 四、關鍵函式
- 4.1 掃描所有有效的AP
- 4.2 獲取上次掃描中找到的AP串列
- 4.3 獲取上次掃描中找到的AP數
一、快速運行
- 示例專案中,選擇
WiFi—>scan menuconfig配置ESP32C3-Specific—>Rec 0- 芯片選擇
ESP32-C3(Built-in USB JTAG) - 快速運行
ESP-IDF Build, Flash and Monitor(左下角)

二、運行效果


三、程式流程
nvs_flash_init,初始化默認 NVS 磁區,esp_netif_init,初始化底層TCP/IP堆疊esp_event_loop_create_default,創建默認事件回圈,esp_netif_create_default_wifi_sta,使用默認WiFi Station配置創建esp_netif物件,將netif連接到WiFi并注冊默認WiFi處理程式,esp_wifi_init,為 WiFi 驅動初始化 WiFi 分配資源,如 WiFi 控制結構、RX/TX 緩沖區、WiFi NVS 結構等,這個 WiFi 也啟動 WiFi 任務,必須先呼叫此API,然后才能呼叫所有其他WiFi APIesp_wifi_set_mode,設定WiFi作業模式為station、soft-AP或station+soft-AP,默認模式為soft-AP模式,本程式設定為stationesp_wifi_start,根據配置,啟動WiFiesp_wifi_scan_start,掃描所有有效的APesp_wifi_scan_get_ap_records,獲取上次掃描中找到的AP串列esp_wifi_scan_get_ap_num,獲取上次掃描中找到的AP數
四、關鍵函式
4.1 掃描所有有效的AP
config,掃描的配置block,是否堵塞程式運行
/**
* @brief Scan all available APs.
*
* @attention If this API is called, the found APs are stored in WiFi driver dynamic allocated memory and the
* will be freed in esp_wifi_scan_get_ap_records, so generally, call esp_wifi_scan_get_ap_records to cause
* the memory to be freed once the scan is done
* @attention The values of maximum active scan time and passive scan time per channel are limited to 1500 milliseconds.
* Values above 1500ms may cause station to disconnect from AP and are not recommended.
*
* @param config configuration of scanning
* @param block if block is true, this API will block the caller until the scan is done, otherwise
* it will return immediately
*
* @return
* - ESP_OK: succeed
* - ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by esp_wifi_init
* - ESP_ERR_WIFI_NOT_STARTED: WiFi was not started by esp_wifi_start
* - ESP_ERR_WIFI_TIMEOUT: blocking scan is timeout
* - ESP_ERR_WIFI_STATE: wifi still connecting when invoke esp_wifi_scan_start
* - others: refer to error code in esp_err.h
*/
esp_err_t esp_wifi_scan_start(const wifi_scan_config_t *config, bool block);
4.2 獲取上次掃描中找到的AP串列
number,回傳的最大ap個數ap_records,回傳的app記錄陣列
/**
* @brief Get AP list found in last scan
*
* @param[inout] number As input param, it stores max AP number ap_records can hold.
* As output param, it receives the actual AP number this API returns.
* @param ap_records wifi_ap_record_t array to hold the found APs
*
* @return
* - ESP_OK: succeed
* - ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by esp_wifi_init
* - ESP_ERR_WIFI_NOT_STARTED: WiFi is not started by esp_wifi_start
* - ESP_ERR_INVALID_ARG: invalid argument
* - ESP_ERR_NO_MEM: out of memory
*/
esp_err_t esp_wifi_scan_get_ap_records(uint16_t *number, wifi_ap_record_t *ap_records);
4.3 獲取上次掃描中找到的AP數
number,存盤上次掃描中找到的API數
/**
* @brief Get number of APs found in last scan
*
* @param[out] number store number of APs found in last scan
*
* @attention This API can only be called when the scan is completed, otherwise it may get wrong value.
*
* @return
* - ESP_OK: succeed
* - ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by esp_wifi_init
* - ESP_ERR_WIFI_NOT_STARTED: WiFi is not started by esp_wifi_start
* - ESP_ERR_INVALID_ARG: invalid argument
*/
esp_err_t esp_wifi_scan_get_ap_num(uint16_t *number);
覺得好,就一鍵三連唄(點贊+收藏+關注)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/303118.html
標籤:其他
