3875|0

85

帖子

0

TA的资源

一粒金砂(中级)

楼主
 

【零知ESP8266教程】快速入门16 WiFiScan库的使用 扫描你附近的WiFi [复制链接]

本帖最后由 roc2 于 2019-10-29 18:19 编辑

上次我们已经学习并分享了零知ESP8266一键配网的操作流程,接下来,我们需要了解ESP8266是怎样找到这些WiFi热点的。
引述:
 为了让手机连上一个WiFi热点,基本上都是打开手机设置里面的WiFi设置功能,然后会看到里面有个WiFi热点列表,然后选择连接。 基本上你只要打开手机的WiFi功能,就会发现附近有超级多的各种来路不明的WiFi热点(WiFi有风险,连接需谨慎),那么手机是怎么找得到附近的WiFi的呢?

    通常,无线网络提供的WiFi热点,大部分都开放了SSID广播(记得之前博主讲过WiFi热点也可以隐藏的),Scan WiFi的功能就是扫描出所有附近的WiFi热点的SSID信息,这样一来,客户端就可以根据需要选择不同的SSID连入对应的无线网络中。
 Scan WiFi库提供了两种方式实现扫描过程:
①同步扫描:通过单个函数在一次运行中完成,需要等待完成所有操作才能继续运行下面的操作。
②异步扫描:把上面的过程分成几个步骤,每个步骤由一个单独函数完成,我们可以在扫描过程中执行其他任务。

我们的零知ESP8266本身就是WiFi模块,当然也可以扫描到WiFi的热点,接下来,我们一起学习并且找到附近所有的WiFi热点吧。

一、硬件
电脑,windows系统
零知ES8266开发板
micro-usb线

二、软件库
具备ESP8266WiFiScan库。

ESP8266WiFiScan库,大家使用的时候只需要引入,所以要在IDE中安装相应的数据支持库,当然也可以留言,我免费私给你链接!

#include<ESP8266WiFi.h>

(1)扫描操作方法
① scanNetworks —— 同步扫描周边有效wifi网络
函数说明:

/**
 * Start scan WiFi networks available
 * @param async         run in async mode(是否启动异步扫描)
 * @param show_hidden   show hidden networks(是否扫描隐藏网络)
 * @param channel       scan only this channel (0 for all channels)(是否扫描特定通道)
 * @param ssid*         scan for only this ssid (NULL for all ssid's)(是否扫描特定的SSID)
 * @return Number of discovered networks
 */int8_t scanNetworks(bool async = false, bool show_hidden = false, uint8 channel = 0, uint8* ssid = NULL);

② scanNetworks(async ) —— 异步扫描周边有效wifi网络
函数说明:

/**
 * Start scan WiFi networks available
 * @param async         run in async mode(是否启动异步扫描)
 * @param show_hidden   show hidden networks(是否扫描隐藏网络)
 * @param channel       scan only this channel (0 for all channels)(是否扫描特定通道)
 * @param ssid*         scan for only this ssid (NULL for all ssid's)(是否扫描特定的SSID)
 * @return Number of discovered networks
 */int8_t scanNetworks(bool async = false, bool show_hidden = false, uint8 channel = 0, uint8* ssid = NULL);

③ scanNetworksAsync —— 异步扫描周边wifi网络,并回调结果
函数说明:

/**
 * Starts scanning WiFi networks available in async mode
 * @param onComplete    the event handler executed when the scan is done
 * @param show_hidden   show hidden networks
  */void scanNetworksAsync(std::function<void(int)> onComplete, bool show_hidden = false);

④ scanComplete —— 检测异步扫描的结果
函数说明:

/** * called to get the scan state in Async mode(异步扫描的结果函数)
 * @return scan result or status
 *          -1 if scan not find
 *          -2 if scan not triggered
 */
int8_t scanComplete();

⑤ scanDelete —— 从内存中删掉最近扫描结果
函数说明:

/**
 * delete last scan result from RAM(从内存中删除最近的扫描结果)
 */void scanDelete();

注意:如果不删除,将会叠加上次扫描的结果;

(2)扫描结果方法


① SSID —— 获取wifi网络名字
函数说明:

/**
 * Return the SSID discovered during the network scan.
 * @param i     specify from which network item want to get the information
 * @return       ssid string of the specified item on the networks scanned list
 */String SSID(uint8_t networkItem);

② RSSI —— 获取wifi网络信号强度
函数说明:

/**
 * Return the RSSI of the networks discovered during the scanNetworks(信号强度)
 * @param i specify from which network item want to get the information
 * @return  signed value of RSSI of the specified item on the networks scanned list
 */int32_t RSSI(uint8_t networkItem);

③ encryptionType —— 获取wifi网络加密方式
函数说明:

