From 71facb7d18f3964cf36f3ca591eea69c67168e57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20M=C3=BCller?= Date: Thu, 11 Apr 2024 14:38:45 +0200 Subject: [PATCH] Add socket server compatible with original software --- src/main.cpp | 6 +++++- src/socketserver.h | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 src/socketserver.h diff --git a/src/main.cpp b/src/main.cpp index 2172d63..f367f2c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,9 +4,11 @@ #include "credentials.h" #include "webserver.h" +#include "socketserver.h" #include "util.h" static ESP8266WiFiMulti wifiMulti; +static SocketServer socketServer; void setup() { // serial console, for use via USB (also exposed to TXD0/RXD0 GPIOs) @@ -43,10 +45,12 @@ void setup() { // configure HTTP endpoints configureWebServer(); - // don't forget to start the webserver! + // don't forget to start all the services webServer.begin(); + socketServer.begin(); } void loop() { webServer.handleClient(); + socketServer.handleClient(); } diff --git a/src/socketserver.h b/src/socketserver.h new file mode 100644 index 0000000..2ab39ab --- /dev/null +++ b/src/socketserver.h @@ -0,0 +1,34 @@ +#pragma once + +class SocketServer { +public: + static constexpr int Port = 3000; + + void begin() { + _server.begin(); + } + + void handleClient() { + auto client = _server.available(); + + if (client) { + Serial.println("Client connected to socket server: " + client.remoteIP().toString() + ":" + client.remotePort()); + } else { + return; + } + + while (client.connected()) { + while (client.available() > 0) { + auto chr = client.read(); + Serial.write(chr); + Serial1.write(chr); + } + } + client.stop(); + Serial.println(); + Serial.println("Client disconnected"); + } + +private: + WiFiServer _server{Port}; +};