-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathaqua_NODEMCU.ino
1176 lines (1063 loc) · 50.4 KB
/
aqua_NODEMCU.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
/*
The MIT License (MIT)
Copyright (c) by respective library files owners
Copyright (c) 2021 by Aniket Patra
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Initial Beta Version
V1.0 OTA update, oled connection, wifi icons(connection/disconnection and WiFi signal strength display) 15/11/21
v1.2 Added DS3231 for back time keeping, added a way to power up when there is no wifi signal 16/11/21
v1.3 Added relays to the circuit (4 channel active-low) 17/11/21
v1.4 Added relay initialization function (helpful after powerloss), WEB SERVER (for controlling relays, getting temperature and time), further optimizations 25/11/21
v1.5 Added control to turn off/on oled display as you want (via Web Server) 09/12/21
v1.6 Added control to turn off/on Aqaurium Light as you want (via Web Server). Previously the timer would overwrite the settings. {Needs optimization for restarts} 14/12/21
v1.7 Optimizations for timers {Still needs optimizations} 16/12/21
v1.71 Bug updates 21/12/21
v1.8 Added Auto/Manual Control for all relays. Updates to HTML of Web Server.
v1.9 Added option to update time via web-server and other updates
v1.10 Added option to auto start relay (when off) and added visual message over screen when time is updating
v1.11 Updated the UI of AutoTimers to give options for 5 min, 10min, 15 min, 25 min, 30 min, 45 min timers and other mild updates
v1.12 Added Power Saver Mode (Powerhead and Skimmer would automatically ON for 10 min then OFF for 10 min and so on) and like other features, these can be customised.
v1.121 Added security feature to automatically update time if rtc resets to 1970
*/
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <NTPClient.h>
#include <DS3231.h>
#include <string.h>
#include "global.h" //remove this
DS3231 Clock;
bool century = false;
bool h12Flag = false;
bool pmFlag;
#ifndef STASSID
#define STASSID "YOUR_SSID" // WIFI NAME/SSID
#define STAPSK "YOUR_PASSWORD" // WIFI PASSWORD
#endif
const char *ssid = pssid; // remove "pssid" and write "STASSID"
const char *password = ppass; // remove "ppass" and write "STAPSK"
// Set web server port number to 80
WiFiServer server(80);
// Variable to store the HTTP request
String header;
// Auxiliar variables to store the current output state
String Relay1State = "off";
String Relay2State = "off";
String Relay3State = "off";
String Relay4State = "off";
// Define timeout time in milliseconds (example: 2000ms = 2s)
unsigned long currentTime = millis();
unsigned long previousTime = 0;
const long timeoutTime = 2000; // client timeout for the webserver
unsigned long lastTime = 0;
const long timerDelay = 30000; // check relay status every 30 seconds
unsigned long lastTime1 = 0;
const long timerDelay1 = 5000; // check wifi status every 900 m.seconds
// timer settings for autotimers
unsigned long lastAutoTimer1 = 0, lastAutoTimer2 = 0, lastAutoTimer3 = 0, lastAutoTimer4 = 0;
long autoTimerDelay1 = 0, autoTimerDelay2 = 0, autoTimerDelay3 = 0, autoTimerDelay4 = 0; // example if it would have to check after 5 mins i.e. 5*60*1000=300000
// utc offset according to India, please change according to your location
const long utcOffsetInSeconds = 19800;
char week[7][20] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
// Define NTP Client to get time
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "asia.pool.ntp.org", utcOffsetInSeconds);
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET LED_BUILTIN // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C // 0x3c for 0x78
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// different WiFi icons Full, Medium, Half and Low Signal. ! for no connection
static const unsigned char PROGMEM wifiFull[] = {
0x00, 0x00, 0x00, 0x00, 0x07, 0xE0, 0x3F, 0xFC, 0x70, 0x0E, 0xE0, 0x07, 0x47, 0xE2, 0x1E, 0x78,
0x18, 0x18, 0x03, 0xC0, 0x07, 0xE0, 0x00, 0x00, 0x01, 0x80, 0x01, 0x80, 0x00, 0x00, 0x00, 0x00
};
static const unsigned char PROGMEM wifiMed[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xE0, 0x1E, 0x78,
0x18, 0x18, 0x03, 0xC0, 0x07, 0xE0, 0x00, 0x00, 0x01, 0x80, 0x01, 0x80, 0x00, 0x00, 0x00, 0x00
};
static const unsigned char PROGMEM wifiHalf[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x03, 0xC0, 0x07, 0xE0, 0x00, 0x00, 0x01, 0x80, 0x01, 0x80, 0x00, 0x00, 0x00, 0x00
};
static const unsigned char PROGMEM wifiLow[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x80, 0x01, 0x80, 0x00, 0x00, 0x00, 0x00
};
const unsigned char picture[] PROGMEM = { // picture of a window (during boot)
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xff, 0xfe, 0x7f, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xff, 0xfe, 0x7f, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xff, 0xfe, 0x7f, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xff, 0xfe, 0x7f, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xff, 0xfe, 0x7f, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xff, 0xfe, 0x7f, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xff, 0xfe, 0x7f, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xff, 0xfe, 0x7f, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xff, 0xfe, 0x7f, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xff, 0xfe, 0x7f, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xff, 0xfe, 0x7f, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xff, 0xfe, 0x7f, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xff, 0xfe, 0x7f, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xff, 0xfe, 0x7f, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xff, 0xfe, 0x7f, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xff, 0xfe, 0x7f, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xff, 0xfe, 0x7f, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xff, 0xfe, 0x7f, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xff, 0xfe, 0x7f, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xff, 0xfe, 0x7f, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xff, 0xfe, 0x7f, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xff, 0xfe, 0x7f, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xff, 0xfe, 0x7f, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xff, 0xfe, 0x7f, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xff, 0xfe, 0x7f, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xff, 0xfe, 0x7f, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xff, 0xfe, 0x7f, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xff, 0xfe, 0x7f, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xff, 0xfe, 0x7f, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xff, 0xfe, 0x7f, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xff, 0xfe, 0x7f, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xff, 0xfe, 0x7f, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xff, 0xfe, 0x7f, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xff, 0xfe, 0x7f, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xff, 0xfe, 0x7f, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xff, 0xfe, 0x7f, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xff, 0xfe, 0x7f, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xff, 0xfe, 0x7f, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
// forward declarations
void checkTimeFor(int, int, int);
void showWifiSignal();
void showTime();
void updateRTC();
void relayStatusPrinter();
void clientPage(WiFiClient, String);
// relay gpio pins
const byte relayPin1 = 0;
const byte relayPin2 = 12;
const byte relayPin3 = 13;
const byte relayPin4 = 14;
// status flags
byte R1Flag = 0, R2Flag = 0, R3Flag = 0, R4Flag = 0, R1PowerSave = 0, R1PsAlt = 2, R2PowerSave = 0, R2PsAlt = 2; // R1PsAlt=2 means garbage state
byte OLEDConfig = 0, R1Config = 2, R2Config = 2, R3Config = 0, R4Config = 1; // Config=1 means auto (based on timer),Config=0 means Manual and Config=2 means PowSave. Same for aquarium light
byte OLEDStatus = 1, updateTime = 0; // Status = 1 means OLED DISPLAY is ON and reversed for 0 (works only if Config is 0)
byte autoTimer1 = 0, autoTimer2 = 0, autoTimer3 = 0, autoTimer4 = 0;
unsigned long R1PslastTime1 = 0, R2PslastTime1 = 0;
long R1PstimerDelay1 = 0, R2PstimerDelay1 = 0;
// Relay activation
#define RELAY1 false // mark true to activate, and false to deactivate (activation means the timer activation)
#define RELAY2 false
#define RELAY3 false
#define RELAY4 true
// Relay Timing and definitions
#define RELAY1TIME 1200, 1900, 1 // first number is "ON" time in 24 hours. i.e. 2:35pm would be 1435, second one is turn "OFF" time, and the last one is Relay Number
#define RELAY1ON digitalWrite(relayPin1, HIGH)
#define RELAY1OFF digitalWrite(relayPin1, LOW)
#define RELAY2TIME 1200, 1900, 2
#define RELAY2ON digitalWrite(relayPin2, HIGH)
#define RELAY2OFF digitalWrite(relayPin2, LOW)
#define RELAY3TIME 1200, 1900, 3
#define RELAY3ON digitalWrite(relayPin3, HIGH)
#define RELAY3OFF digitalWrite(relayPin3, LOW)
#define RELAY4TIME 800, 1900, 4
#define RELAY4ON digitalWrite(relayPin4, HIGH)
#define RELAY4OFF digitalWrite(relayPin4, LOW)
// oled display off at night timings (needs improvement)
#define OLED_OFF 9 // Hour only. In 24 hours mode i.e. 23 for 11 pm
#define OLED_ON 7
const String newHostname = "AquariumNode";
void relayInitialize() // checks and initializes all relay in case of powerloss (takes effect only if relay is activated above). Default: Turns ON the relay
{
if (RELAY1) {
checkTimeFor(RELAY1TIME);
} else {
RELAY1ON;
R1Flag = 1;
Relay1State = "on";
}
if (RELAY2) {
checkTimeFor(RELAY2TIME);
} else {
RELAY2ON;
R2Flag = 1;
Relay2State = "on";
}
if (RELAY3) {
checkTimeFor(RELAY3TIME);
} else {
RELAY3ON;
R3Flag = 1;
Relay3State = "on";
}
if (RELAY4) {
checkTimeFor(RELAY4TIME);
} else {
RELAY4ON;
R4Flag = 1;
Relay4State = "on";
}
}
void setup() {
pinMode(relayPin1, OUTPUT);
pinMode(relayPin2, OUTPUT);
pinMode(relayPin3, OUTPUT);
pinMode(relayPin4, OUTPUT);
Wire.begin();
Serial.begin(57600);
relayInitialize();
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
delay(3000);
ESP.restart();
}
display.clearDisplay();
display.drawBitmap(0, 0, picture, 128, 64, WHITE);
display.display();
delay(1000);
display.clearDisplay();
display.setCursor(30, 25); // Start at top-left corner
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(WHITE); // Draw white text
display.println(F("Connecting..."));
display.display();
// Wifi connection
WiFi.mode(WIFI_STA);
WiFi.hostname(newHostname.c_str()); // Set new hostname
WiFi.begin(ssid, password);
int count = 0;
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
display.clearDisplay();
display.setTextSize(2);
display.setCursor(0, 0); // Start at top-left corner
display.println(F("Connection\n\nFailed!"));
display.display();
delay(5000);
// ESP.restart();
count = 1;
break;
}
// OTA UPDATE SETTINGS !! CAUTION !! PROCEED WITH PRECAUTION
ArduinoOTA.onStart([]() {
String type;
if (ArduinoOTA.getCommand() == U_FLASH) {
type = "sketch";
} else { // U_FS
type = "filesystem";
}
// NOTE: if updating FS this would be the place to unmount FS using FS.end()
display.clearDisplay();
display.setTextSize(2);
display.setCursor(0, 0); // Start at top-left corner
display.println(F("OTA Update\n"));
display.setTextSize(1);
display.print("Type: ");
display.print(type);
display.display();
delay(2000);
});
ArduinoOTA.onEnd([]() {
display.clearDisplay();
display.setTextSize(2);
display.setCursor(15, 25); // Start at top-left corner
display.println(F("Rebooting"));
display.display();
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
display.clearDisplay();
display.setTextSize(2);
display.setCursor(0, 10); // Start at top-left corner
display.print("Progress: ");
display.setCursor(50, 35);
display.print((progress / (total / 100)));
display.setCursor(88, 35);
display.print("%");
display.display();
});
ArduinoOTA.onError([](ota_error_t error) {
display.clearDisplay();
display.setTextSize(2);
display.setCursor(0, 10); // Start at top-left corner
display.print("Error!");
display.display();
//Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) {
display.clearDisplay();
display.setTextSize(2);
display.setCursor(0, 10); // Start at top-left corner
display.print("Auth Failed");
display.display();
} else if (error == OTA_BEGIN_ERROR) {
display.clearDisplay();
display.setTextSize(2);
display.setCursor(0, 10); // Start at top-left corner
display.print("Begin Failed");
display.display();
} else if (error == OTA_CONNECT_ERROR) {
display.clearDisplay();
display.setTextSize(2);
display.setCursor(0, 10); // Start at top-left corner
display.print("Connect Failed");
display.display();
} else if (error == OTA_RECEIVE_ERROR) {
display.clearDisplay();
display.setTextSize(2);
display.setCursor(0, 10); // Start at top-left corner
display.print("Receive Failed");
display.display();
} else if (error == OTA_END_ERROR) {
display.clearDisplay();
display.setTextSize(2);
display.setCursor(0, 10); // Start at top-left corner
display.print("End Failed");
display.display();
}
});
ArduinoOTA.begin();
// OTA UPDATE END
// checks if wifi connected
if (count == 0) {
display.clearDisplay();
display.setTextSize(2);
display.setCursor(0, 0); // Start at top-left corner
display.println(F("Connected!"));
display.setTextSize(1);
display.println("");
display.println(WiFi.SSID());
display.print("\nIP address:\n");
display.println(WiFi.localIP());
display.display();
delay(4000);
display.clearDisplay();
display.setCursor(0, 0);
timeClient.begin(); // NTP time
timeClient.update();
display.println(F("Started Time Client"));
display.display();
delay(2000);
display.clearDisplay();
updateRTC();
display.clearDisplay();
server.begin();
display.println(F("Server Started!"));
display.display();
delay(2000);
display.clearDisplay();
}
}
void loop() {
ArduinoOTA.handle();
WiFiClient client = server.available(); // Listen for incoming clients
//****************************************RELAY POWERSAVE FEATURE********************************
// RELAY 1 10 min ON and 20 min OFF
if (R1PowerSave == 1) {
R1PstimerDelay1 = 600000; // 10 mins
R1PslastTime1 = millis();
RELAY1ON;
R1PowerSave = 0;
R1PsAlt = 1;
R1Flag = 1;
Relay1State = "on";
R1Config = 2;
}
if (R1PsAlt == 1 && R1Config == 2) {
if ((millis() - R1PslastTime1) > R1PstimerDelay1) {
RELAY1OFF;
R1PsAlt = 0;
R1PstimerDelay1 = 600000;
R1PslastTime1 = millis();
R1Flag = 0;
Relay1State = "off";
}
} else if (R1PsAlt == 0 && R1Config == 2) {
if ((millis() - R1PslastTime1) > R1PstimerDelay1) {
RELAY1ON;
R1PsAlt = 1;
R1PstimerDelay1 = 600000;
R1PslastTime1 = millis();
R1Flag = 1;
Relay1State = "on";
}
}
// RELAY 2 10 min ON and 10 min OFF
if (R2PowerSave == 1) {
R2PstimerDelay1 = 600000; // 10 mins
R2PslastTime1 = millis();
RELAY2ON;
R2PowerSave = 0;
R2PsAlt = 1;
R2Flag = 1;
Relay2State = "on";
R2Config = 2;
}
if (R2PsAlt == 1 && R2Config == 2) {
if ((millis() - R2PslastTime1) > R2PstimerDelay1) {
RELAY2OFF;
R2PsAlt = 0;
R2PstimerDelay1 = 600000;
R2PslastTime1 = millis();
R2Flag = 0;
Relay2State = "off";
}
} else if (R2PsAlt == 0 && R2Config == 2) {
if ((millis() - R2PslastTime1) > R2PstimerDelay1) {
RELAY2ON;
R2PsAlt = 1;
R2PstimerDelay1 = 600000;
R2PslastTime1 = millis();
R2Flag = 1;
Relay2State = "on";
}
}
//**************************************END RELAY POWER SAVE FEATURE****************************************
if (updateTime == 1) {
updateRTC();
updateTime = 0;
}
if (client) { // If a new client connects
String currentLine = ""; // make a String to hold incoming data from the client
currentTime = millis();
previousTime = currentTime;
clientPage(client, currentLine);
// Clear the header variable
header = "";
// Close the connection
client.stop();
}
display.clearDisplay();
// checking timers for relays
if ((millis() - lastTime) > timerDelay) {
if (R1Config == 0) {
} else if (R1Config == 1) {
if (RELAY1) {
checkTimeFor(RELAY1TIME);
}
}
if (R2Config == 0) {
} else if (R2Config == 1) {
if (RELAY2) {
checkTimeFor(RELAY2TIME);
}
}
if (R3Config == 0) {
} else if (R3Config == 1) {
if (RELAY3) {
checkTimeFor(RELAY3TIME);
}
}
if (R4Config == 0) {
} else if (R4Config == 1) {
if (RELAY4) {
checkTimeFor(RELAY4TIME);
}
}
lastTime = millis();
}
// autoTimer checkings
if (autoTimer1 == 1) {
if ((millis() - lastAutoTimer1) > autoTimerDelay1) {
R1Flag = 1;
RELAY1ON;
Relay1State = "on";
autoTimer1 = 0;
}
}
if (autoTimer2 == 1) {
if ((millis() - lastAutoTimer2) > autoTimerDelay2) {
R2Flag = 1;
RELAY2ON;
Relay2State = "on";
autoTimer2 = 0;
}
}
if (autoTimer3 == 1) {
if ((millis() - lastAutoTimer3) > autoTimerDelay3) {
R3Flag = 1;
RELAY3ON;
Relay3State = "on";
autoTimer3 = 0;
}
}
if (autoTimer4 == 1) {
if ((millis() - lastAutoTimer4) > autoTimerDelay4) {
R4Flag = 1;
RELAY4ON;
Relay4State = "on";
autoTimer4 = 0;
}
}
// checking wifi strength
if ((millis() - lastTime1) > timerDelay1) {
showTime();
showWifiSignal(); // at (112,0), wifi sampling once in 900 m.seconds
relayStatusPrinter();
if (OLEDConfig == 0) // Automatic/Manual Control of OLED Display
{
if (OLEDStatus == 0) {
display.clearDisplay();
display.display();
} else
display.display();
} else if (OLEDConfig == 1) {
byte currTime = Clock.getHour(h12Flag, pmFlag); // stores current hour temporarily
if (OLED_OFF > OLED_ON) {
if ((currTime >= OLED_ON) && (currTime <= OLED_OFF))
display.display();
else {
display.clearDisplay();
display.display();
}
} else {
if (((currTime >= OLED_ON) && (currTime >= OLED_OFF)) || ((currTime <= OLED_ON) && (currTime <= OLED_OFF))) // oled display turn of at night
display.display();
else {
display.clearDisplay();
display.display();
}
}
}
lastTime1 = millis();
}
}
void showWifiSignal() {
int x = WiFi.RSSI();
if (WiFi.status() != WL_CONNECTED) {
display.setCursor(112, 0);
// display.setTextSize(2);
display.println(F("!"));
} else {
if (x <= (-80)) {
display.drawBitmap(112, 0, wifiLow, 16, 16, WHITE); // worst signal
} else if ((x <= (-70)) && (x > (-80))) {
display.drawBitmap(112, 0, wifiHalf, 16, 16, WHITE); // poor signal
} else if ((x <= (-60)) && (x > (-70))) // good signal
{
display.drawBitmap(112, 0, wifiMed, 16, 16, WHITE); // best signal
} else if (x > (-60)) {
display.drawBitmap(112, 0, wifiFull, 16, 16, WHITE); // excellent signal
}
}
}
void showTime() {
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0, 0);
if (Clock.getHour(h12Flag, pmFlag) < 10)
display.print(0, DEC);
display.print(Clock.getHour(h12Flag, pmFlag)); // Time
display.print(":");
if (Clock.getMinute() < 10)
display.print(0, DEC);
display.print(Clock.getMinute());
// display.print(":");
// if (Clock.getSecond() < 10)
// display.print(0, DEC);
// display.println(Clock.getSecond());
display.println(" ");
display.setTextSize(1);
display.print(week[Clock.getDoW()]);
display.print(", ");
display.print(Clock.getDate());
display.print("-");
display.print(Clock.getMonth(century));
display.print("-20");
display.println(Clock.getYear());
display.setCursor(90, 25);
display.print(Clock.getTemperature(), 1);
display.println(" C");
if (Clock.getYear() == 70)
updateRTC();
}
void updateRTC() {
display.clearDisplay();
display.setCursor(30, 25); // Start at top-left corner
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(WHITE); // Draw white text
display.println(F("Updating Time"));
display.display();
delay(2000);
timeClient.update();
time_t rawtime = timeClient.getEpochTime();
struct tm *ti;
ti = localtime(&rawtime);
uint16_t year = ti->tm_year + 1900;
uint8_t x = year % 10;
year = year / 10;
uint8_t y = year % 10;
year = y * 10 + x;
uint8_t month = ti->tm_mon + 1;
uint8_t day = ti->tm_mday;
uint8_t hours = ti->tm_hour;
uint8_t minutes = ti->tm_min;
uint8_t seconds = ti->tm_sec;
uint8_t dow = ti->tm_wday;
Clock.setClockMode(false); // set to 24h
// setClockMode(true); // set to 12h
Clock.setYear(year);
Clock.setMonth(month);
Clock.setDate(day);
Clock.setDoW(dow);
Clock.setHour(hours);
Clock.setMinute(minutes);
Clock.setSecond(seconds);
}
void checkTimeFor(int onTime, int offTime, int number) {
int h = Clock.getHour(h12Flag, pmFlag);
int m = Clock.getMinute();
int timeString = h * 100 + m; // if h=12 and m=23 then 12*100 + 23 = 1223 hours
if (offTime > onTime) // when off timing is greater than on timing
{
if ((timeString > onTime) && (timeString < offTime)) {
if (number == 1) {
R1Flag = 1;
RELAY1ON;
Relay1State = "on";
autoTimer1 = 0;
} else if (number == 2) {
R2Flag = 1;
RELAY2ON;
Relay2State = "on";
autoTimer2 = 0;
} else if (number == 3) {
R3Flag = 1;
RELAY3ON;
Relay3State = "on";
autoTimer3 = 0;
} else if (number == 4) {
R4Flag = 1;
RELAY4ON;
Relay4State = "on";
autoTimer4 = 0;
}
} else {
if (number == 1) {
R1Flag = 0;
RELAY1OFF;
Relay1State = "off";
} else if (number == 2) {
R2Flag = 0;
RELAY2OFF;
Relay2State = "off";
} else if (number == 3) {
R3Flag = 0;
RELAY3OFF;
Relay3State = "off";
} else if (number == 4) {
R4Flag = 0;
RELAY4OFF;
Relay4State = "off";
}
}
} else {
if (((timeString > onTime) && (timeString > offTime)) || ((timeString < onTime) && (timeString < offTime))) {
if (number == 1) {
R1Flag = 1;
RELAY1ON;
Relay1State = "on";
} else if (number == 2) {
R2Flag = 1;
RELAY2ON;
Relay2State = "on";
} else if (number == 3) {
R3Flag = 1;
RELAY3ON;
Relay3State = "on";
} else if (number == 4) {
R4Flag = 1;
RELAY4ON;
Relay4State = "on";
}
} else {
if (number == 1) {
R1Flag = 0;
RELAY1OFF;
Relay1State = "off";
} else if (number == 2) {
R2Flag = 0;
RELAY2OFF;
Relay2State = "off";
} else if (number == 3) {
R3Flag = 0;
RELAY3OFF;
Relay3State = "off";
} else if (number == 4) {
R4Flag = 0;
RELAY4OFF;
Relay4State = "off";
}
}
}
}
void relayStatusPrinter() {
display.setTextSize(1);
display.setTextColor(WHITE);
if (R1Flag) {
display.setCursor(0, 57);
display.print("R1");
}
if (R2Flag) {
display.setCursor(20, 57);
display.print("R2");
}
if (R3Flag) {
display.setCursor(40, 57);
display.print("R3");
}
if (R4Flag) {
display.setCursor(60, 57);
display.print("R4");
}
}
void clientPage(WiFiClient client, String currentLine) {
while (client.connected() && currentTime - previousTime <= timeoutTime) { // loop while the client's connected
currentTime = millis();
if (client.available()) {
// if there's bytes to read from the client,
char c = client.read(); // read a byte, then
Serial.write(c); // print it out the serial monitor
header += c;
if (c == '\n') { // if the byte is a newline character
// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println("Connection: close");
client.println();
// turns the GPIOs on and off
if (header.indexOf("GET /0/on") >= 0) {
R1Config = 1;
autoTimer1 = 0;
} else if (header.indexOf("GET /0/off") >= 0)
R1Config = 0;
else if (header.indexOf("GET /1/on") >= 0) {
Relay1State = "on";
R1Flag = 1;
RELAY1ON;
autoTimer1 = 0;
} else if (header.indexOf("GET /1/off") >= 0) {
Relay1State = "off";
R1Flag = 0;
RELAY1OFF;
}
if (header.indexOf("GET /2/on") >= 0) {
R2Config = 1;
autoTimer2 = 0;
} else if (header.indexOf("GET /2/off") >= 0)
R2Config = 0;
else if (header.indexOf("GET /3/on") >= 0) {
Relay2State = "on";
R2Flag = 1;
RELAY2ON;
autoTimer2 = 0;
} else if (header.indexOf("GET /3/off") >= 0) {
Relay2State = "off";
R2Flag = 0;
RELAY2OFF;
}
if (header.indexOf("GET /4/on") >= 0) {
R3Config = 1;
autoTimer3 = 0;
} else if (header.indexOf("GET /4/off") >= 0)
R3Config = 0;
else if (header.indexOf("GET /5/on") >= 0) {
Relay3State = "on";
R3Flag = 1;
RELAY3ON;
autoTimer3 = 0;
} else if (header.indexOf("GET /5/off") >= 0) {
Relay3State = "off";
R3Flag = 0;
RELAY3OFF;
}
if (header.indexOf("GET /6/on") >= 0) {
R4Config = 1;
autoTimer4 = 0;
} else if (header.indexOf("GET /6/off") >= 0)
R4Config = 0;
else if (header.indexOf("GET /7/on") >= 0) {
// Serial.println("GPIO 4 off");
Relay4State = "on";
R4Flag = 1;
RELAY4ON;
autoTimer4 = 0;
} else if (header.indexOf("GET /7/off") >= 0) {
// Serial.println("GPIO 4 off");
Relay4State = "off";
R4Flag = 0;
RELAY4OFF;
} else if (header.indexOf("GET /8/on") >= 0)
OLEDConfig = 1;
else if (header.indexOf("GET /8/off") >= 0)
OLEDConfig = 0;
else if (header.indexOf("GET /9/off") >= 0)
OLEDStatus = 1;
else if (header.indexOf("GET /9/on") >= 0)
OLEDStatus = 0;
else if (header.indexOf("GET /10/update") >= 0)
updateTime = 1;
else if (header.indexOf("GET /11/5") >= 0) {
autoTimer1 = 1;
lastAutoTimer1 = millis();
autoTimerDelay1 = 300000;
} else if (header.indexOf("GET /11/10") >= 0) {
autoTimer1 = 1;
lastAutoTimer1 = millis();
autoTimerDelay1 = 600000;
} else if (header.indexOf("GET /11/15") >= 0) {
autoTimer1 = 1;
lastAutoTimer1 = millis();
autoTimerDelay1 = 900000;
} else if (header.indexOf("GET /11/25") >= 0) {
autoTimer1 = 1;
lastAutoTimer1 = millis();
autoTimerDelay1 = 1500000;
} else if (header.indexOf("GET /11/30") >= 0) {
autoTimer1 = 1;
lastAutoTimer1 = millis();
autoTimerDelay1 = 1800000;
} else if (header.indexOf("GET /11/45") >= 0) {
autoTimer1 = 1;
lastAutoTimer1 = millis();
autoTimerDelay1 = 2700000;
} else if (header.indexOf("GET /12/5") >= 0) {
autoTimer2 = 1;
lastAutoTimer2 = millis();
autoTimerDelay2 = 300000;
} else if (header.indexOf("GET /12/10") >= 0) {
autoTimer2 = 1;
lastAutoTimer2 = millis();
autoTimerDelay2 = 600000;
} else if (header.indexOf("GET /12/15") >= 0) {
autoTimer2 = 1;
lastAutoTimer2 = millis();
autoTimerDelay2 = 900000;
} else if (header.indexOf("GET /12/25") >= 0) {
autoTimer2 = 1;
lastAutoTimer2 = millis();
autoTimerDelay2 = 1500000;
} else if (header.indexOf("GET /12/30") >= 0) {
autoTimer2 = 1;
lastAutoTimer2 = millis();
autoTimerDelay2 = 1800000;
} else if (header.indexOf("GET /12/45") >= 0) {
autoTimer2 = 1;
lastAutoTimer2 = millis();
autoTimerDelay2 = 2700000;
} else if (header.indexOf("GET /13/5") >= 0) {
autoTimer3 = 1;
lastAutoTimer3 = millis();
autoTimerDelay3 = 300000;
} else if (header.indexOf("GET /13/10") >= 0) {
autoTimer3 = 1;
lastAutoTimer3 = millis();