-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServer.ino
125 lines (101 loc) · 3.41 KB
/
Server.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <WiFi.h>
#include <WebServer.h>
#include <ArduinoJson.h>
/* Put your SSID & Password */
const char* ssid = "CueTune"; // Enter SSID here
const char* password = "password"; //Enter Password here
/* Put IP Address details */
IPAddress local_ip(192,168,1,2);
IPAddress gateway(192,168,1,1);
IPAddress subnet(255,255,255,0);
WebServer server(80);
void server_setup() {
Serial.begin(9600);
WiFi.softAP(ssid, password);
WiFi.softAPConfig(local_ip, gateway, subnet);
delay(100);
server.on("/", handle_on_connect);
server.on("/upload", handle_upload);
server.on("/rfid", handle_rfid);
server.onNotFound(handle_not_found);
server.begin();
}
void server_loop() {
server.handleClient();
}
void handle_on_connect() {
server.send(200, "text/html", SendHTML());
}
void handle_upload() {
String body = server.arg("plain");
// Parse JSON
DynamicJsonDocument data(1024);
DeserializationError error = deserializeJson(data, body);
if (error) {
Serial.println("JSON Parsing Error: " + String(error.c_str()));
server.send(400, "application/json", "{\"error\": \"Invalid JSON\"}");
return;
}
String str = data["data"];
bool success = write_data(str);
if (success) server.send(200, "application/json", "{\"data\": \"Uploaded data\"}");
else server.send(500, "application/json", "{\"data\": \"Error writing data\"}");
}
void handle_rfid() {
String data = read_data();
server.send(200, "application/json", "{\"data\": \"" + data + "\"}");
}
void handle_not_found() {
Serial.println("404 Not Found: " + server.uri()); // Print the request URI
server.send(404, "text/plain", "Not Found");
}
String SendHTML(){
String ptr = R"delimiter(
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0" charset="UTF-8">
<title>CueTune</title>
<style>
</style>
</head>
<body>
<div id="main">
<H1> Cue Tune</H1>
<label for="rfid_input">RFID Data</label>
<input type="text" id="rfid_input" />
<button id="upload_button" type="button">Upload Data!</button>
<p id="rfid_data"><p>
<button id="rfid_button" type="button">Get RFID data!</button>
</div>
<script>
document.getElementById('upload_button').addEventListener('click', () => {
const rfid_data = document.getElementById("rfid_input").value;
fetch("upload", {
headers: { "Content-Type": "application/json"},
method: "POST",
body: JSON.stringify({data: rfid_data})
})
.then(res => {
if (res.status != 200) alert("Sorry, there was an error uploading!")
})
});
document.getElementById('rfid_button').addEventListener('click', () => {
fetch("rfid", {
headers: { "Content-Type": "application/json"},
method: "GET",
})
.then(res => {
if (res.status != 200) alert("Sorry, there was an error uploading!")
else return res.json();
})
.then(json => {
document.getElementById('rfid_data').innerText = "Data:" + json.data;
})
});
</script>
</body>
</html>
)delimiter";
return ptr;
}