找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 6523|回复: 0
打印 上一主题 下一主题
收起左侧

【零知ESP8266】教程:WiFiScan库的使用

[复制链接]
跳转到指定楼层
楼主
本帖最后由 roc2 于 2019-6-19 10:55 编辑

概述:
无线网络提供的WiFi热点,大部分都开放了SSID广播(记得之前博主讲过WiFi热点也可以隐藏的),Scan WiFi的功能就是扫描出所有附近的WiFi热点的SSID信息,这样一来,客户端就可以根据需要选择不同的SSID连入对应的无线网络中。
Scan WiFi库提供了两种方式实现扫描过程:
同步扫描:通过单个函数在一次运行中完成,需要等待完成所有操作才能继续运行下面的操作。
异步扫描:把上面的过程分成几个步骤,每个步骤由一个单独函数完成,我们可以在扫描过程中执行其他任务。
ESP8266WiFiScan库
ESP8266WiFiScan库,大家使用的时候只需要引入
  1. #include<ESP8266WiFi.h>
复制代码
扫描操作方法1.scanNetworks —— 同步扫描周边有效wifi网络函数说明:
  1. /**
  2. * Start scan WiFi networks available
  3. * @param async         run in async mode(是否启动异步扫描)
  4. * @param show_hidden   show hidden networks(是否扫描隐藏网络)
  5. * @param channel       scan only this channel (0 for all channels)(是否扫描特定通道)
  6. * @param ssid*         scan for only this ssid (NULL for all ssid's)(是否扫描特定的SSID)
  7. * @return Number of discovered networks
  8. */int8_t scanNetworks(bool async = false, bool show_hidden = false, uint8 channel = 0, uint8* ssid = NULL);
复制代码
2.scanNetworks(async ) —— 异步扫描周边有效wifi网络
函数说明:
  1. /**
  2. * Start scan WiFi networks available
  3. * @param async         run in async mode(是否启动异步扫描)
  4. * @param show_hidden   show hidden networks(是否扫描隐藏网络)
  5. * @param channel       scan only this channel (0 for all channels)(是否扫描特定通道)
  6. * @param ssid*         scan for only this ssid (NULL for all ssid's)(是否扫描特定的SSID)
  7. * @return Number of discovered networks
  8. */int8_t scanNetworks(bool async = false, bool show_hidden = false, uint8 channel = 0, uint8* ssid = NULL);
复制代码
3.scanNetworksAsync —— 异步扫描周边wifi网络,并回调结果
函数说明:
  1. /**
  2. * Starts scanning WiFi networks available in async mode
  3. * @param onComplete    the event handler executed when the scan is done
  4. * @param show_hidden   show hidden networks
  5.   */void scanNetworksAsync(std::function<void(int)> onComplete, bool show_hidden = false);
复制代码
4. scanComplete —— 检测异步扫描的结果
函数说明:
  1. /**
  2. * called to get the scan state in Async mode(异步扫描的结果函数)
  3. * @return scan result or status
  4. *          -1 if scan not find
  5. *          -2 if scan not triggered
  6. */
  7. int8_t scanComplete();
复制代码
5.scanDelete —— 从内存中删掉最近扫描结果
函数说明:
  1. /**
  2. * delete last scan result from RAM(从内存中删除最近的扫描结果)
  3. */void scanDelete();
复制代码
扫描结果方法1. SSID —— 获取wifi网络名字
函数说明:
  1. /**
  2. * Return the SSID discovered during the network scan.
  3. * @param i     specify from which network item want to get the information
  4. * @return       ssid string of the specified item on the networks scanned list
  5. */String SSID(uint8_t networkItem);
复制代码
2.RSSI —— 获取wifi网络信号强度
函数说明:
  1. /**
  2. * Return the RSSI of the networks discovered during the scanNetworks(信号强度)
  3. * @param i specify from which network item want to get the information
  4. * @return  signed value of RSSI of the specified item on the networks scanned list
  5. */int32_t RSSI(uint8_t networkItem);
复制代码
3. encryptionType —— 获取wifi网络加密方式
函数说明:
  1. /**
  2. * Return the encryption type of the networks discovered during the scanNetworks(加密方式)
  3. * @param i specify from which network item want to get the information
  4. * @return  encryption type (enum wl_enc_type) of the specified item on the networks scanned list
  5. * ............ Values map to 802.11 encryption suites.....................
  6. *    AUTH_OPEN          ---->     ENC_TYPE_WEP  = 5,
  7. *    AUTH_WEP           ---->     ENC_TYPE_TKIP = 2,
  8. *    AUTH_WPA_PSK       ---->     ENC_TYPE_CCMP = 4,
  9. * ........... except these two, 7 and 8 are reserved in 802.11-2007.......
  10. *    AUTH_WPA2_PSK      ---->     ENC_TYPE_NONE = 7,
  11. *    AUTH_WPA_WPA2_PSK  ---->     ENC_TYPE_AUTO = 8
  12. */uint8_t encryptionType(uint8_t networkItem);