/**
 * Return the encryption type of the networks discovered during the scanNetworks(加密方式)
 * @param i specify from which network item want to get the information
 * @return  encryption type (enum wl_enc_type) of the specified item on the networks scanned list
 * ............ Values map to 802.11 encryption suites.....................
 *    AUTH_OPEN          ---->     ENC_TYPE_WEP  = 5,
 *    AUTH_WEP           ---->     ENC_TYPE_TKIP = 2,
 *    AUTH_WPA_PSK       ---->     ENC_TYPE_CCMP = 4,
 * ........... except these two, 7 and 8 are reserved in 802.11-2007.......
 *    AUTH_WPA2_PSK      ---->     ENC_TYPE_NONE = 7,
 *    AUTH_WPA_WPA2_PSK  ---->     ENC_TYPE_AUTO = 8
 */uint8_t encryptionType(uint8_t networkItem);

④ BSSID —— 获取wifi网络mac地址
函数说明:

/**
 * return MAC / BSSID of scanned wifi (物理地址)
 * @param i specify from which network item want to get the information
 * @return uint8_t * MAC / BSSID of scanned wifi
 */uint8_t * BSSID(uint8_t networkItem); 
/**
 * return MAC / BSSID of scanned wifi (物理地址)
 * @param i specify from which network item want to get the information
 * @return uint8_t * MAC / BSSID of scanned wifi
 */String BSSIDstr(uint8_t networkItem);

⑤ getNetworkInfo —— 获取整体网络信息,名字,信号强度等
函数说明:

/**
 * loads all infos from a scanned wifi in to the ptr parameters
 * @param networkItem uint8_t
 * @param ssid  const char**
 * @param encryptionType uint8_t *
 * @param RSSI int32_t *
 * @param BSSID uint8_t **
 * @param channel int32_t *
 * @param isHidden bool *
 * @return (true if ok)
 */        bool getNetworkInfo(uint8_t networkItem, String &ssid, uint8_t &encryptionType, int32_t &RSSI, uint8_t* &BSSID, int32_t &channel, bool &isHidden);

注意:入参前面多数加了&,意味着调完函数后外面获取到详细信息;
⑥ channel —— 获取wifi网络通道号
函数说明:

/**
 * return channel of scanned wifi(通道号)
 */int32_t channel(uint8_t networkItem);

⑦ isHidden —— 判断wifi网络是否是隐藏网络
函数说明:

/**
 * return if the scanned wifi is Hidden (no SSID)(判断扫描到的wifi是否是隐藏wifi)
 * @param networkItem specify from which network item want to get the information
 * @return bool (true == hidden)
 */bool isHidden(uint8_t networkItem);

动手操作
多说不宜,实验是检验真理的唯一标准,下面我们就来实际操作一下吧
三、
(1)打开零知开源开发工具,如下图:


(2)电脑连接零知ESP8266开发板:

(3)输入以下代码,验证,上传

/**
 * Demo:
 *    STA模式下,演示同步扫描Scan wifi功能
 * @author 云上上云
 * @date 2019/06/01
 */#include <ESP8266WiFi.h>
 //以下三个定义为调试定义#define DebugBegin(baud_rate)    Serial.begin(baud_rate)#define DebugPrintln(message)    Serial.println(message)#define DebugPrint(message)    Serial.print(message)
 void setup() {  //设置串口波特率,以便打印信息
  DebugBegin(115200);  //延时5s 为了演示效果
  delay(5000);  // 我不想别人连接我,只想做个站点
  WiFi.mode(WIFI_STA);  //断开连接
  WiFi.disconnect();
  delay(100);
  DebugPrintln("Setup done");
} 
void loop() {
  DebugPrintln("scan start");  // 同步扫描,等待返回结果
  int n = WiFi.scanNetworks();
  DebugPrintln("scan done");  if (n == 0){
    DebugPrintln("no networks found");
  }else{
    DebugPrint(n);
    DebugPrintln(" networks found");    for (int i = 0; i < n; ++i){
      DebugPrint(i + 1);
      DebugPrint(": ");      //打印wifi账号
      DebugPrint(WiFi.SSID(i));
      DebugPrint(",");
      DebugPrint(String("Ch:")+WiFi.channel(i));
      DebugPrint(",");
      DebugPrint(WiFi.isHidden(i)?"hide":"show");
      DebugPrint(" (");      //打印wifi信号强度
      DebugPrint(WiFi.RSSI(i));
      DebugPrint("dBm");
      DebugPrint(")");      //打印wifi加密方式
      DebugPrintln((WiFi.encryptionType(i) == ENC_TYPE_NONE)?"open":"*");
      delay(10);
    }
  }
  DebugPrintln("");  // 延时5s之后再次扫描
  delay(5000);
}

四、测试结果(附近潜在的WiFi热点):
(我就不一一展示了,它可以扫描完附近所有WiFi)

此帖出自stm32/stm8论坛
点赞 关注
 

回复
举报
您需要登录后才可以回帖 登录 | 注册

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/8 下一条

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2025 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表