文章目录
一、知识点
ESP32的官方已经将WiFi相关的使用封装成 Wi-Fi 库,并提供API供用户使用。更加方便好上手。
1.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);
1.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);
1.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);
二、参考例程
ESP-IDF 中有一个WiFi Fast Scan的例程,实现的是扫描附近WiFi AP。可以参考。
三、今日作业
- 基于ESP32-C3-DevKitM开发板
- 开机扫描附近WiFi AP信息
- 按键短按,按顺序输出打印 扫描到的WiFi AP信息
- 按键长按,重新扫描附近WiFi AP信息,并全部输出打印
四、参考答案
独立完成,明天来看~
五、打卡~
作业完成后,别忘了跟帖打卡(附上源码和图片)~
完成打卡的每人可有新程序员杂志。并且根据完成质量和打卡时间,评选出一二三和特等奖,并送出精美礼品~