#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <EEPROM.h>
// 定义AP模式下的WiFi名称和密码
const char* AP_SSID = "ESP8266_WiFi_Config";
const char* AP_PASSWORD = "12345678";
// 定义Web服务器端口
ESP8266WebServer server(80);
// WiFi配置结构体
struct WiFiConfig {
char ssid[32];
char password[64];
};
void clearEEPROM() {
EEPROM.begin(4096); // ESP8266的EEPROM大小为4KB
// 用0填充整个EEPROM
for (int i = 0; i < 4096; i++) {
EEPROM.write(i, 0);
}
EEPROM.commit();
EEPROM.end();
Serial.println("EEPROM已清除");
}
// 从EEPROM读取WiFi配置
bool loadWiFiConfig(WiFiConfig &config) {
EEPROM.begin(sizeof(WiFiConfig));
EEPROM.get(0, config);
EEPROM.end();
// 检查配置是否有效
return (config.ssid[0] != 0 && strlen(config.ssid) > 0);
}
// 保存WiFi配置到EEPROM
void saveWiFiConfig(const WiFiConfig &config) {
EEPROM.begin(sizeof(WiFiConfig));
EEPROM.put(0, config);
EEPROM.commit();
EEPROM.end();
}
// 处理根目录请求
void handleRoot() {
String html = "<html><head> <meta charset=\"utf-8\"><title>WiFi配置</title></head><body>";
html += "<h1>WiFi配置</h1>";
html += "<form method='post' action='/connect'>";
html += "SSID: <input type='text' name='ssid'><br>";
html += "密码: <input type='password' name='password'><br>";
html += "<input type='submit' value='连接'>";
html += "</form>";
html += "</body></html>";
server.send(200, "text/html", html);
}
// 处理连接请求
void handleConnect() {
String ssid = server.arg("ssid");
String password = server.arg("password");
if (ssid.length() == 0) {
server.send(200, "text/html", "<html><body>SSID不能为空<a href='/'>返回</a></body></html>");
return;
}
// 尝试连接WiFi
WiFi.begin(ssid.c_str(), password.c_str());
// 等待连接
int attempts = 0;
while (WiFi.status() != WL_CONNECTED && attempts < 20) {
delay(500);
attempts++;
}
if (WiFi.status() == WL_CONNECTED) {
// 保存配置
WiFiConfig config;
strncpy(config.ssid, ssid.c_str(), sizeof(config.ssid));
strncpy(config.password, password.c_str(), sizeof(config.password));
saveWiFiConfig(config);
server.send(200, "text/html", "<html><body>连接成功!<br>设备将在3秒后重启...</body></html>");
delay(3000);
ESP.restart();
} else {
server.send(200, "text/html", "<html><body>连接失败<a href='/'>返回</a></body></html>");
}
}
// 启动AP模式
void startAPMode() {
WiFi.mode(WIFI_AP);
WiFi.softAP(AP_SSID, AP_PASSWORD);
// 设置Web服务器
server.on("/", handleRoot);
server.on("/connect", handleConnect);
server.begin();
Serial.println("AP模式已启动");
Serial.print("AP SSID: ");
Serial.println(AP_SSID);
Serial.print("AP IP地址: ");
Serial.println(WiFi.softAPIP());
}
void setup() {
Serial.begin(115200);
// clearEEPROM();
delay(1000);
// 尝试加载已保存的WiFi配置
WiFiConfig config;
if (loadWiFiConfig(config)) {
Serial.println("找到已保存的WiFi配置");
Serial.print("尝试连接: ");
Serial.println(config.ssid);
WiFi.begin(config.ssid, config.password);
// 等待连接
int attempts = 0;
while (WiFi.status() != WL_CONNECTED && attempts < 20) {
delay(500);
Serial.print(".");
attempts++;
}
if (WiFi.status() == WL_CONNECTED) {
Serial.println("");
Serial.println("WiFi连接成功");
Serial.print("IP地址: ");
Serial.println(WiFi.localIP());
return; // 连接成功,不需要进入AP模式
}
}
// 如果没有保存的配置或连接失败,启动AP模式
startAPMode();
}
void loop() {
// 如果在AP模式下,处理客户端请求
if (WiFi.getMode() == WIFI_AP) {
server.handleClient();
}
// 这里可以添加你的主程序逻辑
}
更多精彩,请关注公众号

微信公众号