Files
2026-02-07 23:14:57 +08:00

348 lines
10 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 智能家居控制系统
这是一个基于微信小程序的智能家居控制系统,通过 MQTT 协议与 ESP32 等物联网设备进行通信。
## 功能特点
- 实时监控 ESP32 设备传感器数据(温度、湿度等)
- 远程控制智能家居设备LED、风扇、窗帘等
- 设备状态管理
- 统一的物联网设备通信协议
## 通信协议
### 消息结构
所有通信消息均采用统一的 JSON 格式:
```json
{
"type": "device_message|control_command|response",
"device_id": "string",
"device_type": "string",
"timestamp": 1234567890123,
"message_type": "string",
"request_id": "string (optional)",
"data": {
// 具体数据内容
},
"status_code": 200,
"status_message": "string (optional)"
}
```
### 字段说明
- `type`: 消息类型
- `device_message`: 设备主动上报的消息(如传感器数据、状态变化)
- `control_command`: 控制命令(如开关设备、设置参数,包含配置更新)
- `response`: 响应消息(设备对命令的响应)
- `device_id`: 设备唯一标识符,例如:"esp32_bedroom_001"
- `device_type`: 设备类型,固定为:"bedroom_controller"(卧室控制器)
- `timestamp`: Unix 时间戳(毫秒),由设备或服务器提供
- `message_type`: 消息子类型,如:"sensor_data", "status_report", "control_request", "config_set"
- `request_id`: 请求 ID用于匹配请求和响应格式为"req_YYYYMMDDHHMMSS_xxx"
- `data`: 具体的数据内容,包含状态、遥测、控制或配置信息
- `status_code`: 状态码200 表示成功,其他表示错误)
- `status_message`: 状态描述,可选字段
## 3. 智能卧室系统专用消息类型
### 3.1 传感器数据上传 (device_message)
ESP32 定期上报卧室环境数据:
```json
{
"type": "device_message",
"device_id": "esp32_bedroom_001",
"device_type": "bedroom_controller",
"timestamp": 1768223104055,
"message_type": "sensor_data",
"data": {
"state": {
"online": true,
"current_mode": "night_mode",
"home_status": true,
"standby_mode": false,
"error_code": 0
},
"telemetry": {
"temperature": 23.5,
"humidity": 65.2,
"light_intensity": 300,
"air_quality": 850,
"curtain_state": "closed",
"led_state": true,
"led_brightness": 80,
"fan_state": false,
"fan_speed": 0,
"buzzer_state": false,
"battery_level": 100,
"signal_strength": -55
}
}
}
```
**参数解释:**
- `state.online`: 设备是否在线
- `state.current_mode`: 当前运行模式("away_mode", "home_mode", "wake_mode", "day_mode", "night_mode", "cooling_mode", "high_temp_alert", "display_on", "ventilation_mode"
- `state.home_status`: 用户是否在家状态(对应"到家状态"
- `state.standby_mode`: 是否处于待机状态
- `telemetry.temperature`: 温度传感器(DHT22)读取的当前温度(摄氏度)
- `telemetry.humidity`: 湿度传感器(DHT22)读取的当前湿度(百分比)
- `telemetry.light_intensity`: 光敏电阻读取的光照强度(数值越大表示越亮)
- `telemetry.air_quality`: 空气质量传感器读取的 CO2 浓度ppm
- `telemetry.curtain_state`: 舵机控制的窗帘状态("open", "closed"
- `telemetry.led_state`: LED 灯开关状态
- `telemetry.led_brightness`: LED 灯亮度0-100 百分比)
- `telemetry.fan_state`: 风扇开关状态
- `telemetry.fan_speed`: 风扇速度档位0-3
- `telemetry.buzzer_state`: 蜂鸣器开关状态
### 控制命令消息格式
微信小程序向 ESP32 发送控制指令,同时包含设备控制和配置更新:
```json
{
"type": "control_command",
"device_id": "esp32_bedroom_001",
"device_type": "bedroom_controller",
"timestamp": 1768223104055,
"message_type": "control_request",
"request_id": "req_123456789_abcde",
"data": {
"controls": {
"mode": "night_mode",
"curtain": "open",
"led_power": true,
"led_brightness": 80,
"fan_power": true,
"fan_speed": 2,
"buzzer_power": false
},
"config": {
"wake_up_time": "08:00",
"day_mode_start": "06:00",
"night_mode_start": "18:00",
"cooling_temp_threshold": 26,
"high_temp_alarm_threshold": 35,
"ventilation_air_quality_threshold": 1000,
"report_interval": 30,
"oled_display_enabled": true
}
}
}
```
**参数解释:**
- `controls.mode`: 设置系统运行模式("wake_mode", "day_mode", "night_mode", "cooling_mode", "ventilation_mode"等)
- `controls.curtain`: 控制窗帘开关("open", "close"
- `controls.led_power`: 控制 LED 灯开关
- `controls.led_brightness`: 设置 LED 灯亮度0-100 百分比)
- `controls.fan_power`: 控制风扇开关
- `controls.fan_speed`: 设置风扇速度0-3 档)
- `controls.buzzer_power`: 控制蜂鸣器开关
- `config.wake_up_time`: 起床模式触发时间HH:MM 格式)
- `config.day_mode_start`: 白天模式开始时间
- `config.night_mode_start`: 夜间模式开始时间
- `config.cooling_temp_threshold`: 降温模式启动温度阈值(摄氏度)
- `config.high_temp_alarm_threshold`: 高温提醒阈值35°C 时触发)
- `config.ventilation_air_quality_threshold`: 自动通风启动空气质量阈值CO2>1000ppm 时启动)
- `config.report_interval`: 传感器数据上报间隔(秒)
- `config.oled_display_enabled`: OLED 显示屏是否启用
### 设备状态上报消息格式
```json
{
"type": "device_message",
"device_id": "esp32_bedroom_001",
"device_type": "bedroom_controller",
"timestamp": 1768223104055,
"message_type": "sensor_data",
"data": {
"state": {
"online": true,
"current_mode": "day_mode",
"led_state": true,
"fan_state": false
},
"telemetry": {
"temperature": 23.5,
"humidity": 65.2,
"light_intensity": 300,
"air_quality": 45
}
}
}
```
### 响应消息 (response)
ESP32 对控制命令的响应:
```json
{
"type": "response",
"device_id": "esp32_bedroom_001",
"device_type": "bedroom_controller",
"timestamp": 1768223104060,
"message_type": "control_response",
"request_id": "req_123456789_abcde",
"data": {
"result": {
"led_power": true,
"led_brightness": 80,
"execution_time": 120
}
},
"status_code": 200,
"status_message": "Command executed successfully"
}
```
**参数解释:**
- `request_id`: 对应的请求 ID用于匹配请求和响应
- `result.execution_time`: 命令执行耗时(毫秒)
- `status_code`: 执行结果状态码200 成功500 失败等)
- `status_message`: 执行结果描述
## MQTT 主题
- **设备数据上报**: `sensor/{device_id}`
- **控制命令下发**: `control/{device_id}`
## 设备管理
- 自动注册:设备首次发送消息时自动添加到系统
- 数据更新:根据设备发送的消息自动更新状态
- 在线状态:实时跟踪设备连接状态
## 控制功能
- LED 开关控制
- 风扇开关和速度控制
- 窗帘开关控制
- 设备配置更新(温度阈值、警报阈值等)
## 使用方法
1. 配置 MQTT 服务器参数
2. 连接到 ESP32 设备
3. 实时查看传感器数据
4. 通过控制面板发送控制命令
5. 更新设备配置参数
```javascript
/**
* 示例切换LED状态
*/
toggleLed() {
// 获取当前LED状态
const currentLedState = this.extractESP32Info("data.telemetry.led_state");
// 发送控制命令切换LED状态
this.sendControlCommand({
led_power: !currentLedState,
});
},
/**
* 示例:打开窗帘
*/
setCurtainOpen() {
this.sendControlCommand({
curtain: "open",
});
},
/**
* 示例:关闭窗帘
*/
setCurtainClose() {
this.sendControlCommand({
curtain: "close",
});
},
/**
* 示例:设置风扇速度
*/
setFanSpeed(speed) {
this.sendControlCommand({
fan_power: true,
fan_speed: parseInt(speed),
});
},
/**
* 示例:更新设备配置
*/
updateDeviceConfig(config) {
// 发送配置更新命令(现在通过控制命令发送)
this.sendControlCommand({}, { config: config });
},
/**
* 示例:设置温度阈值
*/
setTemperatureThreshold(threshold) {
this.updateDeviceConfig({
cooling_temp_threshold: threshold,
});
},
/**
* 示例:设置高温警报阈值
*/
setHighTempAlarmThreshold(threshold) {
this.updateDeviceConfig({
high_temp_alarm_threshold: threshold,
});
},
/**
* 示例:显示设备最新数据
*/
showLatestData() {
// 提取温度
const temperature = this.extractESP32Info("data.telemetry.temperature");
console.log("当前温度:", temperature);
// 提取湿度
const humidity = this.extractESP32Info("data.telemetry.humidity");
console.log("当前湿度:", humidity);
// 提取当前模式
const mode = this.extractESP32Info("data.state.current_mode");
console.log("当前模式:", mode);
// 提取所有设备数据
const allData = this.extractESP32Info();
console.log("全部数据:", allData);
},
```
以上的代码片段中,我们定义了一些示例方法,用于切换 LED 状态、打开窗帘、关闭窗帘、设置风扇速度、更新设备配置、设置温度阈值、设置高温警报阈值和显示设备最新数据。
这些方法通过调用`this.sendControlCommand()``this.updateDeviceConfig()`方法来发送控制命令和配置更新命令,并使用`this.extractESP32Info()`方法来提取设备数据。
这些方法中,`this.sendControlCommand()``this.updateDeviceConfig()`方法用于发送控制命令和配置更新命令, respectively。 它们接受一个参数,即控制命令或配置更新。
`this.extractESP32Info()`方法用于从设备数据中提取信息。 它接受一个参数,即要提取的信息的路径。 例如,`this.extractESP32Info("data.telemetry.temperature")`将返回当前温度。
```javascript
this.sendControlCommand(
{ light: "on", fan: "off" }, // 控制参数
{ brightness: 80 }, // 配置参数
(response) => {
// 回调函数
console.log("响应:", response);
}
);
```