Explorar o código

恢复出厂设置功能

moki hai 1 día
pai
achega
c387506509
Modificáronse 3 ficheiros con 37 adicións e 7 borrados
  1. 4 0
      docs/BLE_API.md
  2. 24 6
      firmware/README.md
  3. 9 1
      firmware/ai_light/ai_light.ino

+ 4 - 0
docs/BLE_API.md

@@ -88,6 +88,7 @@
 | mqtt_client | string | 否 | AI-Light | MQTT Client ID |
 | mqtt_topic | string | 否 | opencode/status | 订阅主题 |
 | mqtt_status | string | 否 | openCodeLight/status | 状态发布主题 |
+| factory_reset | boolean | 否 | false | 恢复出厂设置,清除所有配置并重启 |
 
 **注意**:所有字段都是可选的,只更新提供的字段,未提供的字段保持原值。
 
@@ -378,6 +379,9 @@ func main() {
 		"mqtt_client": "AI-Light",
 		"mqtt_topic": "opencode/status"
 	}`)
+	
+	// 恢复出厂设置
+	ailight.SetConfig(`{"factory_reset": true}`)
 }
 ```
 

+ 24 - 6
firmware/README.md

@@ -63,7 +63,7 @@ Config Characteristic: b8b7e003-7a6b-4f4f-9a8b-11c0ffee0001  (WiFi/MQTT 配置)
   "pin_red": 4,
   "pin_green": 3,
   "pin_yellow": 2,
-  "ota_url": "https://example.com/firmware.bin"
+  "factory_reset": true
 }
 ```
 
@@ -83,13 +83,19 @@ mosquitto_pub -h 192.168.1.100 -t "agent/status/config" -m '{
 }'
 ```
 
-### OTA 固件更新
+### 恢复出厂设置
 
-配置 `ota_url` 后,可通过以下方式触发 OTA:
-- BLE:向 Mode 特征写入 `ota`
-- MQTT:发送 `{"code": "ota"}`
+发送 `{"factory_reset": true}` 可清除所有配置并重启:
 
-设备会从 URL 下载固件并自动更新重启。
+```bash
+# BLE 方式:向 Config 特征写入
+# {"factory_reset": true}
+
+# MQTT 方式
+mosquitto_pub -h 192.168.1.100 -t "agent/status/config" -m '{"factory_reset": true}'
+```
+
+设备会清除 NVS 中所有配置,重启后进入 BLE 配置模式。
 
 ## 双模式版
 
@@ -148,3 +154,15 @@ ESP32 IO9   -> BOOT 按钮(固定)
 - `Preferences.h`(ESP32 内置)
 
 通过 Arduino IDE → Sketch → Include Library → Manage Libraries 安装。
+
+## 配置持久化
+
+配置保存在 ESP32 的 NVS(Non-Volatile Storage)分区,与固件分区独立:
+
+| 操作 | 配置保留? |
+|------|-----------|
+| Arduino IDE 正常上传 | ✅ 保留 |
+| 只刷写固件(不擦除 flash) | ✅ 保留 |
+| 擦除整个 flash 后刷写 | ❌ 丢失 |
+
+如需完全重置,可通过发送 `{"factory_reset": true}` 或在 Arduino IDE 中擦除 flash。

+ 9 - 1
firmware/ai_light/ai_light.ino

@@ -34,7 +34,8 @@
 //     "mqtt_topic_config": "agent/status/config",
 //     "pin_red": 4,
 //     "pin_green": 3,
-//     "pin_yellow": 2
+//     "pin_yellow": 2,
+//     "factory_reset": true   // 恢复出厂设置,清除所有配置并重启
 //   }
 //
 // 接线方式(默认引脚,可通过配置修改):
@@ -158,6 +159,13 @@ void saveConfigFromJson(const String& json) {
     return;
   }
 
+  if (doc.containsKey("factory_reset") && doc["factory_reset"].as<bool>()) {
+    Serial.println("Factory reset! Clearing all config...");
+    preferences.clear();
+    delay(500);
+    ESP.restart();
+  }
+
   if (doc.containsKey("wifi_ssid"))
     preferences.putString("wifi_ssid", doc["wifi_ssid"].as<String>());
   if (doc.containsKey("wifi_pass"))