отправка конфига в сокеты

This commit is contained in:
Dmitry Borisenko
2021-12-16 00:19:08 +01:00
parent 715d20c21f
commit f7a97c2d36
8 changed files with 55 additions and 49 deletions

View File

@@ -1,28 +1,29 @@
[ [
{ {
"type": "button-out", "type": "binary",
"subtype": "button-out",
"id": "btn1", "id": "btn1",
"gpio": 1, "gpio": 1,
"inv": false, "inv": false
"web": {
"widget": "toggle",
"page": "Кнопки",
"descr": "Освещение1",
"order": 1,
"topic": "/prefix/123456-123456/btn2"
}
}, },
{ {
"type": "button-out", "type": "sensor",
"id": "btn2", "subtype": "bme280",
"gpio": 1, "units": "temp",
"inv": false, "id": "tmp1",
"web": { "addr": "0x48",
"widget": "toggle", "int": 10,
"page": "Кнопки", "c": 1,
"descr": "Освещение2", "s": 0
"order": 1, },
"topic": "/prefix/123456-123456/btn2" {
} "type": "sensor",
"subtype": "bme280",
"units": "temp",
"id": "tmp2",
"addr": "0x48",
"int": 10,
"c": 1,
"s": 0
} }
] ]

View File

@@ -2,8 +2,6 @@
#include <Arduino.h> #include <Arduino.h>
void mqttInit(); void mqttInit();
void selectBroker(); void selectBroker();
void getMqttData1(); void getMqttData1();

6
include/WebSocket.h Normal file
View File

@@ -0,0 +1,6 @@
#pragma once
#include "Global.h"
void wsSendSetup();
void wsPublishData(String topic, String data);

View File

@@ -180,14 +180,14 @@ boolean mqttConnect() {
void mqttCallback(char* topic, uint8_t* payload, size_t length) { void mqttCallback(char* topic, uint8_t* payload, size_t length) {
String topicStr = String(topic); String topicStr = String(topic);
//SerialPrint("I", "=>MQTT", topicStr); // SerialPrint("I", "=>MQTT", topicStr);
String payloadStr; String payloadStr;
payloadStr.reserve(length + 1); payloadStr.reserve(length + 1);
for (size_t i = 0; i < length; i++) { for (size_t i = 0; i < length; i++) {
payloadStr += (char)payload[i]; payloadStr += (char)payload[i];
} }
//SerialPrint("I", "=>MQTT", payloadStr); // SerialPrint("I", "=>MQTT", payloadStr);
if (payloadStr.startsWith("HELLO")) { if (payloadStr.startsWith("HELLO")) {
SerialPrint("I", F("MQTT"), F("Full update")); SerialPrint("I", F("MQTT"), F("Full update"));
@@ -321,8 +321,8 @@ void publishWidgets() {
line = all_widgets.substring(psn_1, psn_2); line = all_widgets.substring(psn_1, psn_2);
line.replace("\n", ""); line.replace("\n", "");
line.replace("\r\n", ""); line.replace("\r\n", "");
//jsonWriteStr(line, "id", String(counter)); // jsonWriteStr(line, "id", String(counter));
//jsonWriteStr(line, "pageId", String(counter)); // jsonWriteStr(line, "pageId", String(counter));
counter++; counter++;
sendMQTT("config", line); sendMQTT("config", line);
Serial.println("[V] " + line); Serial.println("[V] " + line);

View File

@@ -2,11 +2,10 @@
#include "Global.h" #include "Global.h"
#include "WebServer.h" #include "WebServer.h"
#include "WebSocket.h"
void SerialPrint(String errorLevel, String module, String msg) { void SerialPrint(String errorLevel, String module, String msg) {
String tosend = prettyMillis(millis()) + " [" + errorLevel + "] [" + module + "] " + msg; String tosend = prettyMillis(millis()) + " [" + errorLevel + "] [" + module + "] " + msg;
Serial.println(tosend); Serial.println(tosend);
// mqttRootDevice + wsPublishData(F("log"), tosend);
ws.textAll("/core/" + tosend);
} }

View File

@@ -4,6 +4,7 @@
#include "FSEditor.h" #include "FSEditor.h"
#include "Utils/FileUtils.h" #include "Utils/FileUtils.h"
#include "Utils/WebUtils.h" #include "Utils/WebUtils.h"
#include "WebSocket.h"
AsyncWebSocket ws("/ws"); AsyncWebSocket ws("/ws");
AsyncEventSource events("/events"); AsyncEventSource events("/events");
@@ -112,9 +113,9 @@ void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventTyp
} }
Serial.printf("%s\n", msg.c_str()); Serial.printf("%s\n", msg.c_str());
if (msg.startsWith("config")) { if (msg.startsWith("/config")) {
SerialPrint("I", F("WS"), F("config send")); SerialPrint("I", F("WS"), F("config send"));
sendEspSetupToWS(); wsSendSetup();
// publishWidgetsWS(); // publishWidgetsWS();
// publishStateWS(); // publishStateWS();
@@ -211,18 +212,3 @@ void HttpServerinitWS() {
server.addHandler(&events); server.addHandler(&events);
#endif #endif
} }
//===========web sockets==============================
void sendEspSetupToWS() {
File file = seekFile("/setup.json");
DynamicJsonDocument doc(1024);
file.find("[");
do {
deserializeJson(doc, file);
// Serial.println(doc.as<String>());
ws.textAll(doc.as<String>());
} while (file.findUntil(",", "]"));
}

18
src/WebSocket.cpp Normal file
View File

@@ -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<String>());
} while (file.findUntil(",", "]"));
}
void wsPublishData(String topic, String data) {
data = "[" + topic + "]" + data;
ws.textAll(data);
}

View File

@@ -100,9 +100,7 @@ void setup() {
// setupESP(); // setupESP();
sendEspSetupToWS(); SerialPrint("I", F("System"), F("✔ Initialization completed"));
SerialPrint("I", F("System"), F("✔ Initialization completed"));
} }
void loop() { void loop() {