ESP8266的AP配网,可以自行配置网络,一个简单的demo,文档最后有所有的代码,已经测试通过.
查看SPIFFS文件管理系统中的文件 账号密码是否存在,如不存在进入AP配网,如存在进入wifi连接模式
// 检查Wi-Fi凭据if (isWiFiConfigured()) {Serial.println("找到Wi-Fi凭据,尝试连接Wi-Fi...");String ssid = loadWiFiSSID();String pass = loadWiFiPassword();WiFi.begin(ssid.c_str(), pass.c_str());unsigned long startAttemptTime = millis();// 等待最多15秒来连接Wi-Fiwhile (WiFi.status() != WL_CONNECTED && millis() - startAttemptTime < wifiConnectTimeout) {delay(100);Serial.print(".");}if (WiFi.status() == WL_CONNECTED) {Serial.println("Wi-Fi连接成功!");Serial.print("IP地址: ");Serial.println(WiFi.localIP());} else {Serial.println("Wi-Fi连接失败,进入AP模式...");startAPMode();}} else {Serial.println("未找到Wi-Fi凭据,进入AP模式...");startAPMode();}// 设置Web服务器路由server.on("/", HTTP_GET, handleRoot);server.on("/save", HTTP_POST, handleSaveWiFi);// 启动Web服务器server.begin();
}
启用AP模式
// 启动AP模式,供用户配置Wi-Fi
void startAPMode() {WiFi.softAP(ap_ssid, ap_password);Serial.print("AP模式启动,IP地址:");Serial.println(WiFi.softAPIP());
}
AP配网的网页端代码(这个是直接写入,没有采用SPIFFS管理)
// 网页表单HTML
const char* htmlForm = R"rawliteral(<html><head><title>Wi-Fi 配置</title></head><body><h2>请输入Wi-Fi配置</h2><form action="/save" method="POST"><label for="ssid">SSID:</label><br><input type="text" id="ssid" name="ssid" required><br><br><label for="pass">密码:</label><br><input type="password" id="pass" name="pass" required><br><br><input type="submit" value="保存配置"></form></body></html>
)rawliteral";
总代码(先进入AP配网,将wifi密码输入上去后进入wifi模式,可连接自己的wifi)
// 检查是否有保存的Wi-Fi凭据
bool isWiFiConfigured() {File ssidFile = SPIFFS.open(WIFI_SSID_FILE, "r");File passFile = SPIFFS.open(WIFI_PASS_FILE, "r");return ssidFile && passFile; // 如果两个文件都存在,返回true
}
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <FS.h>const char* ap_ssid = "ESP8266-Config";
const char* ap_password = "";
const unsigned long wifiConnectTimeout = 20000; // 20秒超时ESP8266WebServer server(80);#define WIFI_SSID_FILE "/wifi_ssid.txt"
#define WIFI_PASS_FILE "/wifi_pass.txt"// 网页表单HTML
const char* htmlForm = R"rawliteral(<html><head><title>Wi-Fi 配置</title></head><body><h2>请输入Wi-Fi配置</h2><form action="/save" method="POST"><label for="ssid">SSID:</label><br><input type="text" id="ssid" name="ssid" required><br><br><label for="pass">密码:</label><br><input type="password" id="pass" name="pass" required><br><br><input type="submit" value="保存配置"></form></body></html>
)rawliteral";void setup() {Serial.begin(115200);delay(1000); // 等待串口初始化// 初始化SPIFFS文件系统if (!SPIFFS.begin()) {Serial.println("文件系统初始化失败!");return;}// 检查Wi-Fi凭据if (isWiFiConfigured()) {Serial.println("找到Wi-Fi凭据,尝试连接Wi-Fi...");String ssid = loadWiFiSSID();String pass = loadWiFiPassword();WiFi.begin(ssid.c_str(), pass.c_str());unsigned long startAttemptTime = millis();// 等待最多15秒来连接Wi-Fiwhile (WiFi.status() != WL_CONNECTED && millis() - startAttemptTime < wifiConnectTimeout) {delay(100);Serial.print(".");}if (WiFi.status() == WL_CONNECTED) {Serial.println("Wi-Fi连接成功!");Serial.print("IP地址: ");Serial.println(WiFi.localIP());} else {Serial.println("Wi-Fi连接失败,进入AP模式...");startAPMode();}} else {Serial.println("未找到Wi-Fi凭据,进入AP模式...");startAPMode();}// 设置Web服务器路由server.on("/", HTTP_GET, handleRoot);server.on("/save", HTTP_POST, handleSaveWiFi);// 启动Web服务器server.begin();
}void loop() {server.handleClient();
}// 启动AP模式,供用户配置Wi-Fi
void startAPMode() {WiFi.softAP(ap_ssid, ap_password);Serial.print("AP模式启动,IP地址:");Serial.println(WiFi.softAPIP());
}// 显示Wi-Fi配置页面
void handleRoot() {server.send(200, "text/html", htmlForm);
}// 保存Wi-Fi配置
void handleSaveWiFi() {String ssid = server.arg("ssid");String pass = server.arg("pass");// 保存Wi-Fi配置到SPIFFSsaveWiFiCredentials(ssid.c_str(), pass.c_str());// 返回保存成功的页面String response = "<html><body><h2>Wi-Fi配置已保存!</h2>";Serial.println("Wi-Fi配置已保存!");response += "<p>SSID: " + ssid + "</p>";response += "<p>密码: " + pass + "</p>";response += "</body></html>";server.send(200, "text/html", response);// 保存后重启设备以连接Wi-FiESP.restart();
}// 保存Wi-Fi凭据到SPIFFS
void saveWiFiCredentials(const char* ssid, const char* pass) {File ssidFile = SPIFFS.open(WIFI_SSID_FILE, "w");File passFile = SPIFFS.open(WIFI_PASS_FILE, "w");if (ssidFile) {ssidFile.print(ssid);ssidFile.close();} else {Serial.println("保存SSID失败");}if (passFile) {passFile.print(pass);passFile.close();} else {Serial.println("保存密码失败");}
}// 加载保存的Wi-Fi SSID
String loadWiFiSSID() {File ssidFile = SPIFFS.open(WIFI_SSID_FILE, "r");if (!ssidFile) {Serial.println("无法读取SSID");return "";}String ssid = ssidFile.readString();Serial.print("SSID: ");Serial.println(ssid);ssidFile.close();return ssid;
}// 加载保存的Wi-Fi密码
String loadWiFiPassword() {File passFile = SPIFFS.open(WIFI_PASS_FILE, "r");if (!passFile) {Serial.println("无法读取密码");return "";}String pass = passFile.readString();Serial.print("Password: ");Serial.println(pass);passFile.close();return pass;
}// 检查是否有保存的Wi-Fi凭据
bool isWiFiConfigured() {File ssidFile = SPIFFS.open(WIFI_SSID_FILE, "r");File passFile = SPIFFS.open(WIFI_PASS_FILE, "r");return ssidFile && passFile; // 如果两个文件都存在,返回true
}