-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpages.cpp
451 lines (390 loc) · 15.4 KB
/
webpages.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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
// global includes (libraries)
#include <stdint.h>
#include <string.h>
#include <pgmspace.h>
#include <ESP8266WiFi.h> // Base ESP8266 includes
#include <ESP8266mDNS.h> // multicast DNS
#include <WiFiUdp.h> // UDP handling
#include <DNSServer.h> // Local DNS Server used for redirecting all requests to the configuration portal
#include <ESP8266WebServer.h> // Local WebServer used to serve the configuration portal
#include <EEPROM.h>
#include <FastLED.h>
#include <PubSubClient.h> // MQTT client
#include <ArduinoJson.h> //https://github.com/bblanchon/ArduinoJson
#include "eepromdata.h"
#include "webpages.h"
#include "effects.h"
#include "boblight.h"
extern char mqtt_server[];
extern char mqtt_port[];
extern char username[];
extern char password[];
extern char c_idx[];
extern char zoneLEDType[][7];
extern uint16_t numLEDs[];
extern uint8_t gBrightness;
void saveMQTTConfig();
extern effects_t selectedEffect;
void changeEffect(effects_t effect);
static const char HTML_HEAD[] PROGMEM = "<html>\n<head>\n<title>LED strip</title>\n"
"<meta name=\"viewport\" content=\"initial-scale=1, maximum-scale=1.0, minimum-scale=1, user-scalable=no, width=device-width\">\n"
"<style>body {background-color:#cccccc;font-family:Arial,Helvetica,Sans-Serif;Color:#000088;}@media screen and (max-width:479px) {body{font-size:14pt;}</style>\n"
"</head>\n<body>\n";
static const char HTML_FOOT[] PROGMEM = "</body>\n</html>";
static const char _WS2801[] PROGMEM = "WS2801";
static const char _WS2811[] PROGMEM = "WS2811";
static const char _WS2812[] PROGMEM = "WS2812";
static const char _SELECTED[] PROGMEM = " selected";
static const char _LED_OPTION[] PROGMEM = "<option value=\"%S\"%S>%S</option>\n"; // %S for PROGMEM strings, %s for regular
static const char _EFFECT_OPTION[] PROGMEM = "<option value=\"%d\"%S>%s</option>\n"; // %S for PROGMEM strings, %s for regular
static const char _LED_BRIGHTNESS[] PROGMEM = "<tr><td>Brightness:</td><td><input name=\"b\" size=\"3\" value=\"%d\"></td></tr>\n";
void handleRoot() {
char tmp[128];
String sections, postForm = FPSTR(HTML_HEAD);
postForm += F("<form method=\"post\" enctype=\"application/x-www-form-urlencoded\" action=\"/\">\n<table>\n");
if (server.method() == HTTP_POST) {
if ( server.args() == 0 ) {
postForm += F("<tr><td colspan=\"2\">BAD ARGUMENT</td></tr>\n");
}
gBrightness = min(255,max(1,(int)server.arg("b").toInt()));
FastLED.setBrightness(gBrightness);
changeEffect((effects_t) server.arg("effect").toInt());
} else {
if ( server.args() > 0 ) {
if ( server.arg("b") != "" ) { // brightness
gBrightness = min(255,max(1,(int)server.arg("b").toInt()));
FastLED.setBrightness(gBrightness);
}
if ( server.arg("e") != "" ) { // effect
changeEffect((effects_t) server.arg("e").toInt());
}
}
}
postForm += F("<tr><td>Effect:</td><td><select name=\"effect\" size=\"1\" onchange=\"this.form.submit();\">\n");
for ( int i=0; i<=LAST_EFFECT; i++ ) {
snprintf_P(tmp, 127, _EFFECT_OPTION, i, (i==selectedEffect ? _SELECTED : PSTR("")), effects[i].name);
postForm += tmp;
}
postForm += F("</select></td></tr>\n");
snprintf_P(tmp, 127, _LED_BRIGHTNESS, gBrightness);
postForm += tmp;
postForm += F("<tr><td colspan=\"2\" align=\"center\"><input type=\"submit\" value=\"Submit\"></td></tr>\n</table>\n</form>\n");
postForm += F("<a href=\"/set/\">Configure LED strips</a><br>\n");
postForm += F("<a href=\"/bob/\">Configure Boblight</a><br>\n");
postForm += F("<a href=\"/update\">Firmware update</a><br>\n");
postForm += FPSTR(HTML_FOOT);
server.send(200, "text/html", postForm);
}
void handleSet() {
char buffer[128];
bool mqtt = false;
if (server.method() != HTTP_POST) {
String sections, postForm = FPSTR(HTML_HEAD);
postForm += F("<body>\n"
"<form method=\"post\" enctype=\"application/x-www-form-urlencoded\" action=\"/set/\">\n"
"<table>\n"
"<tr><td>Domoticz IDX:</td><td><input type=\"text\" name=\"idx\" value=\"");
postForm += String(atoi(c_idx));
postForm += F("\"></td></tr>\n");
snprintf_P(buffer, 127, _LED_BRIGHTNESS, gBrightness);
postForm += buffer;
for ( int i=0; i<MAXZONES; i++ ) {
sections = "0";
for ( int j=1; j<numSections[i]; j++ ) {
sections += "," + String(sectionStart[i][j]);
}
postForm += F("<tr><td colspan=\"2\" align=\"center\">Zone ");
postForm += String(i);
postForm += F(":</td></tr>\n");
postForm += F("<tr><td># of LEDs:</td><td><input type=\"text\" name=\"leds");
postForm += String(i);
postForm += F("\" value=\"");
postForm += String(numLEDs[i]);
postForm += F("\"></td></tr>\n");
if ( i == 0 ) {
postForm += F("<tr><td>LED type (WS28xx):</td><td><select name=\"ledtype");
postForm += String(i);
postForm += F("\" size=\"1\">\n");
snprintf_P(buffer, 64, _LED_OPTION, _WS2812, (strncmp_P(zoneLEDType[i], _WS2812, 6)==0 ? _SELECTED : PSTR("")), _WS2812);
postForm += buffer;
snprintf_P(buffer, 64, _LED_OPTION, _WS2811, (strncmp_P(zoneLEDType[i], _WS2811, 6)==0 ? _SELECTED : PSTR("")), _WS2811);
postForm += buffer;
snprintf_P(buffer, 64, _LED_OPTION, _WS2801, (strncmp_P(zoneLEDType[i], _WS2801, 6)==0 ? _SELECTED : PSTR("")), _WS2801);
postForm += buffer;
postForm += F("</select></td></tr>\n");
} else if ( i >= 1 && i <= 3 ) {
postForm += F("<tr><td>LED type (WS28xx):</td><td><select name=\"ledtype");
postForm += String(i);
postForm += F("\" size=\"1\">\n");
snprintf_P(buffer, 64, _LED_OPTION, _WS2812, (strncmp_P(zoneLEDType[i], _WS2812, 6)==0 ? _SELECTED : PSTR("")), _WS2812);
postForm += buffer;
snprintf_P(buffer, 64, _LED_OPTION, _WS2811, (strncmp_P(zoneLEDType[i], _WS2811, 6)==0 ? _SELECTED : PSTR("")), _WS2811);
postForm += buffer;
postForm += F("</select></td></tr>\n");
} else {
postForm += F("<tr><td>LED type:</td><td><input type=\"text\" name=\"ledtype");
postForm += String(i);
postForm += F("\" value=\"WS2812\" readonly></td></tr>\n");
}
postForm += F("<tr><td>LED sections:</td><td><input type=\"text\" name=\"sections");
postForm += String(i);
postForm += F("\" value=\"");
postForm += sections;
postForm += F("\"></td></tr>\n");
}
snprintf_P(buffer, 127, PSTR("<tr><td>MQTT server:</td><td><input name=\"mqttserver\" type=\"text\" size=\"40\" value=\"%s\"></td></tr>\n"), mqtt_server);
postForm += buffer;
snprintf_P(buffer, 127, PSTR("<tr><td>MQTT port:</td><td><input name=\"mqttport\" type=\"text\" size=\"5\" value=\"%s\"></td></tr>\n"), mqtt_port);
postForm += buffer;
snprintf_P(buffer, 127, PSTR("<tr><td>MQTT username:</td><td><input name=\"mqttuser\" type=\"text\" size=\"32\" value=\"%s\"></td></tr>\n"), username);
postForm += buffer;
snprintf_P(buffer, 127, PSTR("<tr><td>MQTT password:</td><td><input name=\"mqttpass\" type=\"password\" size=\"32\" value=\"%s\"></td></tr>\n"), password);
postForm += buffer;
postForm += F("<tr><td colspan=\"2\" align=\"center\"><input type=\"submit\" value=\"Submit\"></td></tr>\n</table>\n</form>\n</body>\n");
postForm += FPSTR(HTML_FOOT);
server.send(200, "text/html", postForm);
} else {
if (server.args() == 0) {
return server.send(500, "text/plain", "BAD ARGS");
}
int zones;
char tnum[4];
memset(&e, 0, sizeof(e));
memcpy_P(e.esp, PSTR("esp"), 3);
String message = F("<html>\n<head>\n<meta http-equiv='refresh' content='15; url=/' />\n<title>ESP8266 settings applied</title>\n</head>\n");
message += F("<body>\nSettings applied.<br>\n");
for ( uint8_t i = 0; i < server.args(); i++ ) {
String argN = server.argName(i);
String argV = server.arg(i);
if ( argN != "plain" ) {
message += " " + argN + ": " + argV + "<br>\n";
} else {
continue;
}
if ( argN == "idx" ) {
int idx = max(min((int)argV.toInt(),999),0);
sprintf_P(c_idx, PSTR("%3d"), idx);
memcpy(e.idx, c_idx, 3);
#if DEBUG
Serial.print("idx: ");
Serial.println(c_idx);
#endif
}
if ( argN == "b" ) {
e.iBrightness = max(1,min(255,(int)argV.toInt()));
#if DEBUG
Serial.print("brightness: ");
Serial.println(e.iBrightness);
#endif
}
if ( argN.substring(0,4) == "leds" ) {
int z = atoi(argN.substring(4).c_str());
int l = max(min((int)argV.toInt(),999),0);
sprintf_P(tnum, PSTR("%3d"),l);
memcpy(e.zoneData[z].zoneLEDs, tnum, 3);
#if DEBUG
Serial.print(z, DEC);
Serial.print("leds: ");
Serial.println(tnum);
#endif
}
if ( argN.substring(0,7) == "ledtype" ) {
int z = atoi(argN.substring(7).c_str());
memcpy(e.zoneData[z].ledType, argV.c_str(), 6);
#if DEBUG
Serial.print("LED type: ");
Serial.println(argV);
#endif
}
if ( argN.substring(0,8) == "sections" ) {
int z = atoi(argN.substring(8).c_str());
char tmp[64];
argV.toCharArray(tmp, 64);
char *buf = strtok(tmp, ",;");
int sections=0;
while ( buf != NULL && sections<MAXSECTIONS ) {
sprintf_P(tnum, PSTR("%3d"), max(min(atoi(buf),999),0));
memcpy(e.zoneData[z].sectionStart[sections++], tnum, 3);
buf = strtok(NULL, ",;");
}
#if DEBUG
Serial.print("Section: ");
Serial.println(argV);
#endif
}
if ( argN.substring(0,9) == "mqttserver" ) {
char mqttserver[40];
strcpy(mqttserver,argV.substring(0,39).c_str());
if ( strlen(mqttserver) > 0 && strcasecmp(mqttserver,mqtt_server) ) {
strcpy(mqtt_server,mqttserver);
mqtt = true;
}
}
if ( argN.substring(0,7) == "mqttport" ) {
char mqttport[7];
strcpy(mqttport,argV.substring(0,4).c_str());
if ( strlen(mqttport) > 0 && strcasecmp(mqttport,mqtt_port) ) {
strcpy(mqtt_port,mqttport);
mqtt = true;
}
}
if ( argN.substring(0,7) == "mqttuser" ) {
char mqttuser[33];
strcpy(mqttuser,argV.substring(0,31).c_str());
if ( strlen(mqttuser) > 0 && strcasecmp(mqttuser,username) ) {
strcpy(username,mqttuser);
mqtt = true;
}
}
if ( argN.substring(0,7) == "mqttpass" ) {
char mqttpass[33];
strcpy(mqttpass,argV.substring(0,31).c_str());
if ( strlen(mqttpass) > 0 && strcasecmp(mqttpass,password) ) {
strcpy(password,mqttpass);
mqtt = true;
}
}
}
message += F("<br>ESP is restarting, please wait.<br>\n");
message += F("</body>\n</html>");
server.send(200, "text/html", message);
if ( mqtt ) {
saveMQTTConfig();
}
for ( zones=1; zones<MAXZONES; zones++ ) {
memcpy(tnum, e.zoneData[zones].zoneLEDs, 3);
tnum[4] = '\0';
if ( atoi(tnum) == 0 )
break;
}
e.nZones = (char)zones + '0';
#if DEBUG
Serial.print("Zones: ");
Serial.println(zones, DEC);
Serial.print(F("EEPROM data: "));
for ( int i=0; i<sizeof(e); i++ ) {
Serial.print(((char*)&e)[i], HEX);
Serial.print(":");
}
Serial.println();
#endif
// store configuration to EEPROM
EEPROM.begin(EEPROM_SIZE);
for ( int i=0; i<sizeof(e); i++ ) {
EEPROM.write(i, ((char*)&e)[i]);
}
EEPROM.commit();
EEPROM.end();
delay(250);
// restart ESP
ESP.reset();
delay(2000);
}
}
void handleBob() {
char buffer[128];
if (server.method() != HTTP_POST)
{
int b=0,r=0,t=0,l=0;
float pct=0.0;
String postForm = F(
"<html>\n"
"<head>\n"
"<title>Configure Boblight</title>\n"
"<style>body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }</style>\n"
"</head>\n"
"<body>\n"
"<form method=\"post\" enctype=\"application/x-www-form-urlencoded\" action=\"/bob/\">\n"
"<table>\n"
);
for ( int i=0; i<numLights; i++ )
{
switch ( lights[i].lightname[0] )
{
case 'b' : b++; if (!pct) pct = lights[i].vscan[1] - lights[i].vscan[0]; break;
case 'l' : l++; if (!pct) pct = lights[i].hscan[1] - lights[i].hscan[0]; break;
case 't' : t++; if (!pct) pct = lights[i].vscan[1] - lights[i].vscan[0]; break;
case 'r' : r++; if (!pct) pct = lights[i].hscan[1] - lights[i].hscan[0]; break;
}
}
snprintf_P(buffer, 127, PSTR("<tr><td>Bottom LEDs:</td><td><input type=\"text\" name=\"bottom\" value=\"%d\"></td></tr>\n"), b);
postForm += buffer;
snprintf_P(buffer, 127, PSTR("<tr><td>Left LEDs:</td><td><input type=\"text\" name=\"left\" value=\"%d\"></td></tr>\n"), l);
postForm += buffer;
snprintf_P(buffer, 127, PSTR("<tr><td>Top LEDs:</td><td><input type=\"text\" name=\"top\" value=\"%d\"></td></tr>\n"), t);
postForm += buffer;
snprintf_P(buffer, 127, PSTR("<tr><td>Right LEDs:</td><td><input type=\"text\" name=\"right\" value=\"%d\"></td></tr>\n"), r);
postForm += buffer;
snprintf_P(buffer, 127, PSTR("<tr><td>Depth of scan (%%):</td><td><input type=\"text\" name=\"pct\" value=\"%.1f\"></td></tr>\n"), pct);
postForm += buffer;
postForm += F("<tr><td colspan=\"2\" align=\"center\"><input type=\"submit\" value=\"Submit\"></td></tr>\n</table>\n</form>\n</body>\n</html>");
server.send(200, "text/html", postForm);
}
else
{
if (server.args() == 0)
{
return server.send(500, "text/plain", "BAD ARGS");
}
String message = F(
"<html>\n"
"<head>\n"
"<meta http-equiv='refresh' content='10; url=/' />\n"
"<title>Configure Boblight</title>\n"
"</head>\n"
"<body>\n"
);
for ( uint8_t i = 0; i < server.args(); i++ )
{
String argN = server.argName(i);
String argV = server.arg(i);
if ( argN != "plain" )
{
message += " " + argN + ": " + argV + "<br>\n";
}
else
{
continue;
}
}
int bottom = server.arg("bottom").toInt();
int left = server.arg("left").toInt();
int top = server.arg("top").toInt();
int right = server.arg("right").toInt();
float pct = server.arg("pct").toFloat();
if ( bottom+left+top+right > MAX_LEDS || bottom+left+top+right > numLEDs[bobStrip] )
{
message += F("Too many LEDs specified. Try lower values.<br>\n");
message += F("</body>\n</html>");
server.send(200, "text/html", message);
}
else
{
message += F("Settings applied.<br>\n");
//message += F("<br>ESP is restarting, please wait.<br>\n");
message += F("</body>\n</html>");
server.send(200, "text/html", message);
fillBobLights(bottom, left, top, right, pct);
saveBobConfig();
/*
// restart ESP
ESP.reset();
delay(2000);
*/
}
}
}
void handleNotFound() {
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);
}