-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathInteractiveWebServer.ino
356 lines (314 loc) · 11.2 KB
/
InteractiveWebServer.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
/*
Web server for remote relay control with LilyGo T-Relay board
Copyright 2022 James Tittsler
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Original example: Copyright 2022 Xinyuan-LilyGO
*/
#if defined(RELAY8)
#define RELAY_PIN_1 33
#define RELAY_PIN_2 32
#define RELAY_PIN_3 13
#define RELAY_PIN_4 12
#define RELAY_PIN_5 21
#define RELAY_PIN_6 19
#define RELAY_PIN_7 18
#define RELAY_PIN_8 5
#else
#define RELAY_PIN_1 21
#define RELAY_PIN_2 19
#define RELAY_PIN_3 18
#define RELAY_PIN_4 5
#endif
#define LED_PIN 25
#define BLINK_FAST 0
#define BLINK_HEARTBEAT 1
#define BLINK_PATTERN_LEN 4
#define WIFI_CHECK_INTERVAL 3000
#include <Arduino.h>
/* ESP32 Dependencies */
#include <WiFi.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <ESPDash.h>
/* Your WiFi Credentials */
#include "WiFi-credentials.h"
unsigned long now = 0;
unsigned long timestamp = 0;
unsigned long wifiTime = 0;
int blinkPattern = BLINK_FAST;
int blinkCycle = 0;
int wifiStatus = WL_IDLE_STATUS;
unsigned long blinks[2][4] = {
{101, 100, 101, 100}, /* odd -> LED on phase */
{ 75, 150, 75, 700}
};
#if defined(RELAY8)
#define NUMBER_OF_RELAYS 8
unsigned long ontime[8] = { /* momentary relay on time in millis*/
0, 0, 0, 400, 0, 0, 0, 800
};
unsigned long offtime[8] = { /* when to turn off momentary switch*/
0, 0, 0, 0, 0, 0, 0, 0
};
#else
#define NUMBER_OF_RELAYS 4
unsigned long ontime[4] = { /* momentary relay on time in millis*/
0, 0, 0, 400
};
unsigned long offtime[4] = { /* when to turn off momentary switch*/
0, 0, 0, 0
};
#endif
/* Start Webserver */
AsyncWebServer server(80);
/* Attach ESP-DASH to AsyncWebServer */
ESPDash dashboard(&server);
/*
Button Card
Format - (Dashboard Instance, Card Type, Card Name)
*/
Card Relay_1(&dashboard, BUTTON_CARD, "Relay_1");
Card Relay_2(&dashboard, BUTTON_CARD, "Relay_2");
Card Relay_3(&dashboard, BUTTON_CARD, "Relay_3");
Card Relay_4(&dashboard, BUTTON_CARD, "Relay_4_momentary");
#if defined(RELAY8)
Card Relay_5(&dashboard, BUTTON_CARD, "Relay_5");
Card Relay_6(&dashboard, BUTTON_CARD, "Relay_6");
Card Relay_7(&dashboard, BUTTON_CARD, "Relay_7");
Card Relay_8(&dashboard, BUTTON_CARD, "Relay_8_momentary");
#endif
void setup()
{
Serial.begin(115200);
// Onboard LED
pinMode(RELAY_PIN_1, OUTPUT);
pinMode(RELAY_PIN_2, OUTPUT);
pinMode(RELAY_PIN_3, OUTPUT);
pinMode(RELAY_PIN_4, OUTPUT);
#if defined(RELAY8)
pinMode(RELAY_PIN_5, OUTPUT);
pinMode(RELAY_PIN_6, OUTPUT);
pinMode(RELAY_PIN_7, OUTPUT);
pinMode(RELAY_PIN_8, OUTPUT);
#endif
pinMode(LED_PIN, OUTPUT);
delay(100);
digitalWrite(RELAY_PIN_1, LOW);
digitalWrite(RELAY_PIN_2, LOW);
digitalWrite(RELAY_PIN_3, LOW);
digitalWrite(RELAY_PIN_4, LOW);
#if defined(RELAY8)
digitalWrite(RELAY_PIN_5, LOW);
digitalWrite(RELAY_PIN_6, LOW);
digitalWrite(RELAY_PIN_7, LOW);
digitalWrite(RELAY_PIN_8, LOW);
digitalWrite(LED_PIN, LOW);
#endif
/* Connect WiFi */
Serial.print("Connecting to: ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
timestamp = millis();
while (WiFi.status() != WL_CONNECTED) {
if (millis() - timestamp > blinks[blinkPattern][blinkCycle]) {
digitalWrite(LED_PIN, ((blinks[blinkPattern][blinkCycle] & 1) == 1));
timestamp = millis();
blinkCycle = (blinkCycle + 1) % BLINK_PATTERN_LEN;
}
}
digitalWrite(LED_PIN, HIGH);
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
Serial.print("RSSI: ");
Serial.println(WiFi.RSSI());
/* Attach Relay_1 Callback */
Relay_1.attachCallback([&](bool value) {
/* Print our new button value received from dashboard */
Serial.println("Relay_1 Triggered: " + String((value) ? "true" : "false"));
digitalWrite(RELAY_PIN_1, value);
/* Make sure we update our button's value and send update to dashboard */
Relay_1.update(value);
dashboard.sendUpdates();
/* if momentary switch turning on, start timer */
if (value && (ontime[0] > 0)) {
offtime[0] = millis() + ontime[0];
}
});
/* Attach Relay_2 Callback */
Relay_2.attachCallback([&](bool value) {
/* Print our new button value received from dashboard */
Serial.println("Relay_2 Triggered: " + String((value) ? "true" : "false"));
digitalWrite(RELAY_PIN_2, value);
/* Make sure we update our button's value and send update to dashboard */
Relay_2.update(value);
dashboard.sendUpdates();
/* if momentary switch turning on, start timer */
if (value && (ontime[1] > 0)) {
offtime[1] = millis() + ontime[1];
}
});
/* Attach Button Callback */
Relay_3.attachCallback([&](bool value) {
/* Print our new button value received from dashboard */
Serial.println("Relay_3 Triggered: " + String((value) ? "true" : "false"));
digitalWrite(RELAY_PIN_3, value);
/* Make sure we update our button's value and send update to dashboard */
Relay_3.update(value);
dashboard.sendUpdates();
/* if momentary switch turning on, start timer */
if (value && (ontime[2] > 0)) {
offtime[2] = millis() + ontime[2];
}
});
/* Attach Button Callback */
Relay_4.attachCallback([&](bool value) {
/* Print our new button value received from dashboard */
Serial.println("Relay_4 Triggered: " + String((value) ? "true" : "false"));
digitalWrite(RELAY_PIN_4, value);
/* Make sure we update our button's value and send update to dashboard */
Relay_4.update(value);
dashboard.sendUpdates();
/* if momentary switch turning on, start timer */
if (value && (ontime[3] > 0)) {
offtime[3] = millis() + ontime[3];
}
});
#if defined(RELAY8)
/* Attach Relay_5 Callback */
Relay_5.attachCallback([&](bool value) {
/* Print our new button value received from dashboard */
Serial.println("Relay_5 Triggered: " + String((value) ? "true" : "false"));
digitalWrite(RELAY_PIN_5, value);
/* Make sure we update our button's value and send update to dashboard */
Relay_5.update(value);
dashboard.sendUpdates();
/* if momentary switch turning on, start timer */
if (value && (ontime[4] > 0)) {
offtime[4] = millis() + ontime[4];
}
});
/* Attach Relay_6 Callback */
Relay_6.attachCallback([&](bool value) {
/* Print our new button value received from dashboard */
Serial.println("Relay_6 Triggered: " + String((value) ? "true" : "false"));
digitalWrite(RELAY_PIN_6, value);
/* Make sure we update our button's value and send update to dashboard */
Relay_6.update(value);
dashboard.sendUpdates();
/* if momentary switch turning on, start timer */
if (value && (ontime[5] > 0)) {
offtime[5] = millis() + ontime[5];
}
});
/* Attach Button Callback */
Relay_7.attachCallback([&](bool value) {
/* Print our new button value received from dashboard */
Serial.println("Relay_7 Triggered: " + String((value) ? "true" : "false"));
digitalWrite(RELAY_PIN_7, value);
/* Make sure we update our button's value and send update to dashboard */
Relay_7.update(value);
dashboard.sendUpdates();
/* if momentary switch turning on, start timer */
if (value && (ontime[6] > 0)) {
offtime[6] = millis() + ontime[6];
}
});
/* Attach Button Callback */
Relay_8.attachCallback([&](bool value) {
/* Print our new button value received from dashboard */
Serial.println("Relay_8 Triggered: " + String((value) ? "true" : "false"));
digitalWrite(RELAY_PIN_8, value);
/* Make sure we update our button's value and send update to dashboard */
Relay_8.update(value);
dashboard.sendUpdates();
/* if momentary switch turning on, start timer */
if (value && (ontime[7] > 0)) {
offtime[7] = millis() + ontime[7];
}
});
#endif
/* Start AsyncWebServer */
server.begin();
}
void loop()
{
int i = 0;
bool needUpdate = false;
now = millis();
wifiStatus = WiFi.status();
/* turn off momentary switches */
for (i=0; i<NUMBER_OF_RELAYS; i++) {
if ((offtime[i] > 0) && (now > offtime[i])) {
offtime[i] = 0;
needUpdate = true;
switch (i) {
case 0:
digitalWrite(RELAY_PIN_1, false);
Relay_1.update(false);
break;
case 1:
digitalWrite(RELAY_PIN_2, false);
Relay_2.update(false);
break;
case 2:
digitalWrite(RELAY_PIN_3, false);
Relay_3.update(false);
break;
case 3:
digitalWrite(RELAY_PIN_4, false);
Relay_4.update(false);
break;
#if defined(RELAY8)
case 4:
digitalWrite(RELAY_PIN_5, false);
Relay_5.update(false);
break;
case 5:
digitalWrite(RELAY_PIN_6, false);
Relay_6.update(false);
break;
case 6:
digitalWrite(RELAY_PIN_7, false);
Relay_7.update(false);
break;
case 7:
digitalWrite(RELAY_PIN_8, false);
Relay_8.update(false);
break;
#endif
}
if (needUpdate) {
dashboard.sendUpdates();
}
}
}
if ((wifiStatus != WL_CONNECTED) && (now-wifiTime > WIFI_CHECK_INTERVAL)) {
Serial.println("Reconnecting to WiFi...");
WiFi.disconnect();
WiFi.reconnect();
wifiTime = now;
timestamp = now;
blinkPattern = BLINK_FAST;
blinkCycle = 0;
} else if ((wifiStatus == WL_CONNECTED) && (blinkPattern == BLINK_FAST)) {
timestamp = now;
blinkPattern = BLINK_HEARTBEAT;
blinkCycle = 0;
}
/* show LED heartbeat */
if (now - timestamp > blinks[blinkPattern][blinkCycle]) {
digitalWrite(LED_PIN, ((blinks[blinkPattern][blinkCycle] & 1) == 1));
timestamp = now;
blinkCycle = (blinkCycle + 1) % BLINK_PATTERN_LEN;
}
}