forked from Winston-Lu/ESP8266-LED-Shelf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebServer.cpp
387 lines (359 loc) · 15.7 KB
/
WebServer.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
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#include <ESP8266WebServer.h>
#include <FastLED.h>
#include <LittleFS.h> //for file system
#include "WebServer.h"
#include "Lighting.h"
#include "NTPTime.h"
#include "Backlight.h"
ESP8266WebServer webServer(80);
void updateServer(){
webServer.handleClient();
}
void setupServer(){
//Runs when site is loaded
webServer.on("/getsettings", HTTP_GET, []() {
String value = parseSettings();
Serial.println("Sending: " + value);
webServer.sendHeader("Access-Control-Allow-Origin", "*");
webServer.send(200, "text/plain", value);
});
webServer.on("/power", HTTP_GET, []() {
power = (webServer.arg("value")=="true") ? 1 : 0;
lightingChanges.power = true;
lastUpdate = 0;
updateSettings = true;
webServer.send(200, "text/plain", "1");
});
webServer.on("/transparency", HTTP_GET, []() {
setForegroundTransparency((byte)webServer.arg("value").toInt()%256);
lightingChanges.foregroundTransparency = true;
lastUpdate = 0;
updateSettings = true;
webServer.send(200, "text/plain", "1");
});
webServer.on("/autobrightness", HTTP_GET, []() {
autobrightness = (webServer.arg("value")=="true")%256;
lightingChanges.autobrightness = true;
lastUpdate = 0;
updateSettings = true;
webServer.send(200, "text/plain", "1");
});
webServer.on("/brightness", HTTP_GET, []() {
setSegmentBrightness((byte)webServer.arg("value").toInt()%256);
lightingChanges.segmentBrightness = true;
lastUpdate = 0;
updateSettings = true;
webServer.send(200, "text/plain", "1");
});
webServer.on("/spotlightbrightness", HTTP_GET, []() {
setSpotlightBrightness((byte)webServer.arg("value").toInt()%256);
lightingChanges.spotlightBrightness = true;
lastUpdate = 0;
updateSettings = true;
webServer.send(200, "text/plain", "1");
});
webServer.on("/backgroundbrightness", HTTP_GET, []() {
setBackgroundBrightness((byte)webServer.arg("value").toInt()%256);
lightingChanges.backgroundBrightness = true;
lastUpdate = 0;
updateSettings = true;
webServer.send(200, "text/plain", "1");
});
webServer.on("/foregroundTransparency", HTTP_GET, []() {
setForegroundTransparency((byte)webServer.arg("value").toInt()%256);
lightingChanges.foregroundTransparency = true;
lastUpdate = 0;
updateSettings = true;
webServer.send(200, "text/plain", "1");
});
webServer.on("/bgcolor", HTTP_GET, []() {
CRGB color = CRGB::Black;
color.red = webServer.arg("red").toInt()%256;
color.green = webServer.arg("green").toInt()%256;
color.blue = webServer.arg("blue").toInt()%256;
bg = color;
lightingChanges.bg = true;
lastUpdate = 0;
autoEffect = 0; //turn off scheduled lighting effects and toggle back on regular effects
updateSettings = true;
webServer.send(200, "text/plain", "1");
});
webServer.on("/bg2color", HTTP_GET, []() {
CRGB color = CRGB::Black;
color.red = webServer.arg("red").toInt()%256;
color.green = webServer.arg("green").toInt()%256;
color.blue = webServer.arg("blue").toInt()%256;
bg2 = color;
lightingChanges.bg2 = true;
lastUpdate = 0;
autoEffect = 0; //turn off scheduled lighting effects and toggle back on regular effects
updateSettings = true;
webServer.send(200, "text/plain", "1");
});
webServer.on("/h1color", HTTP_GET, []() {
CRGB color = CRGB::Black;
color.red = webServer.arg("red").toInt()%256;
color.green = webServer.arg("green").toInt()%256;
color.blue = webServer.arg("blue").toInt()%256;
h_ten_color = color;
lightingChanges.h_ten_color = true;
lastUpdate = 0;
autoEffect = 0; //turn off scheduled lighting effects and toggle back on regular effects
updateSettings = true;
webServer.send(200, "text/plain", "1");
});
webServer.on("/h2color", HTTP_GET, []() {
CRGB color = CRGB::Black;
color.red = webServer.arg("red").toInt()%256;
color.green = webServer.arg("green").toInt()%256;
color.blue = webServer.arg("blue").toInt()%256;
h_one_color = color;
lightingChanges.h_one_color = true;
lastUpdate = 0;
autoEffect = 0; //turn off scheduled lighting effects and toggle back on regular effects
updateSettings = true;
webServer.send(200, "text/plain", "1");
});
webServer.on("/m1color", HTTP_GET, []() {
CRGB color = CRGB::Black;
color.red = webServer.arg("red").toInt()%256;
color.green = webServer.arg("green").toInt()%256;
color.blue = webServer.arg("blue").toInt()%256;
m_ten_color = color;
lightingChanges.m_ten_color = true;
lastUpdate = 0;
autoEffect = 0; //turn off scheduled lighting effects and toggle back on regular effects
updateSettings = true;
webServer.send(200, "text/plain", "1");
});
webServer.on("/m2color", HTTP_GET, []() {
CRGB color = CRGB::Black;
color.red = webServer.arg("red").toInt()%256;
color.green = webServer.arg("green").toInt()%256;
color.blue = webServer.arg("blue").toInt()%256;
m_one_color = color;
lightingChanges.m_one_color = true;
lastUpdate = 0;
autoEffect = 0; //turn off scheduled lighting effects and toggle back on regular effects
updateSettings = true;
webServer.send(200, "text/plain", "1");
});
webServer.on("/spotcolor", HTTP_GET, []() {
CRGB color = CRGB::Black;
int index = webServer.arg("value").toInt();
color.red = webServer.arg("red").toInt()%256;
color.green = webServer.arg("green").toInt()%256;
color.blue = webServer.arg("blue").toInt()%256;
if(index >= WIDTH*HEIGHT){webServer.send(400, "text/plain", index + " not supported"); return;}
setSpotlightColor(index-1,color);
if(index==1) setSpotlight1(color);
else if(index==2) setSpotlight2(color);
lightingChanges.spotlights[index-1] = true;
lastUpdate = 0;
autoEffect = 0; //turn off scheduled lighting effects and toggle back on regular effects
updateSettings = true;
webServer.send(200, "text/plain", "1");
});
webServer.on("/effectfg", HTTP_GET, []() {
String pattern = webServer.arg("value");
if(pattern == "off") foregroundPattern = 0;
else if(pattern == "solid") foregroundPattern = 1;
else if(pattern == "rainbow") foregroundPattern = 2;
else if(pattern == "gradient") foregroundPattern = 3;
else{webServer.send(400, "text/plain", pattern + " not supported"); return;}
lightingChanges.foregroundPattern = true;
lastUpdate = 0;
autoEffect = 0; //turn off scheduled lighting effects and toggle back on regular effects
updateSettings = true;
webServer.send(200, "text/plain", "1");
});
webServer.on("/effectbg", HTTP_GET, []() {
String pattern = webServer.arg("value");
if(pattern == "off") backgroundPattern = 0;
else if(pattern == "solid") backgroundPattern = 1;
else if(pattern == "rainbow") backgroundPattern = 2;
else if(pattern == "gradient") backgroundPattern = 3;
else if(pattern == "rain") backgroundPattern = 4;
else if(pattern == "sparkle") backgroundPattern = 5;
else if(pattern == "fire") backgroundPattern = 6;
else if(pattern == "loop") backgroundPattern = 255;
else{webServer.send(400, "text/plain", pattern + " not supported"); return;}
lightingChanges.backgroundPattern = true;
clearLightingCache();
lastUpdate = 0;
autoEffect = 0; //turn off scheduled lighting effects and toggle back on regular effects
updateSettings = true;
webServer.send(200, "text/plain", "1");
});
webServer.on("/effectsl", HTTP_GET, []() {
String pattern = webServer.arg("value");
if(pattern == "off") spotlightPattern = 0;
else if(pattern == "solid") spotlightPattern = 1;
else if(pattern == "rainbow") spotlightPattern = 2;
else if(pattern == "gradient") spotlightPattern = 3;
else if(pattern == "rain") spotlightPattern = 4;
else if(pattern == "sparkle") spotlightPattern = 5;
else if(pattern == "fire") spotlightPattern = 6;
else{webServer.send(400, "text/plain", pattern + " not supported"); return;}
lightingChanges.spotlightPattern = true;
clearLightingCache();
lastUpdate = 0;
autoEffect = 0; //turn off scheduled lighting effects and toggle back on regular effects
updateSettings = true;
webServer.send(200, "text/plain", "1");
});
#ifdef BACKLIGHT
webServer.on("/effectbl", HTTP_GET, []() {
String pattern = webServer.arg("value");
if(pattern == "off") setBacklightEffectID(0);
else if(pattern == "solid") setBacklightEffectID(1);
else if(pattern == "rainbow") setBacklightEffectID(2);
else if(pattern == "gradient") setBacklightEffectID(3);
else if(pattern == "rain") setBacklightEffectID(4);
else if(pattern == "sparkle") setBacklightEffectID(5);
else if(pattern == "fire") setBacklightEffectID(6);
else if(pattern == "loop") setBacklightEffectID(255);
else{webServer.send(400, "text/plain", pattern + " not supported"); return;}
lastUpdate = 0;
autoEffect = 0; //turn off scheduled lighting effects and toggle back on regular effects
updateSettings = true;
webServer.send(200, "text/plain", "1");
});
webServer.on("/blcolor", HTTP_GET, []() {
CRGB color = CRGB::Black;
color.red = webServer.arg("red").toInt()%256;
color.green = webServer.arg("green").toInt()%256;
color.blue = webServer.arg("blue").toInt()%256;
setBacklightColor(0, color);
lastUpdate = 0;
autoEffect = 0; //turn off scheduled lighting effects and toggle back on regular effects
updateSettings = true;
webServer.send(200, "text/plain", "1");
});
webServer.on("/bl2color", HTTP_GET, []() {
CRGB color = CRGB::Black;
color.red = webServer.arg("red").toInt()%256;
color.green = webServer.arg("green").toInt()%256;
color.blue = webServer.arg("blue").toInt()%256;
setBacklightColor(1, color);
lastUpdate = 0;
autoEffect = 0; //turn off scheduled lighting effects and toggle back on regular effects
updateSettings = true;
webServer.send(200, "text/plain", "1");
});
webServer.on("/backlightbrightness", HTTP_GET, []() {
setBacklightBrightness((byte)webServer.arg("value").toInt()%256);
lastUpdate = 0;
updateSettings = true;
webServer.send(200, "text/plain", "1");
});
#endif
webServer.on("/utcoffset", HTTP_GET, []() {
storeUtcOffset(webServer.arg("value").toDouble());
setNewOffset();
webServer.send(200, "text/plain", "1");
});
webServer.on("/cmd", HTTP_GET, []() {
String command = webServer.arg("c");
String val = webServer.arg("v");
command.toLowerCase();
val.toLowerCase();
if(command=="help" || command=="?"){
String resp = String("Available commands:<br> settings - Gets the current settings<br>" + String("") + //Doesnt like it when I just do *char[] + *char[] here, so I need to do this
"utcoffset ## - Set UTF offset in hours for clock [current offset: " + String(getOffset()) + "]<br>" +
"rainbowrate ## - Set how rainbowy the rainbow is [current rate: " + String(rainbowRate) + "]<br>" +
"fps ## - Sets frames per second [current rate: " + String(FRAMES_PER_SECOND) + "]<br>" +
"effecttimer ## - Turns on or off scheduled time configurations. [current value: " + String(autoEffect) + "]<br>" +
"hyphen ## - Sets the length of the hyphen seperator. 0 Disables hyphen. [current length: " + String(hyphenLength) + "]<br>" +
"hyphencolor HEX - Sets the hyphen color (format should be RRGGBB in hex, like FFA400). [current color: " + String(crgbToCss(hyphenColor)) + "]<br>" +
"reset - Reset all settings<br>" +
"resetprofile - Reset lighting settings<br>" +
"save - Saves all current settings<br>" +
"reboot - Reboots the device after 3 seconds");
webServer.send(200, "text/plain", resp);
}else if(command=="settings"){
webServer.send(200, "text/plain", getCurrentSettings("<br>"));
}else if(command=="utcoffset"){
utcOffset = val.toDouble();
setNewOffset();
updateTime();
storeUtcOffset(utcOffset);
webServer.send(200, "text/plain", "Set UTC offset to " + String(getUtcOffset()));
}else if(command=="rainbowrate"){
rainbowRate = (double)val.toInt();
lightingChanges.rainbowRate = true;
lastUpdate = EEPROM_UPDATE_DELAY*FRAMES_PER_SECOND;
updateSettings = true;
webServer.send(200, "text/plain", "Set rainbow rate to " + String(rainbowRate));
}else if(command=="fps"){
FRAMES_PER_SECOND = max((byte)1,(byte)val.toInt()); //Setting to 0 sets off the watchdog since it never refreshes
lightingChanges.fps = true;
lastUpdate = EEPROM_UPDATE_DELAY*FRAMES_PER_SECOND;
updateSettings = true;
webServer.send(200, "text/plain", "Set fps to " + String(FRAMES_PER_SECOND));
}else if(command=="reset"){
defaultSettings();
saveAllSettings();
webServer.send(200, "text/plain", "Reset Settings");
}else if(command=="resetprofile"){
deleteSettings();
webServer.send(200, "text/plain", "Profile will reset to default on reboot");
}else if(command=="hyphen"){
hyphenLength = max(min((byte)val.toInt(),(byte)LEDS_PER_LINE),(byte)0);
lightingChanges.hyphenLength = true;
lastUpdate = EEPROM_UPDATE_DELAY*FRAMES_PER_SECOND;
updateSettings = true;
webServer.send(200, "text/plain", "Set hyphen length to " + String(hyphenLength));
}else if(command=="hyphencolor"){
Serial.println("Color: #" + val.substring(0,2) + val.substring(2,4) + val.substring(4,6));
Serial.println("Value: " + String(hexToByte(val.substring(0,2))) + " " + String(hexToByte(val.substring(2,4))) + " " + String(hexToByte(val.substring(4,6))));
hyphenColor = CRGB(hexToByte(val.substring(0,2)),hexToByte(val.substring(2,4)),hexToByte(val.substring(4,6)));
lightingChanges.hyphenColor = true;
lastUpdate = EEPROM_UPDATE_DELAY*FRAMES_PER_SECOND;
updateSettings = true;
webServer.send(200, "text/plain", "Set hyphen color to " + String(crgbToCss(hyphenColor)));
}else if(command=="save"){
saveAllSettings();
webServer.send(200, "text/plain", "Saved Settings");
}else if(command=="reboot" || command=="restart"){
//Wont actually send
//webServer.send(200, "text/plain", "Rebooting in 3 seconds...");
ESP.wdtEnable(3000); // Turn on watchdog then let program hang. Im not sure if the 15ms is actually implemented in the ESP source code
for(;;) {} //Make watchdog reset the device
}else if(command="effecttimer"){
autoEffect = (val.toInt() != 0); //force bool cast
webServer.send(200, "text/plain", "Set effecttimer to " + String(autoEffect));
}else{
webServer.send(200, "text/plain", "Command not found");
}
});
webServer.serveStatic("/", LittleFS, "/", "max-age=86400");
webServer.begin();
Serial.println("HTTP web server initialized");
}
byte hexToByte(String hex){
return ((hexCharToNum(hex.charAt(0))<<4) + (hexCharToNum(hex.charAt(1)))); //bitshift <<4 doesnt work
}
byte hexCharToNum(char letter){
switch(letter){ //could do better with an ascii representation
case '0': return 0;
case '1': return 1;
case '2': return 2;
case '3': return 3;
case '4': return 4;
case '5': return 5;
case '6': return 6;
case '7': return 7;
case '8': return 8;
case '9': return 9;
case 'a': case 'A': return 10;
case 'b': case 'B': return 11;
case 'c': case 'C': return 12;
case 'd': case 'D': return 13;
case 'e': case 'E': return 14;
case 'f': case 'F': return 15;
default:
Serial.println("Unknown value: " + String(letter));
return 0;
}
}