-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathESP32_4_Channel_Relay_Ethernet-v1.1.ino
710 lines (693 loc) · 35.8 KB
/
ESP32_4_Channel_Relay_Ethernet-v1.1.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
/*
This software, the ideas and concepts is Copyright (c) David Bird 2024
All rights to this software are reserved.
It is prohibited to redistribute or reproduce of any part or all of the software contents in any form other than the following:
1. You may print or download to a local hard disk extracts for your personal and non-commercial use only.
2. You may copy the content to individual third parties for their personal use, but only if you acknowledge the author David Bird as the source of the material.
3. You may not, except with my express written permission, distribute or commercially exploit the content.
4. You may not transmit it or store it in any other website or other form of electronic retrieval system for commercial purposes.
5. You MUST include all of this copyright and permission notice ('as annotated') and this shall be included in all copies or substantial portions of the software
and where the software use is visible to an end-user.
THE SOFTWARE IS PROVIDED "AS IS" FOR PRIVATE USE ONLY, IT IS NOT FOR COMMERCIAL USE IN WHOLE OR PART OR CONCEPT.
FOR PERSONAL USE IT IS SUPPLIED 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 AUTHOR OR COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OR
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
See more at http://dsbird.org.uk
*/
//################# LIBRARIES ################
#include <ETH.h>
#include <WiFi.h>
#include <ESPmDNS.h> // Built-in
#include <SPIFFS.h> // Built-in
#include "ESPAsyncWebServer.h" // Built-in
#include "AsyncTCP.h" // https://github.com/me-no-dev/AsyncTCP
//################ VERSION ###########################################
String version = "1.1"; // Programme version, see change log at end
//################ VARIABLES ###########################################
const char* ServerName = "Controller"; // Connect to the server with http://controller.local/ e.g. if name = "myserver" use http://myserver.local/
#define Channels 4 // n-Channels
#define NumOfEvents 4 // Number of events per-day, 4 is a practical limit
#define noRefresh false // Set auto refresh OFF
#define Refresh true // Set auto refresh ON
#define ON true // Set the Relay ON
#define OFF false // Set the Relay OFF
#define Channel1 0 // Define Channel-1
#define Channel2 1 // Define Channel-2
#define Channel3 2 // Define Channel-3
#define Channel4 3 // Define Channel-4
// Now define the GPIO pins to be used for relay control
#define Channel1_Pin 2 // Define the Relay Control pin
#define Channel2_Pin 4 // Define the Relay Control pin
#define Channel3_Pin 5 // Define the Relay Control pin
#define Channel4_Pin 17 // Define the Relay Control pin
#define ETH_ADDR 1
#define ETH_POWER_PIN 16 //-1 //16 // Do not use it, it can cause conflict during the software reset.
#define ETH_POWER_PIN_ALTERNATIVE 16 //17
#define ETH_MDC_PIN 23
#define ETH_MDIO_PIN 18
#define ETH_TYPE ETH_PHY_LAN8720
#define ETH_CLK_MODE ETH_CLOCK_GPIO17_OUT // ETH_CLOCK_GPIO0_IN
IPAddress local_ip(192, 168, 1, 100);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress dns1(8, 8, 8, 8);
IPAddress dns2 = (uint32_t)0x00000000;
#define LEDPIN 5 // Define the LED Control pin
#define ChannelReverse false // Set to true for Relay that requires a signal HIGH for ON, usually relays need a LOW to actuate
struct settings {
String DoW; // Day of Week for the programmed event
String Start[NumOfEvents]; // Start time
String Stop[NumOfEvents]; // End time
};
String DataFile = "params.txt"; // Storage file name on flash
String Time_str, DoW_str; // For Date and Time
settings Timer[Channels][7]; // Timer settings, n-Channels each 7-days of the week
//################ VARIABLES ################
const char* ssid = "your SSID"; // WiFi SSID replace with details for your local network
const char* password = "your PASSWORD"; // WiFi Password replace with details for your local network
const char* Timezone = "GMT0BST,M3.5.0/01,M10.5.0/02";
// Example time zones
//const char* Timezone = "MET-1METDST,M3.5.0/01,M10.5.0/02"; // Most of Europe
//const char* Timezone = "CET-1CEST,M3.5.0,M10.5.0/3"; // Central Europe
//const char* Timezone = "EST-2METDST,M3.5.0/01,M10.5.0/02"; // Most of Europe
//const char* Timezone = "EST5EDT,M3.2.0,M11.1.0"; // EST USA
//const char* Timezone = "CST6CDT,M3.2.0,M11.1.0"; // CST USA
//const char* Timezone = "MST7MDT,M4.1.0,M10.5.0"; // MST USA
//const char* Timezone = "NZST-12NZDT,M9.5.0,M4.1.0/3"; // Auckland
//const char* Timezone = "EET-2EEST,M3.5.5/0,M10.5.5/0"; // Asia
//const char* Timezone = "ACST-9:30ACDT,M10.1.0,M4.1.0/3": // Australia
// System values
String sitetitle = "4-Channel Relay Controller";
String Year = "2024"; // For the footer line
bool ManualOverride = false; // Manual override
String Units = "M"; // or Units = "I" for °F and 12:12pm time format
static bool eth_connected = false;
String webpage = ""; // General purpose variables to hold HTML code for display
int TimerCheckDuration = 1000; // Check for timer event every 1-second
int wifi_signal = 0; // WiFi signal strength
int LastTimerSwitchCheck = 0; // Counter for last timer check
int UnixTime = 0; // Time now (when updated) of the current time
String Channel1_State = "OFF"; // Status of the channel
String Channel2_State = "OFF"; // Status of the channel
String Channel3_State = "OFF"; // Status of the channel
String Channel4_State = "OFF"; // Status of the channel
bool Channel1_Override = false;
bool Channel2_Override = false;
bool Channel3_Override = false;
bool Channel4_Override = false;
AsyncWebServer server(80); // Server on IP address port 80 (web-browser default, change to your requirements, e.g. 8080
// To access server from outside of a WiFi (LAN) network e.g. on port 8080 add a rule on your Router that forwards a connection request
// to http://your_WAN_address:8080/ to http://your_LAN_address:8080 and then you can view your ESP server from anywhere.
// Example http://yourhome.ip:8080 and your ESP Server is at 192.168.0.40, then the request will be directed to http://192.168.0.40:8080
//#########################################################################################
void setup() {
SetupSystem(); // General system setup
WiFi.onEvent(NetworkEvent); // Start Network services
SetupTime(); // Start NTP clock services
StartSPIFFS(); // Start SPIFFS filing system
Initialise_Array(); // Initialise the array for storage and set some values
RecoverSettings(); // Recover settings from LittleFS
SetupDeviceName(ServerName); // Set logical device name
// Set handler for '/'
server.on("/", HTTP_GET, [](AsyncWebServerRequest * request) {
request->redirect("/homepage"); // Go to home page
});
// Set handler for '/homepage'
server.on("/homepage", HTTP_GET, [](AsyncWebServerRequest * request) {
Homepage();
request->send(200, "text/html", webpage);
});
// Set handler for '/timer1'
server.on("/timer1", HTTP_GET, [](AsyncWebServerRequest * request) {
TimerSet(Channel1);
request->send(200, "text/html", webpage);
});
// Set handler for '/timer2'
server.on("/timer2", HTTP_GET, [](AsyncWebServerRequest * request) {
TimerSet(Channel2);
request->send(200, "text/html", webpage);
});
// Set handler for '/timer3'
server.on("/timer3", HTTP_GET, [](AsyncWebServerRequest * request) {
TimerSet(Channel3);
request->send(200, "text/html", webpage);
});
// Set handler for '/timer4'
server.on("/timer4", HTTP_GET, [](AsyncWebServerRequest * request) {
TimerSet(Channel4);
request->send(200, "text/html", webpage);
});
// Set handler for '/setup'
server.on("/setup", HTTP_GET, [](AsyncWebServerRequest * request) {
Setup();
request->send(200, "text/html", webpage);
});
// Set handler for '/help'
server.on("/help", HTTP_GET, [](AsyncWebServerRequest * request) {
Help();
request->send(200, "text/html", webpage);
});
// Set handler for '/handletimer0' inputs
server.on("/handletimer0", HTTP_GET, [](AsyncWebServerRequest * request) {
for (byte dow = 0; dow < 7; dow++) {
for (byte p = 0; p < 4; p++) {
Timer[0][dow].Start[p] = request->arg(String(dow) + "." + String(p) + ".Start");
Timer[0][dow].Stop[p] = request->arg(String(dow) + "." + String(p) + ".Stop");
}
}
SaveSettings();
request->redirect("/homepage"); // Go back to home page
});
// Set handler for '/handletimer1' inputs
server.on("/handletimer1", HTTP_GET, [](AsyncWebServerRequest * request) {
for (byte dow = 0; dow < 7; dow++) {
for (byte p = 0; p < 4; p++) {
Timer[1][dow].Start[p] = request->arg(String(dow) + "." + String(p) + ".Start");
Timer[1][dow].Stop[p] = request->arg(String(dow) + "." + String(p) + ".Stop");
}
}
SaveSettings();
request->redirect("/homepage"); // Go back to home page
});
// Set handler for '/handletimer2' inputs
server.on("/handletimer2", HTTP_GET, [](AsyncWebServerRequest * request) {
for (byte dow = 0; dow < 7; dow++) {
for (byte p = 0; p < 4; p++) {
Timer[2][dow].Start[p] = request->arg(String(dow) + "." + String(p) + ".Start");
Timer[2][dow].Stop[p] = request->arg(String(dow) + "." + String(p) + ".Stop");
}
}
SaveSettings();
request->redirect("/homepage"); // Go back to home page
});
// Set handler for '/handletimer3' inputs
server.on("/handletimer3", HTTP_GET, [](AsyncWebServerRequest * request) {
for (byte dow = 0; dow < 7; dow++) {
for (byte p = 0; p < 4; p++) {
Timer[3][dow].Start[p] = request->arg(String(dow) + "." + String(p) + ".Start");
Timer[3][dow].Stop[p] = request->arg(String(dow) + "." + String(p) + ".Stop");
}
}
SaveSettings();
request->redirect("/homepage"); // Go back to home page
});
// Set handler for '/handlesetup' inputs
server.on("/handlesetup", HTTP_GET, [](AsyncWebServerRequest * request) {
if (request->hasArg("manualoverride1")) {
String stringArg = request->arg("manualoverride1");
if (stringArg == "ON") Channel1_Override = true; else Channel1_Override = false;
}
if (request->hasArg("manualoverride2")) {
String stringArg = request->arg("manualoverride2");
if (stringArg == "ON") Channel2_Override = true; else Channel2_Override = false;
}
if (request->hasArg("manualoverride3")) {
String stringArg = request->arg("manualoverride3");
if (stringArg == "ON") Channel3_Override = true; else Channel3_Override = false;
}
if (request->hasArg("manualoverride4")) {
String stringArg = request->arg("manualoverride4");
if (stringArg == "ON") Channel4_Override = true; else Channel4_Override = false;
}
SaveSettings();
request->redirect("/homepage"); // Go back to home page
});
server.begin();
LastTimerSwitchCheck = millis() + TimerCheckDuration; // preload timer value with update duration
}
//#########################################################################################
void loop() {
if (millis() > LastTimerSwitchCheck) {
LastTimerSwitchCheck = millis() + TimerCheckDuration; // Check at time-out for a change
UpdateLocalTime(); // Updates Time UnixTime to 'now'
CheckTimerEvent(); // Check for schedule actuated
}
}
//#########################################################################################
void Homepage() {
bool TimerSummary[4][7][48]; // 7 days each with 48 timer periods
append_HTML_header(Refresh);
webpage += "<h2>Channel Status @ " + Time_str + "</h2><br>";
webpage += "<table class='centre'>";
webpage += "<tr>";
webpage += " <td>Channel-1</td>";
webpage += " <td>Channel-2</td>";
webpage += " <td>Channel-3</td>";
webpage += " <td>Channel-4</td>";
webpage += "</tr>";
webpage += "<tr>";
webpage += " <td><div class='c1 Circle'><a class='" + String((Channel1_State == "ON" ? "on'" : "off'")) + " href='/timer1'>" + String(Channel1_State) + "</a></div></td>";
webpage += " <td><div class='c2 Circle'><a class='" + String((Channel2_State == "ON" ? "on'" : "off'")) + " href='/timer2'>" + String(Channel2_State) + "</a></div></td>";
webpage += " <td><div class='c3 Circle'><a class='" + String((Channel3_State == "ON" ? "on'" : "off'")) + " href='/timer3'>" + String(Channel3_State) + "</a></div></td>";
webpage += " <td><div class='c4 Circle'><a class='" + String((Channel4_State == "ON" ? "on'" : "off'")) + " href='/timer4'>" + String(Channel4_State) + "</a></div></td>";
webpage += "</tr>";
webpage += "</table>";
webpage += "<br>";
webpage += "<h3>Summary of Channel Schedules</h3>";
webpage += "<table class='centre sum'>";
webpage += "<tr>";
webpage += "<td>Time</td>";
webpage += "<td colspan='4'>" + Timer[0][0].DoW + "</td>";
for (byte dow = 1; dow < 6; dow++) webpage += "<td colspan='4'>" + Timer[0][dow].DoW + "</td>";
webpage += "<td colspan='4'>" + Timer[0][6].DoW + "</td>";
webpage += "</tr>";
String TimeNow_Str;
for (byte channel = 0; channel < Channels; channel++) {
for (byte DoW = 0; DoW < 7; DoW++) {
for (byte timer = 0; timer < 48; timer++) { // 0 to 24 hours in mins, reported in 15-min increments hence 60 / (2) = 30
TimeNow_Str = String((timer / 2 < 10 ? "0" : "")) + String(timer / 2) + ":" + String(((timer % 2) * 30) < 10 ? "0" : "") + String((timer % 2) * 30);
TimerSummary[channel][DoW][timer] = CheckTime(channel, String(DoW), TimeNow_Str);
}
}
}
for (byte timer = 0; timer < 48; timer++) { // 0 to 24 hours in mins, reported in 30-min increments hence 60 / (2) = 30
webpage += "<tr>";
webpage += "<td>" + String((timer / 2 < 10 ? "0" : "")) + String(timer / 2) + ":" + String(((timer % 2) * 30) < 10 ? "0" : "") + String((timer % 2) * 30) + "</td>"; // 4 x 15-min intervals per hour
for (byte DoW = 0; DoW < 7; DoW++) {
webpage += "<td class='" + String(TimerSummary[0][DoW][timer] ? "c1on" : "coff") + "'></td>";
webpage += "<td class='" + String(TimerSummary[1][DoW][timer] ? "c2on" : "coff") + "'></td>";
webpage += "<td class='" + String(TimerSummary[2][DoW][timer] ? "c3on" : "coff") + "'></td>";
webpage += "<td class='" + String(TimerSummary[3][DoW][timer] ? "c4on" : "coff") + "'></td>";
}
webpage += "</tr>";
}
webpage += "</table>";
webpage += "<br>";
append_HTML_footer();
}
//#########################################################################################
bool CheckTime(byte channel, String DoW_Str, String TimeNow_Str) {
for (byte dow = 0; dow < 7; dow++) { // Look for any valid timer events, if found turn the heating on
for (byte p = 0; p < NumOfEvents; p++) {
// Now check for a scheduled ON time, if so Switch the Timer ON and check the temperature against target temperature
if (String(dow) == DoW_Str && (TimeNow_Str >= Timer[channel][dow].Start[p] && TimeNow_Str < Timer[channel][dow].Stop[p] && Timer[channel][dow].Start[p] != ""))
return true;
}
}
return false;
}
//#########################################################################################
void TimerSet(int channel) {
append_HTML_header(noRefresh);
webpage += "<h2>Channel-" + String(channel + 1) + " Schedule Setup</h2><br>";
webpage += "<h3>Enter required time, use Clock symbol for ease of time entry</h3><br>";
webpage += "<FORM action='/handletimer" + String(channel) + "'>";
webpage += "<table class='centre timer'>";
webpage += "<col><col><col><col><col><col><col><col>";
webpage += "<tr><td>Control</td>";
webpage += "<td>" + Timer[channel][0].DoW + "</td>";
for (byte dow = 1; dow < 6; dow++) { // Heading line showing DoW
webpage += "<td>" + Timer[channel][dow].DoW + "</td>";
}
webpage += "<td>" + Timer[channel][6].DoW + "</td>";
webpage += "</tr>";
for (byte p = 0; p < NumOfEvents; p++) {
webpage += "<tr>";
webpage += "<td>Start</td>";
webpage += "<td><input type='time' name='" + String(0) + "." + String(p) + ".Start' value='" + Timer[channel][0].Start[p] + "'></td>";
for (int dow = 1; dow < 6; dow++) {
webpage += "<td><input type='time' name='" + String(dow) + "." + String(p) + ".Start' value='" + Timer[channel][dow].Start[p] + "'></td>";
}
webpage += "<td><input type='time' name='" + String(6) + "." + String(p) + ".Start' value='" + Timer[channel][6].Start[p] + "'></td>";
webpage += "</tr>";
webpage += "<tr><td>Stop</td>";
webpage += "<td><input type='time' name='" + String(0) + "." + String(p) + ".Stop' value='" + Timer[channel][0].Stop[p] + "'></td>";
for (int dow = 1; dow < 6; dow++) {
webpage += "<td><input type='time' name='" + String(dow) + "." + String(p) + ".Stop' value='" + Timer[channel][dow].Stop[p] + "'></td>";
}
webpage += "<td><input type='time' name='" + String(6) + "." + String(p) + ".Stop' value='" + Timer[channel][6].Stop[p] + "'></td>";
webpage += "</tr>";
if (p < (NumOfEvents - 1)) {
webpage += "<tr><td></td><td></td>";
for (int dow = 2; dow < 7; dow++) {
webpage += "<td>-</td>";
}
webpage += "<td></td>";
webpage += "</tr>";
}
}
webpage += "</table>";
webpage += "<div class='centre'>";
webpage += "<br><input type='submit' value='Enter'><br><br>";
webpage += "</div></form>";
append_HTML_footer();
}
//#########################################################################################
void Setup() {
append_HTML_header(noRefresh);
webpage += "<h2>Manual Override</h2><br>";
webpage += "<h3>Enter required values</h3>";
webpage += "<FORM action='/handlesetup'>";
webpage += "<table class='centre'>";
webpage += "<tr>";
webpage += "<td>Setting</td><td>Value</td>";
webpage += "</tr>";
webpage += "<tr>";
webpage += "<td><label>Channel-1 Manual Override </label></td>";
webpage += "<td><select name='manualoverride1'><option value='ON'>ON</option>";
webpage += "<option selected value='OFF'>OFF</option></select></td>"; // ON/OFF
webpage += "</tr>";
webpage += "<tr>";
webpage += "<td><label>Channel-2 Manual Override </label></td>";
webpage += "<td><select name='manualoverride2'><option value='ON'>ON</option>";
webpage += "<option selected value='OFF'>OFF</option></select></td>"; // ON/OFF
webpage += "</tr>";
webpage += "<tr>";
webpage += "<td><label>Channel-3 Manual Override </label></td>";
webpage += "<td><select name='manualoverride3'><option value='ON'>ON</option>";
webpage += "<option selected value='OFF'>OFF</option></select></td>"; // ON/OFF
webpage += "</tr>";
webpage += "<tr>";
webpage += "<td><label>Channel-4 Manual Override </label></td>";
webpage += "<td><select name='manualoverride4'><option value='ON'>ON</option>";
webpage += "<option selected value='OFF'>OFF</option></select></td>"; // ON/OFF
webpage += "</tr>";
webpage += "</table>";
webpage += "<br><input type='submit' value='Enter'><br><br>";
webpage += "</form>";
append_HTML_footer();
}
//#########################################################################################
void Help() {
append_HTML_header(noRefresh);
webpage += "<h2>Help</h2><br>";
webpage += "<div style='text-align: left;font-size:1.1em;'>";
webpage += "<br><u><b>Status Page</b></u>";
webpage += "<p>Provides a channel overview showing either On or OFF for each channel, READ for ON and GREEN for OFF.</p>";
webpage += "<p>Each position has a link to the Channel's timer settings, so clicking on Channel-2 'ON' or 'OFF' goes to the channel's timer settings page.</p>";
webpage += "<p>The weekly summary shows 4-channels per day, from 00:00 to 23:30 using the channel's colour to indicate if it is scheduled to be ON.</p>";
webpage += "<p>NOTE: the minimum time period that can be displayed is 30-minutes. e.g. 12:05 to 12:20 won't be displayed, but 12:00 to 12:20 will be.</p>";
webpage += "<p>This is becuase the minimum resolution is 30-mins for the summary, this does not affect the timing accuracy.</p>";
webpage += "<br><u><b>Channel Setting 1 to 4</b></u>";
webpage += "<p>Each channel can be set for 7-days per week with up-to 4-periods per day.</p>";
webpage += "<p>You can either use a single slot to switch the channel ON between e.g. 09:00 and 13:00</p>";
webpage += "<p>or you could set period-1 from 09:00 to 10:00; period-2 from 10:00 to 11:00; period-3 from 11:00 to 12:00 and period-4 from 12:00 to 13:00</p>";
webpage += "<u><b>Setup Page</b></u>";
webpage += "<p>Enables a channel(s) to be overridden to ON, but only until a valid schedule period comes into effect.</p>";
webpage += "<p>The manual-override is cleared when a valid timer period begins.</p>";
webpage += "</div>";
append_HTML_footer();
}
//#########################################################################################
void CheckTimerEvent() {
String TimeNow;
TimeNow = ConvertUnixTime(UnixTime); // Get the current time e.g. 15:35
Channel1_State = "OFF"; // Switch Channel OFF until the schedule decides otherwise
Channel2_State = "OFF"; // Switch Channel OFF until the schedule decides otherwise
Channel3_State = "OFF"; // Switch Channel OFF until the schedule decides otherwise
Channel4_State = "OFF"; // Switch Channel OFF until the schedule decides otherwise
if (Channel1_Override) { // If manual override is requested then turn the Channel on
Channel1_State = "ON";
ActuateChannel(ON, Channel1, Channel1_Pin); // Switch Channel ON if requested
}
if (Channel2_Override) {
Channel2_State = "ON";
ActuateChannel(ON, Channel2, Channel2_Pin); // Switch Channel ON if requested
}
if (Channel3_Override) {
Channel3_State = "ON";
ActuateChannel(ON, Channel3, Channel3_Pin); // Switch Channel ON if requested
}
if (Channel4_Override) {
Channel4_State = "ON";
ActuateChannel(ON, Channel4, Channel4_Pin); // Switch Channel ON if requested
}
for (byte channel = 0; channel < Channels; channel++) {
for (byte dow = 0; dow < 7; dow++) { // Look for any valid timer events, if found turn the heating on
for (byte p = 0; p < NumOfEvents; p++) {
// Now check for a scheduled ON time, if so Switch the Timer ON and check the temperature against target temperature
if (String(dow) == DoW_str && (TimeNow >= Timer[channel][dow].Start[p] && TimeNow < Timer[channel][dow].Stop[p] && Timer[channel][dow].Start[p] != ""))
{
if (channel == 0) Channel1_State = "ON";
if (channel == 1) Channel2_State = "ON";
if (channel == 2) Channel3_State = "ON";
if (channel == 3) Channel4_State = "ON";
}
}
}
}
if (Channel1_State == "ON") ActuateChannel(ON, Channel1, Channel1_Pin); else ActuateChannel(OFF, Channel1, Channel1_Pin);
if (Channel2_State == "ON") ActuateChannel(ON, Channel2, Channel2_Pin); else ActuateChannel(OFF, Channel2, Channel2_Pin);
if (Channel3_State == "ON") ActuateChannel(ON, Channel3, Channel3_Pin); else ActuateChannel(OFF, Channel3, Channel3_Pin);
if (Channel4_State == "ON") ActuateChannel(ON, Channel4, Channel4_Pin); else ActuateChannel(OFF, Channel4, Channel4_Pin);
}
//#########################################################################################
void ActuateChannel(bool demand, byte channel, byte channel_pin) {
if (demand) {
if (ChannelReverse) digitalWrite(channel_pin, HIGH); else digitalWrite(channel_pin, LOW);
Serial.println("Putting Channel-" + String(channel) + " ON");
}
else
{
if (ChannelReverse) digitalWrite(channel_pin, LOW); else digitalWrite(channel_pin, HIGH);
Serial.println("Putting Channel-" + String(channel) + " OFF");
}
if (Channel1_State == "ON" || // Switch ON LED if any channel is ON otherwise switch ot OFF
Channel2_State == "ON" ||
Channel3_State == "ON" ||
Channel4_State == "ON") digitalWrite(LEDPIN, LOW); else digitalWrite(LEDPIN, HIGH);
}
//#########################################################################################
void append_HTML_header(bool refreshMode) {
webpage = "<!DOCTYPE html><html lang='en'>";
webpage += "<head>";
webpage += "<title>" + sitetitle + "</title>";
webpage += "<meta charset='UTF-8'>";
if (refreshMode) webpage += "<meta http-equiv='refresh' content='5'>"; // 5-secs refresh time, test needed to prevent auto updates repeating some commands
webpage += "<script src=\"https://code.jquery.com/jquery-3.2.1.min.js\"></script>";
webpage += "<style>";
webpage += "body {width:68em;margin-left:auto;margin-right:auto;font-family:Arial,Helvetica,sans-serif;font-size:14px;color:blue;background-color:#e1e1ff;text-align:center;}";
webpage += ".centre {margin-left:auto;margin-right:auto;}";
webpage += "h2 {margin-top:0.3em;margin-bottom:0.3em;font-size:1.4em;}";
webpage += "h3 {margin-top:0.3em;margin-bottom:0.3em;font-size:1.2em;}";
webpage += ".on {color:red;text-decoration:none;}";
webpage += ".off {color:limegreen;text-decoration:none;}";
webpage += ".topnav {overflow: hidden;background-color:lightcyan;}";
webpage += ".topnav a {float:left;color:blue;text-align:center;padding:1em 1.14em;text-decoration:none;font-size:1.3em;}";
webpage += ".topnav a:hover {background-color:deepskyblue;color:white;}";
webpage += ".topnav a.active {background-color:lightblue;color:blue;}";
webpage += "table.timer tr {padding:0.2em 0.5em 0.2em 0.5em;font-size:1.1em;}";
webpage += "table.timer td {padding:0.2em 0.5em 0.2em 0.5em;font-size:1.1em;}";
webpage += "table.sum tr {padding:0.2em 0.5em 0.2em 0.5em;font-size:1.1em;border:1px solid blue;}";
webpage += "table.sum td {padding:0.2em 0.6em 0.2em 0.6em;font-size:1.1em;border:1px solid blue;}";
webpage += "col:first-child {background:lightcyan}col:nth-child(2){background:#CCC}col:nth-child(8){background:#CCC}";
webpage += "tr:first-child {background:lightcyan}";
webpage += ".medium {font-size:1.4em;padding:0;margin:0}";
webpage += ".ps {font-size:0.7em;padding:0;margin:0}";
webpage += "footer {padding:0.08em;background-color:cyan;font-size:1.1em;}";
webpage += ".Circle {border-radius:50%;width:2.7em;height:2.7em;padding:0.2em;text-align:center;font-size:3em;display:inline-flex;justify-content:center;align-items:center;}";
webpage += ".c1 {border:0.15em solid yellow;background-color:lightgray;}";
webpage += ".c2 {border:0.15em solid orange;background-color:lightgray;}";
webpage += ".c3 {border:0.15em solid red;background-color:lightgray;}";
webpage += ".c4 {border:0.15em solid pink;background-color:lightgray;}";
webpage += ".coff {background-color:gainsboro;}";
webpage += ".c1on {background-color:yellow;}";
webpage += ".c2on {background-color:orange;}";
webpage += ".c3on {background-color:red;}";
webpage += ".c4on {background-color:pink;}";
webpage += ".wifi {padding:3px;position:relative;top:1em;left:0.36em;}";
webpage += ".wifi, .wifi:before {display:inline-block;border:9px double transparent;border-top-color:currentColor;border-radius:50%;}";
webpage += ".wifi:before {content:'';width:0;height:0;}";
webpage += "</style></head>";
webpage += "<body>";
webpage += "<div class='topnav'>";
webpage += " <a href='/'>Status</a>";
webpage += " <a href='timer1'>Timer-1</a>";
webpage += " <a href='timer2'>Timer-2</a>";
webpage += " <a href='timer3'>Timer-3</a>";
webpage += " <a href='timer4'>Timer-4</a>";
webpage += " <a href='setup'>Override</a>";
webpage += " <a href='help'>Help</a>";
webpage += " <div class='wifi'></div>" + WiFiSignal();
webpage += "</div>";
}
//#########################################################################################
void append_HTML_footer() {
webpage += "<footer>";
webpage += "<p class='medium'>4-Channel Controller</p>";
webpage += "<p class='ps'><i>Copyright © D L Bird " + String(Year) + " V" + version + "</i></p>";
webpage += "</footer>";
webpage += "</body></html>";
}
//#########################################################################################
void SetupDeviceName(const char *DeviceName) {
if (MDNS.begin(DeviceName)) { // The name that will identify your device on the network
Serial.println("mDNS responder started");
Serial.print("Device name: ");
Serial.println(DeviceName);
MDNS.addService("n8i-mlp", "tcp", 23); // Add service
}
else
Serial.println("Error setting up MDNS responder");
}
//#########################################################################################
void NetworkEvent(WiFiEvent_t event) {
ETH.begin(ETH_ADDR, ETH_POWER_PIN, ETH_MDC_PIN, ETH_MDIO_PIN, ETH_TYPE, ETH_CLK_MODE); // Enable ETH
switch (event) {
case SYSTEM_EVENT_ETH_START:
Serial.println("ETH Started");
//set eth hostname here
ETH.setHostname("esp32-ethernet");
break;
case SYSTEM_EVENT_ETH_CONNECTED:
Serial.println("ETH Connected");
break;
case SYSTEM_EVENT_ETH_GOT_IP:
Serial.print("ETH MAC: ");
Serial.print(ETH.macAddress());
Serial.print(", IPv4: ");
Serial.print(ETH.localIP());
if (ETH.fullDuplex()) {
Serial.print(", FULL_DUPLEX");
}
Serial.print(", ");
Serial.print(ETH.linkSpeed());
Serial.println("Mbps");
eth_connected = true;
break;
case SYSTEM_EVENT_ETH_DISCONNECTED:
Serial.println("ETH Disconnected");
eth_connected = false;
break;
case SYSTEM_EVENT_ETH_STOP:
Serial.println("ETH Stopped");
eth_connected = false;
break;
default:
break;
}
}
//#########################################################################################
void SetupSystem() {
Serial.begin(115200); // Initialise serial communications
while (!Serial);
Serial.println(__FILE__);
Serial.println("Starting...");
pinMode(Channel1_Pin, OUTPUT);
pinMode(Channel2_Pin, OUTPUT);
pinMode(Channel3_Pin, OUTPUT);
pinMode(Channel4_Pin, OUTPUT);
pinMode(LEDPIN, OUTPUT);
pinMode(ETH_POWER_PIN_ALTERNATIVE, OUTPUT);
digitalWrite(ETH_POWER_PIN_ALTERNATIVE, HIGH);
}
//#########################################################################################
boolean SetupTime() {
configTime(0, 0, "time.nist.gov"); // (gmtOffset_sec, daylightOffset_sec, ntpServer)
setenv("TZ", Timezone, 1); // setenv()adds "TZ" variable to the environment, only used if set to 1, 0 means no change
tzset();
delay(200);
bool TimeStatus = UpdateLocalTime();
return TimeStatus;
}
//#########################################################################################
boolean UpdateLocalTime() {
struct tm timeinfo;
time_t now;
char time_output[30];
while (!getLocalTime(&timeinfo, 15000)) { // Wait for up to 15-sec for time to synchronise
return false;
}
time(&now);
UnixTime = now;
//See http://www.cplusplus.com/reference/ctime/strftime/
strftime(time_output, sizeof(time_output), "%H:%M", &timeinfo); // Creates: '14:05'
Time_str = time_output;
strftime(time_output, sizeof(time_output), "%w", &timeinfo); // Creates: '0' for Sun
DoW_str = time_output;
return true;
}
//#########################################################################################
String ConvertUnixTime(int unix_time) {
time_t tm = unix_time;
struct tm *now_tm = localtime(&tm);
char output[40];
strftime(output, sizeof(output), "%H:%M", now_tm); // Returns 21:12
return output;
}
//#########################################################################################
void StartSPIFFS() {
Serial.println("Starting SPIFFS");
boolean SPIFFS_Status;
SPIFFS_Status = SPIFFS.begin();
if (SPIFFS_Status == false)
{ // Most likely SPIFFS has not yet been formated, so do so
Serial.println("Formatting SPIFFS (it may take some time)...");
SPIFFS.begin(true); // Now format SPIFFS
File datafile = SPIFFS.open("/" + DataFile, "r");
if (!datafile || !datafile.isDirectory()) {
Serial.println("SPIFFS failed to start..."); // Nothing more can be done, so delete and then create another file
SPIFFS.remove("/" + DataFile); // The file is corrupted!!
datafile.close();
}
}
else Serial.println("SPIFFS Started successfully...");
}
//#########################################################################################
void Initialise_Array() {
for (int channel = 0; channel < Channels; channel++) {
Timer[channel][0].DoW = "Sun";
Timer[channel][1].DoW = "Mon";
Timer[channel][2].DoW = "Tue";
Timer[channel][3].DoW = "Wed";
Timer[channel][4].DoW = "Thu";
Timer[channel][5].DoW = "Fri";
Timer[channel][6].DoW = "Sat";
}
}
//#########################################################################################
void SaveSettings() {
Serial.println("Getting ready to Save settings...");
File dataFile = SPIFFS.open("/" + DataFile, "w");
if (dataFile) { // Save settings
Serial.println("Saving settings...");
for (byte channel = 0; channel < Channels; channel++) {
for (byte dow = 0; dow < 7; dow++) {
//Serial.println("Day of week = " + String(dow));
for (byte p = 0; p < NumOfEvents; p++) {
dataFile.println(Timer[channel][dow].Start[p]);
dataFile.println(Timer[channel][dow].Stop[p]);
//Serial.println("Period: " + String(p) + " from: " + Timer[channel][dow].Start[p] + " to: " + Timer[channel][dow].Stop[p]);
}
}
}
dataFile.close();
Serial.println("Settings saved...");
}
}
//#########################################################################################
void RecoverSettings() {
String Entry;
Serial.println("Reading settings...");
File dataFile = SPIFFS.open("/" + DataFile, "r");
if (dataFile) { // if the file is available, read it
Serial.println("Recovering settings...");
while (dataFile.available()) {
for (byte channel = 0; channel < Channels; channel++) {
//Serial.println("Channel-" + String(channel + 1));
for (byte dow = 0; dow < 7; dow++) {
//Serial.println("Day of week = " + String(dow));
for (byte p = 0; p < NumOfEvents; p++) {
Timer[channel][dow].Start[p] = dataFile.readStringUntil('\n'); Timer[channel][dow].Start[p].trim();
Timer[channel][dow].Stop[p] = dataFile.readStringUntil('\n'); Timer[channel][dow].Stop[p].trim();
//Serial.println("Period: " + String(p) + " from: " + Timer[channel][dow].Start[p] + " to: " + Timer[channel][dow].Stop[p]);
}
}
}
dataFile.close();
Serial.println("Settings recovered...");
}
}
}
//#########################################################################################
String WiFiSignal() {
float Signal = WiFi.RSSI();
Signal = 90 / 40.0 * Signal + 212.5; // From Signal = 100% @ -50dBm and Signal = 10% @ -90dBm and y = mx + c
if (Signal > 100) Signal = 100;
return " " + String(Signal, 0) + "%";
}
/*
Version 1.0 667 lines of code
All HTML fully validated by W3C
*/