-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathlightclockwifi.ino
2655 lines (2244 loc) · 75.7 KB
/
lightclockwifi.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
#include <Arduino.h>
/*This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <WebSocketsServer.h>
#include <math.h>
#include <Time.h>
#include <TimeLib.h>
//#include <TimeAlarms.h>
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <ESP8266SSDP.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <DNSServer.h>
#include <NeoPixelBus.h>
#include <EEPROM.h>
#include <ntp.h>
#include <Ticker.h>
//#include <FastLED.h>
#include "h/settings.h"
#include "h/root.h"
#include "h/timezone.h"
#include "h/timezonesetup.h"
#include "h/css.h"
#include "h/webconfig.h"
#include "h/importfonts.h"
#include "h/clearromsure.h"
#include "h/password.h"
#include "h/buttongradient.h"
#include "h/externallinks.h"
#include "h/spectrumcss.h"
#include "h/send_progmem.h"
#include "h/colourjs.h"
#include "h/clockjs.h"
#include "h/spectrumjs.h"
#include "h/alarm.h"
#include "h/game.h"
#include <ESP8266HTTPUpdateServer.h>
#define clockPin 4 //GPIO pin that the LED strip is on
int pixelCount = 120; //number of pixels in RGB clock
#define night 0 // for switching between various clock modes
#define alarm 1
#define normal 2
#define dawnmode 3
#define game 4
int clockmode = normal;
//for switching various night clock modes
#define black 0
#define dots 1
#define dim 2
#define moonphase 3
#define disabled 4
#define moonmode 1
bool dawnbreak;
int sleeptype = dots;
#define gamestartpoints 20 //number of points a player starts with, this will determine how long a game lasts
//#define SECS_PER_HOUR 3600 //number of seconds in an hour
byte mac[6]; // MAC address
String macString;
String ipString;
String netmaskString;
String gatewayString;
String clockname = "thelightclock";
IPAddress dns(8, 8, 8, 8); //Google dns
const char* ssid = "The Light Clock"; //The ssid when in AP mode
MDNSResponder mdns;
WebSocketsServer webSocket = WebSocketsServer(81);
ESP8266WebServer server(80);
//ESP8266WebServer httpServer(80);
ESP8266HTTPUpdateServer httpUpdater;
DNSServer dnsServer;
IPAddress apIP(192, 168, 4, 1);
IPAddress netMsk(255, 255, 255, 0);
const byte DNS_PORT = 53;
//WiFiUDP UDP;
unsigned int localPort = 2390; // local port to listen on for magic locator packets
char packetBuffer[255]; //buffer to hold incoming packet
char ReplyBuffer[] = "I'm a light clock!"; // a string to send back
NeoPixelBus* clockleds = NULL; // NeoPixelBus(pixelCount, clockPin); //Clock Led on Pin 4
time_t getNTPtime(void);
NTP NTPclient;
Ticker NTPsyncclock;
WiFiClient DSTclient;
Ticker alarmtick;
int alarmprogress = 0;
Ticker pulseBrightnessTicker;
Ticker gamestartticker;
int pulseBrightnessCounter =0;
Ticker dawntick;//a ticker to establish how far through dawn we are
int dawnprogress = 0;
const char* DSTTimeServer = "api.timezonedb.com";
bool DSTchecked = 0;
const int restartDelay = 3; //minimal time for button press to reset in sec
const int humanpressDelay = 50; // the delay in ms untill the press should be handled as a normal push by human. Button debouce. !!! Needs to be less than restartDelay & resetDelay!!!
const int resetDelay = 20; //Minimal time for button press to reset all settings and boot to config mode in sec
int webMode; //decides if we are in setup, normal or local only mode
const int debug = 0; //Set to one to get more log to serial
bool updateTime = true;
unsigned long count = 0; //Button press time counter
String st; //WiFi Stations HTML list
int testrun;
//to be read from EEPROM Config
String esid = "";
String epass = "";
float latitude;
float longitude;
RgbColor hourcolor; // starting colour of hour
RgbColor minutecolor; //starting colour of minute
RgbColor alarmcolor; //the color the alarm will be
int brightness = 50; // a variable to dim the over-all brightness of the clock
int maxBrightness = 100;
uint8_t blendpoint = 40; //level of default blending
int randommode; //face changes colour every hour
int hourmarks = 1; //where marks should be made (midday/quadrants/12/brianmode)
int sleep = 22; //when the clock should go to night mode
int sleepmin = 0; //when the clock should go to night mode
int wake = 7; //when clock should wake again
int wakemin = 0; //when clock should wake again
int nightmode = 0;
unsigned long lastInteraction;
float timezone = 10; //Australian Eastern Standard Time
int timezonevalue;
int DSTtime; //add one if we're in DST
bool showseconds; //should the seconds hand tick around
bool DSTauto; //should the clock automatically update for DST
int alarmmode = 0;
int gamearray[6];
RgbColor playercolors[6];
int playersremaining = 0;
int playercount = 0;
int nextplayer =0;
int gamebrightness = 100;
int speed = 0;
bool gamestarted = 0;
int loopcounter;
//new_moon = makeTime(0, 0, 0, 7, 0, 1970);
int accelerator = 5;
int prevsecond;
int hourofdeath; //saves the time incase of an unplanned reset
int minuteofdeath; //saves the time incase of an unplanned reset
//-----------------------------------standard arduino setup and loop-----------------------------------------------------------------------
void setup() {
playercolors[0] = RgbColor(255, 0, 0);
playercolors[1] = RgbColor(0, 255, 0);
playercolors[2] = RgbColor(0, 0, 255);
playercolors[3] = RgbColor(255, 255, 0);
playercolors[4] = RgbColor(255, 0, 255);
playercolors[5] = RgbColor(0, 255, 255);//needed to move the colour declaration to setup.
httpUpdater.setup(&server);
EEPROM.begin(512);
//write a magic byte to eeprom 196 to determine if we've ever booted on this device before
if (EEPROM.read(500) != 196) {
//if not load default config files to EEPROM
writeInitalConfig();
}
loadConfig();
delay(10);
Serial.begin(115200);
delete clockleds;
clockleds = new NeoPixelBus(pixelCount, clockPin);
clockleds->Begin();
nightCheck();
updateface();
clockleds->Show();
initWiFi();
lastInteraction = millis();
//adjustTime(36600);
delay(1000);
if (DSTauto == 1) {
readDSTtime();
}
//initialise the NTP clock sync function
if (webMode == 1) {
NTPclient.begin("2.au.pool.ntp.org", timezone + DSTtime);
setSyncInterval(SECS_PER_HOUR);
setSyncProvider(getNTPtime);
macString = String(WiFi.macAddress());
ipString = StringIPaddress(WiFi.localIP());
netmaskString = StringIPaddress(WiFi.subnetMask());
gatewayString = StringIPaddress(WiFi.gatewayIP());
}
//UDP.begin(localPort);
prevsecond = second();// initalize prev second for main loop
//update sleep/wake to current
nightCheck();
// start webSocket server
webSocket.begin();
webSocket.onEvent(webSocketEvent);
MDNS.addService("http", "tcp", 80);
MDNS.addService("ws", "tcp", 81);
}
void loop() {
//Serial.print("Loop ");
if (webMode == 0) {
//initiate web-capture mode
dnsServer.processNextRequest();
}
if (webMode == 0 && millis() - lastInteraction > 300000) {//if the clock is looking for wifi network and hasn't found one after 5 minutes, and no one has tried to set it up, then reboot the clock
lastInteraction = millis();
ESP.reset();
}
webSocket.loop();
server.handleClient();
delay(50);
if (second() != prevsecond) {
if (webMode != 0 && second() == 0 && minute() % 10 == 0) { //only record "time of death" if we're in normal running mode.
EEPROM.begin(512);
delay(10);
EEPROM.write(193, hour());
EEPROM.write(194, minute());
EEPROM.write(191, brightness);
EEPROM.commit();
delay(50); // this section of code will save the "time of death" to the clock so if it unexpectedly resets it should be seemless to the user.
saveFace(0);
}
if (second() == 0) {
if (hour() == sleep && minute() == sleepmin) {
clockmode = night;
}
if (hour() == wake && minute() == wakemin) {
clockmode = normal;
}
if ((hour() + 25) % 24 == wake && minute() == wakemin) {
if (dawnbreak) {
dawnprogress = 0;
clockmode = dawnmode;
dawntick.attach(14, dawnadvance);
}
}
//nightCheck();
}
// updateface();
prevsecond = second();
// Serial.print("loopcounter: ");
// Serial.println(loopcounter);
// loopcounter = 0;
}
updateface();
clockleds->Show();
loopcounter++;
}
//--------------------UDP responder functions----------------------------------------------------
//void checkUDP(){
// //Serial.println("checking UDP");
// // if there's data available, read a packet
// int packetSize = UDP.parsePacket();
// if (packetSize) {
// Serial.print("Received packet of size ");
// Serial.println(packetSize);
// Serial.print("From ");
// IPAddress remoteIp = UDP.remoteIP();
// Serial.print(remoteIp);
// Serial.print(", port ");
// Serial.println(UDP.remotePort());
//
// // read the packet into packetBufffer
// int len = UDP.read(packetBuffer, 255);
// if (len > 0) {
// packetBuffer[len] = 0;
// }
// Serial.println("Contents:");
// Serial.println(packetBuffer);
//
// // send a reply, to the IP address and port that sent us the packet we received
// UDP.beginPacket(UDP.remoteIP(), UDP.remotePort());
// UDP.write(ReplyBuffer);
// UDP.endPacket();
// }
//}
//--------------------EEPROM initialisations-----------------------------------------------------
void loadConfig() {
Serial.println("reading settings from EEPROM");
//Tries to read ssid and password from EEPROM
EEPROM.begin(512);
delay(10);
for (int i = 0; i < 32; ++i)
{
esid += char(EEPROM.read(i));
}
Serial.print("SSID: ");
Serial.println(esid);
for (int i = 32; i < 96; ++i)
{
epass += char(EEPROM.read(i));
}
Serial.print("PASS: ");
Serial.println(epass);
clockname = "";
for (int i = 195; i < 228; ++i)
{
clockname += char(EEPROM.read(i));
}
clockname = clockname.c_str();
Serial.print("clockname: ");
Serial.println(clockname);
loadFace(0);
latitude = readLatLong(175);
Serial.print("latitude: ");
Serial.println(latitude);
longitude = readLatLong(177);
Serial.print("longitude: ");
Serial.println(longitude);
timezonevalue = EEPROM.read(179);
Serial.print("timezonevalue: ");
Serial.println(timezonevalue);
interpretTimeZone(timezonevalue);
Serial.print("timezone: ");
Serial.println(timezone);
randommode = EEPROM.read(180);
Serial.print("randommode: ");
Serial.println(randommode);
hourmarks = EEPROM.read(181);
Serial.print("hourmarks: ");
Serial.println(hourmarks);
sleep = EEPROM.read(182);
Serial.print("sleep: ");
Serial.println(sleep);
sleeptype = EEPROM.read(228);
Serial.print("sleep: ");
Serial.println(sleep);
sleepmin = EEPROM.read(183);
Serial.print("sleepmin: ");
Serial.println(sleepmin);
showseconds = EEPROM.read(184);
Serial.print("showseconds: ");
Serial.println(showseconds);
DSTauto = EEPROM.read(185);
Serial.print("DSTauto: ");
Serial.println(DSTauto);
webMode = EEPROM.read(186);
Serial.print("webMode: ");
Serial.println(webMode);
wake = EEPROM.read(189);
Serial.print("wake: ");
Serial.println(wake);
wakemin = EEPROM.read(190);
Serial.print("wakemin: ");
Serial.println(wakemin);
brightness = EEPROM.read(191);
Serial.print("brightness: ");
Serial.println(brightness);
DSTtime = EEPROM.read(192);
Serial.print("DST (true/false): ");
Serial.println(DSTtime);
hourofdeath = EEPROM.read(193);
Serial.print("Hour of Death: ");
Serial.println(hourofdeath);
minuteofdeath = EEPROM.read(194);
Serial.print("minuteofdeath: ");
Serial.println(minuteofdeath);
setTime(hourofdeath, minuteofdeath, 30, 0, 0, 0);
dawnbreak = EEPROM.read(229);
Serial.print("dawnbreak: ");
Serial.println(dawnbreak);
pixelCount = EEPROM.read(230);
Serial.print("pixelcount: ");
Serial.println(pixelCount);
maxBrightness = EEPROM.read(231);
Serial.print("maxBrightness: ");
Serial.println(maxBrightness);
}
void writeInitalConfig() {
Serial.println("can't find settings so writing defaults");
EEPROM.begin(512);
delay(10);
writeLatLong(-36.1214, 175); //default to wodonga
writeLatLong(146.881, 177);//default to wodonga
EEPROM.write(179, 10);//timezone default AEST
EEPROM.write(180, 0);//default randommode off
EEPROM.write(181, 0); //default hourmarks to off
EEPROM.write(182, 22); //default to sleep at 22:00
EEPROM.write(183, 0);
EEPROM.write(184, 1); //default to showseconds to yes
EEPROM.write(185, 0); //default DSTauto off until user sets lat/long
EEPROM.write(186, 0); //default webMode to setup mode off until user sets local wifi
EEPROM.write(500, 196);//write magic byte to 500 so that system knows its set up.
EEPROM.write(228, 1);//default sleeptype to 1 (dots)
EEPROM.write(189, 7);//default wake 7 hours
EEPROM.write(190, 0); //default to wake at 00 minutes
EEPROM.write(191, 100); //default to full brightness on USB so as not to crash
EEPROM.write(192, 0); //default no daylight savings
EEPROM.write(193, 10); //default "hour of death" is 10am
EEPROM.write(220, 1); //default dawnbreak to "on"
EEPROM.write(230, 120); //default to normal size light clock
EEPROM.write(231, 255); //default to mains power for max brightness
for (int i = 195; i < 228; i++) {//zero (instead of null) the values where clockname will be written.
EEPROM.write(i, 0);
}
EEPROM.write(194, 10); //default "minute of death" is 10am
for (int i = 0; i < clockname.length(); ++i) {
EEPROM.write(195 + i, clockname[i]);
Serial.print(clockname[i]);
}
EEPROM.commit();
delay(500);
//face 1 defaults
hourcolor = RgbColor(255, 255, 0);
minutecolor = RgbColor(0, 57, 255);
blendpoint = 70;
saveFace(0);
saveFace(1);
//face 2 defaults
hourcolor = RgbColor(255, 0, 0);
minutecolor = RgbColor(0, 0, 255);
blendpoint = 60;
saveFace(2);
//face 3 defaults
hourcolor = RgbColor(255, 0, 0);
minutecolor = RgbColor(255, 255, 0);
blendpoint = 90;
saveFace(3);
}
void initWiFi() {
Serial.println();
Serial.println();
Serial.println("Startup");
esid.trim();
if (webMode == 2) {
WiFi.mode(WIFI_AP);
WiFi.softAPConfig(apIP, apIP, netMsk);
WiFi.softAP(ssid);
// WiFi.begin((char*) ssid.c_str()); // not sure if need but works
//dnsServer.setErrorReplyCode(DNSReplyCode::NoError);
//dnsServer.start(DNS_PORT, "*", apIP);
Serial.println("USP Server started");
Serial.print("Access point started with name ");
Serial.println(ssid);
//server.on("/generate_204", handleRoot); //Android captive
server.onNotFound(handleRoot);
launchWeb(2);
return;
}
if (webMode == 1) {
// test esid
WiFi.disconnect();
delay(100);
WiFi.mode(WIFI_STA);
Serial.print("Connecting to WiFi ");
Serial.println(esid);
WiFi.begin(esid.c_str(), epass.c_str());
if ( testWifi() == 20 ) {
launchWeb(1);
return;
}
}
logo();
setupAP();
}
int testWifi(void) {
int c = 0;
Serial.println("Waiting for Wifi to connect");
while ( c < 30 ) {
if (WiFi.status() == WL_CONNECTED) {
return (20);
}
delay(500);
Serial.print(".");
c++;
}
Serial.println("Connect timed out, opening AP");
return (10);
}
void setupAP(void) {
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);
int n = WiFi.scanNetworks();
Serial.println("scan done");
if (n == 0) {
Serial.println("no networks found");
st = "<label><input type='radio' name='ssid' value='No networks found' onClick='regularssid()'>No networks found</input></label><br>";
} else {
Serial.print(n);
Serial.println("Networks found");
st = "";
for (int i = 0; i < n; ++i)
{
// Print SSID and RSSI for each network found
Serial.print(i + 1);
Serial.print(": ");
Serial.print(WiFi.SSID(i));
Serial.print(" (");
Serial.print(WiFi.RSSI(i));
Serial.print(")");
Serial.println((WiFi.encryptionType(i) == ENC_TYPE_NONE) ? " (OPEN)" : "*");
// Print to web SSID and RSSI for each network found
st += "<label><input type='radio' name='ssid' value='";
st += WiFi.SSID(i);
st += "' onClick='regularssid()'>";
st += i + 1;
st += ": ";
st += WiFi.SSID(i);
st += " (";
st += WiFi.RSSI(i);
st += ")";
st += (WiFi.encryptionType(i) == ENC_TYPE_NONE) ? " (OPEN)" : "*";
st += "</input></label><br>";
delay(10);
}
//st += "</ul>";
}
Serial.println("");
WiFi.disconnect();
delay(100);
WiFi.mode(WIFI_AP);
WiFi.softAPConfig(apIP, apIP, netMsk);
WiFi.softAP(ssid);
//WiFi.begin((char*) ssid.c_str()); // not sure if need but works
dnsServer.setErrorReplyCode(DNSReplyCode::NoError);
dnsServer.start(DNS_PORT, "*", apIP);
Serial.println("USP Server started");
Serial.print("Access point started with name ");
Serial.println(ssid);
//WiFi.begin((char*) ssid.c_str()); // not sure if need but works
Serial.print("Access point started with name ");
Serial.println(ssid);
launchWeb(0);
}
//------------------------------------------------------Web handle sections-------------------------------------------------------------------
void launchWeb(int webtype) {
webMode = webtype;
int clockname_len = clockname.length() + 1;
char clocknamechar[clockname_len];
Serial.println("");
Serial.println("WiFi connected");
switch (webtype) {
case 0:
//set up wifi network to connect to since we are in setup mode.
webMode == 0;
Serial.println(WiFi.softAPIP());
server.on("/", webHandleConfig);
server.on("/a", webHandleConfigSave);
server.on("/timezonesetup", webHandleTimeZoneSetup);
server.on("/passwordinput", webHandlePassword);
server.on("/clockmenustyle.css", handleCSS);
server.on("/switchwebmode", webHandleSwitchWebMode);
server.on("/generate_204", webHandleConfig); //Android captive
server.onNotFound(webHandleConfig);
break;
case 1:
//setup DNS since we are a client in WiFi net
clockname.toCharArray(clocknamechar, clockname_len);
if (!mdns.begin(clocknamechar)) {
Serial.println("Error setting up MDNS responder!");
while (1) {
delay(1000);
}
} else {
Serial.println("mDNS responder started");
}
Serial.printf("Starting SSDP...\n");
SSDP.setSchemaURL("description.xml");
SSDP.setHTTPPort(80);
SSDP.setName("The Light Clock");
SSDP.setSerialNumber("4");
SSDP.setURL("index");
SSDP.setModelName("The Light Clock v1");
SSDP.setModelNumber("4");
SSDP.setModelURL("http://www.thelightclock.com");
SSDP.setManufacturer("Omnino Realis");
SSDP.setManufacturerURL("http://www.thelightclock.com");
SSDP.begin();
Serial.println(WiFi.localIP());
setUpServerHandle();
break;
case 2:
//direct control over clock through it's own wifi network
setUpServerHandle();
break;
}
if (webtype == 0) {
} else {
}
//server.onNotFound(webHandleRoot);
server.begin();
Serial.println("Web server started");
//Store global to use in loop()
}
void setUpServerHandle() {
server.on("/", handleRoot);
server.on("/index.html", handleRoot);
server.on("/description.xml", ssdpResponder);
server.on("/cleareeprom", webHandleClearRom);
server.on("/cleareepromsure", webHandleClearRomSure);
server.on("/settings", handleSettings);
server.on("/timezone", handleTimezone);
server.on("/clockmenustyle.css", handleCSS);
server.on("/spectrum.css", handlespectrumCSS);
server.on("/spectrum.js", handlespectrumjs);
server.on("/Colour.js", handlecolourjs);
server.on("/clock.js", handleclockjs);
server.on("/switchwebmode", webHandleSwitchWebMode);
server.on("/nightmodedemo", webHandleNightModeDemo);
server.on("/timeset", webHandleTimeSet);
server.on("/alarm", webHandleAlarm);
server.on("/reflection", webHandleReflection);
server.on("/dawn", webHandleDawn);
server.on("/moon", webHandleMoon);
server.on("/brighttest", brighttest);
server.on("/lightup", lightup);
server.on("/game", webHandleGame);
server.on("/speed",speedup);
server.begin();
}
void speedup() {
speed++;
speed = speed%3;
server.send(200, "text/html", "Speed Up: " + speed);
}
void webHandleSwitchWebMode() {
Serial.println("Sending webHandleSwitchWebMode");
if ((webMode == 0) || (webMode == 1)) {
webMode = 2;
server.send(200, "text/html", "webMode set to 2");
} else {
webMode = 1;
server.send(200, "text/html", "webMode set to 1");
}
EEPROM.begin(512);
delay(10);
EEPROM.write(186, webMode);
Serial.println(webMode);
EEPROM.commit();
delay(1000);
EEPROM.end();
ESP.reset();
}
void webHandleConfig() {
lastInteraction = millis();
Serial.println("Sending webHandleConfig");
IPAddress ip = WiFi.softAPIP();
String ipStr = String(ip[0]) + '.' + String(ip[1]) + '.' + String(ip[2]) + '.' + String(ip[3]);
String s;
String toSend = FPSTR(webconfig_html);
//toSend.replace("$css", css_file);
toSend.replace("$ssids", st);
server.send(200, "text/html", toSend);
}
void webHandlePassword() {
Serial.println("Sending webHandlePassword");
String toSend = FPSTR(password_html);
//toSend.replace("$css", css_file);
server.send(200, "text/html", toSend);
String qsid;
if (server.arg("ssid") == "other") {
qsid = server.arg("other");
} else {
qsid = server.arg("ssid");
}
cleanASCII(qsid);
Serial.println(qsid);
Serial.println("");
Serial.println("clearing old ssid.");
clearssid();
EEPROM.begin(512);
delay(10);
Serial.println("writing eeprom ssid.");
//addr += EEPROM.put(addr, qsid);
for (int i = 0; i < qsid.length(); ++i)
{
EEPROM.write(i, qsid[i]);
Serial.print(qsid[i]);
}
Serial.println("");
EEPROM.commit();
delay(1000);
EEPROM.end();
}
void cleanASCII(String &input) {
input.replace("%21", "!");
input.replace("%22", "\"");
input.replace("%23", "#");
input.replace("%24", "$");
input.replace("%25", "%");
input.replace("%26", "&");
input.replace("%27", "'");
input.replace("%28", "(");
input.replace("%29", ")");
input.replace("%2A", "*");
input.replace("%2B", "+");
input.replace("%2C", ",");
input.replace("%2D", "-");
input.replace("%2E", ".");
input.replace("%2F", "/");
input.replace("%3A", ":");
input.replace("%3B", ";");
input.replace("%3C", "<");
input.replace("%3D", "=");
input.replace("%3E", ">");
input.replace("%3F", "?");
input.replace("%40", "@");
input.replace("%5B", "[");
input.replace("%5D", "]");
input.replace("%5E", "^");
input.replace("%5F", "_");
input.replace("%60", "`");
input.replace("%7B", "{");
input.replace("%7C", "|");
input.replace("%7D", "}");
input.replace("%7E", "~");
input.replace("%7F", "");
input.replace("+", " ");
}
void webHandleTimeZoneSetup() {
Serial.println("Sending webHandleTimeZoneSetup");
String toSend = FPSTR(timezonesetup_html);
//toSend.replace("$css", css_file);
toSend.replace("$timezone", String(timezone));
toSend.replace("$latitude", String(latitude));
toSend.replace("$longitude", String(longitude));
server.send(200, "text/html", toSend);
Serial.println("clearing old pass.");
clearpass();
String qpass;
qpass = server.arg("pass");
cleanASCII(qpass);
Serial.println(qpass);
Serial.println("");
//int addr=0;
EEPROM.begin(512);
delay(10);
Serial.println("writing eeprom pass.");
//addr += EEPROM.put(addr, qpass);
for (int i = 0; i < qpass.length(); ++i)
{
EEPROM.write(32 + i, qpass[i]);
Serial.print(qpass[i]);
}
Serial.println("");
EEPROM.write(186, 1);
EEPROM.commit();
delay(1000);
EEPROM.end();
}
void webHandleConfigSave() {
lastInteraction = millis();
Serial.println("Sending webHandleConfigSave");
// /a?ssid=blahhhh&pass=poooo
String s;
s = "<p>Settings saved to memory. Clock will now restart and you can find it on your local WiFi network. <p>Please reconnect your phone to your WiFi network first</p>\r\n\r\n";
server.send(200, "text/html", s);
EEPROM.begin(512);
if (server.hasArg("timezone")) {
String timezonestring = server.arg("timezone");
timezonevalue = timezonestring.toInt();//atoi(c);
interpretTimeZone(timezonevalue);
EEPROM.write(179, timezonevalue);
DSTauto = 0;
EEPROM.write(185, 0);
}
if (server.hasArg("DST")) {
DSTtime = 1;
EEPROM.write(192, 1);
}
if (server.hasArg("pixelCount")) {
String pixelCountString = server.arg("pixelCount"); //get value from blend slider
pixelCount = pixelCountString.toInt();//atoi(c); //get value from html5 color element
ChangeNeoPixels(pixelCount, clockPin);
EEPROM.write(230, pixelCount);
}
if (server.hasArg("powerType")) {
String powerTypeString = server.arg("powerType"); //get value from blend slider
int powerType = powerTypeString.toInt();//atoi(c); //get value from html5 color element
if (powerType == 1) {
maxBrightness = 255;
} else {
maxBrightness = 100;
}
brightness = maxBrightness;
EEPROM.write(191, brightness);
EEPROM.write(231, maxBrightness);
}
if (server.hasArg("latitude")) {
String latitudestring = server.arg("latitude"); //get value from blend slider
latitude = latitudestring.toInt();//atoi(c); //get value from html5 color element
writeLatLong(175, latitude);
}
if (server.hasArg("longitude")) {
String longitudestring = server.arg("longitude"); //get value from blend slider
longitude = longitudestring.toInt();//atoi(c); //get value from html5 color element
writeLatLong(177, longitude);
DSTauto = 1;
EEPROM.write(185, 1);
EEPROM.write(179, timezone);
}
EEPROM.commit();
delay(1000);
EEPROM.end();
Serial.println("Settings written, restarting!");
ESP.reset();
}
void handleNotFound() {
Serial.println("Sending handleNotFound");
Serial.print("\t\t\t\t URI Not Found: ");
Serial.println(server.uri());
server.send ( 200, "text/plain", "URI Not Found" );
}
void handleCSS() {
server.send(200, "text/css", css_file);
//WiFiClient client = server.client();
//sendProgmem(client, css_file);
Serial.println("Sending CSS");
}
void handlecolourjs() {
server.send(200, "text/plain", FPSTR(colourjs));
//WiFiClient client = server.client();
//sendProgmem(client, colourjs);
Serial.println("Sending colourjs");
}
void handlespectrumjs() {
server.send(200, "text/plain", spectrumjs);
//WiFiClient client = server.client();
//sendProgmem(client, spectrumjs);
Serial.println("Sending spectrumjs");
}
void handleclockjs() {
server.send(200, "text/plain", FPSTR(clockjs));
//WiFiClient client = server.client();
//sendProgmem(client, clockjs);
Serial.println("Sending clockjs");
}
void handlespectrumCSS() {
server.send(200, "text/css", FPSTR(spectrumCSS));
//WiFiClient client = server.client();
//sendProgmem(client, spectrumCSS);
Serial.println("Sending spectrumCSS");
}
void handleRoot() {
float alarmHour;
float alarmMin;
float alarmSec;
EEPROM.begin(512);
RgbColor tempcolor;
HslColor tempcolorHsl;
if (server.hasArg("pixelCount")) {
String pixelCountString = server.arg("pixelCount"); //get value from blend slider
pixelCount = pixelCountString.toInt();//atoi(c); //get value from html5 color element
ChangeNeoPixels(pixelCount, clockPin);
EEPROM.write(230, pixelCount);
}
if (server.hasArg("powerType")) {
String powerTypeString = server.arg("powerType"); //get value from blend slider
int powerType = powerTypeString.toInt();//atoi(c); //get value from html5 color element
if (powerType == 1) {
maxBrightness = 255;
} else {
maxBrightness = 100;
brightness = _min(maxBrightness, brightness);
}