-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebServer.ino
193 lines (180 loc) · 5.3 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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include <WebServer.h>
WebServer server(80);
Ticker webServerTask;
File fsUploadFile;
void setupWebServer() {
server.on("/status", HTTP_GET, []() {
if (hasSSIDStored()) {
readFile("/disconnect.html");
} else {
readFile("/connect.html");
}
});
server.on("/stats/restart-count", HTTP_GET, []() {
server.send(200, "text/html", String(preferences.getUInt("counter", 0)));
});
server.on("/stats/node-id", HTTP_GET, []() {
server.send(200, "text/html", nodeId());
});
server.on("/stats/dht/humidity", HTTP_GET, []() {
server.send(200, "text/html", String(dhtHumidity));
});
server.on("/stats/dht/temperature", HTTP_GET, []() {
server.send(200, "text/html", String(dhtTemperature));
});
server.on("/led/on", HTTP_POST, []() {
digitalWrite(LED_BUILTIN, HIGH);
okResponse();
});
server.on("/led/off", HTTP_POST, []() {
digitalWrite(LED_BUILTIN, LOW);
okResponse();
});
server.on("/cpu", HTTP_GET, []() {
server.send(200, "text/html", String(ESP.getCpuFreqMHz()));
});
server.on("/cpu/80", HTTP_POST, []() {
setCpuFrequencyMhz(80);
server.send(200, "text/html", "80");
});
server.on("/cpu/160", HTTP_POST, []() {
setCpuFrequencyMhz(160);
server.send(200, "text/html", "160");
});
server.on("/cpu/240", HTTP_POST, []() {
setCpuFrequencyMhz(240);
server.send(200, "text/html", "240");
});
server.on("/wifi", HTTP_DELETE, []() {
unsetStorage();
readFile("/connect.html");
});
server.on("/wifi", HTTP_POST, []() {
if (!server.hasArg("ssid") || server.arg("ssid").isEmpty()) {
server.sendHeader("HX-Retarget", "#error-message");
server.sendHeader("HX-Reswap", "innerHTML");
return server.send(200, "text/html", hError("Error: You are missing the Wifi name."));
}
if (!server.hasArg("password") || server.arg("password").isEmpty()) {
server.sendHeader("HX-Retarget", "#error-message");
server.sendHeader("HX-Reswap", "innerHTML");
return server.send(200, "text/html", hError("Error: Your Password is empty."));
}
String ssid = server.arg("ssid");
ssid.trim();
String password = server.arg("password");
password.trim();
WiFi.begin(ssid, password);
uint8_t status = WiFi.waitForConnectResult(3000);
Serial.print("Wifi Status: ");
Serial.println(status);
if (status == WL_NO_SSID_AVAIL) {
server.sendHeader("HX-Retarget", "#error-message");
server.sendHeader("HX-Reswap", "innerHTML");
return server.send(200, "text/html", hError("Error: We couldn't find that Wifi name."));
}
if (status == WL_CONNECT_FAILED || status == WL_DISCONNECTED) {
server.sendHeader("HX-Retarget", "#error-message");
server.sendHeader("HX-Reswap", "innerHTML");
return server.send(200, "text/html", hError("Error: Password was not accepted."));
}
setWifiConnection(ssid, password);
readFile("/disconnect.html");
});
server.on("/list", HTTP_GET, []() {
if (!server.hasArg("dir")) {
server.send(500, "text/plain", "BAD ARGS");
return;
}
String path = server.arg("dir");
File root = LittleFS.open(path);
path = String();
String output = "";
if (root.isDirectory()) {
File file = root.openNextFile();
while (file) {
output += "{\"type\":\"";
output += (file.isDirectory()) ? "dir" : "file";
output += "\",\"name\":\"";
output += String(file.path()).substring(1);
output += "\"}\n";
file = root.openNextFile();
}
}
server.send(200, "text/jsonl", output);
});
server.on(
"/file", HTTP_POST, []() {
okResponse();
},
[]() {
HTTPUpload& upload = server.upload();
if (upload.status == UPLOAD_FILE_START) {
String filename = upload.filename;
if (!filename.startsWith("/")) {
filename = "/" + filename;
}
fsUploadFile = LittleFS.open(filename, "w");
return;
}
if (!fsUploadFile) {
return;
}
if (upload.status == UPLOAD_FILE_WRITE) {
yield();
fsUploadFile.write(upload.buf, upload.currentSize);
return;
}
if (upload.status == UPLOAD_FILE_END) {
fsUploadFile.close();
return;
}
});
server.on("/file", HTTP_GET, []() {
if (!server.hasArg("path")) {
server.send(500, "text/plain", "BAD ARGS");
return;
}
if (!readFile(server.arg("path"))) {
fileNotFound();
}
});
server.on("/file", HTTP_DELETE, []() {
if (!server.hasArg("path")) {
server.send(500, "text/plain", "BAD ARGS");
return;
}
if (!LittleFS.exists(server.arg("path"))) {
fileNotFound();
}
LittleFS.remove(server.arg("path"));
okResponse();
});
server.onNotFound([]() {
if (!readFile("/index.html")) {
fileNotFound();
}
});
server.begin();
webServerTask.attach_ms(10, []() {
stepWebServer();
});
}
void okResponse() {
server.send(200, "text/plain", "OK");
}
void fileNotFound() {
server.send(404, "text/plain", "File Not Found");
}
bool readFile(String path) {
if (LittleFS.exists(path)) {
File file = LittleFS.open(path, "r");
server.streamFile(file, getContentType(path, server.hasArg("download")));
file.close();
return true;
}
return false;
}
void stepWebServer() {
server.handleClient();
}