-
Notifications
You must be signed in to change notification settings - Fork 0
/
IoT_Timer_Ver_1.10.ino
1949 lines (1847 loc) Β· 61.8 KB
/
IoT_Timer_Ver_1.10.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
// FOR SONOFF BASIC
// SELECT GENERIC ESP8266
// SELECT 1M (64K SPIFFS)
// SELECT FLASH MODE = DOUT
// FOR ESP8266-01
// SELECT GENERIC ESP8266
// SELECT 512K (64K SPIFFS)
// SELECT FLASH MODE = DIN
//
// FOR NODEMCU and WEMOS D1 MINI
// SELECT NODEMCU 1.00
// SELECT 4M (1M SPIFFS)
// SELECT FLASH MODE = DIN
//
// # # # N O T E # # #
//========================
//
// UNCOMMENT the first line for Sonoff Devices ONLY
// This will remap the I/O pins to the correct numbers
//
// For ESP8266-01, do NOT remap the I/O pins
//
// For NodeMCU and Wemos D1, change the I/O pins to your requirements.
//#define Sonoff 1
/*REVISION HISTORY
* Ver 1.10
* --------
* ESPIFFS removed - Should work on latest ESP-01 moduls
* Network settings now saved in EEPROM
*
* Ver 10.9
* --------
* Buf fix line 1765
*
* Ver 1.08
* --------
* Fixed Mode ON and OFF issues
*
* Ver 1.07
* --------
* Added 2nd default NTP IP address
*
* Ver 1.06
* --------
* Streamlined reading of request.
* No more 1 second delay if no data available
*
* Ver 1.05
* --------
* Changes to Manual Override made
* Minor bug fixes
*
* Ver 1.04
* --------
* Added Override-When-On timer function.
* Slight changes to web interface made.
*
* Ver 1.03
* ---------
* Increased wifiManager.setTimeout() from 120 to 180
* Disable Access Point after connection
* Changes to Override LED indications
*
* Ver 1.02
* --------
* WiFiManagerchanged to activate only when button pressed
*
* Ver 1.01
* --------
* NTP Time update streamlined
* Fixed NTP time update bug
* NTPtimeOk == false;
* instead of
* NTPtimeOk = false;
*
* Ver 1.00
* --------
* New User Interface with menu
*
*
*/
#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>
#include <TimeLib.h>
#include <WiFiUdp.h>
#include <EEPROM.h>
// Defines
#define DefaultName "IoT Smart Timer" // Default device name
#define NTPfastReq 10 // NTP time request in seconds when time not set
#define NTPslowReq 3600 // NTP time request in seconds after time is set
#ifdef Sonoff
#define Version "1.10 (Sonoff Basic)" // Firmware version
#else
#define Version "1.10 (ESP8266-01)" // Firmware version
#endif
// NTP Server details
//-------------------
IPAddress timeServer(129, 6, 15, 28); // time.nist.gov NTP server
WiFiUDP Udp;
unsigned int localPort = 3232; // Local port to listen for UDP packets
WiFiServer server(80);
//define your default values here, if there are different values in config.json, they are overwritten.
//length should be max size + 1
//-----------------------------
char static_ip[16] = "192.168.4.1";
char static_gw[16] = "192.168.4.1";
char static_sn[16] = "255.255.255.0";
//flag for saving configuration data
//----------------------------------
bool shouldSaveConfig = false;
// I/O config
//-----------
#ifdef Sonoff
#define Relay 12 // Output relay
#define Button 0 // Button
#define LED 13 // Satatus LED
#else
#define Relay 3 // Output relay
#define Button 2 // Button
#define LED 0 // Satatus LED
#endif
// Variables
//----------
#define ErrMsg "<font color=\"red\"> < < Error !</font>"
#define EEPROM_chk 110
String DevName = DefaultName;
float TimeZone = 0;
byte TimeZoneH = 0;
byte TimeZoneM = 0;
boolean ResetWiFi = false;
long timeNow = 0;
long timeOld = 300000;
boolean TimeOk = false;
boolean NTPtimeOk = false;
String request = "";
byte Page = 1;
int IP_1 = 129;
int IP_2 = 6;
int IP_3 = 15;
int IP_4 = 28;
boolean TimeCheck = false;
byte Mode = 0;
byte OnMode = 0;
byte OffMode = 0;
int TimerMinuteOn = 0;
int TimerHourOn = 0;
int TimerMinuteOff = 0;
int TimerHourOff = 0;
boolean TimedOff = false;
boolean TimedOn = false;
int NewHH = 0;
int NewMM = 0;
int Newdd = 0;
int Newmm = 0;
int Newyy = 0;
int PgmNr = 0;
int OnHour = 0;
int OnMinute = 0;
int OffHour = 0;
int OffMinute = 0;
boolean ManualOff = false;
boolean ManualOn = false;
boolean ManualTimeOff = false;
boolean ManualTimeOn = false;
long ManualSecOff = 0;
long ManualSecOn = 0;
int LastHH = 0;
int LastMM = 0;
int Lastdd = 0;
int Lastmm = 0;
int Lastyy = 0;
byte old_sec = 0;
long old_ms = 0;
boolean WebButton = false;
boolean PgmPrev = false;
boolean PgmNext = false;
boolean PgmSave = false;
boolean Error1 = false;
boolean Error2 = false;
boolean Error3 = false;
boolean Error3b = false;
boolean Error4 = false;
boolean Error5 = false;
boolean Error6 = false;
boolean Error7 = false;
boolean Error8 = false;
boolean D[8] = {false, false, false, false, false, false, false, false};
int On_Time[7];
int Off_Time[7];
boolean On_Days[7][8];
IPAddress _ip,_gw,_sn;
//###############################################################################################
// Setup
//
//###############################################################################################
void setup() {
// For debug only - REMOVE
//----------------------------------------------------------------------
//Serial.begin(115200);
// Setup I/O pins
//----------------------------------------------------------------------
pinMode(Button,INPUT_PULLUP);
pinMode(Relay,OUTPUT);
pinMode(LED,OUTPUT);
#ifdef Sonoff
digitalWrite(Relay,LOW);
digitalWrite(LED,HIGH);
#else
digitalWrite(Relay,LOW);
digitalWrite(LED,LOW);
#endif
// If button pressed, reset WiFi connection
//----------------------------------------------------------------------
CheckReset();
// Start WiFi and connect to AP
//----------------------------------------------------------------------
StartWiFi();
#ifdef Sonoff
digitalWrite(LED,HIGH);
#else
digitalWrite(LED,LOW);
#endif
}
//###############################################################################################
// Main program loop
//
//###############################################################################################
void loop() {
// Scan Button
//----------------------------------------------------------------------
ScanButton();
// Scan for NTP time changes
//----------------------------------------------------------------------
CheckNTPtime();
// See if time has changed
//----------------------------------------------------------------------
DoTimeCheck();
// Update LED
//----------------------------------------------------------------------
UpdateLED();
// See if any data was received via WiFi
//----------------------------------------------------------------------
WiFiClient client = server.available();
String request = "";
boolean DataAvailable = false;
if (client != 0) {
// Read requests from web page if available
//----------------------------------------------------------------------
request = client.readStringUntil('\r');
DataAvailable = true;
client.flush();
}
// Respond to requests from web page
//----------------------------------------------------------------------
if ( (DataAvailable == true) and (request.indexOf("/") != -1) ) {
//Serial.println(request);
if (request.indexOf("Link=1") != -1) Page = 1;
if (request.indexOf("Link=2") != -1) Page = 2;
if (request.indexOf("Link=3") != -1) Page = 3;
if (request.indexOf("Link=4") != -1) Page = 4;
if (request.indexOf("Link=5") != -1) Page = 5;
if (request.indexOf("GET / HTTP") != -1) Page = 1;
Error1 = false;
Error2 = false;
Error3 = false;
Error3b= false;
Error4 = false;
Error5 = false;
Error6 = false;
Error7 = false;
// Respond to Buttons
//==================================
// PAGE 1 - STATUS
//----------------
// See if Save Button was presed
//----------------------------------------------------------------------
if (request.indexOf("SaveBtn1=") != -1) {
Page = 1;
WebButton = true;
ScanButton();
}
// See if Refresh Button was pressed
//----------------------------------------------------------------------
if (request.indexOf("RefreshBtn1=") != -1) {
Page = 1;
}
// PAGE 2 - PROGRAMS
//------------------
// See if Previous Buttomn was pressed
//----------------------------------------------------------------------
if (request.indexOf("SaveBtnPrev=") != -1) {
Page = 2;
PgmPrev = true;
PgmNext = false;
PgmSave = true;
}
// See if Next Button was pressed
//----------------------------------------------------------------------
if (request.indexOf("SaveBtnNext=") != -1) {
Page = 2;
PgmPrev = false;
PgmNext = true;
PgmSave = true;
}
// See if Save Button was pressed
//----------------------------------------------------------------------
if (request.indexOf("SaveBtn2=") != -1) {
Page = 2;
PgmSave = true;
PgmPrev = false;
PgmNext = false;
}
// See if Clear Button was pressed
//----------------------------------------------------------------------
if (request.indexOf("ClearBtn1=") != -1) {
Page = 2;
On_Time[PgmNr] = 0;
Off_Time[PgmNr] = 0;
for (byte i = 0; i < 7; i++ ) {
On_Days[PgmNr][i] = false;
}
PgmPrev = false;
PgmNext = false;
// Save program data
SaveProgram();
}
// Get program data if any button was pressed
//----------------------------------------------------------------------
if (PgmSave == true) {
PgmSave = false;
// On Hour
if (request.indexOf("OnH=") != -1) {
String Tmp = request;
int t1 = Tmp.indexOf("OnH=");
Tmp.remove(0,t1+4);
t1 = Tmp.indexOf("&");
Tmp.remove(t1);
OnHour = Tmp.toInt();
if ( (OnHour < 0) or (OnHour > 23) ) Error1 = true;
}
// On Minute
if (request.indexOf("OnM=") != -1) {
String Tmp = request;
int t1 = Tmp.indexOf("OnM=");
Tmp.remove(0,t1+4);
t1 = Tmp.indexOf("&");
Tmp.remove(t1);
OnMinute = Tmp.toInt();
if ( (OnMinute < 0) or (OnMinute > 59) ) Error1 = true;
}
// Off Hour
if (request.indexOf("OffH=") != -1) {
String Tmp = request;
int t1 = Tmp.indexOf("OffH=");
Tmp.remove(0,t1+5);
t1 = Tmp.indexOf("&");
Tmp.remove(t1);
OffHour = Tmp.toInt();
if ( (OffHour < 0) or (OffHour > 23) ) Error2 = true;
}
// Off Minute
if (request.indexOf("OffM=") != -1) {
String Tmp = request;
int t1 = Tmp.indexOf("OffM=");
Tmp.remove(0,t1+5);
t1 = Tmp.indexOf("&");
if (t1 == -1) {
t1 = Tmp.indexOf(" ");
}
Tmp.remove(t1);
OffMinute = Tmp.toInt();
if ( (OffMinute < 0) or (OffMinute > 59) ) Error2 = true;
}
// Reset day flags
D[0] = false;
D[1] = false;
D[2] = false;
D[3] = false;
D[4] = false;
D[5] = false;
D[6] = false;
D[7] = false;
// Day 1
if (request.indexOf("D1=on") != -1) D[0] = true;
// Day 2
if (request.indexOf("D2=on") != -1) D[1] = true;
// Day 3
if (request.indexOf("D3=on") != -1) D[2] = true;
// Day 4
if (request.indexOf("D4=on") != -1) D[3] = true;
// Day 5
if (request.indexOf("D5=on") != -1) D[4] = true;
// Day 6
if (request.indexOf("D6=on") != -1) D[5] = true;
// Day 7
if (request.indexOf("D7=on") != -1) D[6] = true;
// Update program if no errors
if ( (Error1 == false) and (Error2 == false) ) {
On_Time[PgmNr] = (OnHour * 100) + OnMinute;
Off_Time[PgmNr] = (OffHour * 100) + OffMinute;
for (byte i = 0; i < 7; i++) {
if (D[i] == true) On_Days[PgmNr][i] = true; else On_Days[PgmNr][i] = false;
}
// Save program data
ManualOff = false;
ManualOn = false;
ManualTimeOn = false;
ManualTimeOff = false;
SaveProgram();
timeOld = 0;
}
else {
PgmPrev = false;
PgmNext = false;
}
}
// Change to Prev/Next Program
if (PgmPrev == true) {
PgmPrev = false;
PgmNr = PgmNr - 1;
if (PgmNr <0) PgmNr = 6;
}
if (PgmNext == true) {
PgmNext = false;
PgmNr = PgmNr + 1;
if (PgmNr > 6) PgmNr = 0;
}
// PAGE 3 - CONFIG
//----------------
// See if Save Button was pressed
//----------------------------------------------------------------------
if (request.indexOf("SaveBtn3=") != -1) {
Page = 3;
// Device Name
if (request.indexOf("Dev=") != -1) {
String Tmp = request;
int t1 = Tmp.indexOf("Dev=");
Tmp.remove(0,t1+4);
t1 = Tmp.indexOf("&");
Tmp.remove(t1);
Tmp.replace("+"," ");
DevName = Tmp;
}
// Mode select
if (request.indexOf("mode=Auto") != -1) Mode = 2;
if (request.indexOf("mode=On") != -1) Mode = 1;
if (request.indexOf("mode=Off") != -1) Mode = 0;
//get button when on
if (request.indexOf("BtnOff=0") != -1) OffMode = 0;
if (request.indexOf("BtnOff=1") != -1) OffMode = 1;
if (request.indexOf("BtnOff=2") != -1) OffMode = 2;
// get button when off
if (request.indexOf("BtnOn=0") != -1) OnMode = 0;
if (request.indexOf("BtnOn=1") != -1) OnMode = 1;
if (request.indexOf("BtnOn=2") != -1) OnMode = 2;
// get timer off hour
if (request.indexOf("ToffH=") != -1) {
String Tmp = request;
int t1 = Tmp.indexOf("ToffH=");
Tmp.remove(0,t1+6);
t1 = Tmp.indexOf("&");
Tmp.remove(t1);
TimerHourOff = Tmp.toInt();
if ( (TimerHourOff > 12) or (TimerHourOff < 0) ) {
TimerHourOff = 0;
Error3b = true;
}
}
// get timer off min
if (request.indexOf("ToffM=") != -1) {
String Tmp = request;
int t1 = Tmp.indexOf("ToffM=");
Tmp.remove(0,t1+6);
t1 = Tmp.indexOf("&");
Tmp.remove(t1);
TimerMinuteOff = Tmp.toInt();
if ( (TimerMinuteOff > 59) or (TimerMinuteOff < 0) ) {
TimerMinuteOff = 0;
Error3b = true;
}
}
// get timer on hour
if (request.indexOf("TonH=") != -1) {
String Tmp = request;
int t1 = Tmp.indexOf("TonH=");
Tmp.remove(0,t1+5);
t1 = Tmp.indexOf("&");
Tmp.remove(t1);
TimerHourOn = Tmp.toInt();
if ( (TimerHourOn > 12) or (TimerHourOn < 0) ) {
TimerHourOn = 0;
Error3 = true;
}
}
// get timer on min
if (request.indexOf("TonM=") != -1) {
String Tmp = request;
int t1 = Tmp.indexOf("TonM=");
Tmp.remove(0,t1+5);
t1 = Tmp.indexOf("&");
Tmp.remove(t1);
TimerMinuteOn = Tmp.toInt();
if ( (TimerMinuteOn > 59) or (TimerMinuteOn < 0) ) {
TimerMinuteOn = 0;
Error3 = true;
}
}
if ((Error3 == false) and (Error3b== false) ){
SaveConfig();
ManualOff = false;
ManualOn = false;
ManualTimeOn = false;
ManualTimeOff = false;
timeOld = 0;
}
}
// PAGE 4 - NTP Setup
//-------------------
// See if Save Button was pressed
//----------------------------------------------------------------------
if (request.indexOf("SaveBtn4=") != -1) {
Page = 4;
// Time Zone
if (request.indexOf("TZH=") != -1) {
String Tmp = request;
int t1 = Tmp.indexOf("TZH=");
Tmp.remove(0,t1+4);
t1 = Tmp.indexOf("&");
if (t1 == -1) {
t1 = Tmp.indexOf(" ");
}
Tmp.remove(t1);
TimeZone = Tmp.toFloat();
if ( (TimeZone < -12) or (TimeZone > 12) ) {
Error4 = true;
TimeZone = 0;
}
}
// Get NTP IP address
//--------------------------------
if (request.indexOf("IP_1=") != -1) {
String Tmp = request;
int t1 = Tmp.indexOf("IP_1=");
Tmp.remove(0,t1+5);
t1 = Tmp.indexOf("&");
Tmp.remove(t1);
IP_1 = Tmp.toInt();
if ( (IP_1 < 0) or (IP_1 > 255) ) Error5 = true;
}
if (request.indexOf("IP_2=") != -1) {
String Tmp = request;
int t1 = Tmp.indexOf("IP_2=");
Tmp.remove(0,t1+5);
t1 = Tmp.indexOf("&");
Tmp.remove(t1);
IP_2 = Tmp.toInt();
if ( (IP_2 < 0) or (IP_2 > 255) ) Error5 = true;
}
if (request.indexOf("IP_3=") != -1) {
String Tmp = request;
int t1 = Tmp.indexOf("IP_3=");
Tmp.remove(0,t1+5);
t1 = Tmp.indexOf("&");
Tmp.remove(t1);
IP_3 = Tmp.toInt();
if ( (IP_3 < 0) or (IP_3 > 255) ) Error5 = true;
}
if (request.indexOf("IP_4=") != -1) {
String Tmp = request;
int t1 = Tmp.indexOf("IP_4=");
Tmp.remove(0,t1+5);
t1 = Tmp.indexOf("&");
Tmp.remove(t1);
IP_4 = Tmp.toInt();
if ( (IP_4 < 0) or (IP_4 > 255) ) Error5 = true;
}
if ( (Error4 == false) and (Error5 == false) ) {
// Set new NTP IP
timeServer[0] = IP_1;
timeServer[1] = IP_2;
timeServer[2] = IP_3;
timeServer[3] = IP_4;
ManualOff = false;
ManualOn = false;
ManualTimeOn = false;
ManualTimeOff = false;
//Save Time Server Settings
SaveNTP();
NTPtimeOk = false;
setSyncInterval(NTPfastReq);
}
}
// See if Save Button was pressed
//----------------------------------------------------------------------
if (request.indexOf("SaveBtn5=") != -1) {
Page = 4;
//Get new hour
String Tmp = request;
int t1 = Tmp.indexOf("TimeHour=");
Tmp.remove(0,t1+9);
t1 = Tmp.indexOf("&");
Tmp.remove(t1);
NewHH = Tmp.toInt();
if ( (NewHH < 0) or (NewHH > 23) ) Error6 = true;
//Get new minute
Tmp = request;
t1 = Tmp.indexOf("TimeMinute=");
Tmp.remove(0,t1+11);
t1 = Tmp.indexOf("&");
Tmp.remove(t1);
NewMM = Tmp.toInt();
if ( (NewMM < 0) or (NewMM > 59) ) Error6 = true;
//Get new date
Tmp = request;
t1 = Tmp.indexOf("TimeDate=");
Tmp.remove(0,t1+9);
t1 = Tmp.indexOf("&");
Tmp.remove(t1);
Newdd = Tmp.toInt();
if ( (Newdd < 1) or (Newdd > 31) ) Error7 = true;
//Get new month
Tmp = request;
t1 = Tmp.indexOf("TimeMonth=");
Tmp.remove(0,t1+10);
t1 = Tmp.indexOf("&");
Tmp.remove(t1);
Newmm = Tmp.toInt();
if ( (Newmm < 1) or (Newmm > 12) ) Error7 = true;
//Get new year
Tmp = request;
t1 = Tmp.indexOf("TimeYear=");
Tmp.remove(0,t1+9);
t1 = Tmp.indexOf("&");
if (t1 == -1) {
t1 = Tmp.indexOf(" ");
}
Tmp.remove(t1);
Newyy = Tmp.toInt();
if ( (Newyy < 2000) or (Newyy > 2069) ) Error7 = true;
// Update time
//------------
setTime(NewHH, NewMM, 0, Newdd, Newmm, Newyy);
LastHH = NewHH;
LastMM = NewMM;
Lastdd = Newdd;
Lastmm = Newmm;
Lastyy = Newyy;
TimeOk = true;
ManualOff = false;
ManualOn = false;
ManualTimeOn = false;
ManualTimeOff = false;
timeOld = 0;
setSyncInterval(NTPslowReq);
}
// See if Refresh Button was pressed
//----------------------------------------------------------------------
if (request.indexOf("RefreshBtn5=") != -1) {
Page = 4;
}
// Check time before updating web page
//----------------------------------------------------------------------
DoTimeCheck();
// Web Page HTML Code
//==================================
client.println("<!doctype html>");
client.println("<html lang='en'>");
client.println("<head>");
// Refresh home page every 60 sec
//client.println("<META HTTP-EQUIV=""refresh"" CONTENT=""60"">");
client.print("<style> body {background-color: #C3FCF7;Color: #2B276E;}</style>");
client.println("<title>");
client.println(DevName);
client.println("</title>");
client.println("</head>");
client.print("<body>");
client.print("<font size = \"5\"><b>");
client.print(DevName);
client.print("</font></b><br>");
// Show time
//----------------------------------------------------------------------
client.print("<p style=\"color:#180BF4;\";>");
client.print("<font size = \"5\"><b>");
if (hour() < 10) client.print(" ");
client.print(hour());
client.print(":");
if (minute() < 10) client.print("0");
client.print(minute());
client.print("</font></p>");
// Day of the week
//----------------------------------------------------------------------
switch (weekday()) {
case 1: client.print("Sunday, ");
break;
case 2: client.print("Monday, ");
break;
case 3: client.print("Tuesday, ");
break;
case 4: client.print("Wednesday, ");
break;
case 5: client.print("Thursday, ");
break;
case 6: client.print("Friday, ");
break;
case 7: client.print("Saturday, ");
break;
default: client.print("");
break;
}
// Date
//----------------------------------------------------------------------
client.print(day());
// Month
//----------------------------------------------------------------------
switch (month()) {
case 1: client.print(" January ");
break;
case 2: client.print(" February ");
break;
case 3: client.print(" March ");
break;
case 4: client.print(" April ");
break;
case 5: client.print(" May ");
break;
case 6: client.print(" June ");
break;
case 7: client.print(" July ");
break;
case 8: client.print(" August ");
break;
case 9: client.print(" September ");
break;
case 10: client.print(" October ");
break;
case 11: client.print(" November ");
break;
case 12: client.print(" December ");
break;
default: client.print(" ");
break;
}
// Year
//----------------------------------------------------------------------
client.print(year());
client.print("<br><br>");
// Show system status
//----------------------------------------------------------------------
client.print("Output: </b>");
if ( (TimeOk == false) and (Mode == 2) ) {
client.print("Blocked - Time not set!");
}
else {
if ( (ManualOff == true) and (Mode == 2) ){
client.print("STANDBY until next event");
}
if ( (ManualOn == true) and (Mode == 2) ){
client.print("ON until next event");
}
if ( (ManualTimeOn == true) and (Mode == 2) ){
client.print("ON for ");
if ( ( (ManualSecOn + 1)/3600) > 0) {
client.print(( (ManualSecOn + 1)/3600));
client.print(" hours, ");
}
client.print( ( ( (ManualSecOn + 1)%3600)/60) );
client.print(" minutes");
}
if ( (ManualTimeOff == true) and (Mode == 2) ){
client.print("Standby for ");
if ( ( (ManualSecOff + 1)/3600) > 0) {
client.print(( (ManualSecOff + 1)/3600));
client.print(" hours, ");
}
client.print( ( ( (ManualSecOff + 1)%3600)/60) );
client.print(" minutes");
}
if ( (ManualOff == false) and (ManualOn == false) and (ManualTimeOff == false) and (ManualTimeOn == false) ) {
if (digitalRead(Relay) == 1) client.print("ON"); else client.print("OFF");
}
}
client.println("<br><br>");
//Menu
//----------------------------------------------------------------------
client.print("<form action= method=\"get\"><b>");
client.print("<a href=\"Link=1\">Home</a> ");
client.print("<a href=\"Link=2\">Programs</a> ");
client.print("<a href=\"Link=3\">Config</a> ");
client.print("<a href=\"Link=4\">Time</a><br>");
// Draw line
//----------------------------------------------------------------------
client.print("</b><hr />");
// Status PAGE
//============
if (Page == 1) {
client.print("<font size = \"4\"><b>Status</font></b><br><br>");
client.print("<b>Mode : </b>");
switch (Mode) {
case 0 : client.print("Off");
break;
case 1 : client.print("On");
break;
case 2 : client.print("Auto");
break;
}
client.print("<br><br>");
client.print("<b>Override : </b>");
if ( (ManualOff == true) or (ManualOn == true) or (ManualTimeOn == true) or (ManualTimeOff == true) ) {
client.print("Active");
}
else {
client.print("Off");
}
client.print("<br>");
//Button 1
client.println("<br>");
client.println("<input type=\"submit\" name =\"SaveBtn1\" value=\"Manual Override\">");
//Button 2
client.println(" ");
client.println("<input type=\"submit\" name =\"RefreshBtn1\" value=\"Refresh\">");
client.println("<br>");
// Draw line
client.print("<hr />");
// Show last time synch
client.print("<font size = \"2\">");
client.print("</b>Version ");
client.print(Version);
client.print("<br>");
client.print("Time was last updated on ");
client.print(Lastdd);
client.print("/");
if (Lastmm < 10) client.print("0");
client.print(Lastmm);
client.print("/");
client.print(Lastyy);
client.print(" at ");
if (LastHH < 10) client.print("0");
client.print(LastHH);
client.print(":");
if (LastMM < 10) client.print("0");
client.print(LastMM);
client.print("</font>");
}
//Program PAGE
//============
if (Page == 2) {
// Program number
client.print("<font size = \"4\"><b>");
client.print("Program ");
client.print(PgmNr + 1);
client.print(" of 7");
//Previous Button
client.println(" <input type=\"submit\" name =\"SaveBtnPrev\" value=\" << \">");
client.println(" ");
//Next Button
client.println("<input type=\"submit\" name =\"SaveBtnNext\" value=\" >> \"></font></b><br><br>");
//On time
client.print("On Time: <input type=\"text\"<input maxlength=\"2\" size=\"2\" name=\"OnH\"value =\"");
// if ( (On_Time[PgmNr] / 100) < 10) client.print("0");
client.print(On_Time[PgmNr]/100);
client.print("\"> : <input type=\"text\"<input maxlength=\"2\" size=\"2\" name=\"OnM\"value =\"");
if ( (On_Time[PgmNr] % 100) < 10) client.print("0");
client.print(On_Time[PgmNr]%100);
client.print("\">");
if (Error1 == true) client.print(ErrMsg);
client.print("<br><br>");
//Off time
client.print("Off Time: <input type=\"text\"<input maxlength=\"2\" size=\"2\" name=\"OffH\"value =\"");
// if ( (Off_Time[PgmNr] / 100) < 10) client.print("0");
client.print(Off_Time[PgmNr]/100);
client.print("\"> : <input type=\"text\"<input maxlength=\"2\" size=\"2\" name=\"OffM\"value =\"");
if ( (Off_Time[PgmNr] % 100) < 10) client.print("0");
client.print(Off_Time[PgmNr]%100);
client.print("\">");
if (Error2 == true) client.print(ErrMsg);
client.print("<br><br>");
//Day 1
client.print("<input type=\"Checkbox\" name=\"D1\"");
if (On_Days[PgmNr][0]==true) client.print("checked"); else client.print("unchecked");
client.print("> Sun<br>");
//Day 2
client.print("<input type=\"Checkbox\" name=\"D2\"");
if (On_Days[PgmNr][1]==true) client.print("checked"); else client.print("unchecked");
client.print("> Mon<br>");
//Day 3
client.print("<input type=\"Checkbox\" name=\"D3\"");
if (On_Days[PgmNr][2]==true) client.print("checked"); else client.print("unchecked");
client.print("> Tue<br>");
//Day 4
client.print("<input type=\"Checkbox\" name=\"D4\"");
if (On_Days[PgmNr][3]==true) client.print("checked"); else client.print("unchecked");
client.print("> Wed<br>");
//Day 5
client.print("<input type=\"Checkbox\" name=\"D5\"");
if (On_Days[PgmNr][4]==true) client.print("checked"); else client.print("unchecked");
client.print("> Thu<br>");
//Day 6
client.print("<input type=\"Checkbox\" name=\"D6\"");
if (On_Days[PgmNr][5]==true) client.print("checked"); else client.print("unchecked");
client.print("> Fri<br>");
//Day 7
client.print("<input type=\"Checkbox\" name=\"D7\"");
if (On_Days[PgmNr][6]==true) client.print("checked"); else client.print("unchecked");
client.print("> Sat<br>");
//Button
client.println("<br>");
client.println("<input type=\"submit\" name =\"SaveBtn2\" value=\"Save\">");
client.print(" ");
client.println("<input type=\"submit\" name =\"ClearBtn1\" value=\"Clear\">");
}
// Config PAGE
//============
if (Page == 3) {
client.print("<font size = \"4\"><b>Configuration</font></b><br><br>");
// Device Name
client.print("Device Name: ");
client.print("<input type=\"text\"<input maxlength=\"30\" size=\"35\" name=\"Dev\" value =\"");
client.print(DevName);
client.println("\"><br><br>");
//Mode Select
client.println("<b>Mode: </b>");
client.print("<input type=\"radio\" name=\"mode\" value=\"Auto\"");
if (Mode == 2) client.print("checked");
client.print("/> Auto ");
client.print("<input type=\"radio\" name=\"mode\" value=\"On\"");
if (Mode == 1) client.print("checked");
client.print("/> On ");
client.print("<input type=\"radio\" name=\"mode\" value=\"Off\"");
if (Mode == 0) client.print("checked");
client.print("/> Off <br><br>");
client.print("<font size = \"4\"><b>Button Function</font></b><br>");
// Device Name
// When output ON
client.print("When ");
client.print(DevName);
client.print(" is on:<br>");
// do nothing
client.print("<input type=\"radio\" name=\"BtnOff\" value=\"0\"");
if (OffMode == 0) client.print("checked");
client.print("/> Do nothing<br>");
// turn off
client.print("<input type=\"radio\" name=\"BtnOff\" value=\"1\"");
if (OffMode == 1) client.print("checked");
client.print("/> Turn OFF until next event<br>");
// turn on for specific time
client.print("<input type=\"radio\" name=\"BtnOff\" value=\"2\"");
if (OffMode == 2) client.print("checked");
client.print("/> Turn OFF for specific time<br>");
// time input
client.print("  <input type=\"text\"<input maxlength=\"2\" size=\"3\" name=\"ToffH\"value =\"");
client.print(TimerHourOff);
client.print("\">");
client.print(" : <input type=\"text\"<input maxlength=\"2\" size=\"3\" name=\"ToffM\"value =\"");
if (TimerMinuteOff < 10) client.print("0");
client.print(TimerMinuteOff);
client.print("\">");
if (Error3b == true) {
client.print(ErrMsg);
}
else {