-
Notifications
You must be signed in to change notification settings - Fork 0
/
webserver.ino
88 lines (74 loc) · 2 KB
/
webserver.ino
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
83
84
85
86
87
88
// HTTP SERVER
void setupWebServer()
{
Serial.println("-setup WebServer");
// setup the webpages
server.on("/send", []() {
handleSend();
});
server.on("/", []() {
handleRoot();
});
server.onNotFound ( handleNotFound );
server.begin();
Serial.println("-setup WebServer - OK");
}
void handleRoot()
{
String out = "<html><head><title>";
out += HOSTNAME;
out += "</title></head>";
out += "<body>";
out += "Ready to serve, master!";
out += "<br><br>Last received code was: ";
out += lastcode;
out += "<br><br>Send a code:<br>";
out += "<form action='/send'>Password:<input type='text' name='pass'> Code:<input type='text' name='code'><input type='submit' value='send'></input></form>";
out += "</body>";
out += "</html>";
server.send(200, "text/html", out);
}
void handleSend()
{
Serial.println("Connection received");
// check authorization
if (!server.hasArg("pass") || server.arg("pass") != HTTP_PASSCODE)
{
Serial.println("Wrong Passcode! ACCESS DENIED");
server.send(401, "text/html", "Unauthorized");
return;
}
else
{
if (server.hasArg("code"))
{
int code = server.arg("code").toInt();
rf_send(code);
server.send(200, "text/html", "code sent");
Serial.print("code send:");
Serial.println(code);
}
else
{
server.send(200, "text/html", "code error");
Serial.println("no code supplied error");
}
}
}
void handleNotFound()
{
Serial.println("connection - not found");
String message = "File Not Found\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += ( server.method() == HTTP_GET ) ? "GET" : "POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for ( uint8_t i = 0; i < server.args(); i++ )
{
message += " " + server.argName ( i ) + ": " + server.arg ( i ) + "\n";
}
server.send ( 404, "text/plain", message );
}