mirror of
https://github.com/HendrikRauh/dmx-interface.git
synced 2025-07-09 23:25:45 +00:00
add WebSocket support for real-time status updates
This commit is contained in:
parent
c0da3a6af1
commit
e256d16f8d
8 changed files with 112 additions and 30 deletions
|
@ -21,6 +21,7 @@
|
|||
#include <esp_dmx.h>
|
||||
|
||||
#include <LittleFS.h>
|
||||
#include "websocket.h"
|
||||
#include "routes/config.h"
|
||||
#include "routes/networks.h"
|
||||
#include "routes/status.h"
|
||||
|
@ -418,9 +419,6 @@ void setup()
|
|||
server.on("/networks", HTTP_GET, [](AsyncWebServerRequest *request)
|
||||
{ onGetNetworks(request); });
|
||||
|
||||
server.on("/status", HTTP_GET, [](AsyncWebServerRequest *request)
|
||||
{ onGetStatus(request); });
|
||||
|
||||
server.onRequestBody([](AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total)
|
||||
{
|
||||
if (request->url() == "/config" && request->method() == HTTP_PUT) {
|
||||
|
@ -429,7 +427,8 @@ void setup()
|
|||
ESP.restart();
|
||||
} });
|
||||
|
||||
delay(1000);
|
||||
initWebSocket(&server);
|
||||
|
||||
server.begin();
|
||||
Serial.println("Server started!");
|
||||
|
||||
|
@ -501,4 +500,6 @@ void loop()
|
|||
{
|
||||
transmitDmxToArtnet(dmx2, dmx2_data, universe2);
|
||||
}
|
||||
|
||||
webSocketLoop();
|
||||
}
|
||||
|
|
|
@ -19,7 +19,7 @@ int8_t getWiFiStrength()
|
|||
}
|
||||
}
|
||||
|
||||
void onGetStatus(AsyncWebServerRequest *request)
|
||||
JsonDocument buildStatusJson()
|
||||
{
|
||||
JsonDocument doc;
|
||||
|
||||
|
@ -39,7 +39,5 @@ void onGetStatus(AsyncWebServerRequest *request)
|
|||
doc["psram"]["total"] = ESP.getPsramSize();
|
||||
doc["connection"]["signalStrength"] = getWiFiStrength();
|
||||
|
||||
String jsonString;
|
||||
serializeJson(doc, jsonString);
|
||||
request->send(200, "application/json", jsonString);
|
||||
return doc;
|
||||
}
|
|
@ -2,4 +2,4 @@
|
|||
#include <ArduinoJson.h>
|
||||
#include <driver/temp_sensor.h>
|
||||
|
||||
void onGetStatus(AsyncWebServerRequest *request);
|
||||
JsonDocument buildStatusJson();
|
||||
|
|
52
src/websocket.cpp
Normal file
52
src/websocket.cpp
Normal file
|
@ -0,0 +1,52 @@
|
|||
#include "websocket.h"
|
||||
|
||||
AsyncWebSocket ws("/ws");
|
||||
|
||||
long webSocketLastUpdate = 0;
|
||||
const int WS_UPDATE_INTERVAL = 10 * 1000; // 10 seconds
|
||||
|
||||
String buildStatusString()
|
||||
{
|
||||
JsonDocument doc;
|
||||
doc["type"] = "status";
|
||||
doc["data"] = buildStatusJson();
|
||||
|
||||
String jsonString = "";
|
||||
serializeJson(doc, jsonString);
|
||||
return jsonString;
|
||||
}
|
||||
|
||||
void onEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type, void *arg, uint8_t *data, size_t len)
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
case WS_EVT_CONNECT:
|
||||
Serial.printf("[WS] Client %u connected from %s\n", client->id(), client->remoteIP().toString().c_str());
|
||||
// directly send status to client
|
||||
ws.text(client->id(), buildStatusString());
|
||||
break;
|
||||
case WS_EVT_DISCONNECT:
|
||||
Serial.printf("[WS] Client %u disconnected\n", client->id());
|
||||
break;
|
||||
case WS_EVT_DATA:
|
||||
Serial.printf("[WS] Data received from client %u: %s\n", client->id(), (char *)data);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void webSocketLoop()
|
||||
{
|
||||
if (millis() - webSocketLastUpdate > WS_UPDATE_INTERVAL)
|
||||
{
|
||||
ws.textAll(buildStatusString());
|
||||
webSocketLastUpdate = millis();
|
||||
}
|
||||
}
|
||||
|
||||
void initWebSocket(AsyncWebServer *server)
|
||||
{
|
||||
ws.onEvent(onEvent);
|
||||
server->addHandler(&ws);
|
||||
}
|
11
src/websocket.h
Normal file
11
src/websocket.h
Normal file
|
@ -0,0 +1,11 @@
|
|||
#include <AsyncWebServer_ESP32_W5500.h>
|
||||
#include "routes/status.h"
|
||||
|
||||
#ifndef WEBSOCKET_H
|
||||
#define WEBSOCKET_H
|
||||
|
||||
void initWebSocket(AsyncWebServer *server);
|
||||
|
||||
void webSocketLoop();
|
||||
|
||||
#endif
|
Loading…
Add table
Add a link
Reference in a new issue