|
|
@@ -0,0 +1,90 @@
|
|
|
+#include "WiFiConfig.h"
|
|
|
+#include <Arduino.h>
|
|
|
+
|
|
|
+// 持久化存储
|
|
|
+Preferences preferences;
|
|
|
+
|
|
|
+// 持久化存储的参数
|
|
|
+String deviceID;
|
|
|
+String serverAddress;
|
|
|
+uint16_t serverPort;
|
|
|
+
|
|
|
+// 加载服务器参数
|
|
|
+void loadServerParams() {
|
|
|
+ preferences.begin("settings", true);
|
|
|
+ serverAddress = preferences.getString("serverAddress", "192.168.3.31");
|
|
|
+ serverPort = preferences.getUInt("serverPort", 1883);
|
|
|
+ preferences.end();
|
|
|
+ Serial.println("[信息] 加载服务器参数:");
|
|
|
+ Serial.println("服务器地址: " + serverAddress);
|
|
|
+ Serial.println("服务器端口: " + String(serverPort));
|
|
|
+}
|
|
|
+
|
|
|
+// 保存参数回调
|
|
|
+void saveParamsCallback() {
|
|
|
+ Serial.println("[信息] 正在保存自定义参数...");
|
|
|
+ preferences.begin("settings", false);
|
|
|
+ preferences.putString("serverAddress", serverAddress);
|
|
|
+ preferences.putUInt("serverPort", serverPort);
|
|
|
+ preferences.end();
|
|
|
+ Serial.println("[信息] 参数保存成功。");
|
|
|
+}
|
|
|
+
|
|
|
+// WiFi 配网功能
|
|
|
+void setupWiFiConfig() {
|
|
|
+ WiFiManager wifiManager;
|
|
|
+setConstantOn(true); // LED 常亮
|
|
|
+ WiFiManagerParameter customServer("server", "服务器地址", serverAddress.c_str(), 40);
|
|
|
+ WiFiManagerParameter customPort("port", "服务器端口", String(serverPort).c_str(), 6);
|
|
|
+ wifiManager.addParameter(&customServer);
|
|
|
+ wifiManager.addParameter(&customPort);
|
|
|
+
|
|
|
+ wifiManager.setSaveParamsCallback([]() {
|
|
|
+ serverAddress = WiFiManagerParameter("server", "服务器地址", serverAddress.c_str(), 40).getValue();
|
|
|
+ serverPort = String(WiFiManagerParameter("port", "服务器端口", String(serverPort).c_str(), 6).getValue()).toInt();
|
|
|
+ });
|
|
|
+
|
|
|
+ wifiManager.setConfigPortalTimeout(50); // 配置模式超时时间为 60 秒
|
|
|
+
|
|
|
+ if (!wifiManager.startConfigPortal("人体存在感应器_Config")) {
|
|
|
+ Serial.println("[错误] 配置模式超时或未完成配置!");
|
|
|
+ feedWatchdog();
|
|
|
+ // 关闭 LED
|
|
|
+setConstantOn(false);
|
|
|
+ } else {
|
|
|
+ serverAddress = customServer.getValue();
|
|
|
+ serverPort = String(customPort.getValue()).toInt();
|
|
|
+ saveParamsCallback();
|
|
|
+ loadServerParams(); // 重新加载保存的服务器配置
|
|
|
+ // 关闭 LED
|
|
|
+setConstantOn(false);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// 尝试连接 WiFi
|
|
|
+bool connectWiFiWithTimeout(uint32_t timeoutMs) {
|
|
|
+ Serial.println("[信息] 正在尝试连接 Wi-Fi...");
|
|
|
+ WiFi.begin();
|
|
|
+ uint32_t startAttemptTime = millis();
|
|
|
+ feedWatchdog();
|
|
|
+
|
|
|
+ while (WiFi.status() != WL_CONNECTED && (millis() - startAttemptTime) < timeoutMs) {
|
|
|
+ delay(500);
|
|
|
+ addBlinkTask(2);
|
|
|
+ Serial.print(".");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (WiFi.status() == WL_CONNECTED) {
|
|
|
+ Serial.println("\n[信息] Wi-Fi 连接成功!");
|
|
|
+ deviceID = WiFi.macAddress();
|
|
|
+ Serial.println("设备 ID (MAC 地址): " + deviceID);
|
|
|
+ Serial.println("IP 地址: " + WiFi.localIP().toString());
|
|
|
+ return true;
|
|
|
+ } else {
|
|
|
+ Serial.print("\n[错误] Wi-Fi 连接超时,超时时间: ");
|
|
|
+ Serial.print(timeoutMs);
|
|
|
+ Serial.println(" 毫秒");
|
|
|
+ setupWiFiConfig();
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+}
|