斷層的小伙伴們給我留言,我們的學習永不止步,請大家一定要加油學下去,你才發現電子的世界,需要的是持之以恒的你! 上次我們已經學習并分享了零知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中安裝相應的數據支持庫,當然也可以留言,我免費私給你鏈接!
(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 —— 檢測異步掃描的結果
函數說明:
- /**[/size][/font][/color][/align] * 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)打開零知開源開發工具,如下圖:
1.png (123.37 KB, 下載次數: 63)
下載附件
2019-10-29 17:39 上傳
(2)電腦連接零知ESP8266開發板:
2.png (450.8 KB, 下載次數: 46)
下載附件
2019-10-29 17:39 上傳
(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)
4.png (119.66 KB, 下載次數: 53)
下載附件
2019-10-29 17:39 上傳
|