代码语言:javascript复制
#include <ESP8266WiFi.h> // 程序使用ESP8266WiFi库
const char *ssid = "WIFI名"; //WIFI名
const char *pwd = "密码"; //WIFI密码
void setup() {
Serial.begin(9600); //启动串口通讯
WiFi.begin(ssid,pwd); //连接
while (WiFi.status() != WL_CONNECTED){
delay(1000); //每隔一秒重连一次
Serial.print('.');
}
Serial.print("IP:");
Serial.print(WiFi.localIP()); //输出IP
}
void loop() {
// put your main code here, to run repeatedly:
}
连接WIFI并输出IP地址
升级版,多个WIFI备选
代码语言:javascript复制#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
ESP8266WiFiMulti wifiMulti; //实例化对象
void setup()
{
Serial.begin(9600);
wifiMulti.addAP("WI-FI","12345678");
wifiMulti.addAP("WI-FI_5G","12345678");
while (wifiMulti.run() != WL_CONNECTED)
{
delay(1000);
Serial.print('.');
}
Serial.print("Connected to ");
Serial.println(WiFi.SSID());
Serial.print("IP:");
Serial.print(WiFi.localIP());
}
void loop()
{
// put your main code here, to run repeatedly:
}
学习教程-太极创客:http://www.taichi-maker.com/homepage/esp8266-nodemcu-iot/iot-c/