-
Notifications
You must be signed in to change notification settings - Fork 0
/
Alexa_Clock_Timer_Portal_Updates.ino
1495 lines (1357 loc) · 45.7 KB
/
Alexa_Clock_Timer_Portal_Updates.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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************** NEEDED TO MAKE NODEMCU WORK ***************************/
#define FASTLED_INTERRUPT_RETRY_COUNT 0
#define FASTLED_ALLOW_INTERRUPTS 0
#define FASTLED_ESP8266_RAW_PIN_ORDER
/****************** WIFI MANAGER LIBRARIES *************************************/
#include <FS.h>
//#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
#include <ArduinoJson.h> //https://github.com/bblanchon/ArduinoJson//this needs to be first, or it all crashes and burns...
/****************** LIBRARY SECTION *************************************/
#include <FastLED.h> //https://github.com/FastLED/FastLED
#include <SimpleTimer.h> //https://github.com/thehookup/Simple-Timer-Library
#include <PubSubClient.h> //https://github.com/knolleary/pubsubclient
#include <ESP8266WiFi.h> //if you get an error here you need to install the ESP8266 board manager
#include <ESP8266mDNS.h> //if you get an error here you need to install the ESP8266 board manager
#include <ArduinoOTA.h> //ArduinoOTA is now included with the ArduinoIDE
#include <Espalexa.h> //https://github.com/Aircoookie/Espalexa
#include <TimeLib.h> //https://github.com/PaulStoffregen/Time/blob/master/TimeLib.h
/***************** START USER CONFIG SECTION *********************************/
/***************** START USER CONFIG SECTION *********************************/
/***************** START USER CONFIG SECTION *********************************/
/***************** START USER CONFIG SECTION *********************************/
#define DAYLIGHT_SAVINGS 1 // 1 = use daylight savings, 0 = don't use daylight savings
#define HOUR_MODE 0 // 1 = use 12 hour time, 0 = use 24 hour time
#define RESET_PIN D5 // connect this pin to ground to reset the wifimanager config
unsigned int localPort = 8888; // local port to listen for UDP packets
time_t getNtpTime();
void digitalClockDisplay();
void printDigits(int digits);
void sendNTPpacket(IPAddress &address);
WiFiUDP Udp;
//const int timeZone = 1; // Central European Time
int timeZone = -5; // Eastern Standard Time (USA)
//const int timeZone = -4; // Eastern Daylight Time (USA)
//const int timeZone = -8; // Pacific Standard Time (USA)
//const int timeZone = -7; // Pacific Daylight Time (USA)
//define your default values here, if there are different values in config.json, they are overwritten.
char alexa_id1[20];
char alexa_id2[20];
char alexa_id3[20];
char alexa_id4[20];
char savedTimezone[6];
char r1[6];
char r2[6];
char r3[6];
char rT[6];
char g1[6];
char g2[6];
char g3[6];
char gT[6];
char b1[6];
char b2[6];
char b3[6];
char bT[6];
char savedEffect[20];
char savedTimer[20];
char savedBrightness[6];
char dayLightSavings[6];
String alexaString1;
String alexaString2;
String alexaString3;
String alexaString4;
#define USER_MQTT_CLIENT_NAME "LED_Clock" // Used to define MQTT topics, MQTT Client ID, and ArduinoOTA
#define ZONEONE 1 //ZONEONE is pin D1, 1 = zone used 0 = zone unused
#define FIRSTZONE_SECTIONS 7 //Number of rows in your clock
#define FIRSTZONE_LEDS 83 //Number of LEDS total
#define FIRSTZONE_COLOR_ORDER GRB //Color orders, can be RGB, RBG, GRB, GBR, BRG, BGR
/***************************** ROW AND COLUMN SETUP *************************************/
/***************************** ROW AND COLUMN SETUP *************************************/
#if FIRSTZONE_SECTIONS >= 1
#define ZONEONE_SECTION1_START 0 //starting LED for this zone
#define ZONEONE_SECTION1_END 10 //ending LED for this zone
#define ZONEONE_SECTION1_START_FIRE 1 //would you like fire to begin from this point? 0 = no 1 = yes
#define ZONEONE_SECTION1_END_FIRE 1 //would you like fire to begin from this point? 0 = no 1 = yes
#endif
#if FIRSTZONE_SECTIONS >= 2
#define ZONEONE_SECTION2_START 13 //starting LED for this zone
#define ZONEONE_SECTION2_END 23 //ending LED for this zone
#define ZONEONE_SECTION2_START_FIRE 1 //would you like fire to begin from this point? 0 = no 1 = yes
#define ZONEONE_SECTION2_END_FIRE 1 //would you like fire to begin from this point? 0 = no 1 = yes
#endif
#if FIRSTZONE_SECTIONS >= 3
#define ZONEONE_SECTION3_START 24 //starting LED for this zone
#define ZONEONE_SECTION3_END 34 //ending LED for this zone
#define ZONEONE_SECTION3_START_FIRE 1 //would you like fire to begin from this point? 0 = no 1 = yes
#define ZONEONE_SECTION3_END_FIRE 1 //would you like fire to begin from this point? 0 = no 1 = yes
#endif
#if FIRSTZONE_SECTIONS >= 4
#define ZONEONE_SECTION4_START 37 //starting LED for this zone
#define ZONEONE_SECTION4_END 47 //ending LED for this zone
#define ZONEONE_SECTION4_START_FIRE 1 //would you like fire to begin from this point? 0 = no 1 = yes
#define ZONEONE_SECTION4_END_FIRE 1 //would you like fire to begin from this point? 0 = no 1 = yes
#endif
#if FIRSTZONE_SECTIONS >= 5
#define ZONEONE_SECTION5_START 48 //starting LED for this zone
#define ZONEONE_SECTION5_END 58 //ending LED for this zone
#define ZONEONE_SECTION5_START_FIRE 1 //would you like fire to begin from this point? 0 = no 1 = yes
#define ZONEONE_SECTION5_END_FIRE 1 //would you like fire to begin from this point? 0 = no 1 = yes
#endif
#if FIRSTZONE_SECTIONS >= 6
#define ZONEONE_SECTION6_START 61 //starting LED for this zone
#define ZONEONE_SECTION6_END 71 //ending LED for this zone
#define ZONEONE_SECTION6_START_FIRE 1 //would you like fire to begin from this point? 0 = no 1 = yes
#define ZONEONE_SECTION6_END_FIRE 1 //would you like fire to begin from this point? 0 = no 1 = yes
#endif
#if FIRSTZONE_SECTIONS >= 7
#define ZONEONE_SECTION7_START 72 //starting LED for this zone
#define ZONEONE_SECTION7_END 82 //ending LED for this zone
#define ZONEONE_SECTION7_START_FIRE 1 //would you like fire to begin from this point? 0 = no 1 = yes
#define ZONEONE_SECTION7_END_FIRE 1 //would you like fire to begin from this point? 0 = no 1 = yes
#endif
#if FIRSTZONE_SECTIONS >= 8
#define ZONEONE_SECTION8_START 0 //starting LED for this zone
#define ZONEONE_SECTION8_END 100 //ending LED for this zone
#define ZONEONE_SECTION8_START_FIRE 1 //would you like fire to begin from this point? 0 = no 1 = yes
#define ZONEONE_SECTION8_END_FIRE 1 //would you like fire to begin from this point? 0 = no 1 = yes
#endif
#if FIRSTZONE_SECTIONS >= 9
#define ZONEONE_SECTION9_START 0 //starting LED for this zone
#define ZONEONE_SECTION9_END 100 //ending LED for this zone
#define ZONEONE_SECTION9_START_FIRE 1 //would you like fire to begin from this point? 0 = no 1 = yes
#define ZONEONE_SECTION9_END_FIRE 1 //would you like fire to begin from this point? 0 = no 1 = yes
#endif
/***************** END USER CONFIG SECTION *********************************/
/***************** END USER CONFIG SECTION *********************************/
/***************** END USER CONFIG SECTION *********************************/
/***************** END USER CONFIG SECTION *********************************/
/***************** END USER CONFIG SECTION *********************************/
#define RESX 7 // 7 columns
#define RESY 11 // 11 rows
#define DIGITX 3 // width of digits (3 pixels)
#define DIGITY 5 // height of digits (5 pixels)
#define SECONDZONE_LEDS 300 // fake LED Zone to keep the ESP busy
/***************** GENERAL VARIABLES *************************************/
/***************** GENERAL VARIABLES *************************************/
/***************** GENERAL VARIABLES *************************************/
/***************** GENERAL VARIABLES *************************************/
/***************** GENERAL VARIABLES *************************************/
CRGBPalette16 gPal;
long oldTimeInSeconds = 0;
bool shouldSaveConfig = false;
long currentTimer = 0;
uint8_t mark = 0;
uint8_t gHue = 0;
uint8_t startPosition = 0;
uint8_t glitterChance = 250;
int chaseDelay = 1000;
int lastPosition = 1;
int raceSpeed = 12;
int BeatsPerMinute = 62;
uint8_t numberOfRainbows = 7;
int twinkleChance = 250;
int eyeChance = 248;
bool boot = true;
String effect = "Fill_Solid";
bool showLights = true;
int red1 = 255;
int green1 = 0;
int blue1 = 0;
int red2 = 0;
int green2 = 255;
int blue2 = 0;
int red3 = 0;
int green3 = 0;
int blue3 = 255;
int redT = 255;
int greenT = 255;
int blueT = 255;
int brightness = 100;
int displayMode = 1;
bool displayTimer = false;
int DST = 0;
byte notdigits[10][DIGITX * DIGITY] = {
{ 0, 0, 0,
0, 1, 0,
0, 1, 0,
0, 1, 0,
0, 0, 0
}, // 0
{ 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0 }, // 1
{ 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0 }, // 2
{ 0, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0 }, // 3
{ 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0 }, // 4
{ 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0 }, // 5
{ 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0 }, // 6
{ 0, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0 }, // 7
{ 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0 }, // 8
{ 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0 } // 9
};
int grid[23] = { 5, 11, 12, 18, 29, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 53, 59, 60, 66, 77 };
static const char ntpServerName[] = "us.pool.ntp.org";
/***************** DECLARATIONS ****************************************/
WiFiClient espClient;
PubSubClient client(espClient);
SimpleTimer timer;
Espalexa espalexa;
ESP8266WebServer server(80);
void colorLightChanged1(EspalexaDevice* dev);
void colorLightChanged2(EspalexaDevice* dev);
void colorLightChanged3(EspalexaDevice* dev);
void colorLightChanged4(EspalexaDevice* dev);
EspalexaDevice* device1;
EspalexaDevice* device2;
EspalexaDevice* device3;
EspalexaDevice* device4;
boolean connectWifi();
boolean wifiConnected = false;
CRGB firstZone[FIRSTZONE_LEDS];
CRGB secondZone[SECONDZONE_LEDS];
const int Pin_firstZone = 5; //marked as D1 on the board
const int Pin_secondZone = 4; //marked as D2 on the board
int center_firstZone = 0;
int step_firstZone = -1;
int previousLED_firstZone = 0;
/***************** WIFI CONFIG STUFF ****************************************/
/***************** WIFI CONFIG STUFF ****************************************/
/***************** WIFI CONFIG STUFF ****************************************/
/***************** WIFI CONFIG STUFF ****************************************/
/***************** WIFI CONFIG STUFF ****************************************/
//callback notifying us of the need to save config
void saveConfigCallback () {
Serial.println("Should save config");
shouldSaveConfig = true;
}
void configPortal()
{
Serial.println("mounting FS...");
if (SPIFFS.begin())
{
Serial.println("mounted file system");
if (SPIFFS.exists("/color1.json"))
{
//file exists, reading and loading
Serial.println("reading color1 file");
File color1 = SPIFFS.open("/color1.json", "r");
if (color1)
{
Serial.println("opened color1 file ");
size_t size = color1.size();
// Allocate a buffer to store contents of the file.
std::unique_ptr<char[]> buf(new char[size]);
color1.readBytes(buf.get(), size);
DynamicJsonBuffer jsonBuffer;
JsonObject& json = jsonBuffer.parseObject(buf.get());
json.printTo(Serial);
if (json.success())
{
Serial.println("\nparsed json");
strcpy(r1, json["r1"]);
red1 = atoi( r1 );
strcpy(g1, json["g1"]);
green1 = atoi( g1 );
strcpy(b1, json["b1"]);
blue1 = atoi( b1 );
strcpy(savedBrightness, json["savedBrightness"]);
brightness = atoi( savedBrightness );
}
else
{
Serial.println("failed to load json color1");
}
}
}
if (SPIFFS.exists("/color2.json"))
{
//file exists, reading and loading
Serial.println("opened color2 file ");
File color2 = SPIFFS.open("/color2.json", "r");
if (color2)
{
Serial.println("opened color2 file");
size_t size = color2.size();
// Allocate a buffer to store contents of the file.
std::unique_ptr<char[]> buf(new char[size]);
color2.readBytes(buf.get(), size);
DynamicJsonBuffer jsonBuffer;
JsonObject& json = jsonBuffer.parseObject(buf.get());
json.printTo(Serial);
if (json.success())
{
Serial.println("\nparsed json");
strcpy(r2, json["r2"]);
red2 = atoi( r2 );
strcpy(g2, json["g2"]);
green2 = atoi( g2 );
strcpy(b2, json["b2"]);
blue2 = atoi( b2 );
strcpy(savedEffect, json["savedEffect"]);
effect = String((char *)savedEffect);
}
else
{
Serial.println("failed to load json color2");
}
}
}
if (SPIFFS.exists("/color3.json"))
{
//file exists, reading and loading
Serial.println("reading color3 file");
File color3 = SPIFFS.open("/color3.json", "r");
if (color3)
{
Serial.println("opened color3 file");
size_t size = color3.size();
// Allocate a buffer to store contents of the file.
std::unique_ptr<char[]> buf(new char[size]);
color3.readBytes(buf.get(), size);
DynamicJsonBuffer jsonBuffer;
JsonObject& json = jsonBuffer.parseObject(buf.get());
json.printTo(Serial);
if (json.success())
{
Serial.println("\nparsed json");
strcpy(r3, json["r3"]);
red3 = atoi( r3 );
strcpy(g3, json["g3"]);
green3 = atoi( g3 );
strcpy(b3, json["b3"]);
blue3 = atoi( b3 );
boot = false;
}
else
{
Serial.println("failed to load json color3");
}
}
}
if (SPIFFS.exists("/timer.json"))
{
//file exists, reading and loading
Serial.println("reading timer file");
File timer = SPIFFS.open("/timer.json", "r");
if (timer)
{
Serial.println("opened timer file");
size_t size = timer.size();
// Allocate a buffer to store contents of the file.
std::unique_ptr<char[]> buf(new char[size]);
timer.readBytes(buf.get(), size);
DynamicJsonBuffer jsonBuffer;
JsonObject& json = jsonBuffer.parseObject(buf.get());
json.printTo(Serial);
if (json.success())
{
Serial.println("\nparsed json");
strcpy(rT, json["rT"]);
redT = atoi( rT );
strcpy(gT, json["gT"]);
greenT = atoi( gT );
strcpy(bT, json["bT"]);
blueT = atoi( bT );
strcpy(savedTimer, json["savedTimer"]);
currentTimer = atoi( savedTimer );
strcpy(dayLightSavings, json["dayLightSavings"]);
DST = atoi( dayLightSavings );
}
else
{
Serial.println("failed to load json timer");
}
}
}
if (SPIFFS.exists("/config.json"))
{
//file exists, reading and loading
Serial.println("reading config file");
File configFile = SPIFFS.open("/config.json", "r");
if (configFile)
{
Serial.println("opened config file");
size_t size = configFile.size();
// Allocate a buffer to store contents of the file.
std::unique_ptr<char[]> buf(new char[size]);
configFile.readBytes(buf.get(), size);
DynamicJsonBuffer jsonBuffer;
JsonObject& json = jsonBuffer.parseObject(buf.get());
json.printTo(Serial);
if (json.success())
{
Serial.println("\nparsed json");
strcpy(alexa_id1, json["alexa_id1"]);
alexaString1 = String((char *)alexa_id1);
strcpy(alexa_id2, json["alexa_id2"]);
alexaString2 = String((char *)alexa_id2);
strcpy(alexa_id3, json["alexa_id3"]);
alexaString3 = String((char *)alexa_id3);
strcpy(alexa_id4, json["alexa_id4"]);
alexaString4 = String((char *)alexa_id4);
strcpy(savedTimezone, json["savedTimezone"]);
}
else
{
Serial.println("failed to load json config");
}
}
}
}
else
{
Serial.println("failed to mount FS");
}
//end read
// The extra parameters to be configured (can be either global or just in the setup)
// After connecting, parameter.getValue() will get you the configured value
// id/name placeholder/prompt default length
WiFiManagerParameter custom_alexa_id1("ID1", "Main Clock Invocation Name", alexa_id1, 20);
WiFiManagerParameter custom_alexa_id2("ID2", "Color2/Effect Invocation Name", alexa_id2, 20);
WiFiManagerParameter custom_alexa_id3("ID3", "Color3/Mod Invocation Name", alexa_id3, 20);
WiFiManagerParameter custom_alexa_id4("ID4", "Timer Invocation Name", alexa_id4, 20);
WiFiManagerParameter custom_savedTimezone("Timezone", "Timezone (EST -5)", savedTimezone, 20);
//WiFiManager
//Local intialization. Once its business is done, there is no need to keep it around
WiFiManager wifiManager;
//set config save notify callback
wifiManager.setSaveConfigCallback(saveConfigCallback);
//add all your parameters here
wifiManager.addParameter(&custom_alexa_id1);
wifiManager.addParameter(&custom_alexa_id2);
wifiManager.addParameter(&custom_alexa_id3);
wifiManager.addParameter(&custom_alexa_id4);
wifiManager.addParameter(&custom_savedTimezone);
//reset settings - for testing
if (digitalRead(RESET_PIN) == LOW)
{
wifiManager.resetSettings();
}
if (!wifiManager.autoConnect("Clock Setup")) {
Serial.println("failed to connect and hit timeout");
delay(3000);
//reset and try again, or maybe put it to deep sleep
ESP.reset();
delay(5000);
}
//if you get here you have connected to the WiFi
Serial.println("connected...");
//read updated parameters
strcpy(alexa_id1, custom_alexa_id1.getValue());
strcpy(alexa_id2, custom_alexa_id2.getValue());
strcpy(alexa_id3, custom_alexa_id3.getValue());
strcpy(alexa_id4, custom_alexa_id4.getValue());
strcpy(savedTimezone, custom_savedTimezone.getValue());
//save the custom parameters to FS
if (shouldSaveConfig) {
Serial.println("saving config");
DynamicJsonBuffer jsonBuffer;
JsonObject& json = jsonBuffer.createObject();
json["alexa_id1"] = alexa_id1;
json["alexa_id2"] = alexa_id2;
json["alexa_id3"] = alexa_id3;
json["alexa_id4"] = alexa_id4;
json["savedTimezone"] = savedTimezone;
File configFile = SPIFFS.open("/config.json", "w");
if (!configFile) {
Serial.println("failed to open config file for writing");
}
json.printTo(Serial);
json.printTo(configFile);
configFile.close();
//end save
}
}
/***************** GLOBAL LIGHT FUNCTIONS *******************************/
/***************** GLOBAL LIGHT FUNCTIONS *******************************/
/***************** GLOBAL LIGHT FUNCTIONS *******************************/
/***************** GLOBAL LIGHT FUNCTIONS *******************************/
/***************** GLOBAL LIGHT FUNCTIONS *******************************/
void chase()
{
if (startPosition == 5)
{
startPosition = 0;
}
else
{
startPosition++;
}
timer.setTimeout(chaseDelay, chase);
}
void checkIn()
{
client.publish(USER_MQTT_CLIENT_NAME"/checkIn", "OK");
timer.setTimeout(120000, checkIn);
}
void choosePattern()
{
if (showLights == true)
{
if (effect == "Color_Chase")
{
RGB_firstZone();
}
if (effect == "Color_Glitter")
{
ColorGlitter_firstZone(glitterChance);
}
if (effect == "Single_Race")
{
SingleRace_firstZone();
}
if (effect == "Double_Crash")
{
Crash_firstZone();
}
if (effect == "Rainbow")
{
Rainbow_firstZone();
}
if (effect == "Blocked_Colors")
{
Blocked_firstZone();
}
if (effect == "BPM")
{
BPM_firstZone();
}
if (effect == "Twinkle")
{
Twinkle_firstZone();
}
if (effect == "Fill_Solid")
{
Solid_firstZone();
}
if (effect == "Ripple")
{
Ripple_firstZone();
}
}
if (showLights == false)
{
fill_solid(firstZone, FIRSTZONE_LEDS, CRGB::Black);
}
secondZone_doStuff();
}
/*********************** TIME FUNCTIONS ***************************/
/*********************** TIME FUNCTIONS ***************************/
/*********************** TIME FUNCTIONS ***************************/
/*********************** TIME FUNCTIONS ***************************/
/*********************** TIME FUNCTIONS ***************************/
void displayTime()
{
time_t t = now(); // store current time in variable t to prevent changes while updating
choosePattern();
if (HOUR_MODE == 0) {
if (hourFormat12(t) >= 10)
{
showDigit(1, 2, 10);
}
else
{
showDigit(0, 2, 10);
}
showDigit((hourFormat12(t) % 10), 6, 10);
} else if (HOUR_MODE == 1) {
showDigit(hour(t) / 10, 2, 10);
showDigit(hour(t) % 10, 6, 10);
}
showDigit((minute(t) / 10), 2, 4);
showDigit((minute(t) % 10), 6, 4);
}
void showTimer()
{
//unfinsihed function... basically when timer is called add that number of ms to now(), then in this function subtract now() from the saved timer value (also save the timer in SPIFFS) to calculate the time left on the timer.
long timeInSeconds = (currentTimer - now());
int timeInMinutes = timeInSeconds / 60;
choosePattern();
if (currentTimer < now())
{
displayTimer = false;
}
else if (timeInMinutes < 60)
{
showDigit((timeInMinutes / 10), 2, 10);
showDigit((timeInMinutes % 10), 6, 10);
showDigit(((timeInSeconds % 60) / 10), 2, 4);
showDigit(((timeInSeconds % 60) % 10), 6, 4);
Serial.print("Timer is ");
Serial.print((timeInMinutes / 10));
Serial.print((timeInMinutes % 10));
Serial.print(":");
Serial.print((timeInSeconds % 60) / 10);
Serial.println((timeInSeconds % 60) % 10);
}
else
{
showDigit(0, 2, 10);
showDigit(1, 6, 10);
showDigit(((timeInMinutes - 60) / 10), 2, 4);
showDigit(((timeInMinutes - 60) % 10), 6, 4);
Serial.print("Timer is ");
Serial.print("0");
Serial.print("1");
Serial.print(":");
Serial.print(((timeInMinutes - 60) / 10));
Serial.println(((timeInMinutes - 60) % 10));
}
}
void showDigit(byte digit, byte x, byte y)
{
for (byte i = 0; i < 15; i++) {
if (notdigits[digit][i] == 1) setPixel(x + (i - ((i / DIGITX) * DIGITX)) - 2, y - (i / DIGITX));
}
}
void setPixel(byte x, byte y)
{
byte pixel = 0;
int colorMode = 1;
if (x < RESX && y < RESY) {
if ((x % 2) == 0) pixel = x + (x * RESY) + y;
else pixel = ((x + 1) * RESY) - y + x;
if (colorMode == 1) firstZone[pixel] = CRGB::Black;
else firstZone[pixel] = CRGB::Black;
}
}
void gridRemove()
{
for (int i = 0; i < 23; i++)
{
firstZone[grid[i]] = CRGB::Black;
}
}
/*********************** NTP CLOCK FUNCTIONS ***************************/
/*********************** NTP CLOCK FUNCTIONS ***************************/
/*********************** NTP CLOCK FUNCTIONS ***************************/
/*********************** NTP CLOCK FUNCTIONS ***************************/
/*********************** NTP CLOCK FUNCTIONS ***************************/
/*********************** NTP CLOCK FUNCTIONS ***************************/
const int NTP_PACKET_SIZE = 48; // NTP time is in the first 48 bytes of message
byte packetBuffer[NTP_PACKET_SIZE]; //buffer to hold incoming & outgoing packets
time_t getNtpTime()
{
IPAddress ntpServerIP; // NTP server's ip address
while (Udp.parsePacket() > 0) ; // discard any previously received packets
Serial.println("Transmit NTP Request");
// get a random server from the pool
WiFi.hostByName(ntpServerName, ntpServerIP);
Serial.print(ntpServerName);
Serial.print(": ");
Serial.println(ntpServerIP);
sendNTPpacket(ntpServerIP);
uint32_t beginWait = millis();
while (millis() - beginWait < 1500) {
int size = Udp.parsePacket();
if (size >= NTP_PACKET_SIZE) {
Serial.println("Receive NTP Response");
Udp.read(packetBuffer, NTP_PACKET_SIZE); // read packet into the buffer
unsigned long secsSince1900;
// convert four bytes starting at location 40 to a long integer
secsSince1900 = (unsigned long)packetBuffer[40] << 24;
secsSince1900 |= (unsigned long)packetBuffer[41] << 16;
secsSince1900 |= (unsigned long)packetBuffer[42] << 8;
secsSince1900 |= (unsigned long)packetBuffer[43];
return secsSince1900 - 2208988800UL + timeZone * SECS_PER_HOUR;
}
}
Serial.println("No NTP Response :-(");
return 0; // return 0 if unable to get the time
}
// send an NTP request to the time server at the given address
void sendNTPpacket(IPAddress &address)
{
// set all bytes in the buffer to 0
memset(packetBuffer, 0, NTP_PACKET_SIZE);
// Initialize values needed to form NTP request
// (see URL above for details on the packets)
packetBuffer[0] = 0b11100011; // LI, Version, Mode
packetBuffer[1] = 0; // Stratum, or type of clock
packetBuffer[2] = 6; // Polling Interval
packetBuffer[3] = 0xEC; // Peer Clock Precision
// 8 bytes of zero for Root Delay & Root Dispersion
packetBuffer[12] = 49;
packetBuffer[13] = 0x4E;
packetBuffer[14] = 49;
packetBuffer[15] = 52;
// all NTP fields have been given values, now
// you can send a packet requesting a timestamp:
Udp.beginPacket(address, 123); //NTP requests are to port 123
Udp.write(packetBuffer, NTP_PACKET_SIZE);
Udp.endPacket();
}
/***************** SETUP FUNCTION ****************************************/
/***************** SETUP FUNCTION ****************************************/
/***************** SETUP FUNCTION ****************************************/
/***************** SETUP FUNCTION ****************************************/
/***************** SETUP FUNCTION ****************************************/
void setup() {
Serial.begin(115200);
pinMode(RESET_PIN, INPUT_PULLUP);
configPortal();
int tempInt = atoi( savedTimezone );
timeZone = tempInt + DST;
FastLED.addLeds<WS2812B, Pin_firstZone, FIRSTZONE_COLOR_ORDER>(firstZone, FIRSTZONE_LEDS);
FastLED.addLeds<WS2812B, Pin_secondZone, FIRSTZONE_COLOR_ORDER>(secondZone, SECONDZONE_LEDS);
FastLED.setDither( 0 );
WiFi.setSleepMode(WIFI_NONE_SLEEP);
WiFi.mode(WIFI_STA);
wifiConnected = true;
if (wifiConnected) {
server.on("/", HTTP_GET, []() {
server.send(200, "text/plain", "This is an example index page your server may send.");
});
server.on("/test", HTTP_GET, []() {
server.send(200, "text/plain", "This is a second subpage you may have.");
});
server.onNotFound([]() {
if (!espalexa.handleAlexaApiCall(server.uri(), server.arg(0))) //if you don't know the URI, ask espalexa whether it is an Alexa control request
{
//whatever you want to do with 404s
server.send(404, "text/plain", "Not found");
}
});
device1 = new EspalexaDevice(alexaString1, colorLightChanged1, EspalexaDeviceType::color);
device2 = new EspalexaDevice(alexaString2, colorLightChanged2, EspalexaDeviceType::color);
device3 = new EspalexaDevice(alexaString3, colorLightChanged3, EspalexaDeviceType::color);
device4 = new EspalexaDevice(alexaString4, colorLightChanged4, EspalexaDeviceType::color);
espalexa.addDevice(device1);
espalexa.addDevice(device2);
espalexa.addDevice(device3);
espalexa.addDevice(device4);
device1->setValue(brightness);
device1->setColor(red1, green1, blue1);
setEffect();
device2->setColor(red2, green2, blue2);
device3->setValue(127);
device3->setColor(red3, green3, blue3);
device4->setValue(0);
espalexa.begin(&server);
}
else
{
delay(5000);
ESP.restart();
}
Udp.begin(localPort);
setSyncProvider(getNtpTime);
setSyncInterval(300);
ArduinoOTA.setHostname(USER_MQTT_CLIENT_NAME);
ArduinoOTA.begin();
gPal = HeatColors_p;
if (currentTimer > now())
{
displayTimer = true;
}
timer.setTimeout(10000, chase);
timer.setTimeout(120000, checkIn);
}
/***************** MAIN LOOP ****************************************/
/***************** MAIN LOOP ****************************************/
/***************** MAIN LOOP ****************************************/
/***************** MAIN LOOP ****************************************/
/***************** MAIN LOOP ****************************************/
void loop()
{
espalexa.loop();
ArduinoOTA.handle();
timer.run();
EVERY_N_MILLISECONDS( 20 )
{
gHue++;
}
if (displayTimer == true)
{
showTimer();
gridRemove();
FastLED[0].showLeds(brightness);
}
else
{
displayTime();
gridRemove();
FastLED[0].showLeds(brightness);
}
FastLED[1].showLeds(brightness);
}
/***************** ALEXA CALLBACKS ****************************************/
/***************** ALEXA CALLBACKS ****************************************/
/***************** ALEXA CALLBACKS ****************************************/
/***************** ALEXA CALLBACKS ****************************************/
/***************** ALEXA CALLBACKS ****************************************/
void colorLightChanged1(EspalexaDevice* d)
{
int brightnessCommand = d->getValue();
Serial.println("Got Data for device 1");
if (brightnessCommand == 0)
{
showLights = false;
}
else
{
showLights = true;
}
brightness = brightnessCommand;
red1 = d->getR();
green1 = d->getG();
blue1 = d->getB();
itoa(brightnessCommand, savedBrightness, 10);
itoa(red1, r1, 10);
itoa(green1, g1, 10);
itoa(blue1, b1, 10);
Serial.println("saving position");
DynamicJsonBuffer jsonBuffer;
JsonObject& json = jsonBuffer.createObject();
json["r1"] = r1;
json["g1"] = g1;
json["b1"] = b1;
json["savedBrightness"] = savedBrightness;
File storedFile = SPIFFS.open("/color1.json", "w");
if (!storedFile)
{
Serial.println("failed to open config file for writing");
}
json.printTo(Serial);
json.printTo(storedFile);
storedFile.close();
}
void colorLightChanged2(EspalexaDevice* d)
{
Serial.println("Got Data for device 2");
fill_solid(firstZone, FIRSTZONE_LEDS, CRGB::Black);
int brightnessPercent = d->getPercent();
if (brightnessPercent > 0 && brightnessPercent <= 10)
{
effect = "Fill_Solid";
}
if (brightnessPercent > 10 && brightnessPercent <= 20)
{
effect = "Color_Chase";
}
if (brightnessPercent > 20 && brightnessPercent <= 30)
{
effect = "Color_Glitter";
}
if (brightnessPercent > 30 && brightnessPercent <= 40)
{
effect = "Single_Race";
}
if (brightnessPercent > 40 && brightnessPercent <= 50)
{
effect = "Double_Crash";
}
if (brightnessPercent > 50 && brightnessPercent <= 60)
{
effect = "Rainbow";
}
if (brightnessPercent > 60 && brightnessPercent <= 70)
{
effect = "Blocked_Colors";
}
if (brightnessPercent > 70 && brightnessPercent <= 80)
{
effect = "BPM";
}
if (brightnessPercent > 80 && brightnessPercent <= 90)
{
effect = "Twinkle";
}
if (brightnessPercent > 90 && brightnessPercent <= 100)
{
effect = "Ripple";
}
red2 = d->getR();
green2 = d->getG();
blue2 = d->getB();
itoa(red2, r2, 10);
itoa(green2, g2, 10);
itoa(blue2, b2, 10);
effect.toCharArray(savedEffect, effect.length() + 1);
Serial.println("saving position");
DynamicJsonBuffer jsonBuffer;
JsonObject& json = jsonBuffer.createObject();
json["savedEffect"] = savedEffect;
json["r2"] = r2;
json["g2"] = g2;
json["b2"] = b2;
File storedFile = SPIFFS.open("/color2.json", "w");
if (!storedFile)
{
Serial.println("failed to open config file for writing");
}
json.printTo(Serial);
json.printTo(storedFile);
storedFile.close();
}
void colorLightChanged3(EspalexaDevice* d)
{
Serial.println("Got Data for device 3");
int brightnessCommand = d->getValue();
int intPayload = map(brightnessCommand, 0, 255, 0, 500);
if (effect == "Double_Crash" || effect == "Single_Race")
{
raceSpeed = (intPayload / 20);
}
if (effect == "BPM")