-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathLEDs_esp8266.ino
488 lines (397 loc) · 9.69 KB
/
LEDs_esp8266.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
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
/*
* led_class.cpp
*
* Created on: Oct 30, 2016
* Author: mlw
*/
#include "NTPClient.h"
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <ESP8266WebServer.h>
#include <FS.h>
#include "Leds.h"
int timeHour = 0;
int off = 0;
WiFiUDP ntpUDP;
// By default 'time.nist.gov' is used with 60 seconds update interval and
// no offset
// we are PST so -8 hours
NTPClient *timeClient;
extern ESP8266WebServer server;
// we now use a pointer to try to get the config info before we construct.
class Leds *myLeds;
extern void FSBsetup(void);
void ledsOff()
{
myLeds->stop();
}
void handleLedPattern(void) {
String output = "[";
String patName = server.arg("pattern");
if (patName != "")
{
// try to set the pattern to the new value
if (myLeds->setPattern(patName) != true)
{
server.send(404, "text/plain", "Pattern " + patName + " not found");
}
else
{
server.send(200, "text/plain", "Pattern " + patName + " set");
}
}
else
{
output += "{\"pattern\":\"";
output += myLeds->pattern();
output += "\"}";
output += "]";
server.send(200, "text/json", output);
}
}
uint8_t convertHex(const char *str)
{
uint8_t hexVal = 0;
if (str[0] >= '0' && str[0] <= '9')
{
hexVal = str[0] - 0x30;
}
else if (str[0] >= 'a' && str[0] <= 'f')
{
hexVal = str[0] - 'a' + 10;
}
else if (str[0] >= 'A' && str[0] <= 'F')
{
hexVal = str[0] - 'A' + 10;
}
hexVal = hexVal << 4;
if (str[1] >= '0' && str[1] <= '9')
{
hexVal += str[1] - 0x30;
}
else if (str[1] >= 'a' && str[1] <= 'f')
{
hexVal += str[1] - 'a' + 10;
}
else if (str[1] >= 'A' && str[1] <= 'F')
{
hexVal += str[1] - 'A' + 10;
}
return hexVal;
}
void handleLedMode(void) {
String mode = server.arg("mode");
if (mode != "")
{
Serial.print(mode);
if (mode == "Stop")
{
myLeds->setMode(STOP_MODE);
}
else if (mode == "Pattern")
{
myLeds->setMode(PATTERN_MODE);
}
else if (mode == "Pattern_Cycle")
{
myLeds->setMode(PATTERN_CYCLE_MODE);
}
else if (mode == "Color")
{
myLeds->setMode(COLOR_MODE);
}
else
{
server.send(400, "text/plain", "Bad Mode");
return;
}
server.send(200, "text/plain", "Mode set");
}
else
{
server.send(400, "text/plain", "Error No Mode");
}
}
void handleLedColor(void) {
String color = server.arg("color");
CRGB newColor;
const char *colorStr = color.c_str();;
if (color != "")
{
Serial.print("Color length = ");
Serial.print(color.length());
Serial.print(" (");
Serial.print(color);
Serial.println(")");
if (color.length() != 7)
{
// bad format for arg
server.send(400, "text/plain", "Bad len for color");
}
if (colorStr[0] == '#')
{
Serial.print("We got a color (");
Serial.print(colorStr);
Serial.println(")");
// get the rest of the info
newColor.red = convertHex(colorStr + 1);
newColor.green = convertHex(colorStr + 3);
newColor.blue = convertHex(colorStr + 5);
myLeds->setColor(newColor);
server.send(200, "text/plain", "Led color set");
}
else
{
server.send(400, "text/plain", "Bad format for color");
}
}
else
{
// get the color and return it
Serial.println("We got a request for color");
String output = "[";
char tmpBuf[30];
CRGB color = myLeds->color();
sprintf(tmpBuf, "#%02x%02x%02x", color.red, color.green, color.blue);
output += "\"color\":\"";
output += String(tmpBuf);
output += "\"";
output += "]";
server.send(200, "text/json", output);
}
}
void handleLedStartTime(void) {
String newTime = server.arg("startTime");
if (newTime != "")
{
String hour;
String minutes;
int index = newTime.indexOf(':');
if (index != 2)
{
// bad format
server.send(400, "text/plain", "Bad format for Start Time");
}
// format should be hh:mm
hour = newTime.substring(0,index);
minutes = newTime.substring(index + 1);
myLeds->setStartTime( hour.toInt() * 60 + minutes.toInt());
server.send(200, "text/plain", "Start Time set");
}
else
{
// they want to know the start time
}
}
void handleLedStopTime(void) {
String newTime = server.arg("stopTime");
if (newTime != "")
{
String hour;
String minutes;
int index = newTime.indexOf(':');
if (index != 2)
{
// bad format
server.send(400, "text/plain", "Bad format for Stop Time");
}
// format should be hh:mm
hour = newTime.substring(0,index);
minutes = newTime.substring(index + 1);
myLeds->setStopTime( hour.toInt() * 60 + minutes.toInt());
server.send(200, "text/plain", "Stop Time set");
}
else
{
// they want to know the stop time
}
}
void handleLedRunning(void) {
String newStatus = server.arg("running");
if (newStatus != "")
{
if (newStatus == "true")
{
myLeds->play();
server.send(200, "text/plain", "Set running to true");
}
else if (newStatus == "false")
{
myLeds->stop();
server.send(200, "text/plain", "Set running to false");
}
}
else
{
// they want to know the stop time
}
}
void handleLedGet(void) {
if (server.hasArg("pattern"))
{
Serial.println("Got pattern");
handleLedPattern();
}
else if (server.hasArg("color"))
{
Serial.println("Got color");
handleLedColor();
}
else if (server.hasArg("mode"))
{
Serial.println("Got mode");
handleLedMode();
}
else if (server.hasArg("startTime"))
{
Serial.println("Got startTime");
handleLedStartTime();
}
else if (server.hasArg("stopTime"))
{
Serial.println("Got stopTime");
handleLedStopTime();
}
else if (server.hasArg("running"))
{
Serial.println("Got running");
handleLedRunning();
}
else
{
Serial.println("Got unknown");
server.send(405, "text/plain", "Not handled");
}
}
void handleLedPut(void) {
}
void handleLedPatternsGet() {
String output = "{\"Patterns\":[";
std::vector <String> patterns = myLeds->getPatterns();
std::vector <String>::iterator pattern = patterns.begin();
while(pattern != patterns.end())
{
if (pattern != patterns.begin())
{
// we need a comma between entries
output += ",";
}
// add this entry to the array
output += "{\"name\":\"";
output += *pattern++;
output += "\"}";
}
output += "]";
output += "}";
server.send(200, "text/json", output);
}
void handleLedStatusGet() {
String output = "{";
// running status
output += "\"running\":\"";
output += myLeds->isRunning() ? "true" : "false";
output += "\",";
// mode
output += "\"mode\":\"";
output += myLeds->getMode();
output += "\",";
// color
char tmpBuf[30];
CRGB color = myLeds->color();
sprintf(tmpBuf, "#%02x%02x%02x", color.red, color.green, color.blue);
output += "\"color\":\"";
output += String(tmpBuf);
output += "\"";
// pattern
output += ",\"pattern\":\"";
output += myLeds->pattern();
output += "\"";
// time
output += ",\"time\":\"";
output += timeClient->getFormattedTime();
output += "\"";
output += "}";
server.send(200, "text/json", output);
}
void handleTimesGet(void)
{
String output = "{";
char buf[20];
// start time
output += "\"startTime\":\"";
sprintf(buf, "%02d:%02d", myLeds->getStartTime() / 60, myLeds->getStartTime() % 60);
output += String(buf);
output += "\"";
// stop time
output += ",\"stopTime\":\"";
sprintf(buf, "%02d:%02d", myLeds->getStopTime() / 60, myLeds->getStopTime() % 60);
output += String(buf);
output += "\"";
output += "}";
server.send(200, "text/json", output);
}
void handleConfigSave(void) {
if (myLeds->writeConfig())
{
server.send(200, "text/plain", "Config file written");
}
else
{
server.send(400, "text/plain", "Config file write failed");
}
}
void setup() {
Serial.begin(115200);
// setup the SPIFFS first so we can read our config file
SPIFFS.begin();
// we need a way to pass the config into the LED constructor
myLeds = new Leds();
// for now the led config has the timezone
timeClient = new NTPClient(ntpUDP, myLeds->timezone());
// setup the LED page
server.on("/leds", HTTP_GET, handleLedGet);
server.on("/leds", HTTP_PUT, handleLedPut);
server.on("/leds/status", HTTP_GET, handleLedStatusGet);
server.on("/leds/color", handleLedColor);
server.on("/leds/patterns", HTTP_GET, handleLedPatternsGet);
server.on("/leds/times", HTTP_GET, handleTimesGet);
server.on("/leds/consave", HTTP_GET, handleConfigSave);
server.on("/heap", HTTP_GET, [](){
String json = "{";
json += "\"heap\":"+String(ESP.getFreeHeap());
json += "}";
server.send(200, "text/json", json);
json = String();
});
#if 0
//get heap status, analog input value and all GPIO statuses in one json call
server.on("/all", HTTP_GET, [](){
String json = "{";
json += "\"heap\":"+String(ESP.getFreeHeap());
handlepatternArray(json);
json += "}";
server.send(200, "text/json", json);
json = String();
});
#endif
/* stop the test for now
myLeds->ledTest();
*/
// call the browser setup first
FSBsetup();
}
extern void FSBloop(void);
void loop()
{
int curTime = -1;
// make sure we deal with the time
if (timeClient->update())
{
curTime = (timeClient->getEpochTime() % 86400L) / 60;
}
// we are only going to use minutes to control things so get the minutes since 12:00
myLeds->loop(curTime);
FSBloop();
// insert a delay to keep the framerate modest
FastLED.delay(1000/FRAMES_PER_SECOND);
}