使用 Wi-Fi 函式庫

要使用 LinkIt 7697 的 Wi-Fi 功能,可透過 BSP 內的 LWiFi 函式庫達成。LWiFi 函式庫的介面與 Arduino Wi-Fi 函式庫非常相似,因此大多數的 Arduino Wi-Fi 範例都可在不需修改的狀況下直接使用。

範例 1:掃描鄰近的 Wi-Fi 網路

1.使用 File > Examples > LWiFi > ScanNetwork 範例,讓開發板搜尋鄰近的 Wi-Fi AP

2.點選 Upload 按鈕開始編譯與上傳,直到 Done Uploading 訊息顯示為止

3.點選工具列中的 Serial Monitor 按鈕 (如圖):

4.設定 Baud rate9,600。接著就會看到開發板所找到的鄰近 Wi-Fi AP 清單:

若沒有看到訊息的話,可按下 RST 按鈕重啟 LinkIt 7697 再次進行掃描。使用 RST 按鈕時,不需重新開啟 Serial Monitor,因為 RST 按鈕僅會重啟 MT7697 SoC、並不影響 CP2012N 的運作,因此 Serial Monitor 所連接的 COM 埠不會因 SoC 的重啟而產生影響。

範例 2:連接到網頁伺服器

此範例將展示透過使用 LWiFi 函式庫裡的 WiFiClient 類別,與網頁伺服器之間建立 TCP 連線。

首先開啟 File > Examples > LWiFi > WiFiWebClient 範例,並根據所使用的 Wi-Fi AP,在程式碼裡填入正確的 SSID 與密碼資訊:

char ssid[] = "yourNetwork"; // your network SSID (name) 
char pass[] = "secretPassword"; // your network password (use for WPA, or use as key for WEP)

此程式會與 http://download.labs.mediatek.com 網頁伺服器建立 TCP 連線,並發出取得 linkit_7697_ascii.txt TXT 文字檔的要求:

// Make a HTTP request: 
client.println("GET /linkit_7697_ascii.txt HTTP/1.0"); 
client.println("Host: download.labs.mediatek.com"); 
client.println("Accept: */*"); 
client.println("Connection: close"); 
client.println();

所以當 LinkIt 7697 執行這個 sketch 時,開啟 Serial Monitor 即可看到網頁伺服器的 HTTP header 回應、以及 LinkIt 7697 字樣的 ASCII art 圖案 (即 linkit_7697_ascii.txt 的內容) 如下:

  _      _       _    _____ _     ______ __ ___ ______ 
 | |    (_)     | |  |_   _| |   |____  / // _ \____  |
 | |     _ _ __ | | __ | | | |_      / / /| (_) |  / / 
 | |    | | '_ \| |/ / | | | __|    / / '_ \__, | / /  
 | |____| | | | |   < _| |_| |_    / /| (_) |/ / / /   
 |______|_|_| |_|_|\_\_____|\__|  /_/  \___//_/ /_/

Last updated