Update project with radar and wifi manager modules
This commit is contained in:
58
.trae/documents/plan_20260203_063543.md
Normal file
58
.trae/documents/plan_20260203_063543.md
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
# 修改蓝牙发送雷达数据模式
|
||||||
|
|
||||||
|
## 1. 需求分析
|
||||||
|
- 修改蓝牙发送机制,实现基于数据变化的发送
|
||||||
|
- 当心率、呼吸、存在检测、体动状态、睡眠状态发生变化时才发送
|
||||||
|
- 根据小程序设定的时间间隔进行定时检测
|
||||||
|
- 如果检测到变化,立即更新数据
|
||||||
|
- 按设定间隔检测数据更新,有变化则发送全部数据
|
||||||
|
|
||||||
|
## 2. 实现方案
|
||||||
|
|
||||||
|
### 2.1 添加数据存储结构
|
||||||
|
- 在 `radar_manager.h` 中添加 `LastSentData` 结构体,用于存储上一次发送的数据
|
||||||
|
- 包含需要检测变化的字段:心率、呼吸率、存在状态、运动状态、睡眠状态
|
||||||
|
|
||||||
|
### 2.2 修改全局变量
|
||||||
|
- 在 `radar_manager.cpp` 中添加 `lastSentData` 全局变量
|
||||||
|
- 添加 `lastCheckTime` 变量用于记录上次检测时间
|
||||||
|
|
||||||
|
### 2.3 实现数据变化检测函数
|
||||||
|
- 添加 `isDataChanged()` 函数,比较当前数据与上次发送数据
|
||||||
|
- 实现阈值判断,避免微小波动导致频繁发送
|
||||||
|
|
||||||
|
### 2.4 重写 BLE 数据发送任务
|
||||||
|
- 修改 `bleSendTask` 任务,实现以下逻辑:
|
||||||
|
1. 定时检测数据变化(基于 `continuousSendInterval`)
|
||||||
|
2. 检测到变化时立即发送数据
|
||||||
|
3. 发送后更新 `lastSentData` 为当前数据
|
||||||
|
4. 保持与现有命令处理的兼容性
|
||||||
|
|
||||||
|
### 2.5 保持命令处理逻辑
|
||||||
|
- 保留 `processStartContinuousSend` 和 `processStopContinuousSend` 函数
|
||||||
|
- 确保小程序可以正常控制发送模式和间隔
|
||||||
|
|
||||||
|
## 3. 代码修改点
|
||||||
|
|
||||||
|
### 3.1 radar_manager.h
|
||||||
|
- 添加 `LastSentData` 结构体定义
|
||||||
|
- 在全局变量声明中添加 `lastSentData` 和相关变量
|
||||||
|
|
||||||
|
### 3.2 radar_manager.cpp
|
||||||
|
- 初始化 `lastSentData` 变量
|
||||||
|
- 实现 `isDataChanged()` 函数
|
||||||
|
- 重写 `bleSendTask` 任务函数
|
||||||
|
- 确保数据发送格式与现有代码保持一致
|
||||||
|
|
||||||
|
## 4. 技术要点
|
||||||
|
- 使用阈值检测避免微小数据波动导致的频繁发送
|
||||||
|
- 保持定时检测机制,确保数据及时性
|
||||||
|
- 维持与现有 BLE API 的兼容性
|
||||||
|
- 按照现有代码的注释格式编写新代码
|
||||||
|
- 确保内存使用合理,避免内存泄漏
|
||||||
|
|
||||||
|
## 5. 预期效果
|
||||||
|
- 减少不必要的蓝牙数据传输
|
||||||
|
- 只在数据发生实际变化时发送
|
||||||
|
- 保持数据的实时性和准确性
|
||||||
|
- 与现有小程序控制逻辑完全兼容
|
||||||
516
BLE_API.md
Normal file
516
BLE_API.md
Normal file
@@ -0,0 +1,516 @@
|
|||||||
|
# ESP32 BLE API 文档
|
||||||
|
|
||||||
|
## 概述
|
||||||
|
|
||||||
|
本文档描述了 ESP32 雷达设备的 BLE(蓝牙低功耗)通信 API,用于通过蓝牙连接配置和管理设备。
|
||||||
|
|
||||||
|
## 通信协议
|
||||||
|
|
||||||
|
- **传输方式**: BLE (Bluetooth Low Energy)
|
||||||
|
- **数据格式**: JSON
|
||||||
|
- **编码**: UTF-8
|
||||||
|
- **分包大小**: 20 字节/包
|
||||||
|
- **特征值 UUID**: `beb5483e-36e1-4688-b7f5-ea07361b26a8`
|
||||||
|
- **服务 UUID**: `a8c1e5c0-3d5d-4a9d-8d5e-7c8b6a4e2f1a`
|
||||||
|
|
||||||
|
## 通用响应格式
|
||||||
|
|
||||||
|
所有响应都遵循以下 JSON 格式:
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"type": "响应类型",
|
||||||
|
"success": true/false,
|
||||||
|
"message": "可选的消息描述",
|
||||||
|
...
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## API 列表
|
||||||
|
|
||||||
|
### 1. WiFi 扫描
|
||||||
|
|
||||||
|
扫描周围可用的 WiFi 网络。
|
||||||
|
|
||||||
|
#### 请求
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"command": "scanWiFi"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 响应
|
||||||
|
|
||||||
|
成功时:
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"type": "scanWiFiResult",
|
||||||
|
"success": true,
|
||||||
|
"count": 3,
|
||||||
|
"networks": [
|
||||||
|
{
|
||||||
|
"ssid": "WiFi名称1",
|
||||||
|
"rssi": -45,
|
||||||
|
"channel": 6,
|
||||||
|
"encryption": 3
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ssid": "WiFi名称2",
|
||||||
|
"rssi": -60,
|
||||||
|
"channel": 11,
|
||||||
|
"encryption": 4
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
失败时:
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"type": "scanWiFiResult",
|
||||||
|
"success": false,
|
||||||
|
"message": "未扫描到任何WiFi网络",
|
||||||
|
"networks": [],
|
||||||
|
"count": 0
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 字段说明
|
||||||
|
|
||||||
|
| 字段 | 类型 | 说明 |
|
||||||
|
|------|------|------|
|
||||||
|
| `ssid` | string | WiFi 网络名称 |
|
||||||
|
| `rssi` | number | 信号强度(dBm),值越大信号越强 |
|
||||||
|
| `channel` | number | WiFi 通道号(1-13) |
|
||||||
|
| `encryption` | number | 加密类型:<br>0 = 开放<br>1 = WEP<br>2 = WPA_PSK<br>3 = WPA2_PSK<br>4 = WPA_WPA2_PSK |
|
||||||
|
|
||||||
|
#### 示例
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
// 发送扫描命令
|
||||||
|
const scanCommand = JSON.stringify({ command: "scanWiFi" });
|
||||||
|
await characteristic.writeValue(scanCommand);
|
||||||
|
|
||||||
|
// 接收响应
|
||||||
|
characteristic.addEventListener('characteristicvaluechanged', (event) => {
|
||||||
|
const response = JSON.parse(event.target.value);
|
||||||
|
if (response.type === "scanWiFiResult") {
|
||||||
|
console.log(`扫描到 ${response.count} 个网络`);
|
||||||
|
response.networks.forEach(network => {
|
||||||
|
console.log(`SSID: ${network.ssid}, 信号: ${network.rssi} dBm`);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 2. 获取已保存的 WiFi 网络
|
||||||
|
|
||||||
|
获取设备中已保存的 WiFi 网络列表。
|
||||||
|
|
||||||
|
#### 请求
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"command": "getSavedNetworks"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 响应
|
||||||
|
|
||||||
|
成功时:
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"type": "savedNetworks",
|
||||||
|
"success": true,
|
||||||
|
"count": 2,
|
||||||
|
"networks": [
|
||||||
|
{
|
||||||
|
"ssid": "MyWiFi"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ssid": "OfficeWiFi"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
无保存网络时:
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"type": "savedNetworks",
|
||||||
|
"success": true,
|
||||||
|
"count": 0,
|
||||||
|
"networks": []
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 字段说明
|
||||||
|
|
||||||
|
| 字段 | 类型 | 说明 |
|
||||||
|
|------|------|------|
|
||||||
|
| `ssid` | string | 已保存的 WiFi 网络名称(不包含密码) |
|
||||||
|
|
||||||
|
#### 示例
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
// 发送获取命令
|
||||||
|
const getCommand = JSON.stringify({ command: "getSavedNetworks" });
|
||||||
|
await characteristic.writeValue(getCommand);
|
||||||
|
|
||||||
|
// 接收响应
|
||||||
|
characteristic.addEventListener('characteristicvaluechanged', (event) => {
|
||||||
|
const response = JSON.parse(event.target.value);
|
||||||
|
if (response.type === "savedNetworks") {
|
||||||
|
console.log(`已保存 ${response.count} 个网络`);
|
||||||
|
response.networks.forEach(network => {
|
||||||
|
console.log(`SSID: ${network.ssid}`);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 3. 配置 WiFi
|
||||||
|
|
||||||
|
设置 WiFi 网络配置。
|
||||||
|
|
||||||
|
#### 请求
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"command": "setWiFiConfig",
|
||||||
|
"ssid": "WiFi名称",
|
||||||
|
"password": "WiFi密码"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 响应
|
||||||
|
|
||||||
|
成功时:
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"type": "wifiConfigResult",
|
||||||
|
"success": true,
|
||||||
|
"message": "WiFi配置成功"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
失败时:
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"type": "wifiConfigResult",
|
||||||
|
"success": false,
|
||||||
|
"message": "错误描述"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 错误说明
|
||||||
|
|
||||||
|
| 错误信息 | 原因 | 解决方案 |
|
||||||
|
|---------|--------|---------|
|
||||||
|
| `未扫描到任何WiFi网络,请检查设备位置` | 设备扫描不到任何 WiFi 网络 | 将设备移到更靠近路由器的位置 |
|
||||||
|
| `未找到目标WiFi网络,请检查WiFi名称是否正确` | 扫描结果中不存在目标 WiFi | 检查 WiFi 名称是否拼写正确,确保设备在 WiFi 覆盖范围内 |
|
||||||
|
| `目标WiFi信号过弱,请将设备靠近路由器` | 目标 WiFi 信号强度低于阈值(-200 dBm) | 将设备移到更靠近路由器的位置 |
|
||||||
|
| `WiFi配置失败,请检查密码是否正确` | 密码错误或连接超时 | 检查 WiFi 密码是否正确,确保设备在 WiFi 覆盖范围内 |
|
||||||
|
|
||||||
|
#### 字段说明
|
||||||
|
|
||||||
|
| 字段 | 类型 | 必需 | 说明 |
|
||||||
|
|------|------|--------|------|
|
||||||
|
| `ssid` | string | ✅ | WiFi 网络名称(最多 31 字符) |
|
||||||
|
| `password` | string | ✅ | WiFi 密码(最多 63 字符) |
|
||||||
|
|
||||||
|
#### 示例
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
// 发送配置命令
|
||||||
|
const configCommand = JSON.stringify({
|
||||||
|
command: "setWiFiConfig",
|
||||||
|
ssid: "MyWiFi",
|
||||||
|
password: "mypassword"
|
||||||
|
});
|
||||||
|
await characteristic.writeValue(configCommand);
|
||||||
|
|
||||||
|
// 接收响应
|
||||||
|
characteristic.addEventListener('characteristicvaluechanged', (event) => {
|
||||||
|
const response = JSON.parse(event.target.value);
|
||||||
|
if (response.type === "wifiConfigResult") {
|
||||||
|
if (response.success) {
|
||||||
|
console.log("WiFi 配置成功");
|
||||||
|
} else {
|
||||||
|
console.log("WiFi 配置失败:", response.message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 4. 查询设备状态
|
||||||
|
|
||||||
|
查询设备的当前状态,包括 WiFi 连接状态、设备 ID 等。
|
||||||
|
|
||||||
|
#### 请求
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"command": "queryStatus"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 响应
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"type": "deviceStatus",
|
||||||
|
"success": true,
|
||||||
|
"deviceId": 1001,
|
||||||
|
"wifiConfigured": true,
|
||||||
|
"wifiConnected": true,
|
||||||
|
"ipAddress": "192.168.137.241"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 字段说明
|
||||||
|
|
||||||
|
| 字段 | 类型 | 说明 |
|
||||||
|
|------|------|------|
|
||||||
|
| `deviceId` | number | 设备 ID(1000-1999) |
|
||||||
|
| `wifiConfigured` | boolean | 是否已配置 WiFi |
|
||||||
|
| `wifiConnected` | boolean | WiFi 是否已连接 |
|
||||||
|
| `ipAddress` | string | 设备 IP 地址(已连接时) |
|
||||||
|
|
||||||
|
#### 示例
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
// 发送查询命令
|
||||||
|
const queryCommand = JSON.stringify({ command: "queryStatus" });
|
||||||
|
await characteristic.writeValue(queryCommand);
|
||||||
|
|
||||||
|
// 接收响应
|
||||||
|
characteristic.addEventListener('characteristicvaluechanged', (event) => {
|
||||||
|
const response = JSON.parse(event.target.value);
|
||||||
|
if (response.type === "deviceStatus") {
|
||||||
|
console.log(`设备 ID: ${response.deviceId}`);
|
||||||
|
console.log(`WiFi 已配置: ${response.wifiConfigured}`);
|
||||||
|
console.log(`WiFi 已连接: ${response.wifiConnected}`);
|
||||||
|
console.log(`IP 地址: ${response.ipAddress}`);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 5. 设置设备 ID
|
||||||
|
|
||||||
|
设置设备的唯一标识 ID。
|
||||||
|
|
||||||
|
#### 请求
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"command": "setDeviceId",
|
||||||
|
"newDeviceId": 1001
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 响应
|
||||||
|
|
||||||
|
成功时:
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"type": "setDeviceIdResult",
|
||||||
|
"success": true,
|
||||||
|
"message": "设备ID设置成功",
|
||||||
|
"newDeviceId": 1001
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
失败时:
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"type": "setDeviceIdResult",
|
||||||
|
"success": false,
|
||||||
|
"message": "设备ID超出范围,有效范围: 1000-1999"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 字段说明
|
||||||
|
|
||||||
|
| 字段 | 类型 | 必需 | 说明 |
|
||||||
|
|------|------|--------|------|
|
||||||
|
| `newDeviceId` | number | ✅ | 新设备 ID(1000-1999) |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 6. 启动持续发送
|
||||||
|
|
||||||
|
启动雷达数据的持续发送模式。
|
||||||
|
|
||||||
|
#### 请求
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"command": "startContinuousSend",
|
||||||
|
"interval": 200
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 响应
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"type": "startContinuousSendResult",
|
||||||
|
"success": true,
|
||||||
|
"message": "已启动持续发送模式",
|
||||||
|
"interval": 200
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 字段说明
|
||||||
|
|
||||||
|
| 字段 | 类型 | 必需 | 说明 |
|
||||||
|
|------|------|--------|------|
|
||||||
|
| `interval` | number | ❌ | 发送间隔(毫秒),默认 200ms,范围 100-10000 |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 7. 停止持续发送
|
||||||
|
|
||||||
|
停止雷达数据的持续发送模式。
|
||||||
|
|
||||||
|
#### 请求
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"command": "stopContinuousSend"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 响应
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"type": "stopContinuousSendResult",
|
||||||
|
"success": true,
|
||||||
|
"message": "已停止持续发送模式"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 8. 查询雷达数据
|
||||||
|
|
||||||
|
查询当前的雷达传感器数据。
|
||||||
|
|
||||||
|
#### 请求
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"command": "queryRadarData"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 响应
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"type": "radarData",
|
||||||
|
"success": true,
|
||||||
|
"deviceId": 1001,
|
||||||
|
"timestamp": 1234567890,
|
||||||
|
"presence": 1,
|
||||||
|
"heartRate": 75.5,
|
||||||
|
"breathRate": 16.2,
|
||||||
|
"motion": 0,
|
||||||
|
"heartbeatWaveform": 120,
|
||||||
|
"breathingWaveform": 85,
|
||||||
|
"sleepState": 0
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 字段说明
|
||||||
|
|
||||||
|
| 字段 | 类型 | 说明 |
|
||||||
|
|------|------|------|
|
||||||
|
| `timestamp` | number | 时间戳(毫秒) |
|
||||||
|
| `presence` | number | 是否检测到人体(0=无,1=有) |
|
||||||
|
| `heartRate` | number | 心率(次/分钟) |
|
||||||
|
| `breathRate` | number | 呼吸率(次/分钟) |
|
||||||
|
| `motion` | number | 运动状态(0=静止,1=轻微,2=明显) |
|
||||||
|
| `heartbeatWaveform` | number | 心跳波形值 |
|
||||||
|
| `breathingWaveform` | number | 呼吸波形值 |
|
||||||
|
| `sleepState` | number | 睡眠状态(0=清醒,1=浅睡,2=深睡,3=REM) |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 9. 回显测试
|
||||||
|
|
||||||
|
用于测试 BLE 连接是否正常。
|
||||||
|
|
||||||
|
#### 请求
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"command": "echo",
|
||||||
|
"content": "测试内容"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 响应
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"type": "echoResponse",
|
||||||
|
"originalContent": "测试内容",
|
||||||
|
"receivedSuccessfully": true
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 错误响应
|
||||||
|
|
||||||
|
当请求失败时,设备会返回错误响应:
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"type": "error",
|
||||||
|
"message": "错误描述",
|
||||||
|
"receivedData": "原始请求数据"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## 连接流程
|
||||||
|
|
||||||
|
1. 扫描并连接 BLE 设备
|
||||||
|
2. 开启 notify 订阅
|
||||||
|
3. 发送 `queryStatus` 命令获取设备状态
|
||||||
|
4. 根据需要配置 WiFi 或查询雷达数据
|
||||||
|
|
||||||
|
## 注意事项
|
||||||
|
|
||||||
|
1. **分包传输**: 所有 JSON 数据都会被分包发送(每包 20 字节),客户端需要正确拼接
|
||||||
|
2. **UTF-8 编码**: 确保使用 UTF-8 编码处理中文字符
|
||||||
|
3. **超时处理**: 建议为每个命令设置超时时间(推荐 5-10 秒)
|
||||||
|
4. **设备 ID 范围**: 有效范围为 1000-1999
|
||||||
|
5. **WiFi 限制**: 仅支持 2.4GHz WiFi 网络
|
||||||
|
6. **最大保存网络数**: 最多保存 10 个 WiFi 配置
|
||||||
|
|
||||||
|
## 版本历史
|
||||||
|
|
||||||
|
| 版本 | 日期 | 说明 |
|
||||||
|
|------|------|------|
|
||||||
|
| 1.0 | 2025-02-03 | 初始版本,包含所有基础 API |
|
||||||
36
src/Radar.h
36
src/Radar.h
@@ -1,36 +0,0 @@
|
|||||||
#ifndef RADAR_H
|
|
||||||
#define RADAR_H
|
|
||||||
// 在main.cpp顶部添加R60ABD1协议相关定义
|
|
||||||
#define FRAME_HEADER1 0x53 // 帧头字节1
|
|
||||||
#define FRAME_HEADER2 0x59 // 帧头字节2
|
|
||||||
#define FRAME_TAIL1 0x54 // 帧尾字节1
|
|
||||||
#define FRAME_TAIL2 0x43 // 帧尾字节2
|
|
||||||
|
|
||||||
// 控制字定义
|
|
||||||
#define CTRL_PRESENCE 0x80 // 人体存在检测
|
|
||||||
#define CTRL_BREATH 0x81 // 呼吸检测
|
|
||||||
#define CTRL_SLEEP 0x84 // 睡眠监测
|
|
||||||
#define CTRL_HEARTRATE 0x85 // 心率监测
|
|
||||||
|
|
||||||
// 命令字定义
|
|
||||||
#define CMD_REPORT 0x80 // 主动上报
|
|
||||||
#define CMD_QUERY 0x81 // 查询命令
|
|
||||||
#define CMD_SET 0x82 // 设置命令
|
|
||||||
|
|
||||||
// 定义R60ABD1数据结构
|
|
||||||
typedef struct {
|
|
||||||
uint8_t present; // 有人/无人状态 (DP1)
|
|
||||||
uint16_t distance; // 人体距离 (DP3) 单位cm
|
|
||||||
uint8_t heartRate; // 心率 (DP6) 单位BPM
|
|
||||||
uint8_t breathRate; // 呼吸率 (DP8) 单位次/分钟
|
|
||||||
uint8_t heartWave; // 心率波形 (DP7) 数值+128
|
|
||||||
uint8_t breathWave; // 呼吸波形 (DP10) 数值+128
|
|
||||||
uint8_t sleepState; // 睡眠状态 (DP12)
|
|
||||||
uint32_t sleepTime; // 睡眠时长 (DP13) 单位秒
|
|
||||||
uint8_t sleepScore; // 睡眠质量评分 (DP14)
|
|
||||||
uint8_t bedEntry; // 入床/离床状态 (DP11)
|
|
||||||
uint8_t abnormal; // 异常状态 (DP18)
|
|
||||||
} R60ABD1Data;
|
|
||||||
|
|
||||||
|
|
||||||
#endif
|
|
||||||
2963
src/main.cpp
2963
src/main.cpp
File diff suppressed because it is too large
Load Diff
2582
src/main_backup.cpp.bak
Normal file
2582
src/main_backup.cpp.bak
Normal file
File diff suppressed because it is too large
Load Diff
1721
src/radar_manager.cpp
Normal file
1721
src/radar_manager.cpp
Normal file
File diff suppressed because it is too large
Load Diff
235
src/radar_manager.h
Normal file
235
src/radar_manager.h
Normal file
@@ -0,0 +1,235 @@
|
|||||||
|
#ifndef RADAR_MANAGER_H
|
||||||
|
#define RADAR_MANAGER_H
|
||||||
|
|
||||||
|
#include <Arduino.h>
|
||||||
|
#include <ArduinoJson.h>
|
||||||
|
#include <BLEDevice.h>
|
||||||
|
#include <BLEServer.h>
|
||||||
|
#include <BLEUtils.h>
|
||||||
|
#include <BLE2902.h>
|
||||||
|
#include <HTTPClient.h>
|
||||||
|
#include <freertos/FreeRTOS.h>
|
||||||
|
#include <freertos/task.h>
|
||||||
|
#include <freertos/queue.h>
|
||||||
|
#include <Preferences.h>
|
||||||
|
|
||||||
|
class WiFiManager;
|
||||||
|
|
||||||
|
#define SERVICE_UUID "a8c1e5c0-3d5d-4a9d-8d5e-7c8b6a4e2f1a" // BLE服务UUID
|
||||||
|
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8" // BLE特征值UUID
|
||||||
|
#define UART_RX_BUFFER_SIZE 4096 // UART接收缓冲区大小
|
||||||
|
#define QUEUE_SIZE 50 // 队列大小
|
||||||
|
#define TASK_STACK_SIZE 8192 // 任务堆栈大小
|
||||||
|
|
||||||
|
#define FRAME_HEADER1 0x53 // 帧头字节1
|
||||||
|
#define FRAME_HEADER2 0x59 // 帧头字节2
|
||||||
|
#define FRAME_TAIL1 0x54 // 帧尾字节1
|
||||||
|
#define FRAME_TAIL2 0x43 // 帧尾字节2
|
||||||
|
|
||||||
|
// 控制字定义
|
||||||
|
#define CTRL_PRESENCE 0x80 // 人体存在检测
|
||||||
|
#define CTRL_BREATH 0x81 // 呼吸检测
|
||||||
|
#define CTRL_SLEEP 0x84 // 睡眠监测
|
||||||
|
#define CTRL_HEARTRATE 0x85 // 心率监测
|
||||||
|
|
||||||
|
// 命令字定义
|
||||||
|
#define CMD_REPORT 0x80 // 主动上报
|
||||||
|
#define CMD_QUERY 0x81 // 查询命令
|
||||||
|
#define CMD_SET 0x82 // 设置命令
|
||||||
|
|
||||||
|
// 定义R60ABD1数据结构
|
||||||
|
typedef struct {
|
||||||
|
uint8_t present; // 有人/无人状态 (DP1)
|
||||||
|
uint16_t distance; // 人体距离 (DP3) 单位cm
|
||||||
|
uint8_t heartRate; // 心率 (DP6) 单位BPM
|
||||||
|
uint8_t breathRate; // 呼吸率 (DP8) 单位次/分钟
|
||||||
|
uint8_t heartWave; // 心率波形 (DP7) 数值+128
|
||||||
|
uint8_t breathWave; // 呼吸波形 (DP10) 数值+128
|
||||||
|
uint8_t sleepState; // 睡眠状态 (DP12)
|
||||||
|
uint32_t sleepTime; // 睡眠时长 (DP13) 单位秒
|
||||||
|
uint8_t sleepScore; // 睡眠质量评分 (DP14)
|
||||||
|
uint8_t bedEntry; // 入床/离床状态 (DP11)
|
||||||
|
uint8_t abnormal; // 异常状态 (DP18)
|
||||||
|
} R60ABD1Data; // R60ABD1雷达数据结构体
|
||||||
|
|
||||||
|
typedef struct { // 传感器数据结构体
|
||||||
|
float breath_rate; // 呼吸率
|
||||||
|
float heart_rate; // 心率
|
||||||
|
uint8_t breath_valid; // 呼吸率有效标志
|
||||||
|
uint8_t heart_valid; // 心率有效标志
|
||||||
|
uint8_t presence; // 存在状态
|
||||||
|
uint8_t motion; // 运动状态
|
||||||
|
int heartbeat_waveform; // 心跳波形
|
||||||
|
int breathing_waveform; // 呼吸波形
|
||||||
|
uint16_t distance; // 距离
|
||||||
|
uint8_t body_movement; // 身体运动
|
||||||
|
uint8_t breath_status; // 呼吸状态
|
||||||
|
uint8_t sleep_state; // 睡眠状态
|
||||||
|
uint32_t sleep_time; // 睡眠时长
|
||||||
|
uint8_t sleep_score; // 睡眠评分
|
||||||
|
uint8_t sleep_grade; // 睡眠等级
|
||||||
|
uint8_t bed_entry; // 入床状态
|
||||||
|
uint8_t abnormal_state; // 异常状态
|
||||||
|
uint8_t avg_heart_rate; // 平均心率
|
||||||
|
uint8_t avg_breath_rate; // 平均呼吸率
|
||||||
|
uint8_t turn_count; // 翻身次数
|
||||||
|
uint8_t large_move_ratio; // 大幅运动比例
|
||||||
|
uint8_t small_move_ratio; // 小幅运动比例
|
||||||
|
int16_t pos_x; // X坐标
|
||||||
|
int16_t pos_y; // Y坐标
|
||||||
|
int16_t pos_z; // Z坐标
|
||||||
|
int8_t breath_waveform[5]; // 呼吸波形数组
|
||||||
|
int8_t heart_waveform[5]; // 心跳波形数组
|
||||||
|
uint16_t deep_sleep_time; // 深度睡眠时长
|
||||||
|
uint16_t light_sleep_time; // 浅度睡眠时长
|
||||||
|
uint16_t awake_time; // 清醒时长
|
||||||
|
uint16_t sleep_total_time; // 总睡眠时长
|
||||||
|
uint8_t deep_sleep_ratio; // 深度睡眠比例
|
||||||
|
uint8_t light_sleep_ratio; // 浅度睡眠比例
|
||||||
|
uint8_t awake_ratio; // 清醒比例
|
||||||
|
uint8_t turnover_count; // 翻身计数
|
||||||
|
uint8_t struggle_alert; // 挣扎警报
|
||||||
|
uint8_t no_one_alert; // 无人警报
|
||||||
|
uint8_t bed_status; // 床状态
|
||||||
|
uint8_t bed_Out_Time; // 离床时间
|
||||||
|
uint8_t apnea_count; // 呼吸暂停次数
|
||||||
|
} SensorData; // 传感器数据结构体
|
||||||
|
|
||||||
|
typedef struct { // 相位数据结构体
|
||||||
|
int heartbeat_waveform; // 心跳波形
|
||||||
|
int breathing_waveform; // 呼吸波形
|
||||||
|
} PhaseData; // 相位数据结构体
|
||||||
|
|
||||||
|
typedef struct { // 生命体征数据结构体
|
||||||
|
float heart_rate; // 心率
|
||||||
|
float breath_rate; // 呼吸率
|
||||||
|
uint8_t presence; // 存在状态
|
||||||
|
uint8_t motion; // 运动状态
|
||||||
|
uint16_t distance; // 距离
|
||||||
|
uint8_t sleep_state; // 睡眠状态
|
||||||
|
uint8_t sleep_score; // 睡眠评分
|
||||||
|
uint8_t body_movement; // 身体运动
|
||||||
|
uint8_t breath_status; // 呼吸状态
|
||||||
|
uint32_t sleep_time; // 睡眠时长
|
||||||
|
uint8_t bed_entry; // 入床状态
|
||||||
|
uint8_t abnormal_state; // 异常状态
|
||||||
|
uint8_t avg_heart_rate; // 平均心率
|
||||||
|
uint8_t avg_breath_rate; // 平均呼吸率
|
||||||
|
uint8_t turn_count; // 翻身次数
|
||||||
|
uint8_t large_move_ratio; // 大幅运动比例
|
||||||
|
uint8_t small_move_ratio; // 小幅运动比例
|
||||||
|
int16_t pos_x; // X坐标
|
||||||
|
int16_t pos_y; // Y坐标
|
||||||
|
int16_t pos_z; // Z坐标
|
||||||
|
uint16_t deep_sleep_time; // 深度睡眠时长
|
||||||
|
uint16_t light_sleep_time; // 浅度睡眠时长
|
||||||
|
uint16_t awake_time; // 清醒时长
|
||||||
|
uint16_t sleep_total_time; // 总睡眠时长
|
||||||
|
uint8_t deep_sleep_ratio; // 深度睡眠比例
|
||||||
|
uint8_t light_sleep_ratio; // 浅度睡眠比例
|
||||||
|
uint8_t awake_ratio; // 清醒比例
|
||||||
|
uint8_t turnover_count; // 翻身计数
|
||||||
|
uint8_t struggle_alert; // 挣扎警报
|
||||||
|
uint8_t no_one_alert; // 无人警报
|
||||||
|
uint8_t bed_status; // 床状态
|
||||||
|
uint8_t apnea_count; // 呼吸暂停次数
|
||||||
|
int heartbeat_waveform; // 心跳波形
|
||||||
|
int breathing_waveform; // 呼吸波形
|
||||||
|
} VitalData; // 生命体征数据结构体
|
||||||
|
|
||||||
|
typedef struct { // 上次发送数据结构体
|
||||||
|
float heart_rate; // 心率
|
||||||
|
float breath_rate; // 呼吸率
|
||||||
|
uint8_t presence; // 存在状态
|
||||||
|
uint8_t motion; // 运动状态
|
||||||
|
uint8_t sleep_state; // 睡眠状态
|
||||||
|
} LastSentData; // 上次发送数据结构体
|
||||||
|
|
||||||
|
class BLEFlowController { // BLE流控制器类
|
||||||
|
private:
|
||||||
|
size_t maxBytesPerSecond; // 最大每秒发送字节数
|
||||||
|
size_t bytesSent; // 已发送字节数
|
||||||
|
unsigned long lastResetTime; // 上次重置时间
|
||||||
|
unsigned long lastSendTime; // 上次发送时间
|
||||||
|
|
||||||
|
public:
|
||||||
|
BLEFlowController(size_t maxBps);
|
||||||
|
bool canSend(size_t dataSize);
|
||||||
|
bool check();
|
||||||
|
void recordSend(size_t dataSize);
|
||||||
|
void reset();
|
||||||
|
};
|
||||||
|
|
||||||
|
extern SensorData sensorData; // 传感器数据
|
||||||
|
extern HardwareSerial mySerial1; // 硬件串口1
|
||||||
|
extern QueueHandle_t phaseDataQueue; // 相位数据队列
|
||||||
|
extern QueueHandle_t vitalDataQueue; // 生命体征数据队列
|
||||||
|
extern QueueHandle_t uartQueue; // UART数据队列
|
||||||
|
extern TaskHandle_t bleSendTaskHandle; // BLE发送任务句柄
|
||||||
|
extern TaskHandle_t vitalSendTaskHandle; // 生命体征发送任务句柄
|
||||||
|
extern TaskHandle_t uartProcessTaskHandle; // UART处理任务句柄
|
||||||
|
extern BLEServer* pServer; // BLE服务器指针
|
||||||
|
extern BLECharacteristic* pCharacteristic; // BLE特征值指针
|
||||||
|
extern bool deviceConnected; // 设备连接状态
|
||||||
|
extern bool oldDeviceConnected; // 旧设备连接状态
|
||||||
|
extern String receivedData; // 接收到的数据
|
||||||
|
extern String completeData; // 完整数据
|
||||||
|
extern unsigned long lastReceiveTime; // 上次接收数据时间
|
||||||
|
extern bool continuousSendEnabled; // 持续发送使能标志
|
||||||
|
extern unsigned long continuousSendInterval; // 持续发送间隔
|
||||||
|
extern BLEFlowController bleFlow; // BLE流控制器
|
||||||
|
extern unsigned long lastSensorUpdate; // 上次传感器更新时间
|
||||||
|
extern LastSentData lastSentData; // 上次发送的数据
|
||||||
|
extern unsigned long lastCheckTime; // 上次检测时间
|
||||||
|
|
||||||
|
extern uint16_t currentDeviceId; // 当前设备ID
|
||||||
|
extern Preferences preferences; // Flash存储对象
|
||||||
|
extern WiFiManager wifiManager; // WiFi管理器
|
||||||
|
|
||||||
|
void initRadarManager();
|
||||||
|
void initR60ABD1();
|
||||||
|
bool parseR60ABD1Frame(uint8_t *frame, uint16_t frameLen);
|
||||||
|
int16_t parseSignedCoordinate(uint16_t raw_value);
|
||||||
|
void sendRadarCommand(uint8_t ctrl, uint8_t cmd, uint8_t value);
|
||||||
|
void IRAM_ATTR serialRxCallback();
|
||||||
|
|
||||||
|
void bleSendTask(void *parameter);
|
||||||
|
void vitalSendTask(void *parameter);
|
||||||
|
void radarDataTask(void *parameter);
|
||||||
|
void uartProcessTask(void *parameter);
|
||||||
|
|
||||||
|
void sendDataInChunks(const String& data);
|
||||||
|
void sendJSONDataToBLE(const String& jsonData);
|
||||||
|
bool sendCustomJSONData(const String& jsonType, const String& jsonString);
|
||||||
|
void sendRadarDataToBLE();
|
||||||
|
|
||||||
|
bool processQueryRadarData(JsonDocument& doc);
|
||||||
|
bool processStartContinuousSend(JsonDocument& doc);
|
||||||
|
bool processStopContinuousSend(JsonDocument& doc);
|
||||||
|
void processBLEConfig();
|
||||||
|
|
||||||
|
bool processSetDeviceId(JsonDocument& doc);
|
||||||
|
bool processQueryStatus(JsonDocument& doc);
|
||||||
|
bool processWiFiConfigCommand(JsonDocument& doc);
|
||||||
|
bool processScanWiFi(JsonDocument& doc);
|
||||||
|
bool processGetSavedNetworks(JsonDocument& doc);
|
||||||
|
bool processEchoRequest(JsonDocument& doc);
|
||||||
|
void sendRawEchoResponse(const String& rawData);
|
||||||
|
void sendStatusToBLE();
|
||||||
|
|
||||||
|
void loadDeviceId();
|
||||||
|
void saveDeviceId();
|
||||||
|
|
||||||
|
void sendDailyDataToInfluxDB(String dailyDataLine);
|
||||||
|
void sendSleepDataToInfluxDB();
|
||||||
|
|
||||||
|
class MyServerCallbacks: public BLEServerCallbacks {
|
||||||
|
void onConnect(BLEServer* pServer);
|
||||||
|
void onDisconnect(BLEServer* pServer);
|
||||||
|
};
|
||||||
|
|
||||||
|
class MyCallbacks: public BLECharacteristicCallbacks {
|
||||||
|
void onWrite(BLECharacteristic *pCharacteristic);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
572
src/wifi_manager.cpp
Normal file
572
src/wifi_manager.cpp
Normal file
@@ -0,0 +1,572 @@
|
|||||||
|
#include "wifi_manager.h"
|
||||||
|
|
||||||
|
// 外部变量和函数声明
|
||||||
|
extern bool deviceConnected;
|
||||||
|
void sendJSONDataToBLE(const String& jsonData);
|
||||||
|
void setNetworkStatus(NetworkStatus status);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief WiFi管理器构造函数
|
||||||
|
* 初始化WiFi管理器的成员变量
|
||||||
|
*/
|
||||||
|
WiFiManager::WiFiManager() {
|
||||||
|
savedNetworkCount = 0;
|
||||||
|
currentState = WIFI_IDLE;
|
||||||
|
lastReconnectAttempt = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 初始化WiFi管理器
|
||||||
|
* 开启Preferences存储,并加载保存的WiFi配置
|
||||||
|
*/
|
||||||
|
void WiFiManager::begin() {
|
||||||
|
preferences.begin("wifi_manager", false);
|
||||||
|
loadWiFiConfigs();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 加载保存的WiFi配置
|
||||||
|
* 从Flash中读取之前保存的WiFi网络配置
|
||||||
|
* @return 是否成功加载到配置
|
||||||
|
*/
|
||||||
|
bool WiFiManager::loadWiFiConfigs() {
|
||||||
|
savedNetworkCount = 0;
|
||||||
|
|
||||||
|
for (int i = 0; i < MAX_WIFI_NETWORKS; i++) {
|
||||||
|
String key = "wifi_" + String(i);
|
||||||
|
String configStr = preferences.getString(key.c_str(), "");
|
||||||
|
|
||||||
|
if (configStr.length() > 0) {
|
||||||
|
JsonDocument doc;
|
||||||
|
DeserializationError error = deserializeJson(doc, configStr);
|
||||||
|
|
||||||
|
if (!error) {
|
||||||
|
const char* ssid = doc["ssid"];
|
||||||
|
const char* password = doc["password"];
|
||||||
|
|
||||||
|
if (ssid && password) {
|
||||||
|
strncpy(savedNetworks[savedNetworkCount].ssid, ssid, 31);
|
||||||
|
savedNetworks[savedNetworkCount].ssid[31] = '\0';
|
||||||
|
strncpy(savedNetworks[savedNetworkCount].password, password, 63);
|
||||||
|
savedNetworks[savedNetworkCount].password[63] = '\0';
|
||||||
|
savedNetworkCount++;
|
||||||
|
|
||||||
|
Serial.printf("📶 加载WiFi配置 %d: %s\n", savedNetworkCount, ssid);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Serial.printf("✅ 共加载 %d 个WiFi配置\n", savedNetworkCount);
|
||||||
|
return savedNetworkCount > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 保存WiFi配置
|
||||||
|
* 将WiFi网络配置保存到Flash中
|
||||||
|
* @param ssid WiFi网络名称
|
||||||
|
* @param password WiFi网络密码
|
||||||
|
* @return 是否保存成功
|
||||||
|
*/
|
||||||
|
bool WiFiManager::saveWiFiConfig(const char* ssid, const char* password) {
|
||||||
|
// 检查是否已存在该网络配置
|
||||||
|
for (int i = 0; i < savedNetworkCount; i++) {
|
||||||
|
if (strcmp(savedNetworks[i].ssid, ssid) == 0) {
|
||||||
|
// 更新现有配置
|
||||||
|
strncpy(savedNetworks[i].password, password, 63);
|
||||||
|
savedNetworks[i].password[63] = '\0';
|
||||||
|
|
||||||
|
JsonDocument doc;
|
||||||
|
doc["ssid"] = ssid;
|
||||||
|
doc["password"] = password;
|
||||||
|
String configStr;
|
||||||
|
serializeJson(doc, configStr);
|
||||||
|
|
||||||
|
String key = "wifi_" + String(i);
|
||||||
|
preferences.putString(key.c_str(), configStr);
|
||||||
|
|
||||||
|
Serial.printf("🔄 更新WiFi配置: %s\n", ssid);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 添加新配置
|
||||||
|
if (savedNetworkCount < MAX_WIFI_NETWORKS) {
|
||||||
|
strncpy(savedNetworks[savedNetworkCount].ssid, ssid, 31);
|
||||||
|
savedNetworks[savedNetworkCount].ssid[31] = '\0';
|
||||||
|
strncpy(savedNetworks[savedNetworkCount].password, password, 63);
|
||||||
|
savedNetworks[savedNetworkCount].password[63] = '\0';
|
||||||
|
|
||||||
|
JsonDocument doc;
|
||||||
|
doc["ssid"] = ssid;
|
||||||
|
doc["password"] = password;
|
||||||
|
String configStr;
|
||||||
|
serializeJson(doc, configStr);
|
||||||
|
|
||||||
|
String key = "wifi_" + String(savedNetworkCount);
|
||||||
|
preferences.putString(key.c_str(), configStr);
|
||||||
|
savedNetworkCount++;
|
||||||
|
|
||||||
|
Serial.printf("➕ 新增WiFi配置: %s (总计: %d)\n", ssid, savedNetworkCount);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
Serial.println("❌ WiFi配置已满,无法添加");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 连接到指定WiFi网络
|
||||||
|
* 尝试连接到给定的WiFi网络
|
||||||
|
* @param ssid WiFi网络名称
|
||||||
|
* @param password WiFi网络密码
|
||||||
|
* @return 是否连接成功
|
||||||
|
*/
|
||||||
|
bool WiFiManager::connectToNetwork(const char* ssid, const char* password) {
|
||||||
|
Serial.printf("🌐 [WiFi] 尝试连接到 SSID: %s\n", ssid);
|
||||||
|
|
||||||
|
currentState = WIFI_CONNECTING;
|
||||||
|
setNetworkStatus(NET_CONNECTING);
|
||||||
|
|
||||||
|
WiFi.mode(WIFI_STA);
|
||||||
|
WiFi.begin(ssid, password);
|
||||||
|
|
||||||
|
unsigned long startTime = millis();
|
||||||
|
unsigned long lastStatusPrint = 0;
|
||||||
|
|
||||||
|
// 等待连接成功或超时
|
||||||
|
while (WiFi.status() != WL_CONNECTED && (millis() - startTime) < WIFI_CONNECT_TIMEOUT) {
|
||||||
|
if (millis() - lastStatusPrint >= 500) {
|
||||||
|
Serial.printf("[WiFi] 连接中,状态: %d\n", WiFi.status());
|
||||||
|
lastStatusPrint = millis();
|
||||||
|
}
|
||||||
|
yield();
|
||||||
|
vTaskDelay(50 / portTICK_PERIOD_MS);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (WiFi.status() == WL_CONNECTED) {
|
||||||
|
Serial.println("✅ [WiFi] 连接成功!");
|
||||||
|
Serial.printf("🌐 IP地址: %s\n", WiFi.localIP().toString().c_str());
|
||||||
|
Serial.printf("🔒 信号强度: %d dBm\n", WiFi.RSSI());
|
||||||
|
|
||||||
|
currentState = WIFI_CONNECTED;
|
||||||
|
setNetworkStatus(NET_CONNECTED);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
Serial.println("❌ [WiFi] 连接超时");
|
||||||
|
currentState = WIFI_DISCONNECTED;
|
||||||
|
setNetworkStatus(NET_DISCONNECTED);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 扫描并匹配WiFi网络
|
||||||
|
* 扫描附近的WiFi网络,并尝试匹配已保存的配置
|
||||||
|
* 如果找到多个匹配的网络,优先连接信号强度最强的那一组
|
||||||
|
* @return 是否成功连接到匹配的网络
|
||||||
|
*/
|
||||||
|
bool WiFiManager::scanAndMatchNetworks() {
|
||||||
|
Serial.println("🔍 [WiFi] 开始扫描WiFi网络...");
|
||||||
|
currentState = WIFI_SCANNING;
|
||||||
|
|
||||||
|
WiFi.disconnect(true);
|
||||||
|
vTaskDelay(100 / portTICK_PERIOD_MS);
|
||||||
|
WiFi.mode(WIFI_STA);
|
||||||
|
vTaskDelay(100 / portTICK_PERIOD_MS);
|
||||||
|
|
||||||
|
int n = WiFi.scanNetworks();
|
||||||
|
Serial.printf("🔍 扫描到 %d 个WiFi网络\n", n);
|
||||||
|
|
||||||
|
if (n <= 0) {
|
||||||
|
Serial.println("❌ 未扫描到任何WiFi网络或扫描失败");
|
||||||
|
currentState = WIFI_DISCONNECTED;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 收集所有匹配的、信号强度符合要求的网络
|
||||||
|
struct {
|
||||||
|
const char* ssid;
|
||||||
|
const char* password;
|
||||||
|
int rssi;
|
||||||
|
} bestNetwork = {nullptr, nullptr, -1000}; // 初始化为非常弱的信号
|
||||||
|
|
||||||
|
// 遍历已保存的网络,寻找匹配的网络
|
||||||
|
for (int i = 0; i < savedNetworkCount; i++) {
|
||||||
|
for (int j = 0; j < n; j++) {
|
||||||
|
if (WiFi.SSID(j) == String(savedNetworks[i].ssid)) {
|
||||||
|
int rssi = WiFi.RSSI(j);
|
||||||
|
Serial.printf("📶 找到匹配网络: %s, 信号: %d dBm\n",
|
||||||
|
savedNetworks[i].ssid, rssi);
|
||||||
|
|
||||||
|
// 检查信号强度是否符合要求
|
||||||
|
if (rssi >= MIN_RSSI_THRESHOLD) {
|
||||||
|
// 检查是否是当前找到的信号最强的网络
|
||||||
|
if (rssi > bestNetwork.rssi) {
|
||||||
|
bestNetwork.ssid = savedNetworks[i].ssid;
|
||||||
|
bestNetwork.password = savedNetworks[i].password;
|
||||||
|
bestNetwork.rssi = rssi;
|
||||||
|
Serial.printf("📈 更新最佳网络: %s, 信号: %d dBm\n",
|
||||||
|
bestNetwork.ssid, bestNetwork.rssi);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Serial.printf("⚠️ 信号强度过低,跳过\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果找到最佳网络,尝试连接
|
||||||
|
if (bestNetwork.ssid != nullptr) {
|
||||||
|
Serial.printf("✅ 选择信号最强的网络: %s, 信号: %d dBm\n",
|
||||||
|
bestNetwork.ssid, bestNetwork.rssi);
|
||||||
|
if (connectToNetwork(bestNetwork.ssid, bestNetwork.password)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Serial.println("❌ 未找到匹配的WiFi网络或信号过弱");
|
||||||
|
currentState = WIFI_DISCONNECTED;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 初始化WiFi连接
|
||||||
|
* 启动WiFi初始化过程,尝试连接到已保存的网络
|
||||||
|
* @return 是否初始化成功
|
||||||
|
*/
|
||||||
|
bool WiFiManager::initializeWiFi() {
|
||||||
|
Serial.println("🚀 [WiFi] 初始化WiFi连接...");
|
||||||
|
|
||||||
|
if (savedNetworkCount == 0) {
|
||||||
|
Serial.println("⚠️ 未保存的WiFi配置");
|
||||||
|
currentState = WIFI_IDLE;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (scanAndMatchNetworks()) {
|
||||||
|
Serial.println("✅ WiFi初始化成功");
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
Serial.println("❌ WiFi初始化失败");
|
||||||
|
currentState = WIFI_DISCONNECTED;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 扫描WiFi网络并发送结果
|
||||||
|
* 扫描附近的WiFi网络,过滤信号弱的网络,将结果通过BLE发送给客户端
|
||||||
|
*/
|
||||||
|
void WiFiManager::scanAndSendResults() {
|
||||||
|
Serial.println("📱 [BLE-WiFi] 开始WiFi扫描...");
|
||||||
|
|
||||||
|
WiFi.disconnect(true);
|
||||||
|
vTaskDelay(100 / portTICK_PERIOD_MS);
|
||||||
|
WiFi.mode(WIFI_STA);
|
||||||
|
vTaskDelay(100 / portTICK_PERIOD_MS);
|
||||||
|
|
||||||
|
int n = WiFi.scanNetworks();
|
||||||
|
Serial.printf("🔍 扫描到 %d 个WiFi网络\n", n);
|
||||||
|
|
||||||
|
if (n <= 0) {
|
||||||
|
Serial.println("❌ 未扫描到任何WiFi网络或扫描失败");
|
||||||
|
if (deviceConnected) {
|
||||||
|
String errorMsg = String("{\"type\":\"scanWiFiResult\",\"success\":false,\"message\":\"未扫描到任何WiFi网络或扫描失败\",\"networks\":[],\"count\":0}");
|
||||||
|
sendJSONDataToBLE(errorMsg);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 构建WiFi网络列表的JSON数据
|
||||||
|
String wifiList = String("{\"type\":\"scanWiFiResult\",\"success\":true,\"count\":") + String(n) + String(",\"networks\":[");
|
||||||
|
|
||||||
|
bool first = true;
|
||||||
|
for (int i = 0; i < n; ++i) {
|
||||||
|
if (WiFi.RSSI(i) >= MIN_RSSI_THRESHOLD) {
|
||||||
|
if (!first) {
|
||||||
|
wifiList += ",";
|
||||||
|
}
|
||||||
|
wifiList += String("{\"ssid\":\"") + WiFi.SSID(i) + String("\",\"rssi\":") +
|
||||||
|
String(WiFi.RSSI(i)) + String(",\"channel\":") +
|
||||||
|
String(WiFi.channel(i)) + String(",\"encryption\":");
|
||||||
|
|
||||||
|
// 根据加密类型添加相应的描述
|
||||||
|
switch (WiFi.encryptionType(i)) {
|
||||||
|
case WIFI_AUTH_OPEN:
|
||||||
|
wifiList += String("\"open\"");
|
||||||
|
break;
|
||||||
|
case WIFI_AUTH_WEP:
|
||||||
|
wifiList += String("\"WEP\"");
|
||||||
|
break;
|
||||||
|
case WIFI_AUTH_WPA_PSK:
|
||||||
|
wifiList += String("\"WPA\"");
|
||||||
|
break;
|
||||||
|
case WIFI_AUTH_WPA2_PSK:
|
||||||
|
wifiList += String("\"WPA2\"");
|
||||||
|
break;
|
||||||
|
case WIFI_AUTH_WPA_WPA2_PSK:
|
||||||
|
wifiList += String("\"WPA/WPA2\"");
|
||||||
|
break;
|
||||||
|
case WIFI_AUTH_WPA2_ENTERPRISE:
|
||||||
|
wifiList += String("\"WPA2-EAP\"");
|
||||||
|
break;
|
||||||
|
case WIFI_AUTH_WPA3_PSK:
|
||||||
|
wifiList += String("\"WPA3\"");
|
||||||
|
break;
|
||||||
|
case WIFI_AUTH_WPA2_WPA3_PSK:
|
||||||
|
wifiList += String("\"WPA2/WPA3\"");
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
wifiList += String("\"unknown\"");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
wifiList += "}";
|
||||||
|
first = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
wifiList += "]}";
|
||||||
|
|
||||||
|
Serial.printf("✅ 发送WiFi扫描结果,包含 %d 个可用网络\n", first ? 0 : n);
|
||||||
|
|
||||||
|
if (deviceConnected) {
|
||||||
|
sendJSONDataToBLE(wifiList);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 开始配网模式
|
||||||
|
* 进入配网模式,扫描WiFi网络并发送结果给客户端
|
||||||
|
* @return 是否成功进入配网模式
|
||||||
|
*/
|
||||||
|
bool WiFiManager::startConfiguration() {
|
||||||
|
Serial.println("⚙️ [WiFi] 开始配网模式...");
|
||||||
|
currentState = WIFI_CONFIGURING;
|
||||||
|
scanAndSendResults();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 处理配网数据
|
||||||
|
* 处理从客户端收到的WiFi配网信息,先扫描WiFi是否有匹配的网络,再尝试连接并保存
|
||||||
|
* @param ssid WiFi网络名称
|
||||||
|
* @param password WiFi网络密码
|
||||||
|
* @return 是否配置成功
|
||||||
|
*/
|
||||||
|
bool WiFiManager::handleConfigurationData(const char* ssid, const char* password) {
|
||||||
|
Serial.printf("📱 [BLE-WiFi] 收到配网信息: SSID='%s'\n", ssid);
|
||||||
|
|
||||||
|
if (ssid == nullptr || password == nullptr || strlen(ssid) == 0) {
|
||||||
|
Serial.println("❌ 配网参数无效");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 先扫描WiFi网络,检查是否存在匹配的网络
|
||||||
|
Serial.println("🔍 [WiFi] 扫描WiFi网络,检查是否存在匹配的网络...");
|
||||||
|
int n = WiFi.scanNetworks();
|
||||||
|
Serial.printf("🔍 扫描到 %d 个WiFi网络\n", n);
|
||||||
|
|
||||||
|
if (n == 0) {
|
||||||
|
Serial.println("❌ 未扫描到任何WiFi网络");
|
||||||
|
|
||||||
|
if (deviceConnected) {
|
||||||
|
String resultMsg = String("{\"type\":\"wifiConfigResult\",\"success\":false,\"message\":\"未扫描到任何WiFi网络,请检查设备位置\"}");
|
||||||
|
sendJSONDataToBLE(resultMsg);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool networkFound = false;
|
||||||
|
bool signalTooWeak = false;
|
||||||
|
int foundRssi = 0;
|
||||||
|
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
if (WiFi.SSID(i) == String(ssid)) {
|
||||||
|
foundRssi = WiFi.RSSI(i);
|
||||||
|
Serial.printf("📶 找到匹配网络: %s, 信号: %d dBm\n", ssid, foundRssi);
|
||||||
|
|
||||||
|
if (foundRssi >= MIN_RSSI_THRESHOLD) {
|
||||||
|
Serial.printf("✅ 信号强度符合要求,准备连接...\n");
|
||||||
|
networkFound = true;
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
Serial.printf("⚠️ 信号强度过低,跳过\n");
|
||||||
|
signalTooWeak = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!networkFound) {
|
||||||
|
String errorMsg;
|
||||||
|
|
||||||
|
if (signalTooWeak) {
|
||||||
|
errorMsg = String("{\"type\":\"wifiConfigResult\",\"success\":false,\"message\":\"目标WiFi信号过弱,请将设备靠近路由器\"}");
|
||||||
|
Serial.printf("❌ 目标WiFi信号过弱: %d dBm (阈值: %d dBm)\n", foundRssi, MIN_RSSI_THRESHOLD);
|
||||||
|
} else {
|
||||||
|
errorMsg = String("{\"type\":\"wifiConfigResult\",\"success\":false,\"message\":\"未找到目标WiFi网络,请检查WiFi名称是否正确\"}");
|
||||||
|
Serial.println("❌ 未找到目标WiFi网络");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (deviceConnected) {
|
||||||
|
sendJSONDataToBLE(errorMsg);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 尝试连接到指定网络
|
||||||
|
if (connectToNetwork(ssid, password)) {
|
||||||
|
// 连接成功后保存配置
|
||||||
|
if (saveWiFiConfig(ssid, password)) {
|
||||||
|
Serial.println("✅ WiFi配置成功并已保存");
|
||||||
|
|
||||||
|
if (deviceConnected) {
|
||||||
|
String resultMsg = String("{\"type\":\"wifiConfigResult\",\"success\":true,\"message\":\"WiFi配置成功\"}");
|
||||||
|
sendJSONDataToBLE(resultMsg);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Serial.println("❌ WiFi配置失败");
|
||||||
|
|
||||||
|
if (deviceConnected) {
|
||||||
|
String resultMsg = String("{\"type\":\"wifiConfigResult\",\"success\":false,\"message\":\"WiFi配置失败,请检查密码是否正确\"}");
|
||||||
|
sendJSONDataToBLE(resultMsg);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 处理WiFi重连
|
||||||
|
* 检查WiFi连接状态,当断开连接时尝试重连
|
||||||
|
*/
|
||||||
|
void WiFiManager::handleReconnect() {
|
||||||
|
// 检查当前是否已连接
|
||||||
|
if (currentState == WIFI_CONNECTED) {
|
||||||
|
if (WiFi.status() == WL_CONNECTED) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// 连接已断开
|
||||||
|
currentState = WIFI_DISCONNECTED;
|
||||||
|
setNetworkStatus(NET_DISCONNECTED);
|
||||||
|
Serial.println("⚠️ WiFi连接断开");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 处理重连逻辑
|
||||||
|
if (currentState == WIFI_DISCONNECTED) {
|
||||||
|
unsigned long currentTime = millis();
|
||||||
|
|
||||||
|
// 按照设定的间隔尝试重连
|
||||||
|
if (currentTime - lastReconnectAttempt >= WIFI_RECONNECT_INTERVAL) {
|
||||||
|
lastReconnectAttempt = currentTime;
|
||||||
|
|
||||||
|
if (scanAndMatchNetworks()) {
|
||||||
|
Serial.println("✅ WiFi重连成功");
|
||||||
|
} else {
|
||||||
|
Serial.println("❌ WiFi重连失败,2秒后重试");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 获取当前WiFi状态
|
||||||
|
* @return 当前的WiFi管理器状态
|
||||||
|
*/
|
||||||
|
WiFiManagerState WiFiManager::getState() {
|
||||||
|
return currentState;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 检查WiFi是否已连接
|
||||||
|
* @return WiFi是否已成功连接
|
||||||
|
*/
|
||||||
|
bool WiFiManager::isConnected() {
|
||||||
|
return currentState == WIFI_CONNECTED && WiFi.status() == WL_CONNECTED;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 断开WiFi连接
|
||||||
|
* 主动断开当前的WiFi连接
|
||||||
|
*/
|
||||||
|
void WiFiManager::disconnect() {
|
||||||
|
WiFi.disconnect(true);
|
||||||
|
currentState = WIFI_DISCONNECTED;
|
||||||
|
setNetworkStatus(NET_DISCONNECTED);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 添加WiFi配置
|
||||||
|
* 向保存的配置中添加新的WiFi网络
|
||||||
|
* @param ssid WiFi网络名称
|
||||||
|
* @param password WiFi网络密码
|
||||||
|
* @return 是否添加成功
|
||||||
|
*/
|
||||||
|
bool WiFiManager::addWiFiConfig(const char* ssid, const char* password) {
|
||||||
|
return saveWiFiConfig(ssid, password);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 清除所有WiFi配置
|
||||||
|
* 删除所有保存的WiFi网络配置
|
||||||
|
*/
|
||||||
|
void WiFiManager::clearAllConfigs() {
|
||||||
|
for (int i = 0; i < MAX_WIFI_NETWORKS; i++) {
|
||||||
|
String key = "wifi_" + String(i);
|
||||||
|
preferences.remove(key.c_str());
|
||||||
|
}
|
||||||
|
savedNetworkCount = 0;
|
||||||
|
Serial.println("🗑️ 已清除所有WiFi配置");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 获取已保存的网络数量
|
||||||
|
* @return 已保存的WiFi网络配置数量
|
||||||
|
*/
|
||||||
|
int WiFiManager::getSavedNetworkCount() {
|
||||||
|
return savedNetworkCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 获取已保存的WiFi网络列表
|
||||||
|
* 将保存的WiFi网络配置通过BLE发送给客户端
|
||||||
|
*/
|
||||||
|
void WiFiManager::getSavedNetworks() {
|
||||||
|
Serial.printf("📋 [WiFi] 获取已保存的WiFi网络列表,共 %d 个\n", savedNetworkCount);
|
||||||
|
|
||||||
|
if (savedNetworkCount == 0) {
|
||||||
|
Serial.println("⚠️ 没有保存的WiFi网络");
|
||||||
|
if (deviceConnected) {
|
||||||
|
String responseMsg = String("{\"type\":\"savedNetworks\",\"success\":true,\"count\":0,\"networks\":[]}");
|
||||||
|
sendJSONDataToBLE(responseMsg);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
String wifiList = String("{\"type\":\"savedNetworks\",\"success\":true,\"count\":") + String(savedNetworkCount) + String(",\"networks\":[");
|
||||||
|
|
||||||
|
for (int i = 0; i < savedNetworkCount; i++) {
|
||||||
|
if (i > 0) {
|
||||||
|
wifiList += ",";
|
||||||
|
}
|
||||||
|
wifiList += String("{\"ssid\":\"") + String(savedNetworks[i].ssid) + String("\"}");
|
||||||
|
}
|
||||||
|
|
||||||
|
wifiList += "]}";
|
||||||
|
|
||||||
|
Serial.printf("📤 [WiFi] 发送已保存的WiFi网络列表: %s\n", wifiList.c_str());
|
||||||
|
|
||||||
|
if (deviceConnected) {
|
||||||
|
sendJSONDataToBLE(wifiList);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 更新WiFi管理器状态
|
||||||
|
* 定期调用此函数,处理WiFi重连等状态管理
|
||||||
|
*/
|
||||||
|
void WiFiManager::update() {
|
||||||
|
handleReconnect();
|
||||||
|
}
|
||||||
121
src/wifi_manager.h
Normal file
121
src/wifi_manager.h
Normal file
@@ -0,0 +1,121 @@
|
|||||||
|
#ifndef WIFI_MANAGER_H
|
||||||
|
#define WIFI_MANAGER_H
|
||||||
|
|
||||||
|
#include <Arduino.h>
|
||||||
|
#include <WiFi.h>
|
||||||
|
#include <Preferences.h>
|
||||||
|
#include <ArduinoJson.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 最大WiFi网络配置数量
|
||||||
|
* 定义设备可以保存的WiFi网络配置的最大数量
|
||||||
|
*/
|
||||||
|
#define MAX_WIFI_NETWORKS 10
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 最小信号强度阈值
|
||||||
|
* 定义WiFi网络信号强度的最低要求,低于此值的网络会被过滤
|
||||||
|
* 单位:dBm
|
||||||
|
*/
|
||||||
|
#define MIN_RSSI_THRESHOLD -200
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief WiFi连接超时时间
|
||||||
|
* 定义WiFi连接的最大等待时间,超过此时间认为连接失败
|
||||||
|
* 单位:毫秒
|
||||||
|
*/
|
||||||
|
#define WIFI_CONNECT_TIMEOUT 15000
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief WiFi重连间隔时间
|
||||||
|
* 定义WiFi断开后,尝试重新连接的时间间隔
|
||||||
|
* 单位:毫秒
|
||||||
|
*/
|
||||||
|
#define WIFI_RECONNECT_INTERVAL 2000
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief WiFi网络信息结构
|
||||||
|
* 存储WiFi网络的详细信息,用于扫描和显示
|
||||||
|
*/
|
||||||
|
typedef struct {
|
||||||
|
char ssid[32]; // WiFi网络名称
|
||||||
|
char password[64]; // WiFi网络密码
|
||||||
|
int rssi; // 信号强度,单位:dBm
|
||||||
|
uint8_t channel; // WiFi通道
|
||||||
|
uint8_t encryption; // 加密类型
|
||||||
|
} WiFiNetworkInfo;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief WiFi配置结构
|
||||||
|
* 存储WiFi网络的配置信息,用于保存和加载
|
||||||
|
*/
|
||||||
|
typedef struct {
|
||||||
|
char ssid[32]; // WiFi网络名称
|
||||||
|
char password[64]; // WiFi网络密码
|
||||||
|
} WiFiConfig;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief WiFi管理器状态枚举
|
||||||
|
* 定义WiFi管理器的不同工作状态
|
||||||
|
*/
|
||||||
|
enum WiFiManagerState {
|
||||||
|
WIFI_IDLE, // 空闲状态
|
||||||
|
WIFI_SCANNING, // 扫描网络中
|
||||||
|
WIFI_CONNECTING, // 连接网络中
|
||||||
|
WIFI_CONNECTED, // 已连接
|
||||||
|
WIFI_DISCONNECTED, // 断开连接
|
||||||
|
WIFI_CONFIGURING // 配网模式
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 网络状态枚举
|
||||||
|
* 定义网络的不同状态,用于LED控制
|
||||||
|
*/
|
||||||
|
enum NetworkStatus {
|
||||||
|
NET_INITIAL, // 初始化/未连接 - 慢闪
|
||||||
|
NET_CONNECTING, // 连接中 - 快闪
|
||||||
|
NET_CONNECTED, // 已连接 - 呼吸灯
|
||||||
|
NET_DISCONNECTED // 断开连接 - 慢闪
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief WiFi管理器类
|
||||||
|
* 负责WiFi网络的扫描、连接、配置和管理
|
||||||
|
*/
|
||||||
|
class WiFiManager {
|
||||||
|
private:
|
||||||
|
Preferences preferences; // 用于存储WiFi配置的Preferences对象
|
||||||
|
WiFiConfig savedNetworks[MAX_WIFI_NETWORKS]; // 保存的WiFi网络配置数组
|
||||||
|
int savedNetworkCount; // 已保存的网络数量
|
||||||
|
WiFiManagerState currentState; // 当前WiFi管理器状态
|
||||||
|
unsigned long lastReconnectAttempt; // 上次尝试重连的时间
|
||||||
|
|
||||||
|
bool scanAndMatchNetworks(); // 扫描并匹配网络
|
||||||
|
bool connectToNetwork(const char* ssid, const char* password); // 连接到指定网络
|
||||||
|
void sendScanResultsViaBLE(); // 发送扫描结果到BLE
|
||||||
|
bool saveWiFiConfig(const char* ssid, const char* password); // 保存WiFi配置
|
||||||
|
bool loadWiFiConfigs(); // 加载WiFi配置
|
||||||
|
|
||||||
|
public:
|
||||||
|
WiFiManager(); // 构造函数
|
||||||
|
void begin(); // 初始化WiFi管理器
|
||||||
|
|
||||||
|
bool initializeWiFi(); // 初始化WiFi连接
|
||||||
|
bool startConfiguration(); // 开始配网模式
|
||||||
|
bool handleConfigurationData(const char* ssid, const char* password); // 处理配网数据
|
||||||
|
void handleReconnect(); // 处理重连
|
||||||
|
|
||||||
|
WiFiManagerState getState(); // 获取当前状态
|
||||||
|
bool isConnected(); // 检查是否已连接
|
||||||
|
void disconnect(); // 断开连接
|
||||||
|
|
||||||
|
void scanAndSendResults(); // 扫描并发送结果
|
||||||
|
bool addWiFiConfig(const char* ssid, const char* password); // 添加WiFi配置
|
||||||
|
void clearAllConfigs(); // 清除所有配置
|
||||||
|
int getSavedNetworkCount(); // 获取已保存的网络数量
|
||||||
|
void getSavedNetworks(); // 获取已保存的WiFi网络列表
|
||||||
|
|
||||||
|
void update(); // 更新WiFi管理器状态
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
Reference in New Issue
Block a user