【ESP8266】设置联网获取静态IP
代码如下:
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266HTTPClient.h>
//设置两个宏定义用于WiFi名和密码
#define WIFINAME "xxxx"
#define WIFIPW "xxxxxxx"
IPAddress local_IP(192, 168, 166, 200);//想要设定的静态ip
IPAddress gateway(192, 168, 166, 247);//当前网关
IPAddress subnet(255, 255, 255, 0);//当前子网掩码
void setup()
{
// put your setup code here, to run once:
pinMode(BUILTIN_LED, OUTPUT);
Serial.begin(115200);
Serial.println("");
WiFi.config(local_IP, gateway, subnet);//设置静态IP
WiFi.mode(WIFI_STA);
WiFi.begin(WIFINAME, WIFIPW);
Serial.print("Connecting..");
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println();
Serial.print("Connected,IP Address:");
Serial.println(WiFi.localIP());
}
void loop()
{
// put your main code here, to run repeatedly:
}
核心代码
//路由器WIFI和密码
const char* ssid = "wifi_name";
const char* password = "88888888";
//静态地址、网关、子网掩码
IPAddress local_IP(192, 168, 0, 200);
IPAddress gateway(192, 168, 0, 1);
IPAddress subnet(255, 255, 255, 0);
//设置
WiFi.config(local_IP, gateway, subnet);//设置静态IP
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);//路由器的WiFi名称和密码