From 36f5a285f4c6b93248d35ed742ab59228f1924ec Mon Sep 17 00:00:00 2001 From: yangzhijia <547486824@qq.com> Date: Wed, 12 Aug 2026 17:19:07 +0800 Subject: [PATCH] =?UTF-8?q?feat=EF=BC=9A=E5=A2=9E=E5=8A=A0=E8=93=9D?= =?UTF-8?q?=E7=89=99=E9=85=8D=E7=BD=91=E8=AE=BE=E5=A4=87=E7=BC=96=E5=8F=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/config.h | 6 +++--- include/wifi_manager.h | 3 +++ src/wifi_manager.cpp | 44 ++++++++++++++++++++++++++++++++++-------- 3 files changed, 42 insertions(+), 11 deletions(-) diff --git a/include/config.h b/include/config.h index 32812fc..f959037 100644 --- a/include/config.h +++ b/include/config.h @@ -19,7 +19,7 @@ // --- 雷达型号选择 --- #define RADAR_TYPE_ABD1 0 // R60ABD1: 心率/呼吸精准, 跌倒一般 #define RADAR_TYPE_AFD1 1 // R60AFD1: 跌倒/姿态精准, 无心率呼吸 -#define RADAR_TYPE RADAR_TYPE_AFD1 // AFD1 (跌倒/姿态精准) +#define RADAR_TYPE RADAR_TYPE_ABD1 // AFD1 (跌倒/姿态精准, 卫生间用) // --- 毫米波雷达 --- #define RADAR_UART_PORT Serial2 @@ -76,9 +76,9 @@ // 3. WiFi 默认配置 (配网使用) // ============================================================ -#define WIFI_AP_SSID "HealthMonitor-Setup" +#define WIFI_AP_SSID_PREFIX "HealthMonitor-" // 末尾拼 MAC 末4位, 保证每台设备唯一 #define WIFI_AP_PASSWORD "1234567890" -#define WIFI_CONFIG_TIMEOUT 0 // 0=不超时, 一直等待配置 +#define WIFI_CONFIG_TIMEOUT 120 // 配网超时(秒), 超时回连上一个 WiFi #define WIFI_CONNECT_TIMEOUT 15 #define WIFI_RETRY_INTERVAL 10000 diff --git a/include/wifi_manager.h b/include/wifi_manager.h index 060349d..eb902e1 100644 --- a/include/wifi_manager.h +++ b/include/wifi_manager.h @@ -113,6 +113,9 @@ private: String _blePendingJson; // 待处理的 BLE 配网 JSON bool _blePendingFlag; // 有未处理的 BLE 数据 void _processBLEPending(); // 在 loop() 上下文处理 + + // AP SSID (前缀+MAC后4位, BLE 命名复用) + String _apSsid; }; #endif // WIFI_MANAGER_H diff --git a/src/wifi_manager.cpp b/src/wifi_manager.cpp index 8928600..132892b 100644 --- a/src/wifi_manager.cpp +++ b/src/wifi_manager.cpp @@ -133,6 +133,24 @@ bool WiFiMgmt::loop() { if (_portalActive) { _dns->processNextRequest(); _server->handleClient(); + + // 配网超时 → 回连上一个 WiFi + if (WIFI_CONFIG_TIMEOUT > 0 && millis() - _portalStartTime > (uint32_t)WIFI_CONFIG_TIMEOUT * 1000) { + Serial.printf("[WiFi] ⏰ 配网超时 %ds, 回连已存 WiFi...\n", WIFI_CONFIG_TIMEOUT); + _stopPortal(); + if (_config.wifi_ssid[0] != '\0') { + _connected = _connectWiFi(); + } else { + _connected = false; + Serial.println("[WiFi] 无已存 WiFi"); + } + if (_connected) { + _setLEDMode(3); // 常亮 = 在线 + } else { + _setLEDMode(1); // 慢闪 = 离线, WiFi.AutoReconnect 后台重试 + } + } + delay(10); return _connected; } @@ -228,8 +246,11 @@ void WiFiMgmt::_startPortal() { delay(200); WiFi.mode(WIFI_AP); WiFi.softAPConfig(IPAddress(192,168,4,1), IPAddress(192,168,4,1), IPAddress(255,255,255,0)); - WiFi.softAP(WIFI_AP_SSID, WIFI_AP_PASSWORD); - Serial.printf("[WiFi] AP: %s IP: 192.168.4.1\n", WIFI_AP_SSID); + // AP 名称 = 前缀 + MAC 后4位 (保证每台设备唯一, 方便区分多设备配网) + String mac = WiFi.macAddress(); // e.g. "8C:AA:B5:12:34:56" + _apSsid = String(WIFI_AP_SSID_PREFIX) + mac.substring(9, 11) + mac.substring(12, 14); + WiFi.softAP(_apSsid.c_str(), WIFI_AP_PASSWORD); + Serial.printf("[WiFi] AP: %s IP: 192.168.4.1\n", _apSsid.c_str()); // DNS 劫持: 所有域名 → 192.168.4.1 → 触发手机强制门户弹窗 _dns = new DNSServer(); @@ -417,8 +438,11 @@ void WiFiMgmt::_startBLE() { if (_bleAdv) { // 重设广告数据 (stop 后 start 需要重新配置) BLEAdvertisementData advData; - advData.setCompleteServices(BLEUUID("4fafc201-1fb5-459e-8fcc-c5c9c331914b")); - advData.setShortName("HealthMon"); + std::string rawName; + rawName += (char)(_apSsid.length() + 1); + rawName += (char)0x09; + rawName += _apSsid.c_str(); + advData.addData(rawName); _bleAdv->setAdvertisementData(advData); _bleAdv->setScanResponse(false); _bleAdv->start(); @@ -427,7 +451,7 @@ void WiFiMgmt::_startBLE() { return; } - BLEDevice::init("HealthMonitor"); + BLEDevice::init(_apSsid.c_str()); // BLE 名称跟随 AP 名称, 完整名 "HealthMonitor-XXXX" _bleServer = BLEDevice::createServer(); BLEService* svc = _bleServer->createService("4fafc201-1fb5-459e-8fcc-c5c9c331914b"); @@ -460,13 +484,17 @@ void WiFiMgmt::_startBLE() { _bleAdv = _bleServer->getAdvertising(); BLEAdvertisementData advData; - advData.setCompleteServices(BLEUUID("4fafc201-1fb5-459e-8fcc-c5c9c331914b")); - advData.setShortName("HealthMon"); + // 完整名通过 addData 嵌入广播包 (AD type 0x09) + std::string rawName; + rawName += (char)(_apSsid.length() + 1); + rawName += (char)0x09; + rawName += _apSsid.c_str(); + advData.addData(rawName); _bleAdv->setAdvertisementData(advData); _bleAdv->setScanResponse(false); // 不需要 scan response, 全在主包 _bleAdv->start(); - Serial.printf("[BLE] ✅ 配网广播: HealthMonitor device_id=%s\n", _config.device_id); + Serial.printf("[BLE] ✅ 配网广播: %s device_id=%s\n", _apSsid.c_str(), _config.device_id); } void WiFiMgmt::_stopBLE() {