首先,为了研究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呀!反正先证明板子能用就行,后面再慢慢研究。