ESP32-C3入门教程 网络篇①——WiFi Scan 快速扫描附近AP无线热点
Posted 小康师兄
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ESP32-C3入门教程 网络篇①——WiFi Scan 快速扫描附近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);
觉得好,就一键三连呗(点赞+收藏+关注)
以上是关于ESP32-C3入门教程 网络篇①——WiFi Scan 快速扫描附近AP无线热点的主要内容,如果未能解决你的问题,请参考以下文章
ESP32-C3入门教程 网络篇⑤——TCP Socket Client 客户端应用示例
蓝牙Mesh笔记 ①ESP32-C3 模组上实现天猫精灵蓝牙 BLE Mesh AliGenie 接入,无需WiFi 连接也可以实现天猫精灵语音控制。
蓝牙Mesh笔记 ①ESP32-C3 模组上实现天猫精灵蓝牙 BLE Mesh AliGenie 接入,无需WiFi 连接也可以实现天猫精灵语音控制。
LittlevGL ESP32 学习笔记 ①移植最新的 LVGL 版本到安信可ESP32C3模组,显示一个二维码。(附带源码)
LittlevGL ESP32 学习笔记 ①移植最新的 LVGL 版本到安信可ESP32C3模组,显示一个二维码。(附带源码)