esp32通过smartconfig连接wifi整体设计流程
1.流程图
2.代码实现
#include <WiFi.h>
#include <SPIFFS.h>
const char* wifi_config_file = "/wifi_config.txt";
const char* ssid = "";
const char* password = "";
const uint32_t RECONNECT_TIMEOUT = 20000;
void setup() {
Serial.begin(115200);
if (!SPIFFS.begin(true)) {
Serial.println("SPIFFS 初始化失败");
return;
}
if (ssid != NULL && password != NULL && ssid[0] != '\0' && password[0] != '\0') {
connectToWifi(ssid, password, 10);
} else {
connectToWifiSSIDFromFlash();
}
}
void loop() {
if (WiFi.status() != WL_CONNECTED) {
handleDisconnect();
} else {
Serial.println("连接WIFI成功");
delay(3000);
}
}
void connectToWifiSSIDFromFlash(void) {
char ssidRead[32];
char passwordRead[64];
readCredentialsFromSPIFFS(ssidRead, passwordRead);
if (ssidRead[0] != '\0' && passwordRead[0] != '\0') {
Serial.print("ssid:");
Serial.print(ssidRead);
Serial.print(" password:");
Serial.println(passwordRead);
connectToWifi(ssidRead, passwordRead, 10);
} else {
startSmartConfig();
}
}
void connectToWifi(const char* ssid, const char* password, uint32_t maxTimeoutSec) {
uint8_t connected = 1;
uint32_t waitTimeMs = 0;
WiFi.begin(ssid, password);
Serial.print("连接WIFI:");
Serial.println(ssid);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
waitTimeMs += 1000;
if (waitTimeMs / 1000 > maxTimeoutSec) {
connected = 0;
break;
}
}
if (connected) {
Serial.println("");
Serial.println("WIFI连接成功");
Serial.print("IP地址:");
Serial.println(WiFi.localIP());
} else {
Serial.println("WIFI连接失败");
}
}
void handleDisconnect() {
static uint32_t firstReconnectAttempt = 0;
static uint8_t reconnectCount = 0;
Serial.println("WIFI连接断开,重新连接");
if (millis() - firstReconnectAttempt > RECONNECT_TIMEOUT) {
reconnectCount = 0;
Serial.println("连接失败,重新进行 SmartConfig");
startSmartConfig();
} else {
if (reconnectCount++ == 1) {
firstReconnectAttempt = millis();
}
connectToWifiSSIDFromFlash();
}
}
void readCredentialsFromSPIFFS(char* ssid, char* password) {
File configFile = SPIFFS.open(wifi_config_file, "r");
if (configFile) {
configFile.readStringUntil('\n').toCharArray(ssid, 32);
configFile.readStringUntil('\n').toCharArray(password, 64);
configFile.close();
Serial.println("从 SPIFFS 读取配置文件成功");
} else {
Serial.println("读取 SPIFFS 文件失败");
}
}
void saveCredentialsToSPIFFS(const char* ssidNew, const char* passwordNew) {
File configFile = SPIFFS.open(wifi_config_file, "w");
if (configFile) {
configFile.print(ssidNew);
configFile.print('\n');
configFile.print(passwordNew);
configFile.close();
Serial.println("将配置信息保存到 SPIFFS 成功");
} else {
Serial.println("写入 SPIFFS 文件失败");
}
}
void smartConfigDoneHandle(void) {
String ssid = WiFi.SSID();
String password = WiFi.psk();
char* ssidSave = (char*)&ssid[0];
char* passwordSave = (char*)&password[0];
Serial.println("SmartConfig 配置成功:");
Serial.print("ssid:");
Serial.print(ssidSave);
Serial.print(" password:");
Serial.println(passwordSave);
saveCredentialsToSPIFFS(ssidSave, passwordSave);
ESP.restart();
}
void startSmartConfig() {
WiFi.mode(WIFI_MODE_STA);
WiFi.beginSmartConfig();
Serial.println("请使用手机 小程序/APP 进行配置, 正在等待SmartConfig配置");
while (!WiFi.smartConfigDone()) {
delay(500);
Serial.print(".");
}
smartConfigDoneHandle();
}