当前位置:首页 » 《关注互联网》 » 正文

ESP32-C3入门教程 网络篇①——WiFi Scan 快速扫描附近AP无线热点_小康师兄

27 人参与  2022年01月20日 12:17  分类 : 《关注互联网》  评论

点击全文阅读


文章目录

  • 一、快速运行
  • 二、运行效果
  • 三、程序流程
  • 四、关键函数
    • 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 API
  • esp_wifi_set_mode,设置WiFi工作模式为station、soft-AP或station+soft-AP,默认模式为soft-AP模式。本程序设置为station
  • esp_wifi_start,根据配置,启动WiFi
  • esp_wifi_scan_start,扫描所有有效的AP
  • esp_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);

觉得好,就一键三连呗(点赞+收藏+关注)


点击全文阅读


本文链接:http://m.zhangshiyu.com/post/33603.html

扫描  上次  中找到  
<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

最新文章

  • 凤青禾,江明远,***枢小说(别人修仙我捡漏,卷王们破防了)最近更新(凤青禾,江明远,***枢)整本无套路阅读
  • 薛梨小说无删减+后续(曾经亲情似草芥)畅享阅读
  • 沈南栀小说(穿越时空,我要修补时空裂缝)章节目录+起点章节(沈南栀)全篇清爽版在线
  • 未婚妻被巨蟒缠身,我该吃就吃该喝就喝前言+后续_阿豪林月周然后续+番外_小说后续在线阅读_无删减免费完结_
  • 陆骁,陆本初小说(陆骁,陆本初)(癫!睁眼穿成老太太挥鞭***逆子)前传+阅读全新作品预订
  • 姐姐含冤而死后冥王另娶,我杀穿整个地府在线阅读_阎罗殿殷红别提一口气完结_小说后续在线阅读_无删减免费完结_
  • (书荒必看)毒后重生:疯王的神医小娇妻沈清歌,萧绝:+后续热血十足
  • 重生后我和太监联手灭了敌国喻辰,林雪续集(重生后我和太监联手灭了敌国)终极反转(喻辰,林雪)全篇一口气阅读
  • 我不做灵媒后,自称灵媒摆渡人的养妹害怕了内容精选_苏晓霍老阿姐无广告_小说后续在线阅读_无删减免费完结_
  • 前传一别再无相见续集:全文+番外戚许许樵风:结局+番外新上热文
  • 嫂子照顾我怀孕生子,我倒欠她一个孩子最新目录_老公婆婆龙凤胎一口气看完_小说后续在线阅读_无删减免费完结_
  • 卿本祸水小说(苏窈,冰冷,萧霁寒)+起点章节(卿本祸水)阅读

    关于我们 | 我要投稿 | 免责申明

    Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1