| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- #include "WiFiConfig.h"
- #include <Arduino.h>
- // 持久化存储
- Preferences preferences;
- // 持久化存储的参数
- String deviceID; //MAC地址 id
- String serverAddress; //mqtt服务器地址
- uint16_t serverPort; //mqtt端口
- uint16_t otaPort; // 新增 OTA 端口号
- // 加载服务器参数
- void loadServerParams() {
- preferences.begin("settings", true);
- serverAddress = preferences.getString("serverAddress", "192.168.3.31"); //mqtt地址
- serverPort = preferences.getUInt("serverPort", 1883); //mqtt端口
- otaPort = preferences.getUInt("otaPort", 2999); // 新增 OTA 端口号,默认 2999
- preferences.end();
- Serial.println("[信息] 加载服务器参数:");
- Serial.println("服务器地址: " + serverAddress); // 打印服务器地址
- Serial.println("服务器端口: " + String(serverPort)); // 打印 MQTT 端口号
- Serial.println("OTA 端口: " + String(otaPort)); // 打印 OTA 端口号
- }
- // 保存参数回调
- void saveParamsCallback() {
- Serial.println("[信息] 正在保存自定义参数...");
- preferences.begin("settings", false);
- preferences.putString("serverAddress", serverAddress); // 保存 MQTT 服务器地址
- preferences.putUInt("serverPort", serverPort); // 保存 MQTT 端口号
- preferences.putUInt("otaPort", otaPort); // 保存 OTA 端口号
- 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);
- WiFiManagerParameter customOtaPort("otaPort", "OTA 端口", String(otaPort).c_str(), 6); // 新增 OTA 端口配置
- wifiManager.addParameter(&customServer);
- wifiManager.addParameter(&customPort);
- wifiManager.addParameter(&customOtaPort); // 添加 OTA 端口配置
- // 修复:捕获 customOtaPort
- wifiManager.setSaveParamsCallback([&customOtaPort]() {
- serverAddress = WiFiManagerParameter("server", "服务器地址", serverAddress.c_str(), 40).getValue();
- serverPort = String(WiFiManagerParameter("port", "服务器端口", String(serverPort).c_str(), 6).getValue()).toInt();
- otaPort = String(customOtaPort.getValue()).toInt(); // 保存 OTA 端口号
- });
- wifiManager.setConfigPortalTimeout(50); // 配置模式超时时间为 60 秒
- if (!wifiManager.startConfigPortal("人体存在感应器_Config")) {
- Serial.println("[错误] 配置模式超时或未完成配置!");
- feedWatchdog();
- setConstantOn(false); // 关闭 LED
- } else {
- serverAddress = customServer.getValue();
- serverPort = String(customPort.getValue()).toInt();
- otaPort = String(customOtaPort.getValue()).toInt(); // 保存 OTA 端口号
- saveParamsCallback();
- loadServerParams(); // 重新加载保存的服务器配置
- setConstantOn(false); // 关闭 LED
- }
- }
- // 尝试连接 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;
- }
- }
|