mirror of
https://github.com/IoTManagerProject/IoTManager.git
synced 2026-03-26 14:12:16 +03:00
отправка конфига в сокеты
This commit is contained in:
@@ -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
|
||||
}
|
||||
]
|
||||
@@ -2,8 +2,6 @@
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
|
||||
|
||||
void mqttInit();
|
||||
void selectBroker();
|
||||
void getMqttData1();
|
||||
|
||||
6
include/WebSocket.h
Normal file
6
include/WebSocket.h
Normal file
@@ -0,0 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "Global.h"
|
||||
|
||||
void wsSendSetup();
|
||||
void wsPublishData(String topic, String data);
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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<String>());
|
||||
ws.textAll(doc.as<String>());
|
||||
|
||||
} while (file.findUntil(",", "]"));
|
||||
}
|
||||
|
||||
18
src/WebSocket.cpp
Normal file
18
src/WebSocket.cpp
Normal 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);
|
||||
}
|
||||
@@ -100,9 +100,7 @@ void setup() {
|
||||
|
||||
// setupESP();
|
||||
|
||||
sendEspSetupToWS();
|
||||
|
||||
SerialPrint("I", F("System"), F("✔ Initialization completed"));
|
||||
SerialPrint("I", F("System"), F("✔ Initialization completed"));
|
||||
}
|
||||
|
||||
void loop() {
|
||||
|
||||
Reference in New Issue
Block a user