-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathirisdowncountdowntimer-remotedisplay-esp32.ino
457 lines (361 loc) · 11.3 KB
/
irisdowncountdowntimer-remotedisplay-esp32.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
/*
Display for UDP Time protocol used by Interspace Industries CDEther Receiver, Irisdown Countdown Timer 2.0, Neodarque Stage Timer 2 and Clock-8001
https://www.interspaceind.com/shop-our-products/product/112-cdether-receiver/category_pathway-37.html
https://www.irisdown.co.uk/countdowntimer.html
https://www.neodarque.com/StageTimer
https://gitlab.com/Depili/clock-8001
I am using that display with clock-8001 that sends the UDP time on two ports: 36700 and 36701
Listens for UDP packets on port 36700. Shows time on:
1) Serial console
Parts of code by David Shepherd
Libraries required:
Board required:
esp32 by Espressive Systems Board v1.0.4 (v1.0.0 has seriously unreliable I2C)
*/
#define ETH_CLK_MODE ETH_CLOCK_GPIO17_OUT
#define ETH_PHY_POWER 12 // ESP32-GATEWAY 5 and ESP32-POE 12 !!!
#define EEPROM_SIZE 1
#include <WiFi.h>
#include "AsyncUDP.h"
#include <Wire.h>
//#include "Adafruit_LEDBackpack.h"
#include <EEPROM.h>
#include <MD_Parola.h>
#include <MD_MAX72xx.h>
#include <SPI.h>
// Uncomment according to your hardware type
#define HARDWARE_TYPE MD_MAX72XX::FC16_HW
//#define HARDWARE_TYPE MD_MAX72XX::GENERIC_HW
// Defining size, and output pins
#define MAX_DEVICES 8
#define CS_PIN 5
MD_Parola Display = MD_Parola(HARDWARE_TYPE, CS_PIN, MAX_DEVICES);
// network vars
bool ethConnected = false;
bool goodHeader = true;
AsyncUDP udp;
const unsigned int udpPort = 36700;
unsigned long udpLastPacketMillis = 0;
const unsigned int udpTimeout = 3000;
unsigned long udpTime = 0; // Time remaining in seconds.
bool udpTimeNegative = false; // If true, udpTime is time since 00:00.
int udpId = 0; // id of the master unit.
// LED display vars
//int ledBrightness = 15; // maxmimum brightness
//Adafruit_7segment ledDisplay = Adafruit_7segment();
// System vars
int id = 0; // id of this unit (to be read from EEPROM)
unsigned long currentMillis = 0;
unsigned long previousMillis = 0;
const int intervalMillis = 100;
bool modeChanged = false;
const int initTimeout = 5000;
int initCounter = 0;
unsigned long initCurrentMillis = 0;
unsigned long initPreviousMillis = 0;
const int initIntervalMillis = 500;
const int menuTimeout = 4000;
unsigned long menuMillis = 0;
const char* ssid = "cma";
const char* password = "@@cmacma@@";
// Menu button.
const int buttonPin = 0;
int buttonState = HIGH;
int lastButtonState = HIGH;
// opMode will change to match current status of the device:
enum opMode {
Menu = -1, // Show 'id: x' and increment on button press. Clear after menuTimeout.
Init = 0, // Chase digits and display id for a few seconds.
No_Eth = 1, // Eth down, show 'conn'.
No_Rx = 2, // No packet with matching id received in the last udpTimeout ms, show '--:--'.
Normal = 3, // Normal operation, show clock.
};
opMode currentMode = Init;
void WiFiEvent(WiFiEvent_t event) { // This event handler updates the connection status of ETH.
switch (event) {
case SYSTEM_EVENT_ETH_START:
Serial.println("ETH Started");
break;
case SYSTEM_EVENT_STA_CONNECTED:
Serial.println("WIFI Connected");
ethConnected = true;
break;
case SYSTEM_EVENT_ETH_GOT_IP:
Serial.print("ETH MAC: ");
Serial.print(WiFi.macAddress());
Serial.print(", IPv4: ");
Serial.print(WiFi.localIP());
WiFi.setHostname("esp32-ethernet");
ethConnected = true;
break;
case SYSTEM_EVENT_ETH_DISCONNECTED:
Serial.println("ETH Disconnected");
ethConnected = false;
break;
case SYSTEM_EVENT_ETH_STOP:
Serial.println("ETH Stopped");
ethConnected = false;
break;
}
}
void udpPacketEvent(AsyncUDPPacket packet) { // This event handler receives every incoming udp packet
// Two checks to validate packet structure:
// 1) Confirm packet length of 3.
//Serial.println("PACKET RECEIVED!");
if (packet.length() == 3) { // Packet length has to be 3 bytes!
// 2) Confirm packet header of 'IDCT:'
//const char header[] = {'I','D','C','T',':','\0'}; //Not this protocol
goodHeader = true;
//Serial.print("Packet length: ");
//Serial.println(packet.length());
if (goodHeader == true) {
//Serial.println("Packet OK! Yummy!");
Serial.print("Byte 1 Minutes: ");
byte byte1[2];
byte1[0] = packet.data()[0] >> 4; //
byte1[1] = packet.data()[0] & 0xf;
//newbyte = ((oldbyte << 4) & 0xf0) | ((oldbyte >> 4) & 0x0f);
String byte1string=String(byte1[1]) + String(byte1[0]);
char charbuf1[50];
byte1string.toCharArray(charbuf1, 50) ;
int minutes = atoi(charbuf1);
Serial.println(minutes);
Serial.print("Byte 2 Seconds: ");
byte byte2[2];
byte2[0] = packet.data()[1] >> 4; //
byte2[1] = packet.data()[1] & 0xf;
String byte2string=String(byte2[1]) + String(byte2[0]);
char charbuf2[50];
byte2string.toCharArray(charbuf2, 50) ;
int seconds = atoi(charbuf2);
Serial.println(seconds);
Serial.print("Byte 3 COLOR: ");
String byte3string=String(packet.data()[2], HEX);
char charbuf3[50];
byte3string.toCharArray(charbuf3, 50);
int color = atoi(charbuf3);
Serial.println(color);
Display.setTextAlignment(PA_LEFT);
Display.print("VIDEO: " + byte1string + ":" + byte2string);
//Add leading zeroes
}
} else {
goodHeader = false;
Serial.println("Packet is not 3 bytes - not for me!");
}
//debug stuff
/*
Serial.print("UDP Packet Type: ");
Serial.print(packet.isBroadcast()?"Broadcast":packet.isMulticast()?"Multicast":"Unicast");
Serial.print(", From: ");
Serial.print(packet.remoteIP());
Serial.print(":");
Serial.print(packet.remotePort());
Serial.print(", To: ");
Serial.print(packet.localIP());
Serial.print(":");
Serial.print(packet.localPort());
Serial.print(", Length: ");
Serial.print(packet.length());
Serial.print(", Data: ");
Serial.write(packet.data(), packet.length);
Serial.println();
*/
}
void setup() {
Serial.begin(115200);
// Init display:
Display.begin();
Display.setIntensity(0);
Display.displayClear();
// Init eth with a random link-local address:
randomSeed(analogRead(0));
IPAddress ip(169, 254, random(1, 255), random(1, 255));
IPAddress gateway(169, 254, 1, 1);
IPAddress subnet(255, 255, 0, 0);
IPAddress dns(169, 254, 1, 1);
WiFi.onEvent(WiFiEvent);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi ..");
while (WiFi.status() != WL_CONNECTED) {
Serial.print('.');
delay(1000);
}
Serial.println(WiFi.localIP());
//WiFi.config(ip, gateway, subnet, dns);
// Start udp listener.
if (udp.listen(udpPort)) {
Serial.print("UDP Listening on Port: ");
Serial.println(udpPort);
udp.onPacket(udpPacketEvent);
}
pinMode(buttonPin, INPUT); // get button ready for input
}
void loop() {
/*
//Display TESTS:
Display.setTextAlignment(PA_LEFT);
Display.print("ESP32");
delay(2000);
Display.setTextAlignment(PA_CENTER);
Display.print("ESP32");
delay(2000);
Display.setTextAlignment(PA_RIGHT);
Display.print("ESP32");
delay(2000);
Display.setTextAlignment(PA_CENTER);
Display.setInvert(true);
Display.print("ESP32");
delay(2000);
Display.setInvert(false);
delay(2000);
// End display tests
*/
// Execute every 100ms.
currentMillis = millis();
if (currentMillis - previousMillis >= intervalMillis) {
previousMillis = currentMillis;
// Update mode?
modeChanged = false;
opMode newMode = GetMode();
if (newMode != currentMode) {
modeChanged = true;
// Mode change!
Serial.print("Mode change: ");
Serial.print(currentMode);
Serial.print(" -> ");
Serial.println(newMode);
}
// Process mode
switch (newMode) {
case Menu :
if (buttonPressed()) {
// increase unit id
incId();
// and stay in menu mode for a bit longer
menuMillis = millis();
}
showMenu();
break;
case Init :
showInit();
break;
case No_Eth :
showNoEth();
break;
case No_Rx :
showNoRx();
break;
case Normal :
showClock();
break;
}
currentMode = newMode;
}
}
opMode GetMode() {
if (millis() < initTimeout) {
return Init; // Enter init at startup.
}
if ((millis() - menuMillis) < menuTimeout) { // Stay in menu mode until menuTimeout has elapsed.
return Menu;
} else {
if (buttonPressed()) { // Enter menu mode.
menuMillis = millis();
return Menu;
}
}
if (!ethConnected) {
return No_Eth; // No ethernet.
}
if (udpLastPacketMillis == 0) {
return No_Rx; // No id matching packet received yet.
}
if ((millis() - udpLastPacketMillis) > udpTimeout) {
return No_Rx; // Last id matching packet was too long ago.
}
return Normal;
}
void showMenu() {
// Draw id:id
}
void showInit() {
// Quick animation to check all digits then show id (from menu).
initCurrentMillis = millis();
if (initCurrentMillis - initPreviousMillis >= initIntervalMillis) {
initPreviousMillis = initCurrentMillis;
if (initCounter > 4) {
showMenu();
return;
}
//ledDisplay.writeDigitRaw(0, 0);
//ledDisplay.writeDigitRaw(1, 0);
//ledDisplay.writeDigitRaw(3, 0);
//ledDisplay.writeDigitRaw(4, 0);
//ledDisplay.writeDigitRaw(initCounter, 127);
//ledDisplay.writeDisplay();
initCounter += 1;
if (initCounter == 2) {
initCounter++; // this jumps over the colon separating minutes and seconds
}
}
}
void showNoEth() {
// Draw 'conn' to indicate we have a connection problem.
//ledDisplay.writeDigitRaw(0, 88);
//ledDisplay.writeDigitRaw(1, 92);
//ledDisplay.writeDigitRaw(3, 84);
//ledDisplay.writeDigitRaw(4, 84);
//ledDisplay.drawColon(false);
//ledDisplay.writeDisplay();
}
void showNoRx() {
// Draw '--:--' to indicate we are in a ready state, but no packets with that id are being detected.
//ledDisplay.writeDigitRaw(0, 64);
//ledDisplay.writeDigitRaw(1, 64);
//ledDisplay.writeDigitRaw(3, 64);
//ledDisplay.writeDigitRaw(4, 64);
//ledDisplay.drawColon(true);
//ledDisplay.writeDisplay();
}
void showClock() {
// Convert to base 60 & just show hours/mins if over 99m59s.
int s = udpTime % 60;
int m = floor(udpTime / 60);
unsigned long displayValue = 0;
if (udpTime <= 5999) {
displayValue = (m * 100) + s;
Serial.println(displayValue);
} else {
// Cant show the time in full so
// just show the hours and minutes.
int h = floor(m / 60);
m = m % 60;
displayValue = (h * 100) + m;
Serial.println(displayValue);
}
// Send the time value to the display.
//ledDisplay.print(displayValue, DEC);
//ledDisplay.drawColon(true);
// Add leading zeroes at positions 1 & 3 '-0:0-' if required.
}
bool buttonPressed() {
bool reply = false;
int buttonState = digitalRead(buttonPin);
if (buttonState == LOW && lastButtonState == HIGH) {
reply = true;
}
lastButtonState = buttonState;
return reply;
}
void incId() {
id += 1;
if (id > 15) {
id = 0;
}
Serial.print("NEW ID: ");
Serial.println(id);
EEPROM.write(0, id);
EEPROM.commit();
}