-
Notifications
You must be signed in to change notification settings - Fork 0
Added support WebSocket for control via a WEB-APP #34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| #ifndef COMMUNICATIONWEBSOCKET_H | ||
| #define COMMUNICATIONWEBSOCKET_H | ||
|
|
||
| #include <WiFi.h> | ||
| #include <AsyncTCP.h> | ||
| #include <ESPAsyncWebServer.h> | ||
| #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 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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") | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Przenieś protokół do pliku z konfiguracją. Obecnie będziemy mieli konflikt portów niestety |
||
| { | ||
| 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"); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Konkatenuj zmieenną z numerem portu - użytkownik może chcieć zmienić |
||
| } | ||
| void CommunicationWebSocket::Loop(){ | ||
| checkConnectionTimeout(); | ||
| ws.cleanupClients(); | ||
| } | ||
| void CommunicationWebSocket::SendData(SensorsData* data){ | ||
| ws.textAll((char*)data, sizeof(SensorsData)); | ||
| } | ||
| void CommunicationWebSocket::checkConnectionTimeout() { | ||
| if (millis() - lastDataTime > 500) { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nie zakładaj sztywnego czasu. W pliku konfiguracyjnym mamy zdefiniowany rogue time |
||
| if (sharedData != nullptr) { | ||
|
|
||
| sharedData->throttle = 0; | ||
| sharedData->yaw = 0; | ||
| sharedData->pitch = 0; | ||
| sharedData->roll = 0; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nie możemy w locie nagle wyłączyć silników. Do tego mamy metodę z odpowiednią procedurą. Póki co jest ona pusta, ale planowo będzie odpowiadała za powolne wyłączanie silników tak aby nie zagrażały nikomu |
||
| } | ||
| } | ||
| } | ||
| 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; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rozłączenie nie może powodować nagłego wyłączenia silników. Raz że mamy od tego specjalną funkcję a dwa rozłączenie może być chwilowe |
||
| } | ||
| } | ||
| 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(); | ||
| } | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Super! Dobrze że jest to async!