forked from dragondaud/myClock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebServer.ino
305 lines (289 loc) · 12.6 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
// Web Server for configuration and updates
static const char* serverHead PROGMEM =
"<!DOCTYPE HTML><html><head>\n<title>myClock</title>\n"
"<meta name='viewport' content='width=device-width, initial-scale=.83'>"
"<link href='stylesheet.css' rel='stylesheet' type='text/css'></head>\n"
"<body><a href='https://github.com/dragondaud/myClock' target='_blank'>\n"
"<h1>myClock " VERSION "</h1></a>\n";
static const char* serverStyle PROGMEM =
"body {background-color: DarkSlateGray; color: White; font-family: sans-serif;}\n"
"a {text-decoration: none; color: LightSteelBlue;}\n"
"a:hover {text-decoration: underline; color: SteelBlue;}\n"
"div {min-width: 420px; max-width: 600px; border: ridge; padding: 10px; background-color: SlateGray;}\n"
"td {padding: 4px; text-align: left;}\n"
"th {padding: 4px; text-align: right;}\n"
"input[type=range] {vertical-align: middle;}\n"
"input[type=file]::-webkit-file-upload-button {padding:10px 15px; -webkit-border-radius: 5px;}\n"
".button {padding:10px 15px; background:#ccc; -webkit-border-radius: 5px;}\n"
"meter {width: 400px; vertical-align: middle;}\n"
"meter::after {content: attr(value); position:relative; top:-17px; color: Black;}\n"
"meter::-webkit-meter-bar {background: none; background-color: LightBlue; "
"box-shadow: 5px 5px 5px SlateGray inset; border: 1px solid; }\n"
".switch {position: relative; display: inline-block; width: 60px; height: 34px;}\n"
".switch input {opacity: 0; width: 0; height: 0;}\n"
".slider {position: absolute; cursor: pointer; top: 0; left: 0; right: 0; bottom: 0; background-color: #ccc; -webkit-transition: .4s; transition: .4s;}\n"
".slider:before {position: absolute; content: ''; height: 26px; width: 26px; left: 4px; bottom: 4px; background-color: white; -webkit-transition: .4s; transition: .4s;}\n"
"input:checked + .slider {background-color: DarkSlateGray;}\n"
"input:focus + .slider {box-shadow: 0 0 1px SlateGray;}\n"
"input:checked + .slider:before {-webkit-transform: translateX(26px); -ms-transform: translateX(26px); transform: translateX(26px);}\n"
".slider.round {border-radius: 34px;}\n"
".slider.round:before {border-radius: 50%;}\n";
static const char* serverOptions PROGMEM =
"<div><h3>%host%</h3>\n"
"<form method='POST' action='/options' id='optionsForm' name='optionsForm'>\n"
"<table><tr><th><label for='myColor'>Color</label></th>\n"
"<td><input type='color' id='myColor' name='myColor' value='%myColor%'></td></tr>\n"
"<tr><th><label for='brightness'>Brightness</label></th>\n"
"<td><input type='number' id='brightNum' name='brightNum' style='width: 3em'"
"min='1' max='255' value='%brightness%' oninput='brightness.value=brightNum.value'> \n"
"<input type='range' id='brightness' name='brightness' "
"min='1' max='255' value='%brightness%' oninput='brightNum.value=brightness.value'></td></tr>\n"
"<tr><th><label for='threshold'>Threshold</label></th>\n"
"<td><input type='number' id='threshNum' name='threshNum' style='width: 3em'"
"min='1' max='999' value='%threshold%' oninput='threshold.value=threshNum.value'> \n"
"<input type='range' id='threshold' name='threshold' "
"min='1' max='999' value='%threshold%' oninput='threshNum.value=threshold.value'></td></tr>\n"
"<tr><th><label for='milTime'>24hour Time</label></th>\n"
"<td><label class='switch'><input type='checkbox' id='milTime' name='milTime' %milTime%>"
"<span class='slider round'></span></label></td></tr>\n"
"<tr><th><label for='celsius'>Celsius</label></th>\n"
"<td><label class='switch'><input type='checkbox' id='celsius' name='celsius' %celsius%>"
"<span class='slider round'></span></label></td></tr>\n"
"<tr><th><label for='language'>Language</label></th>\n"
"<td><select name='language' id='language'>\n"
"<option value='hr'>Croatian</option>\n"
"<option value='cz'>Czech</option>\n"
"<option value='nl'>Dutch</option>\n"
"<option value='en'>English</option>\n"
"<option value='fi'>Finnish</option>\n"
"<option value='fr'>French</option>\n"
"<option value='gl'>Galician</option>\n"
"<option value='de'>German</option>\n"
"<option value='hu'>Hungarian</option>\n"
"<option value='it'>Italian</option>\n"
"<option value='la'>Latvian</option>\n"
"<option value='lt'>Lithuanian</option>\n"
"<option value='pl'>Polish</option>\n"
"<option value='pt'>Portuguese</option>\n"
"<option value='sk'>Slovak</option>\n"
"<option value='sl'>Slovenian</option>\n"
"<option value='es'>Spanish</option></select></td></tr>\n"
"<tr><th><label for='timezone'>Time Zone</label></th>\n"
"<td><input type='text' id='timezone' name='timezone' value='%timezone%'></td></tr>\n"
"<tr><th><label for='location'>Postal Code</label></th>\n"
"<td><input type='text' id='location' name='location' size='10' value='%location%'></td></tr>\n"
"<tr><th><label for='tzKey'>TimeZoneDB Key</label></th>\n"
"<td><input type='text' id='tzKey' name='tzKey' value='%tzKey%'></td></tr>\n"
"<tr><th><label for='owKey'>OpenWeatherMap Key</label></th>\n"
"<td><input type='text' id='owKey' name='owKey' value='%owKey%'></td></tr>\n"
#ifdef SYSLOG
"<tr><th><label for='syslogSrv'>SysLog Server</label></th>\n"
"<td><input type='text' id='syslogSrv' name='owKey' value='%syslogSrv%'></td> \n"
"<th><label for='syslogPort'>Port</label></th>\n"
"<td><input type='number' id='syslogPort' name='owKey' min='1' max='65535' value='%syslogPort%'></td></tr>\n"
#endif
"<tr><th><label for='softAPpass'>Admin Password</label></th>\n"
"<td><input type='password' id='softAPpass' name='softAPpass'\n"
"minlength='8' placeholder='enter new password'></td></tr>\n"
"<tr><td style='text-align: right'>Free Heap: %heap%</td>\n"
#ifdef LIGHT
"<td>Light Level: %light%</td>\n"
#endif
"<td><input type='submit' class='button' value='APPLY CONFIG' autofocus></td>\n"
"</tr></table></form></div><p>\n";
static const char* serverUpdate PROGMEM =
"<div><h3>Update Firmware</h3>\n"
"<form method='POST' action='/update' enctype='multipart/form-data'>\n"
"<input type='file' name='update'>\n"
"<input type='submit' value='UPDATE' class='button'></form><p>\n"
"<p><span><form method='GET' action='/reset' style='display:inline'>\n"
"<input type='submit' value='REBOOT CLOCK' class='button'></form>\n"
"<form method='GET' action='/download' style='display:inline'>\n"
"<input type='submit' value='SAVE CONFIG' class='button'></form>\n"
"<form method='GET' action='/logout' style='display:inline'>\n"
"<input type='submit' value='LOGOUT' class='button'></form></span>\n"
"<p></div></body></html>\n";
static const char* serverReboot PROGMEM =
"<!DOCTYPE HTML><html><head>\n"
"<meta http-equiv=\"refresh\" content=\"15;url=/\" />"
"<style>body {background-color: DarkSlateGray; color: White;}"
"</style></head>\n"
"<body><h1>myClock " VERSION "</h1>"
"Rebooting...</body></html>";
static const char* textPlain PROGMEM = "text/plain";
static const char* textHtml PROGMEM = "text/html";
static const char* textCss PROGMEM = "text/css";
static const char* checked PROGMEM = "checked";
void handleNotFound() {
#ifdef SYSLOG
syslog.log(F("webServer: Not Found"));
#endif
server.sendHeader(F("Location"), F("/"));
server.send(301);
}
bool handleAuth() {
return server.authenticate(ADMIN_USER, softAPpass.c_str());
}
void reqAuth() {
return server.requestAuthentication(DIGEST_AUTH, HOST);
}
void handleStyle() {
if (!handleAuth()) return reqAuth();
server.send(200, textCss, String(serverStyle));
}
void handleOptions() {
if (!handleAuth()) return reqAuth();
if (!server.hasArg(F("myColor"))) return server.send(503, textPlain, F("FAILED"));
String c = server.arg(F("myColor"));
if (c != "") myColor = htmlColor565(c);
uint8_t b = server.arg(F("brightness")).toInt();
if (b) brightness = b;
else brightness = 255;
int t = server.arg(F("threshold")).toInt();
if (t) threshold = t;
else threshold = 500;
c = server.arg(F("milTime"));
if (c == "on") milTime = true;
else milTime = false;
c = server.arg(F("celsius"));
if (c == "on") celsius = true;
else celsius = false;
c = server.arg(F("language"));
if (c != "") language = c;
c = server.arg(F("softAPpass"));
if (c != "") softAPpass = c;
c = server.arg(F("location"));
if (c != "") location = c;
c = server.arg(F("timezone"));
if (c != "") {
c.trim();
c.replace(' ', '_');
if (timezone != c) {
timezone = c;
setNTP(timezone);
delay(1000);
}
}
c = server.arg(F("tzKey"));
if (c != "") tzKey = c;
c = server.arg(F("owKey"));
if (c != "") owKey = c;
displayDraw(brightness);
getWeather();
writeSPIFFS();
server.sendHeader(F("Location"), F("/"));
server.send(301);
}
void handleRoot() {
if (!handleAuth()) return reqAuth();
size_t fh = ESP.getFreeHeap();
#ifdef SYSLOG
syslog.log(F("webServer: root"));
#endif
server.sendHeader(F("Connection"), F("close"));
time_t now = time(nullptr);
String t = ctime(&now);
t.trim();
t = String(F("<span style='float: right'>")) + t + String(F("</span>"));
char c[8];
sprintf(c, "#%06X", color565to888(myColor));
String payload = String(serverHead);
#ifdef DS18
payload += PSTR("<p><meter value='") + String(Temp) + PSTR("' min='-50' max='150'></meter> Temperature\n");
#endif
payload += String(serverOptions);
payload.replace(F("%host%"), String(HOST) + t);
payload.replace(F("%myColor%"), String(c));
payload.replace(F("%brightness%"), String(brightness));
payload.replace(F("%threshold%"), String(threshold));
payload.replace(F("%milTime%"), milTime ? checked : "");
payload.replace(F("%celsius%"), celsius ? checked : "");
payload.replace(PSTR("'") + String(language) + PSTR("'"),
PSTR("'") + String(language) + PSTR("'") + PSTR(" selected"));
payload.replace(F("%location%"), String(location));
payload.replace(F("%timezone%"), String(timezone));
payload.replace(F("%tzKey%"), String(tzKey));
payload.replace(F("%owKey%"), String(owKey));
#ifdef SYSLOG
payload.replace(F("%syslogSrv%"), String(syslogSrv));
payload.replace(F("%syslogPort%"), String(syslogPort));
#endif
payload.replace(F("%heap%"), String(fh));
#ifdef LIGHT
payload.replace(F("%light%"), String(light));
#endif
payload += String(serverUpdate);
server.send(200, textHtml, payload);
}
void handleReset() {
if (!handleAuth()) return reqAuth();
#ifdef SYSLOG
syslog.log(F("webServer: reset"));
#endif
OUT.println(F("webServer: reset"));
server.send(200, textHtml, serverReboot);
server.close();
delay(1000);
ESP.restart();
}
void handleDownload() { // send running config as JSON object to browser
if (!handleAuth()) return reqAuth();
String payload = getSPIFFS();
server.sendHeader(F("Content-Disposition"), F("attachment; filename=config.json"));
server.send(200, F("application/json"), payload);
}
void handleLogout() {
server.send(401, textPlain, F("logged out"));
}
void startWebServer() {
server.on(F("/"), HTTP_GET, handleRoot);
server.on(F("/stylesheet.css"), HTTP_GET, handleStyle);
server.on(F("/options"), handleOptions);
server.on(F("/reset"), HTTP_GET, handleReset);
server.on(F("/download"), HTTP_GET, handleDownload);
server.on(F("/logout"), HTTP_GET, handleLogout);
server.on(F("/favicon.ico"), HTTP_GET, []() {
server.sendHeader(F("Location"), F("https://www.arduino.cc/favicon.ico"));
server.send(301);
});
server.on(F("/update"), HTTP_POST, []() { // upload and flash new image
if (!handleAuth()) return reqAuth();
#ifdef SYSLOG
syslog.log(F("webServer: update"));
#endif
server.send(200, textPlain, (Update.hasError()) ? F("FAIL") : F("OK"));
server.close();
delay(1000);
ESP.restart();
}, []() {
if (!handleAuth()) return reqAuth();
HTTPUpload& upload = server.upload();
if (upload.status == UPLOAD_FILE_START) {
display_ticker.detach();
#if defined(ESP8266)
WiFiUDP::stopAll();
#else
MDNS.end();
#endif
OUT.printf_P(PSTR("Update: %s\n"), upload.filename.c_str());
uint32_t maxSketchSpace = (ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000;
if (!Update.begin(maxSketchSpace)) { //start with max available size
Update.printError(Serial);
}
} else if (upload.status == UPLOAD_FILE_WRITE) {
if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) {
Update.printError(Serial);
}
} else if (upload.status == UPLOAD_FILE_END) {
if (Update.end(true)) { //true to set the size to the current progress
OUT.printf_P(PSTR("Update Success: %u\nRebooting...\n"), upload.totalSize);
} else {
Update.printError(Serial);
}
}
yield();
}); // server.on("/update")
server.onNotFound(handleNotFound);
server.begin();
MDNS.addService(F("_http"), F("_tcp"), 80);
}