diff --git a/platformio.ini b/platformio.ini
index 2c3d395..cd2ac77 100644
--- a/platformio.ini
+++ b/platformio.ini
@@ -12,8 +12,8 @@
platform = espressif32
board = esp32dev
framework = arduino
-;lib_deps =
-;; esp32async/ESPAsyncWebServer@^3.12.0
+lib_deps =
+ WebSockets
; upload via OTA
upload_protocol = espota
diff --git a/src/core/configuration.h b/src/core/configuration.h
index ea546c2..be44ff4 100644
--- a/src/core/configuration.h
+++ b/src/core/configuration.h
@@ -1,3 +1,3 @@
// Contains configuration constants:
-#define FIRMWARE_VERSION "1.0.2"
+#define FIRMWARE_VERSION "1.0.3"
diff --git a/src/services/web_service.cpp b/src/services/web_service.cpp
index a0c256b..dc641ec 100644
--- a/src/services/web_service.cpp
+++ b/src/services/web_service.cpp
@@ -4,7 +4,8 @@ WebService::WebService(DashboardState& state)
:
Service("Web", 10),
dashboardState(state),
- server(80)
+ server(80),
+ webSocket(81)
{
}
@@ -65,31 +66,35 @@ h1 {
-
-
-
@@ -105,18 +110,22 @@ Firmware Version: Loading...
)rawliteral";
-
-// server.send(200, "text/html", html);
-// }
-//
-// void handleUptime() {
-// server.send(200, "text/plain", formatUptime());
-// }
-//
-// void handleVersion() {
-// server.send(200, "text/plain", FIRMWARE_VERSION);
-// }
-//
+void WebService::broadcastState()
+{
+ String json = "{";
+
+ json += "\"uptime\":\"";
+ json += dashboardState.uptime;
+ json += "\",";
+
+ json += "\"version\":\"";
+ json += dashboardState.firmwareVersion;
+ json += "\"";
+
+ json += "}";
+
+ webSocket.broadcastTXT(json);
+}
void WebService::begin() {
// Root path
@@ -128,34 +137,59 @@ void WebService::begin() {
);
});
- // Version path
- server.on("/version", [this](){
- server.send(
- 200,
- "text/plain",
- dashboardState.firmwareVersion
- );
- });
-
- // Uptime path
- server.on("/uptime", [this]() {
- server.send(
- 200,
- "text/plain",
- dashboardState.uptime
- );
-
- });
-
-
server.begin();
+ webSocket.begin();
+
+ // Manage web socket connections
+ webSocket.onEvent(
+ [this](uint8_t clientNum,
+ WStype_t type,
+ uint8_t *payload,
+ size_t length)
+ {
+ if (type == WStype_TEXT) {
+ handleWebSocketMessage(
+ clientNum,
+ payload,
+ length
+ );
+ }
+
+ if (type == WStype_CONNECTED) {
+ Serial.println("WebSocket client connected");
+ broadcastState();
+ }
+ }
+ );
+
Serial.println("Web server started");
}
+void WebService::handleWebSocketMessage(uint8_t clientNum, uint8_t *payload, size_t length) {
+ Serial.println("Client number: " + clientNum);
+ Serial.println("Sent a message of length: " + length);
+ Serial.print("Saying: ");
+ for (int ii = 0; ii < length; ii ++) {
+ Serial.print(payload[ii]);
+ }
+ Serial.print("\n\n\n");
+}
+
void WebService::update()
{
dashboardState.update();
+
server.handleClient();
+ webSocket.loop();
+
+ // Brodcast updates once per second
+ static unsigned long lastUpdate = 0;
+
+ if (millis() - lastUpdate >= 1000)
+ {
+ lastUpdate = millis();
+ broadcastState();
+ }
}
diff --git a/src/services/web_service.h b/src/services/web_service.h
index 86017f4..69878df 100644
--- a/src/services/web_service.h
+++ b/src/services/web_service.h
@@ -1,9 +1,11 @@
#pragma once
-#include "service.h"
-#include "../core/dashboard_state.h"
#include
+#include
+#include "service.h"
+
+#include "../core/dashboard_state.h"
class WebService : public Service {
@@ -17,5 +19,9 @@ public:
private:
WebServer server;
+ WebSocketsServer webSocket;
DashboardState dashboardState;
+
+ void handleWebSocketMessage(uint8_t clientNum, uint8_t *payload, size_t length);
+ void broadcastState();
};