First rough implementation of websockets

This commit is contained in:
GHOSCHT 2022-04-10 17:12:44 +02:00
parent c842e1a1e7
commit c764a7cdee
8 changed files with 114 additions and 49 deletions

1
Firmware/.gitignore vendored
View file

@ -3,3 +3,4 @@
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/ipch
include/Credentials.h

View file

@ -0,0 +1,2 @@
#define WIFI_SSID "ssid"
#define WIFI_PW "password"

View file

@ -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

View file

@ -0,0 +1,50 @@
#include <WebsocketCommunicator.h>
#include <ESPAsyncWebServer.h>
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");
}

View file

@ -0,0 +1,18 @@
#include <Communicator.h>
#include <ESPAsyncWebServer.h>
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;
};

View file

@ -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

View file

@ -2,29 +2,59 @@
#include <LightController.h>
#include <SerialCommunicator.h>
#include <BluetoothCommunicator.h>
#include <PinMap.h>
#include <WebsocketCommunicator.h>
#include <PinMap/PinMap.h>
#include <esp_spi_flash.h>
#include <WiFi.h>
#include <ESPAsyncWebServer.h>
#include <Credentials/Credentials.h>
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();
}