Repository navigation
Expand file tree
/
Copy pathyarcweb.cpp
More file actions
69 lines (61 loc) · 1.64 KB
/
Copy pathyarcweb.cpp
File metadata and controls
69 lines (61 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include "yarcweb.h"
const char *ssid = "YARC";
const char *password = "123456789";
const char *PARAM_MESSAGE = "message";
AsyncWebServer server(80);
AsyncWebSocket ws("/ws");
bool webSocketConnected = false;
bool monitoring = false;
void notifyClients(const char *message)
{
if (webSocketConnected && monitoring)
{
ws.textAll(message);
}
}
void handleWebSocketMessage(void *arg, uint8_t *data, size_t len)
{
AwsFrameInfo *info = (AwsFrameInfo *)arg;
if (info->final && info->index == 0 && info->len == len && info->opcode == WS_TEXT)
{
data[len] = 0;
if (strcmp((char *)data, "startMonitoring") == 0)
{
monitoring = true;
}
else if (strcmp((char *)data, "stopMonitoring") == 0)
{
monitoring = false;
}
}
}
void onEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type,
void *arg, uint8_t *data, size_t len)
{
switch (type)
{
case WS_EVT_CONNECT:
webSocketConnected = true;
Serial.printf("WebSocket client #%u connected from %s\n", client->id(), client->remoteIP().toString().c_str());
break;
case WS_EVT_DISCONNECT:
webSocketConnected = false;
Serial.printf("WebSocket client #%u disconnected\n", client->id());
break;
case WS_EVT_DATA:
handleWebSocketMessage(arg, data, len);
break;
case WS_EVT_PONG:
case WS_EVT_ERROR:
break;
}
}
void initWebSocket()
{
ws.onEvent(onEvent);
server.addHandler(&ws);
}
void notFound(AsyncWebServerRequest *request)
{
request->send(404, "text/plain", "Not found");
}