-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyaWebSerial.cpp
82 lines (73 loc) · 2.12 KB
/
yaWebSerial.cpp
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
70
71
72
73
74
75
76
77
78
79
80
81
82
#include "yaWebSerial.h"
YaWebSerial::YaWebSerial(AsyncWebServer *server)
{
_server = server;
_events = new AsyncEventSource("/events");
_isEcho = false;
}
void YaWebSerial::begin()
{
// Set routs for the web server
_server->on("/", HTTP_GET, [](AsyncWebServerRequest *request){
request->send(200, "text/html", htmlPage);
});
_server->on("/send", HTTP_POST, [this](AsyncWebServerRequest *request){
if (request->params() == 0) {
request->send(400, "text/plain", "No command provided");
return;
}
for (size_t i = 0; i < request->params(); i++) {
AsyncWebParameter* param = request->getParam(i);
if (param->name() == "command") {
String data = param->value();
if(_receiveFunc != NULL)
{
_receiveFunc(&data);
}
// Send answer to client
if (!data.isEmpty())
{
if (_isEcho)
{
String str = "[" + static_cast<String>(millis()) + "] Echo: " + data + "\n";
request->send(200, "text/plain", str);
}
}
else
{
request->send(400, "text/plain", "No command provided");
}
}
if (param->name() == "checkbox")
{
String echoStatus;
if (param->value() == "1")
{
echoStatus = "ON";
_isEcho = true;
}
else
{
echoStatus = "OFF";
_isEcho = false;
}
request->send(200, "text/plain", "Echo: " + echoStatus + "\n");
}
}
});
// Connect event to URL "/events"
_server->addHandler(_events);
}
void YaWebSerial::msgHandler(CallbackFunction _rcvCb)
{
_receiveFunc = _rcvCb;
}
size_t YaWebSerial::write(uint8_t)
{
return(1);
}
size_t YaWebSerial::write(const uint8_t *buffer, size_t)
{
_events->send((const char *)buffer, "message");
return(1);
}