找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索
查看: 2500|回復(fù): 4
打印 上一主題 下一主題
收起左側(cè)

【零知ESP8266教程】快速入門16 WiFiScan庫的使用 掃描你附近的WiFi

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:349555 發(fā)表于 2019-10-29 17:40 | 只看該作者 |只看大圖 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式

斷層的小伙伴們給我留言,我們的學(xué)習(xí)永不止步,請(qǐng)大家一定要加油學(xué)下去,你才發(fā)現(xiàn)電子的世界,需要的是持之以恒的你!

上次我們已經(jīng)學(xué)習(xí)并分享了零知ESP8266一鍵配網(wǎng)的操作流程,接下來,我們需要了解ESP8266是怎樣找到這些WiFi熱點(diǎn)的。
引述:
 為了讓手機(jī)連上一個(gè)WiFi熱點(diǎn),基本上都是打開手機(jī)設(shè)置里面的WiFi設(shè)置功能,然后會(huì)看到里面有個(gè)WiFi熱點(diǎn)列表,然后選擇連接。 基本上你只要打開手機(jī)的WiFi功能,就會(huì)發(fā)現(xiàn)附近有超級(jí)多的各種來路不明的WiFi熱點(diǎn)(WiFi有風(fēng)險(xiǎn),連接需謹(jǐn)慎),那么手機(jī)是怎么找得到附近的WiFi的呢?

    通常,無線網(wǎng)絡(luò)提供的WiFi熱點(diǎn),大部分都開放了SSID廣播(記得之前博主講過WiFi熱點(diǎn)也可以隱藏的),Scan WiFi的功能就是掃描出所有附近的WiFi熱點(diǎn)的SSID信息,這樣一來,客戶端就可以根據(jù)需要選擇不同的SSID連入對(duì)應(yīng)的無線網(wǎng)絡(luò)中。
 Scan WiFi庫提供了兩種方式實(shí)現(xiàn)掃描過程:
①同步掃描:通過單個(gè)函數(shù)在一次運(yùn)行中完成,需要等待完成所有操作才能繼續(xù)運(yùn)行下面的操作。
②異步掃描:把上面的過程分成幾個(gè)步驟,每個(gè)步驟由一個(gè)單獨(dú)函數(shù)完成,我們可以在掃描過程中執(zhí)行其他任務(wù)。

我們的零知ESP8266本身就是WiFi模塊,當(dāng)然也可以掃描到WiFi的熱點(diǎn),接下來,我們一起學(xué)習(xí)并且找到附近所有的WiFi熱點(diǎn)吧。

一、硬件
電腦,windows系統(tǒng)
零知ES8266開發(fā)板
micro-usb線

二、軟件庫
具備ESP8266WiFiScan庫。

ESP8266WiFiScan庫,大家使用的時(shí)候只需要引入,所以要在IDE中安裝相應(yīng)的數(shù)據(jù)支持庫,當(dāng)然也可以留言,我免費(fèi)私給你鏈接!

  1. #include<ESP8266WiFi.h>
復(fù)制代碼

(1)掃描操作方法
① scanNetworks —— 同步掃描周邊有效wifi網(wǎng)絡(luò)
函數(shù)說明:

  1. /**
  2. * Start scan WiFi networks available
  3. * @param async         run in async mode(是否啟動(dòng)異步掃描)
  4. * @param show_hidden   show hidden networks(是否掃描隱藏網(wǎng)絡(luò))
  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);
復(fù)制代碼
② scanNetworks(async ) —— 異步掃描周邊有效wifi網(wǎng)絡(luò)
函數(shù)說明:
  1. /**
  2. * Start scan WiFi networks available
  3. * @param async         run in async mode(是否啟動(dòng)異步掃描)
  4. * @param show_hidden   show hidden networks(是否掃描隱藏網(wǎng)絡(luò))
  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);
復(fù)制代碼

③ scanNetworksAsync —— 異步掃描周邊wifi網(wǎng)絡(luò),并回調(diào)結(jié)果
函數(shù)說明:

  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);
復(fù)制代碼

④ scanComplete —— 檢測(cè)異步掃描的結(jié)果
函數(shù)說明:

  1. /**[/size][/font][/color][/align] * called to get the scan state in Async mode(異步掃描的結(jié)果函數(shù))
  2. * @return scan result or status
  3. *          -1 if scan not find
  4. *          -2 if scan not triggered
  5. */
  6. int8_t scanComplete();
復(fù)制代碼

⑤ scanDelete —— 從內(nèi)存中刪掉最近掃描結(jié)果
函數(shù)說明:

  1. /**
  2. * delete last scan result from RAM(從內(nèi)存中刪除最近的掃描結(jié)果)
  3. */void scanDelete();
復(fù)制代碼

注意:如果不刪除,將會(huì)疊加上次掃描的結(jié)果;

