测试 WIFI
代码
#include <WiFi.h>
#define LED1 27
#define LED2 29
void led_init()
{
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
}
void led_on()
{
digitalWrite(LED1, HIGH);
digitalWrite(LED2, HIGH);
}
void led_off()
{
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
}
char *ssid = "TP-LINK_A8AB"; // 填入 WIFI 名称
char *pswd = "************"; // 填入 WIFI 密码
void setup()
{
led_init();
led_on();
delay(1000);
led_off();
delay(1000);
WiFi.begin(ssid, pswd);
// Will try for about 10 seconds (20x 500ms)
int tryDelay = 500;
int numberOfTries = 20;
while (true)
{
switch (WiFi.status())
{
case WL_NO_SSID_AVAIL:
break;
case WL_CONNECT_FAILED:
return;
break;
case WL_CONNECTION_LOST:
break;
case WL_SCAN_COMPLETED:
break;
case WL_DISCONNECTED:
break;
case WL_CONNECTED:
led_on();
return;
break;
default:
break;
}
delay(tryDelay);
if (numberOfTries <= 0)
{
// Use disconnect function to force stop trying to connect
WiFi.disconnect();
return;
}
else
{
numberOfTries--;
}
}
}
void loop()
{
if (0)
{
// Disconnect from WiFi
WiFi.disconnect();
delay(1000);
}
}
实验
开发板上电之前需要确保 WIFI 已经打开
|