复制代码
4. BSSID —— 获取wifi网络mac地址
函数说明:
  1. /**
  2. * return MAC / BSSID of scanned wifi (物理地址)
  3. * @param i specify from which network item want to get the information
  4. * @return uint8_t * MAC / BSSID of scanned wifi
  5. */uint8_t * BSSID(uint8_t networkItem);
  6. /**
  7. * return MAC / BSSID of scanned wifi (物理地址)
  8. * @param i specify from which network item want to get the information
  9. * @return uint8_t * MAC / BSSID of scanned wifi
  10. */String BSSIDstr(uint8_t networkItem);
复制代码
5.getNetworkInfo —— 获取整体网络信息,名字,信号强度等
函数说明:
  1. /**
  2. * loads all infos from a scanned wifi in to the ptr parameters
  3. * @param networkItem uint8_t
  4. * @param ssid  const char**
  5. * @param encryptionType uint8_t *
  6. * @param RSSI int32_t *
  7. * @param BSSID uint8_t **
  8. * @param channel int32_t *
  9. * @param isHidden bool *
  10. * @return (true if ok)
  11. */        bool getNetworkInfo(uint8_t networkItem, String &ssid, uint8_t &encryptionType, int32_t &RSSI, uint8_t* &BSSID, int32_t &channel, bool &isHidden);
复制代码
6. channel —— 获取wifi网络通道号
函数说明:
  1. /**
  2. * return channel of scanned wifi(通道号)
  3. */int32_t channel(uint8_t networkItem);
复制代码
7. isHidden —— 判断wifi网络是否是隐藏网络
函数说明:
  1. /**
  2. * return if the scanned wifi is Hidden (no SSID)(判断扫描到的wifi是否是隐藏wifi)
  3. * @param networkItem specify from which network item want to get the information
  4. * @return bool (true == hidden)
  5. */bool isHidden(uint8_t networkItem);
复制代码
动手操作
多说不宜,实验是检验真理的唯一标准,下面我们就来实际操作一下吧。
打开零知开源开发工具,新建一个项目,输入以下代码,验证,上传。

测试demo:
  1. /**
  2. * Demo:
  3. *    STA模式下,演示同步扫描Scan wifi功能
  4. * @author 云上上云
  5. * @date 2019/06/01
  6. */#include <ESP8266WiFi.h>
  7. //以下三个定义为调试定义#define DebugBegin(baud_rate)    Serial.begin(baud_rate)#define DebugPrintln(message)    Serial.println(message)#define DebugPrint(message)    Serial.print(message)
  8. void setup() {  //设置串口波特率,以便打印信息
  9.   DebugBegin(115200);  //延时5s 为了演示效果
  10.   delay(5000);  // 我不想别人连接我,只想做个站点
  11.   WiFi.mode(WIFI_STA);  //断开连接
  12.   WiFi.disconnect();
  13.   delay(100);
  14.   DebugPrintln("Setup done");
  15. }
  16. void loop() {
  17.   DebugPrintln("scan start");  // 同步扫描,等待返回结果
  18.   int n = WiFi.scanNetworks();
  19.   DebugPrintln("scan done");  if (n == 0){
  20.     DebugPrintln("no networks found");
  21.   }else{
  22.     DebugPrint(n);
  23.     DebugPrintln(" networks found");    for (int i = 0; i < n; ++i){
  24.       DebugPrint(i + 1);
  25.       DebugPrint(": ");      //打印wifi账号
  26.       DebugPrint(WiFi.SSID(i));
  27.       DebugPrint(",");
  28.       DebugPrint(String("Ch:")+WiFi.channel(i));
  29.       DebugPrint(",");
  30.       DebugPrint(WiFi.isHidden(i)?"hide":"show");
  31.       DebugPrint(" (");      //打印wifi信号强度
  32.       DebugPrint(WiFi.RSSI(i));
  33.       DebugPrint("dBm");
  34.       DebugPrint(")");      //打印wifi加密方式
  35.       DebugPrintln((WiFi.encryptionType(i) == ENC_TYPE_NONE)?"open":"*");
  36.       delay(10);
  37.     }
  38.   }
  39.   DebugPrintln("");  // 延时5s之后再次扫描
  40.   delay(5000);
  41. }
复制代码
测试结果(附近潜在的WiFi热点):它可以扫描完附件所有WiFi。
更多详细资料可到零知实验室官网免费获取。







分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表