首先,爲了研究Arduino在wifi中的應用,上某寶買了一塊D1板子,自己把排針焊上,搞好後實物如下:
但是賣家除了一個網盤上一個資料文件,啥都沒有。
於是只能自己琢磨。
1.下載商家給的Arduino IDE 並解壓縮
2.打開 文件->首選項
在下面Additional Boards Manager URLs中填寫
http://arduino.esp8266.com/stable/package_esp8266com_index.json
3.因爲賣家給的IDE裏安裝的可能是esp8266的2.0.0版本,所以這時候在““工具->板:”裏面還只能看到有限的型號。可以自己再改一下。
打開 “工具->板:->Board Manager”
輸入“esp”就會自動跳出來esp8266
選中後,下面選擇新的版本。(我選了個2.4.0,其他的有興趣同學可以自己試一下)
重啓以下IDE,這時候就可以在““工具->板:”裏看到很多esp8266的版本了。
因爲受傷的是D1 R2的板子,所以就選擇相符的型號。
4.連接好板子,選好端口。
創建一個新的測試文件,代碼如下:
/*
* This sketch demonstrates how to scan WiFi networks.
* The API is almost the same as with the WiFi Shield library,
* the most obvious difference being the different file you need to include:
*/
#include "ESP8266WiFi.h"
void setup() {
Serial.begin(115200);
// Set WiFi to station mode and disconnect from an AP if it was previously connected
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);
Serial.println("Setup done");
}
void loop() {
Serial.println("scan start");
// WiFi.scanNetworks will return the number of networks found
int n = WiFi.scanNetworks();
Serial.println("scan done");
if (n == 0)
Serial.println("no networks found");
else
{
Serial.print(n);
Serial.println(" networks found");
for (int i = 0; i < n; ++i)
{
// Print SSID and RSSI for each network found
Serial.print(i + 1);
Serial.print(": ");
Serial.print(WiFi.SSID(i));
Serial.print(" (");
Serial.print(WiFi.RSSI(i));
Serial.print(")");
Serial.println((WiFi.encryptionType(i) == ENC_TYPE_NONE)?" ":"*");
delay(10);
}
}
Serial.println("");
// Wait a bit before scanning again
delay(5000);
}
這是一個掃描wifi 的程序
上傳完成後,打開串口監視器就可以看到掃描到的WIFI信息了。
PS:網上很多教刷的是示例中的basic->blink,一開始我也是無腦刷了,結果上傳出錯,後來仔細看了一下,我這個板子根本就沒有D13呀!反正先證明板子能用就行,後面再慢慢研究。