diff --git a/WST-FC/include/communicationModules/CommunicationWebSocket.h b/WST-FC/include/communicationModules/CommunicationWebSocket.h new file mode 100644 index 0000000..0642feb --- /dev/null +++ b/WST-FC/include/communicationModules/CommunicationWebSocket.h @@ -0,0 +1,37 @@ +#ifndef COMMUNICATIONWEBSOCKET_H +#define COMMUNICATIONWEBSOCKET_H + +#include +#include +#include +#include "DroneData.h" +#include "Configuration.h" +#include "ICommunicationInterface.h" + +class CommunicationWebSocket : public ICommunicationInterface { +private: + static CommunicationWebSocket* instance; + DroneControlData *sharedData; + DroneStatus *droneStatus; + + unsigned long lastUpdate{0}; + unsigned long lastDataTime = 0; + + AsyncWebServer server; + AsyncWebSocket ws; + +public: + CommunicationWebSocket(DroneControlData *dataPtr, DroneStatus *status); + + void Init() override; + void Loop() override; + void SendData(SensorsData* data) override; + void SendData(DroneControlData* data); + + + static void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type, void *arg, uint8_t *data, size_t len); + + void checkConnectionTimeout(); +}; + +#endif \ No newline at end of file diff --git a/WST-FC/include/communicationModules/ICommunicationInterface.h b/WST-FC/include/communicationModules/ICommunicationInterface.h index 62a82ee..9295471 100644 --- a/WST-FC/include/communicationModules/ICommunicationInterface.h +++ b/WST-FC/include/communicationModules/ICommunicationInterface.h @@ -8,5 +8,6 @@ class ICommunicationInterface{ virtual void Init(); virtual void Loop(); virtual void SendData(SensorsData* data); + virtual void SendData(DroneControlData* data); }; #endif \ No newline at end of file diff --git a/WST-FC/platformio.ini b/WST-FC/platformio.ini index f44cd63..ef70198 100644 --- a/WST-FC/platformio.ini +++ b/WST-FC/platformio.ini @@ -21,6 +21,8 @@ lib_deps = br3ttb/PID@^1.2.1 adafruit/Adafruit ADXL345@^1.3.4 adafruit/Adafruit Unified Sensor@^1.1.14 + esp32async/AsyncTCP@^3.4.10 + esp32async/ESPAsyncWebServer@^3.10.3 [env:esp32-c3-super-mini] platform = espressif32 @@ -38,3 +40,5 @@ lib_deps = br3ttb/PID@^1.2.1 adafruit/Adafruit ADXL345@^1.3.4 adafruit/Adafruit Unified Sensor@^1.1.14 + esp32async/AsyncTCP@^3.4.10 + esp32async/ESPAsyncWebServer@^3.10.3 diff --git a/WST-FC/src/communicationModules/CommunicationWebSocket.cpp b/WST-FC/src/communicationModules/CommunicationWebSocket.cpp new file mode 100644 index 0000000..41010a5 --- /dev/null +++ b/WST-FC/src/communicationModules/CommunicationWebSocket.cpp @@ -0,0 +1,64 @@ +#include "Configuration.h" +#include "communicationModules/CommunicationWebSocket.h" +CommunicationWebSocket* CommunicationWebSocket::instance = nullptr; +CommunicationWebSocket::CommunicationWebSocket(DroneControlData *dataPtr, DroneStatus *status) + : server(80), ws("/ws") +{ + sharedData = dataPtr; + droneStatus = status; + instance = this; +} +void CommunicationWebSocket::Init(){ + ws.onEvent(onWsEvent); + server.addHandler(&ws); + server.begin(); + Serial.println("WebSocket server started on port 80"); +} +void CommunicationWebSocket::Loop(){ + checkConnectionTimeout(); + ws.cleanupClients(); +} +void CommunicationWebSocket::SendData(SensorsData* data){ + ws.textAll((char*)data, sizeof(SensorsData)); +} +void CommunicationWebSocket::checkConnectionTimeout() { + if (millis() - lastDataTime > 500) { + if (sharedData != nullptr) { + + sharedData->throttle = 0; + sharedData->yaw = 0; + sharedData->pitch = 0; + sharedData->roll = 0; + } + } +} +void CommunicationWebSocket::onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, + AwsEventType type, void *arg, uint8_t *data, size_t len) +{ + if (type == WS_EVT_CONNECT) { + Serial.printf("[WS] Client connected! ID: %u\n", client->id()); + } + else if (type == WS_EVT_DISCONNECT) { + Serial.printf("[WS] Client disconnected! ID: %u\n", client->id()); + + if (instance && instance->sharedData) { + instance->sharedData->throttle = 0; + instance->sharedData->yaw = 0; + instance->sharedData->pitch = 0; + instance->sharedData->roll = 0; + } + } + else if (type == WS_EVT_DATA) { + AwsFrameInfo *info = (AwsFrameInfo*)arg; + + if (info->final && info->index == 0 && info->len == len && info->opcode == WS_BINARY) { + + if (len == sizeof(DroneControlData) && instance && instance->sharedData) { + + memcpy(instance->sharedData, data, sizeof(DroneControlData)); + + instance->lastDataTime = millis(); + } + } + } +} \ No newline at end of file