|
|
@@ -22,7 +22,7 @@
|
|
|
// Mode: b8b7e002-... (读写/通知 - 灯效模式)
|
|
|
// Config: b8b7e003-... (读写 - WiFi/MQTT 配置 JSON)
|
|
|
//
|
|
|
-// 配置 JSON 格式(写入 Config 特征):
|
|
|
+// 配置 JSON 格式(写入 Config 特征或 MQTT config topic):
|
|
|
// {
|
|
|
// "wifi_ssid": "xxx",
|
|
|
// "wifi_pass": "xxx",
|
|
|
@@ -31,8 +31,9 @@
|
|
|
// "mqtt_user": "user",
|
|
|
// "mqtt_pass": "pass",
|
|
|
// "mqtt_client": "AI-Light",
|
|
|
-// "mqtt_topic": "opencode/status",
|
|
|
-// "mqtt_status": "openCodeLight/status",
|
|
|
+// "mqtt_topic": "agent/status",
|
|
|
+// "mqtt_status": "agentLight/status",
|
|
|
+// "mqtt_topic_config": "agent/status/config",
|
|
|
// "pin_red": 4,
|
|
|
// "pin_green": 3,
|
|
|
// "pin_yellow": 2,
|
|
|
@@ -102,8 +103,9 @@ uint16_t cfgMqttPort = 1883;
|
|
|
String cfgMqttUser = "";
|
|
|
String cfgMqttPass = "";
|
|
|
String cfgMqttClient = "AI-Light";
|
|
|
-String cfgMqttTopic = "opencode/status";
|
|
|
-String cfgMqttStatus = "openCodeLight/status";
|
|
|
+String cfgMqttTopic = "agent/status";
|
|
|
+String cfgMqttStatus = "agentLight/status";
|
|
|
+String cfgMqttTopicConfig = "agent/status/config";
|
|
|
String cfgOtaUrl = "";
|
|
|
bool otaInProgress = false;
|
|
|
|
|
|
@@ -120,8 +122,9 @@ void loadConfig() {
|
|
|
cfgMqttUser = preferences.getString("mqtt_user", "");
|
|
|
cfgMqttPass = preferences.getString("mqtt_pass", "");
|
|
|
cfgMqttClient = preferences.getString("mqtt_client", "AI-Light");
|
|
|
- cfgMqttTopic = preferences.getString("mqtt_topic", "opencode/status");
|
|
|
- cfgMqttStatus = preferences.getString("mqtt_status", "openCodeLight/status");
|
|
|
+ cfgMqttTopic = preferences.getString("mqtt_topic", "agent/status");
|
|
|
+ cfgMqttStatus = preferences.getString("mqtt_status", "agentLight/status");
|
|
|
+ cfgMqttTopicConfig = preferences.getString("mqtt_topic_cfg", "agent/status/config");
|
|
|
redPin = preferences.getUInt("pin_red", 4);
|
|
|
greenPin = preferences.getUInt("pin_green", 3);
|
|
|
yellowPin = preferences.getUInt("pin_yellow", 2);
|
|
|
@@ -142,6 +145,7 @@ String getConfigJson() {
|
|
|
doc["mqtt_client"] = cfgMqttClient;
|
|
|
doc["mqtt_topic"] = cfgMqttTopic;
|
|
|
doc["mqtt_status"] = cfgMqttStatus;
|
|
|
+ doc["mqtt_topic_config"] = cfgMqttTopicConfig;
|
|
|
doc["comm_mode"] = useMQTT ? 1 : 0;
|
|
|
doc["pin_red"] = redPin;
|
|
|
doc["pin_green"] = greenPin;
|
|
|
@@ -179,6 +183,8 @@ void saveConfigFromJson(const String& json) {
|
|
|
preferences.putString("mqtt_topic", doc["mqtt_topic"].as<String>());
|
|
|
if (doc.containsKey("mqtt_status"))
|
|
|
preferences.putString("mqtt_status", doc["mqtt_status"].as<String>());
|
|
|
+ if (doc.containsKey("mqtt_topic_config"))
|
|
|
+ preferences.putString("mqtt_topic_cfg", doc["mqtt_topic_config"].as<String>());
|
|
|
if (doc.containsKey("pin_red"))
|
|
|
preferences.putUInt("pin_red", doc["pin_red"].as<uint8_t>());
|
|
|
if (doc.containsKey("pin_green"))
|
|
|
@@ -552,6 +558,11 @@ void mqttCallback(char* topic, byte* payload, unsigned int length) {
|
|
|
for (unsigned int i = 0; i < length; i++) message += (char)payload[i];
|
|
|
Serial.print("MQTT: "); Serial.print(topic); Serial.print(" -> "); Serial.println(message);
|
|
|
|
|
|
+ if (String(topic) == cfgMqttTopicConfig) {
|
|
|
+ saveConfigFromJson(message);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
JsonDocument doc;
|
|
|
if (deserializeJson(doc, message)) { setMode(message); return; }
|
|
|
const char* code = doc["code"];
|
|
|
@@ -606,6 +617,7 @@ bool connectMQTT() {
|
|
|
if (mqttClient.connect(cfgMqttClient.c_str(), cfgMqttUser.c_str(), cfgMqttPass.c_str())) {
|
|
|
Serial.println("\nMQTT OK.");
|
|
|
mqttClient.subscribe(cfgMqttTopic.c_str());
|
|
|
+ mqttClient.subscribe(cfgMqttTopicConfig.c_str());
|
|
|
allOff(); breathingGreen(3); publishStatus();
|
|
|
return true;
|
|
|
}
|