From 8c515fa1569c6242eb19c0fc8415beb8b5f88d02 Mon Sep 17 00:00:00 2001 From: GHOSCHT <31184695+GHOSCHT@users.noreply.github.com> Date: Sun, 10 Apr 2022 17:12:44 +0200 Subject: [PATCH] First rough implementation of websockets --- modules/control/Firmware/.gitignore | 1 + .../Credentials/Credentials.h.template | 2 + .../Firmware/{lib => include}/PinMap/PinMap.h | 0 modules/control/Firmware/include/README | 39 --------------- .../WebsocketCommunicator.cpp | 50 +++++++++++++++++++ .../WebsocketCommunicator.h | 18 +++++++ modules/control/Firmware/platformio.ini | 5 +- modules/control/Firmware/src/main.cpp | 48 ++++++++++++++---- 8 files changed, 114 insertions(+), 49 deletions(-) create mode 100644 modules/control/Firmware/include/Credentials/Credentials.h.template rename modules/control/Firmware/{lib => include}/PinMap/PinMap.h (100%) delete mode 100644 modules/control/Firmware/include/README create mode 100644 modules/control/Firmware/lib/WebsocketCommunicator/WebsocketCommunicator.cpp create mode 100644 modules/control/Firmware/lib/WebsocketCommunicator/WebsocketCommunicator.h diff --git a/modules/control/Firmware/.gitignore b/modules/control/Firmware/.gitignore index 89cc49c..e2929d3 100644 --- a/modules/control/Firmware/.gitignore +++ b/modules/control/Firmware/.gitignore @@ -3,3 +3,4 @@ .vscode/c_cpp_properties.json .vscode/launch.json .vscode/ipch +include/Credentials.h \ No newline at end of file diff --git a/modules/control/Firmware/include/Credentials/Credentials.h.template b/modules/control/Firmware/include/Credentials/Credentials.h.template new file mode 100644 index 0000000..7e7fe08 --- /dev/null +++ b/modules/control/Firmware/include/Credentials/Credentials.h.template @@ -0,0 +1,2 @@ +#define WIFI_SSID "ssid" +#define WIFI_PW "password" \ No newline at end of file diff --git a/modules/control/Firmware/lib/PinMap/PinMap.h b/modules/control/Firmware/include/PinMap/PinMap.h similarity index 100% rename from modules/control/Firmware/lib/PinMap/PinMap.h rename to modules/control/Firmware/include/PinMap/PinMap.h diff --git a/modules/control/Firmware/include/README b/modules/control/Firmware/include/README deleted file mode 100644 index 45496b1..0000000 --- a/modules/control/Firmware/include/README +++ /dev/null @@ -1,39 +0,0 @@ - -This directory is intended for project header files. - -A header file is a file containing C declarations and macro definitions -to be shared between several project source files. You request the use of a -header file in your project source file (C, C++, etc) located in `src` folder -by including it, with the C preprocessing directive `#include'. - -```src/main.c - -#include "header.h" - -int main (void) -{ - ... -} -``` - -Including a header file produces the same results as copying the header file -into each source file that needs it. Such copying would be time-consuming -and error-prone. With a header file, the related declarations appear -in only one place. If they need to be changed, they can be changed in one -place, and programs that include the header file will automatically use the -new version when next recompiled. The header file eliminates the labor of -finding and changing all the copies as well as the risk that a failure to -find one copy will result in inconsistencies within a program. - -In C, the usual convention is to give header files names that end with `.h'. -It is most portable to use only letters, digits, dashes, and underscores in -header file names, and at most one dot. - -Read more about using header files in official GCC documentation: - -* Include Syntax -* Include Operation -* Once-Only Headers -* Computed Includes - -https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html diff --git a/modules/control/Firmware/lib/WebsocketCommunicator/WebsocketCommunicator.cpp b/modules/control/Firmware/lib/WebsocketCommunicator/WebsocketCommunicator.cpp new file mode 100644 index 0000000..e29690e --- /dev/null +++ b/modules/control/Firmware/lib/WebsocketCommunicator/WebsocketCommunicator.cpp @@ -0,0 +1,50 @@ +#include +#include + +WebsocketCommunicator::WebsocketCommunicator(AsyncWebSocket &socket, AsyncWebServer &server, __SIZE_TYPE__ bufferSize) : Communicator(bufferSize), socket(socket), server(server) +{ + AwsEventHandler onEvent = [this](AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type, + void *arg, uint8_t *data, size_t len) + { + switch (type) + { + case WS_EVT_CONNECT: + Serial.printf("WebSocket client #%u connected from %s\n", client->id(), client->remoteIP().toString().c_str()); + break; + case WS_EVT_DISCONNECT: + Serial.printf("WebSocket client #%u disconnected\n", client->id()); + break; + case WS_EVT_DATA: + handleMessage(arg, data, len); + break; + case WS_EVT_PONG: + case WS_EVT_ERROR: + break; + } + }; + + socket.onEvent(onEvent); + server.addHandler(&socket); +} + +void WebsocketCommunicator::sendMessage(int *values, __SIZE_TYPE__ numberOfValues) +{ + char message[calculateMessageOutSize(numberOfValues)]; + parseIDs(values, numberOfValues, message); + sendMessage(message); +} + +void WebsocketCommunicator::sendMessage(const char message[]) +{ + socket.textAll(message); +} + +char *WebsocketCommunicator::receiveMessage() +{ + return getBuffer(); +} + +void WebsocketCommunicator::handleMessage(void *arg, uint8_t *data, size_t len) +{ + Serial.println("data"); +} \ No newline at end of file diff --git a/modules/control/Firmware/lib/WebsocketCommunicator/WebsocketCommunicator.h b/modules/control/Firmware/lib/WebsocketCommunicator/WebsocketCommunicator.h new file mode 100644 index 0000000..1e6dbdd --- /dev/null +++ b/modules/control/Firmware/lib/WebsocketCommunicator/WebsocketCommunicator.h @@ -0,0 +1,18 @@ +#include +#include + +class WebsocketCommunicator : public Communicator +{ +private: + void handleMessage(void *arg, uint8_t *data, size_t len); + +protected: + AsyncWebSocket &socket; + AsyncWebServer &server; + +public: + WebsocketCommunicator(AsyncWebSocket &socket, AsyncWebServer &server, __SIZE_TYPE__ bufferSize); + void sendMessage(int *values, __SIZE_TYPE__ numberOfValues) override; + void sendMessage(const char message[]) override; + char *receiveMessage() override; +}; \ No newline at end of file diff --git a/modules/control/Firmware/platformio.ini b/modules/control/Firmware/platformio.ini index 62edb35..de1fa0e 100644 --- a/modules/control/Firmware/platformio.ini +++ b/modules/control/Firmware/platformio.ini @@ -17,4 +17,7 @@ board = az-delivery-devkit-v4 framework = arduino upload_port = COM8 monitor_port = COM8 -lib_deps = erropix/ESP32 AnalogWrite@^0.2 +lib_deps = + erropix/ESP32 AnalogWrite@^0.2 + ESP Async WebServer +board_build.partitions = huge_app.csv diff --git a/modules/control/Firmware/src/main.cpp b/modules/control/Firmware/src/main.cpp index 6f6cc36..a790a26 100644 --- a/modules/control/Firmware/src/main.cpp +++ b/modules/control/Firmware/src/main.cpp @@ -2,29 +2,59 @@ #include #include #include -#include +#include +#include +#include +#include +#include +#include -StreamCommunicator *computer; -StreamCommunicator *phone; -LightController *light; const int STEPS = 5; + BluetoothSerial bt; +AsyncWebServer server(80); +AsyncWebSocket ws("/ws"); + +Communicator *computer; +Communicator *phone; +Communicator *websocket; +LightController *light; + +void TaskBlink(void *pvParameters); +void TaskAnalogRead(void *pvParameters); void setup() { + int bjtCount = 4; const int bjtPin[bjtCount] = {SIG1A, SIG1B, SIG2A, SIG2B}; computer = new SerialCommunicator(Serial, 9600, 5, 50); - phone = new BluetoothCommunicator(bt,5,50); + phone = new BluetoothCommunicator(bt, 5, 50); light = new LightController(bjtPin, bjtCount); + + WiFi.begin(WIFI_SSID, WIFI_PW); + Serial.println("Connecting to WiFi"); + while (WiFi.status() != WL_CONNECTED) + { + Serial.print("."); + delay(1000); + } + Serial.println(WiFi.localIP()); + + websocket = new WebsocketCommunicator(ws, server, 50); } void loop() { light->updateState(computer->receiveMessage(), STEPS); light->updateState(phone->receiveMessage(), STEPS); - computer->sendMessage(light->getBjtState(), light->getBjtCount()); - computer->clearBuffer(); - phone->sendMessage(light->getBjtState(), light->getBjtCount()); - phone->clearBuffer(); + websocket->sendMessage(light->getBjtState(), light->getBjtCount()); + websocket->clearBuffer(); + // computer->sendMessage(light->getBjtState(), light->getBjtCount()); + // computer->clearBuffer(); + // phone->sendMessage(light->getBjtState(), light->getBjtCount()); + // phone->clearBuffer(); + + ws.cleanupClients(); + server.begin(); } \ No newline at end of file