From f7a97c2d3673412ff23fd30857e751c56a72eb38 Mon Sep 17 00:00:00 2001 From: Dmitry Borisenko <49808844+DmitryBorisenko33@users.noreply.github.com> Date: Thu, 16 Dec 2021 00:19:08 +0100 Subject: [PATCH] =?UTF-8?q?=D0=BE=D1=82=D0=BF=D1=80=D0=B0=D0=B2=D0=BA?= =?UTF-8?q?=D0=B0=20=D0=BA=D0=BE=D0=BD=D1=84=D0=B8=D0=B3=D0=B0=20=D0=B2=20?= =?UTF-8?q?=D1=81=D0=BE=D0=BA=D0=B5=D1=82=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- data_svelte/setup.json | 41 ++++++++++++++++++++------------------- include/MqttClient.h | 2 -- include/WebSocket.h | 6 ++++++ src/MqttClient.cpp | 8 ++++---- src/Utils/SerialPrint.cpp | 5 ++--- src/WebServer.cpp | 20 +++---------------- src/WebSocket.cpp | 18 +++++++++++++++++ src/main.cpp | 4 +--- 8 files changed, 55 insertions(+), 49 deletions(-) create mode 100644 include/WebSocket.h create mode 100644 src/WebSocket.cpp diff --git a/data_svelte/setup.json b/data_svelte/setup.json index 1ad580bd..92ca22b3 100644 --- a/data_svelte/setup.json +++ b/data_svelte/setup.json @@ -1,28 +1,29 @@ [ { - "type": "button-out", + "type": "binary", + "subtype": "button-out", "id": "btn1", "gpio": 1, - "inv": false, - "web": { - "widget": "toggle", - "page": "Кнопки", - "descr": "Освещение1", - "order": 1, - "topic": "/prefix/123456-123456/btn2" - } + "inv": false }, { - "type": "button-out", - "id": "btn2", - "gpio": 1, - "inv": false, - "web": { - "widget": "toggle", - "page": "Кнопки", - "descr": "Освещение2", - "order": 1, - "topic": "/prefix/123456-123456/btn2" - } + "type": "sensor", + "subtype": "bme280", + "units": "temp", + "id": "tmp1", + "addr": "0x48", + "int": 10, + "c": 1, + "s": 0 + }, + { + "type": "sensor", + "subtype": "bme280", + "units": "temp", + "id": "tmp2", + "addr": "0x48", + "int": 10, + "c": 1, + "s": 0 } ] \ No newline at end of file diff --git a/include/MqttClient.h b/include/MqttClient.h index 45189279..ff4416fc 100644 --- a/include/MqttClient.h +++ b/include/MqttClient.h @@ -2,8 +2,6 @@ #include - - void mqttInit(); void selectBroker(); void getMqttData1(); diff --git a/include/WebSocket.h b/include/WebSocket.h new file mode 100644 index 00000000..060570ae --- /dev/null +++ b/include/WebSocket.h @@ -0,0 +1,6 @@ +#pragma once + +#include "Global.h" + +void wsSendSetup(); +void wsPublishData(String topic, String data); diff --git a/src/MqttClient.cpp b/src/MqttClient.cpp index 08c808af..6543ea15 100644 --- a/src/MqttClient.cpp +++ b/src/MqttClient.cpp @@ -180,14 +180,14 @@ boolean mqttConnect() { void mqttCallback(char* topic, uint8_t* payload, size_t length) { String topicStr = String(topic); - //SerialPrint("I", "=>MQTT", topicStr); + // SerialPrint("I", "=>MQTT", topicStr); String payloadStr; payloadStr.reserve(length + 1); for (size_t i = 0; i < length; i++) { payloadStr += (char)payload[i]; } - //SerialPrint("I", "=>MQTT", payloadStr); + // SerialPrint("I", "=>MQTT", payloadStr); if (payloadStr.startsWith("HELLO")) { SerialPrint("I", F("MQTT"), F("Full update")); @@ -321,8 +321,8 @@ void publishWidgets() { line = all_widgets.substring(psn_1, psn_2); line.replace("\n", ""); line.replace("\r\n", ""); - //jsonWriteStr(line, "id", String(counter)); - //jsonWriteStr(line, "pageId", String(counter)); + // jsonWriteStr(line, "id", String(counter)); + // jsonWriteStr(line, "pageId", String(counter)); counter++; sendMQTT("config", line); Serial.println("[V] " + line); diff --git a/src/Utils/SerialPrint.cpp b/src/Utils/SerialPrint.cpp index 16a6d235..eb215703 100644 --- a/src/Utils/SerialPrint.cpp +++ b/src/Utils/SerialPrint.cpp @@ -2,11 +2,10 @@ #include "Global.h" #include "WebServer.h" +#include "WebSocket.h" void SerialPrint(String errorLevel, String module, String msg) { String tosend = prettyMillis(millis()) + " [" + errorLevel + "] [" + module + "] " + msg; - Serial.println(tosend); - // mqttRootDevice + - ws.textAll("/core/" + tosend); + wsPublishData(F("log"), tosend); } \ No newline at end of file diff --git a/src/WebServer.cpp b/src/WebServer.cpp index 529def17..52ee5076 100644 --- a/src/WebServer.cpp +++ b/src/WebServer.cpp @@ -4,6 +4,7 @@ #include "FSEditor.h" #include "Utils/FileUtils.h" #include "Utils/WebUtils.h" +#include "WebSocket.h" AsyncWebSocket ws("/ws"); AsyncEventSource events("/events"); @@ -112,9 +113,9 @@ void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventTyp } Serial.printf("%s\n", msg.c_str()); - if (msg.startsWith("config")) { + if (msg.startsWith("/config")) { SerialPrint("I", F("WS"), F("config send")); - sendEspSetupToWS(); + wsSendSetup(); // publishWidgetsWS(); // publishStateWS(); @@ -211,18 +212,3 @@ void HttpServerinitWS() { server.addHandler(&events); #endif } - -//===========web sockets============================== -void sendEspSetupToWS() { - File file = seekFile("/setup.json"); - DynamicJsonDocument doc(1024); - file.find("["); - - do { - deserializeJson(doc, file); - - // Serial.println(doc.as()); - ws.textAll(doc.as()); - - } while (file.findUntil(",", "]")); -} diff --git a/src/WebSocket.cpp b/src/WebSocket.cpp new file mode 100644 index 00000000..05e356b3 --- /dev/null +++ b/src/WebSocket.cpp @@ -0,0 +1,18 @@ +#include "WebSocket.h" + +#include "Global.h" + +void wsSendSetup() { + File file = seekFile("/setup.json"); + DynamicJsonDocument doc(1024); + file.find("["); + do { + deserializeJson(doc, file); + wsPublishData(F("config"), doc.as()); + } while (file.findUntil(",", "]")); +} + +void wsPublishData(String topic, String data) { + data = "[" + topic + "]" + data; + ws.textAll(data); +} diff --git a/src/main.cpp b/src/main.cpp index e19df772..bb9e916c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -100,9 +100,7 @@ void setup() { // setupESP(); - sendEspSetupToWS(); - - SerialPrint("I", F("System"), F("✔ Initialization completed")); + SerialPrint("I", F("System"), F("✔ Initialization completed")); } void loop() {