add WebSocket support for real-time status updates

This commit is contained in:
RaffaelW 2025-04-17 22:35:26 +02:00
parent c0da3a6af1
commit e256d16f8d
8 changed files with 112 additions and 30 deletions

View file

@ -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();
}

View file

@ -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;
}

View file

@ -2,4 +2,4 @@
#include <ArduinoJson.h>
#include <driver/temp_sensor.h>
void onGetStatus(AsyncWebServerRequest *request);
JsonDocument buildStatusJson();

52
src/websocket.cpp Normal file
View 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
View 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