(2)掃描結(jié)果方法
① SSID —— 獲取wifi網(wǎng)絡(luò)名字
函數(shù)說明:

  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);
復(fù)制代碼

② RSSI —— 獲取wifi網(wǎng)絡(luò)信號(hào)強(qiáng)度
函數(shù)說明:

  1. /**
  2. * Return the RSSI of the networks discovered during the scanNetworks(信號(hào)強(qiáng)度)
  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);
復(fù)制代碼

③ encryptionType —— 獲取wifi網(wǎng)絡(luò)加密方式
函數(shù)說明:

  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);
復(fù)制代碼

④ BSSID —— 獲取wifi網(wǎng)絡(luò)mac地址
函數(shù)說明:

  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);
復(fù)制代碼

⑤ getNetworkInfo —— 獲取整體網(wǎng)絡(luò)信息,名字,信號(hào)強(qiáng)度等
函數(shù)說明:

  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);
復(fù)制代碼

注意:入?yún)⑶懊娑鄶?shù)加了&,意味著調(diào)完函數(shù)后外面獲取到詳細(xì)信息;
⑥ channel —— 獲取wifi網(wǎng)絡(luò)通道號(hào)
函數(shù)說明:

  1. /**
  2. * return channel of scanned wifi(通道號(hào))
  3. */int32_t channel(uint8_t networkItem);
復(fù)制代碼

⑦ isHidden —— 判斷wifi網(wǎng)絡(luò)是否是隱藏網(wǎng)絡(luò)
函數(shù)說明:

  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);
復(fù)制代碼

動(dòng)手操作
多說不宜,實(shí)驗(yàn)是檢驗(yàn)真理的唯一標(biāo)準(zhǔn),下面我們就來實(shí)際操作一下吧
三、
(1)打開零知開源開發(fā)工具,如下圖:


(2)電腦連接零知ESP8266開發(fā)板:


(3)輸入以下代碼,驗(yàn)證,上傳

  1. /**
  2. * Demo:
  3. *    STA模式下,演示同步掃描Scan wifi功能
  4. * @author 云上上云
  5. * @date 2019/06/01
  6. */#include <ESP8266WiFi.h>
  7. //以下三個(gè)定義為調(diào)試定義#define DebugBegin(baud_rate)    Serial.begin(baud_rate)#define DebugPrintln(message)    Serial.println(message)#define DebugPrint(message)    Serial.print(message)
  8. void setup() {  //設(shè)置串口波特率,以便打印信息
  9.   DebugBegin(115200);  //延時(shí)5s 為了演示效果
  10.   delay(5000);  // 我不想別人連接我,只想做個(gè)站點(diǎn)
  11.   WiFi.mode(WIFI_STA);  //斷開連接
  12.   WiFi.disconnect();
  13.   delay(100);
  14.   DebugPrintln("Setup done");
  15. }
  16. void loop() {
  17.   DebugPrintln("scan start");  // 同步掃描,等待返回結(jié)果
  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賬號(hào)
  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信號(hào)強(qiáng)度
  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("");  // 延時(shí)5s之后再次掃描
  40.   delay(5000);
  41. }
復(fù)制代碼

四、測(cè)試結(jié)果(附近潛在的WiFi熱點(diǎn)):
(我就不一一展示了,它可以掃描完附近所有WiFi)



分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏1 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:282431 發(fā)表于 2019-10-30 08:30 | 只看該作者
麻煩樓主給你鏈接,謝謝
回復(fù)

使用道具 舉報(bào)

板凳
ID:230251 發(fā)表于 2019-10-31 10:00 | 只看該作者
完成一次搜索需要花多長(zhǎng)時(shí)間呢?
回復(fù)

使用道具 舉報(bào)

地板
ID:349555 發(fā)表于 2019-10-31 11:42
ganlei 發(fā)表于 2019-10-31 10:00
完成一次搜索需要花多長(zhǎng)時(shí)間呢?

搜索一般都很快,具體時(shí)間沒有準(zhǔn)過,三分鐘內(nèi)的,我用的一般在一分鐘就是打開就搜索到的。
我給你連接,瞧瞧看如何0   http://www.lingzhilab.com/wuxian

5#
ID:349555 發(fā)表于 2019-10-31 11:42

6#
ID:349555 發(fā)表于 2019-11-2 14:27 | 只看該作者
wulingqing 發(fā)表于 2019-10-30 08:30
麻煩樓主給你鏈接,謝謝

這里不可以放鏈接,而且一般回復(fù)了,審核不會(huì)通過。你可以百度搜索“零知實(shí)驗(yàn)室”。點(diǎn)擊“無線”就ok
回復(fù)

使用道具 舉報(bào)

7#
ID:483623 發(fā)表于 2020-10-4 10:38 | 只看該作者
程序有幾個(gè)錯(cuò)誤,要改一下。
DebugBegin(115200);  //延時(shí)5s 為了演示效果
要改成  DebugBegin(9600);  //9600波特率,
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表