-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLaserController.ino
976 lines (792 loc) · 30.2 KB
/
LaserController.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
#include "WiFiEsp.h"
#include <ArduinoJson.h>
#include <PubSubClient.h>
#include <TimerThree.h>
#include <arduino-timer.h>
#include "SerialStringReader.h"
#include "secrets.h"
//TODO reinit Wifi completely ever N hours?
//TODO add failed publish counter
//TODO add relay (with decent debounce) for the chiller
//TODO add variable fan control
//TODO publish messages on sleep(s) and wake
//TODO stagger wake up to limit the in-rush current?
//TODO If a job starts and we're asleep, wake up
/*
Origial protoype from:
https://forum.arduino.cc/t/troubles-connecting-esp8266-01-with-arduino-mega-2560/646624/10
Minimal wiring is:
ESP TX -----To Mega RX (Serial 1)
ESP CH_PD - To 3.3v power supply's +
ESP RST --- Can leave open (although I tie it to the 3.3v power supply's + with a 10kOhm resistor)
ESP VCC --- To 3.3v power supply's +
ESP GND --- Connect it to both the Mega's GND, and also to the 3.3v power supply's GND
GPIO2 ----- Can leave open
GPIO0 ----- Can leave open (although I tie it to the 3.3v power supply's + with a 10kOhm resistor)
ESP RX -----To Mega TX (Serial 1)
I connect the RST and GPIO0 pins to the 3.3v power supply's + with the 10kOhm resistors, so those inputs aren't floating. Since one is the Reset, and the other is the Program pin.
For initial testing, I see you are using the Arduino Mega's 3.3v pin as the power supply. This really isn't a great idea, as the ESP8266 chip can pull a bit of power when transmitting. So while it may be ok during your learning phase of just trying to the two talking to each other, you really need a separate 3.3v power supply for the ESP chip.
*/
// ********************************************************* DEFINITIONS *********************************************************
volatile bool DEBUG = false;
volatile int wifiStatus = WL_IDLE_STATUS;
volatile bool wifiInitInProgress = false;
volatile bool mqttInitInProgress = false;
const char* TRUE_STR = "true";
const char* FALSE_STR = "false";
const char* SLEEP_MODE_AWAKE = "awake";
const char* SLEEP_MODE_LIGHT = "light";
const char* SLEEP_MODE_DEEP = "deep";
const char* STARTED_STR = "Started";
const char* FINISHED_STR = "Finished";
volatile unsigned long seqNo = 0;
const int INPUT_LASER_KEY = 47;
const int INPUT_STATUS = 45;
const int INPUT_WIND = 43;
const int INPUT_IDLE = 41;
const int INPUT_ERROR = 39;
const int INPUT_FLOW_SENSOR = 37;
const int INPUT_CHILLER_SENSOR = 35;
const int INPUT_DOOR_SENSOR = 33; //closed == HIGH
const int INPUT_LASER_KEY_IDX = 0;
const int INPUT_STATUS_IDX = 1;
const int INPUT_WIND_IDX = 2;
const int INPUT_IDLE_IDX = 3;
const int INPUT_ERROR_IDX = 4;
const int INPUT_FLOW_SENSOR_IDX = 5;
const int INPUT_CHILLER_SENSOR_IDX = 6;
const int INPUT_DOOR_SENSOR_IDX = 7;
const int INPUT_PIN_COUNT = 8;
const int INPUT_PINS[] = { INPUT_LASER_KEY, INPUT_STATUS, INPUT_WIND, INPUT_IDLE,
INPUT_ERROR, INPUT_FLOW_SENSOR, INPUT_CHILLER_SENSOR, INPUT_DOOR_SENSOR };
volatile int INPUT_STATES[] = { LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW };
const int RELAY_RED_DOT_PRIMARY = 4;
const int RELAY_RED_DOT_SECONDARY = 5;
const int RELAY_LASER_POWER = 6;
const int RELAY_CHILLER = 7;
const int RELAY_AIR_PRIMARY = 8;
const int RELAY_AIR_LOW = 9;
const int RELAY_AIR_HIGH = 10;
const int RELAY_UNUSED_1 = 11;
const int RELAY_RED_DOT_PRIMARY_IDX = 0;
const int RELAY_RED_DOT_SECONDARY_IDX = 1;
const int RELAY_LASER_POWER_IDX = 2;
const int RELAY_CHILLER_IDX = 3;
const int RELAY_AIR_PRIMARY_IDX = 4;
const int RELAY_AIR_LOW_IDX = 5;
const int RELAY_AIR_HIGH_IDX = 6;
const int RELAY_UNUSED_1_IDX = 7;
const int DEFAULT_ON_DEBOUNCE_MS = 500;
const int DEFAULT_OFF_DEBOUNCE_MS = 500;
const int CHILLER_ON_DEBOUNCE_MS = 15 * 1000;
const int CHILLER_OFF_DEBOUNCE_MS = 15 * 1000;
const int LASER_ON_DEBOUNCE_MS = 5 * 1000;
const int LASER_OFF_DEBOUNCE_MS = 250;
const int RELAY_PIN_COUNT = 8;
const int RELAY_PINS[] = { RELAY_RED_DOT_PRIMARY, RELAY_RED_DOT_SECONDARY, RELAY_LASER_POWER,
RELAY_CHILLER, RELAY_AIR_PRIMARY, RELAY_AIR_LOW, RELAY_AIR_HIGH, RELAY_UNUSED_1 };
volatile int RELAY_ON_DEBOUNCE[] = { DEFAULT_ON_DEBOUNCE_MS, DEFAULT_ON_DEBOUNCE_MS, LASER_ON_DEBOUNCE_MS,
CHILLER_ON_DEBOUNCE_MS, DEFAULT_ON_DEBOUNCE_MS, DEFAULT_ON_DEBOUNCE_MS,
DEFAULT_ON_DEBOUNCE_MS, DEFAULT_ON_DEBOUNCE_MS };
volatile int RELAY_OFF_DEBOUNCE[] = { DEFAULT_OFF_DEBOUNCE_MS, DEFAULT_OFF_DEBOUNCE_MS, LASER_OFF_DEBOUNCE_MS,
CHILLER_OFF_DEBOUNCE_MS, DEFAULT_OFF_DEBOUNCE_MS, DEFAULT_OFF_DEBOUNCE_MS,
DEFAULT_OFF_DEBOUNCE_MS, DEFAULT_OFF_DEBOUNCE_MS };
volatile int RELAY_STATES[] = { HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH };
volatile unsigned long RELAY_LAST_SWITCH[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
/* Adapted from the implementation referced below
Sine fade
Produces a sinusoidal fade curve
created 30 Jan 2019
modified 9 June 2019
by Tom Igoe and lighting & interactivity 2019 class
*/
volatile int ledsFadeCurrentLevel = 255;
volatile int ledsTargetConstantLevel = 0;
volatile int ledsFadeChange;
volatile int ledDimCyclesRemaining;
volatile byte ledsLevelTable[256]; // pre-calculated PWM levels
const int LED_DRIVER_PIN = 12;
const int LED_MODE_NA = 0;
const int LED_MODE_FULL = 1;
const int LED_MODE_DIM = 2;
const int LED_MODE_OFF = 3;
const int LED_MODE_QUICK_FLASH_THEN_FULL = 4;
const int LED_MODE_SLOW_FLASH_THEN_DIM = 5;
const int LED_MODE_SLOW_FLASH_THEN_OFF = 6;
volatile int CURRENT_LED_MODE = LED_MODE_OFF;
volatile int NEW_LED_MODE = LED_MODE_FULL;
volatile boolean CURRENT_DOOR_CLOSED = true;
auto timer = timer_create_default(); // create a timer with default settings
const int LIGHT_SLEEP_IDLE_TIME = 10 * 60; //10 minutes
const int DEEP_SLEEP_IDLE_TIME = 20 * 60; //20 minutes
volatile unsigned long laserIdleStart = 0;
volatile bool inLightSleep = false;
volatile bool inDeepSleep = false;
const int POST_JOB_AIR_DELAY = 5; //seconds
volatile unsigned long jobStartTime = 0;
volatile unsigned long jobEndTime = 0;
volatile unsigned int lastJobElapsed = 0;
const int JSON_BUFFER_SIZE = 500;
char JSON_BUFFER[JSON_BUFFER_SIZE];
WiFiEspClient espClient;
PubSubClient mqttClient(espClient);
//SerialStringReader
SerialStringReader serialCommunicator;
Stream& SerialInput = Serial;
Stream& SerialOutput = Serial;
//SerialStringReader
void setup() {
Serial.begin(115200); // initialize serial 0 for debugging
Serial1.begin(115200); // initialize serial 1 for ESP module
Serial.println("--- Setup Started ---");
Serial.println("Initializing replay pin states");
//Set default pin state leveraging the internal resistors
for (int i = 0; i < RELAY_PIN_COUNT; i++) {
pinMode(RELAY_PINS[i], INPUT_PULLUP);
pinMode(RELAY_PINS[i], OUTPUT);
}
Serial.println("Initializing input pin states");
for (int i = 0; i < INPUT_PIN_COUNT; i++) {
pinMode(INPUT_PINS[i], INPUT);
}
//Set default pin state leveraging the internal resistors
pinMode(LED_DRIVER_PIN, INPUT);
pinMode(LED_DRIVER_PIN, OUTPUT);
pinMode(LED_BUILTIN, OUTPUT);
fillLEDSineLevelTable();
serialCommunicator.setup();
initInterruptTimers();
initLoopTimers();
laserIdleStart = millis();
Serial.println("--- Setup Completed ---");
}
void initLoopTimers() {
Serial.println("--- Initializing timers ---");
timer.cancel(); //Cancel all timers
//Offset timers a bit so no single loop() execution performed too much work all at once
//"timer jitter"
// report laser status ever 1 second(s)
timer.every(1000, publishStatusMessage);
//init the WiFi immeiately / don't wait for the timer to kick off the init
timer.in(1500, initialInitWiFi);
// validate wifi status and reinit if necessary every ~30 seconds
timer.every(61000, initWiFi);
// validate mqtt status and reinit if necessary every ~5 seconds
timer.every(5100, initMQTT);
// evaluate laser idle stake and wake/sleep as necessary
timer.every(1000, evaluateLaserIdleState);
Serial.print("--- Timers initialized: ");
Serial.print(timer.size());
Serial.println(" ---");
}
void initInterruptTimers() {
Timer3.initialize(5000); //5ms
Timer3.attachInterrupt(ledTick);
}
//TODO CHRIS - send message on input change?
// ********************************************************* MAIN LOOP *********************************************************
void loop() {
readInputStates();
evaluateState();
timer.tick(); // tick the timer
serialCommunicator.loop();
if (serialCommunicator.messageReceived()) {
processSerialInput(serialCommunicator.getMessage());
}
}
bool initialInitWiFi(void*) {
if (DEBUG) {
Serial.println("--- Starting initialInitWiFi ---");
}
if (wifiInitInProgress) {
Serial.println("Exiting initialInitWiFi as init is already in progress");
return true;
}
WiFi.init(&Serial1);
initWiFi(nullptr);
if (DEBUG) {
Serial.println("--- Initialization execution started ---");
}
return true;
}
bool initWiFi(void*) {
if (wifiInitInProgress) {
Serial.println("Exiting initWiFi as init is already in progress");
return true;
}
if (DEBUG) {
Serial.println("Checking WiFi status...");
}
wifiStatus = WiFi.status();
if (wifiStatus == WL_CONNECTED) {
if (DEBUG) {
Serial.println("WiFi still connected!");
}
return true;
}
if (wifiStatus == WL_NO_SHIELD) {
Serial.println("WiFi module not detected");
return true;
}
Serial.print("WiFi not connected. Attempting to connect WiFi to SSID: ");
Serial.println(WIFI_SSID);
wifiStatus = WiFi.begin(WIFI_SSID, WIFI_PASS);
wifiInitInProgress = true;
Serial.println("Starting WiFi checkWifiInitStatus timer...");
timer.every(500, checkWifiInitStatus);
return true;
}
//TODO add max retries or retry wait time
bool checkWifiInitStatus(void*) {
Serial.println("Checking WiFi initialization status...");
wifiStatus = WiFi.status();
if (wifiStatus == WL_CONNECTED) {
Serial.println("WiFi connected!");
wifiInitInProgress = false;
Serial.println();
printCurrentNet();
printWifiData();
Serial.println();
return false; //Stop timer
}
return true; //Keep the timer going
}
void printWifiData() {
// *** print your WiFi shield's IP address
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
byte mac[6];
WiFi.macAddress(mac);
char buf[20];
sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X", mac[5], mac[4], mac[3], mac[2], mac[1], mac[0]);
Serial.print("MAC address: ");
Serial.println(buf);
}
// Print WiFi connection details to serial Monitor --------------------------------------------------------
void printCurrentNet() {
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// *** print the MAC address of the router you're attached to
byte bssid[6];
WiFi.BSSID(bssid);
char buf[20];
sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X", bssid[5], bssid[4], bssid[3], bssid[2], bssid[1], bssid[0]);
Serial.print("BSSID: ");
Serial.println(buf);
long rssi = WiFi.RSSI();
Serial.print("Signal strength (RSSI): ");
Serial.println(rssi);
}
bool initMQTT(void*) {
if (wifiInitInProgress || mqttInitInProgress) {
Serial.println("Exiting initialInitWiFi as init is already in progress");
return true;
}
if (wifiStatus != WL_CONNECTED) {
Serial.println("WiFi not connected, aborting MQTT connection attempt");
return true;
}
if (mqttClient.connected()) {
if (DEBUG) {
Serial.println("MQTT still connected!");
}
return true;
}
Serial.print("MQTT not connected. A new connection to ");
Serial.print(MQTT_SERVER);
Serial.print(":");
Serial.print(MQTT_PORT);
Serial.println(" will be attempted");
mqttClient.setServer(MQTT_SERVER, MQTT_PORT);
mqttClient.setBufferSize(2048);
mqttInitInProgress = true;
Serial.println("Starting MQTT attemptMQTTConnection timer...");
timer.every(500, attemptMQTTConnection);
return true;
}
//TODO add max retries or retry wait time
bool attemptMQTTConnection(void*) {
Serial.print("Initializing MQTT connection...");
if (mqttClient.connect(MQTT_CLIENT_NAME)) {
Serial.println("MQTT connected!");
mqttInitInProgress = false;
publishLogMessage("Successfully started and connected to WiFi/MQTT");
return false; //Stop timer
} else {
Serial.print("MQTT connection failed, State=");
Serial.println(mqttClient.state());
return true; //Keep the timer going
}
}
void readInputStates() {
INPUT_STATES[INPUT_LASER_KEY_IDX] = getInputPinState(INPUT_LASER_KEY);
INPUT_STATES[INPUT_STATUS_IDX] = getInputPinState(INPUT_STATUS);
INPUT_STATES[INPUT_WIND_IDX] = getInputPinState(INPUT_WIND);
INPUT_STATES[INPUT_IDLE_IDX] = getInputPinState(INPUT_IDLE);
INPUT_STATES[INPUT_ERROR_IDX] = getInputPinState(INPUT_ERROR);
INPUT_STATES[INPUT_FLOW_SENSOR_IDX] = getInputPinState(INPUT_FLOW_SENSOR);
INPUT_STATES[INPUT_CHILLER_SENSOR_IDX] = getInputPinState(INPUT_CHILLER_SENSOR);
INPUT_STATES[INPUT_DOOR_SENSOR_IDX] = getInputPinState(INPUT_DOOR_SENSOR);
}
void evaluateState() {
const unsigned long now = millis();
bool chillerFlowSensorState = INPUT_STATES[INPUT_FLOW_SENSOR_IDX] == HIGH;
bool chillerSensorState = INPUT_STATES[INPUT_CHILLER_SENSOR_IDX] == HIGH;
bool doorClosedSensorState = INPUT_STATES[INPUT_DOOR_SENSOR_IDX] == HIGH;
bool statusSensorState = INPUT_STATES[INPUT_STATUS_IDX] == HIGH;
bool windSensorState = INPUT_STATES[INPUT_WIND_IDX] == HIGH;
bool existingPrimaryRedDotState = isRelayEnabled(RELAY_RED_DOT_PRIMARY_IDX);
bool existingSecondaryRedDotState = isRelayEnabled(RELAY_RED_DOT_SECONDARY_IDX);
bool existingLaserPowerState = isRelayEnabled(RELAY_LASER_POWER_IDX);
bool existingChillerState = isRelayEnabled(RELAY_CHILLER_IDX);
bool existingAirPrimaryState = isRelayEnabled(RELAY_AIR_PRIMARY_IDX);
bool existingAirLowState = isRelayEnabled(RELAY_AIR_LOW_IDX);
bool existingAirHighState = isRelayEnabled(RELAY_AIR_HIGH_IDX);
bool newPrimaryRedDotState = existingPrimaryRedDotState;
bool newSecondaryRedDotState = existingSecondaryRedDotState;
bool newLaserPowerState = existingLaserPowerState;
bool newChillerState = existingChillerState;
bool newAirPrimaryState = existingAirPrimaryState;
bool newAirLowState = existingAirLowState;
bool newAirHighState = existingAirHighState;
newPrimaryRedDotState = !inLightSleep;
newSecondaryRedDotState = !inLightSleep;
newChillerState = !inLightSleep; //TODO CHRIS add delay for turning on to limit in-rush current?
//TODO CHRIS ADD chillerFlowSensorState - disable if not OK
//TODO CHRIS ADD chillerSensorState - disable if not OK
//TODO on any other noticable change, wakeLaser() ? Any way to wake it via lightburn?
if (!statusSensorState && jobStartTime != 0) {
jobEndTime = now;
lastJobElapsed = getJobElapsedTime();
//TODO CHRIS make more useful
publishLogMessage(FINISHED_STR);
}
if (statusSensorState || windSensorState) {
if (jobStartTime == 0) {
jobStartTime = now;
jobEndTime = 0;
//TODO CHRIS make more useful
publishLogMessage(STARTED_STR);
}
} else {
jobStartTime = 0;
}
//Laser power supply
if (!inLightSleep && INPUT_STATES[INPUT_LASER_KEY_IDX] == HIGH) {
newLaserPowerState = true;
} else {
newLaserPowerState = false;
}
//TODO CHRIS Depending on idle state, keep the air on for a while longer
//Air - low
if (!inLightSleep && (statusSensorState || windSensorState)) {
newAirPrimaryState = true;
newAirLowState = true;
} else {
const unsigned long airDelayMs = POST_JOB_AIR_DELAY * 1000;
if (now >= (jobEndTime + airDelayMs)) {
newAirPrimaryState = false;
//Leave the low air open longer to drain the system
if (now >= (jobEndTime + airDelayMs + 4000)) {
newAirLowState = false;
}
}
}
//Air high
if (!inLightSleep && windSensorState) {
newAirHighState = true;
} else {
newAirHighState = false;
}
/* Apply new states */
if (newPrimaryRedDotState != existingPrimaryRedDotState)
setRelayState(RELAY_RED_DOT_PRIMARY_IDX, RELAY_RED_DOT_PRIMARY, newPrimaryRedDotState);
if (newSecondaryRedDotState != existingSecondaryRedDotState)
setRelayState(RELAY_RED_DOT_SECONDARY_IDX, RELAY_RED_DOT_SECONDARY, newSecondaryRedDotState);
if (newLaserPowerState != existingLaserPowerState)
setRelayState(RELAY_LASER_POWER_IDX, RELAY_LASER_POWER, newLaserPowerState);
if (newChillerState != existingChillerState)
setRelayState(RELAY_CHILLER_IDX, RELAY_CHILLER, newChillerState);
if (newAirPrimaryState != existingAirPrimaryState)
setRelayState(RELAY_AIR_PRIMARY_IDX, RELAY_AIR_PRIMARY, newAirPrimaryState);
if (newAirHighState != existingAirHighState)
setRelayState(RELAY_AIR_HIGH_IDX, RELAY_AIR_HIGH, newAirHighState);
if (newAirLowState != existingAirLowState)
setRelayState(RELAY_AIR_LOW_IDX, RELAY_AIR_LOW, newAirLowState);
if (statusSensorState) {
wakeLaser(); //Keeps laserIdleStart moving forward to indicate the laser is not idle
}
//Force awake (if needed) when a job is running or when we detect a door state change
if (doorClosedSensorState != CURRENT_DOOR_CLOSED) {
wakeLaser();
}
//Update door state
CURRENT_DOOR_CLOSED = doorClosedSensorState;
}
unsigned int getJobElapsedTime() {
if (jobStartTime == 0) {
return 0;
}
return (millis() - jobStartTime) / 1000;
}
const char* getSleepModeStr() {
if (inDeepSleep) {
return SLEEP_MODE_DEEP;
} else if (inLightSleep) {
return SLEEP_MODE_LIGHT;
} else {
return SLEEP_MODE_AWAKE;
}
}
int getInputPinState(int pinNumber) {
return digitalRead(pinNumber);
}
const char* boolToBooleanStr(bool value) {
if (value == HIGH) {
return TRUE_STR;
}
return FALSE_STR;
}
const char* digitalValueToBooleanStr(int value) {
if (value == HIGH) {
return TRUE_STR;
}
return FALSE_STR;
}
bool isRelayEnabled(int arrayIndex) {
return RELAY_STATES[arrayIndex] == LOW;
}
void setRelayState(int arrayIndex, int pinNumber, bool state) {
const unsigned long now = millis();
const unsigned long lastRelaySwitch = RELAY_LAST_SWITCH[arrayIndex];
const unsigned long timeDelta = now - lastRelaySwitch;
int relayDebounce;
if (state) {
relayDebounce = RELAY_ON_DEBOUNCE[arrayIndex];
} else {
relayDebounce = RELAY_OFF_DEBOUNCE[arrayIndex];
}
if (timeDelta < relayDebounce) {
return;
}
RELAY_LAST_SWITCH[arrayIndex] = now;
if (state) {
if (RELAY_STATES[arrayIndex] == HIGH) {
digitalWrite(pinNumber, LOW);
RELAY_STATES[arrayIndex] = LOW;
}
} else {
if (RELAY_STATES[arrayIndex] == LOW) {
digitalWrite(pinNumber, HIGH);
RELAY_STATES[arrayIndex] = HIGH;
}
}
}
char* generateStatusJSON() {
JsonDocument doc;
doc["seqNo"] = ++seqNo;
doc["idleTime"] = getIdleTime();
// doc["jobStartTime"] = jobStartTime;
// doc["jobEndTime"] = jobEndTime;
doc["jobElapsedTime"] = getJobElapsedTime();
doc["lastJobElapsedTime"] = lastJobElapsed;
doc["sleepMode"] = getSleepModeStr();
doc["lightsMode"] = CURRENT_LED_MODE; //TODO CHRIS translate to string
doc["lightsLevel"] = ledsFadeCurrentLevel;
JsonObject inputs = doc["inputs"].to<JsonObject>();
inputs["laserKey"] = digitalValueToBooleanStr(INPUT_STATES[INPUT_LASER_KEY_IDX]);
inputs["status"] = digitalValueToBooleanStr(INPUT_STATES[INPUT_STATUS_IDX]);
inputs["wind"] = digitalValueToBooleanStr(INPUT_STATES[INPUT_WIND_IDX]);
inputs["idle"] = digitalValueToBooleanStr(INPUT_STATES[INPUT_IDLE_IDX]);
inputs["error"] = digitalValueToBooleanStr(INPUT_STATES[INPUT_ERROR_IDX]);
inputs["flowSensor"] = digitalValueToBooleanStr(INPUT_STATES[INPUT_FLOW_SENSOR_IDX]);
inputs["chillerSensor"] = digitalValueToBooleanStr(INPUT_STATES[INPUT_CHILLER_SENSOR_IDX]);
inputs["doorClosed"] = digitalValueToBooleanStr(INPUT_STATES[INPUT_DOOR_SENSOR_IDX]);
JsonObject relays = doc["relays"].to<JsonObject>();
relays["primaryRedDot"] = digitalValueToBooleanStr(isRelayEnabled(RELAY_RED_DOT_PRIMARY_IDX));
relays["secondaryRedDot"] = digitalValueToBooleanStr(isRelayEnabled(RELAY_RED_DOT_SECONDARY_IDX));
relays["laser"] = digitalValueToBooleanStr(isRelayEnabled(RELAY_LASER_POWER_IDX));
relays["chiller"] = digitalValueToBooleanStr(isRelayEnabled(RELAY_CHILLER_IDX));
relays["airPrimary"] = digitalValueToBooleanStr(isRelayEnabled(RELAY_AIR_PRIMARY_IDX));
relays["airLow"] = digitalValueToBooleanStr(isRelayEnabled(RELAY_AIR_LOW_IDX));
relays["airHigh"] = digitalValueToBooleanStr(isRelayEnabled(RELAY_AIR_HIGH_IDX));
serializeJson(doc, JSON_BUFFER, JSON_BUFFER_SIZE);
return JSON_BUFFER;
}
template<typename StringType>
bool publishLogMessage(StringType logMsg) {
JsonDocument doc;
doc["message"] = logMsg;
serializeJson(doc, JSON_BUFFER, JSON_BUFFER_SIZE);
if (DEBUG) {
Serial.println(JSON_BUFFER);
}
publishMessage(JSON_BUFFER);
return true;
}
bool publishStatusMessage(void*) {
//char* msg = generateStatusJSON();
//publishMessage(msg);
generateStatusJSON();
if (DEBUG) {
Serial.println(JSON_BUFFER);
}
publishMessage(JSON_BUFFER);
return true; //Keep the timer going
}
bool publishMessage(char* msg) {
if (wifiStatus != WL_CONNECTED) {
if (DEBUG) {
Serial.println("Skipping MQTT publish as WiFi client is not connected");
}
return false;
}
if (!mqttClient.connected()) {
if (DEBUG) {
Serial.println("Skipping MQTT publish as MQTT client is not connected");
}
return false;
}
unsigned long start;
if (DEBUG) {
Serial.println("Publishing MQTT message...");
start = millis();
}
bool publishSucces = mqttClient.publish(MQTT_TOPIC, msg);
if (DEBUG) {
unsigned long elapsed = millis() - start;
Serial.print("Publish success: ");
Serial.print(boolToBooleanStr(publishSucces));
Serial.print(", Publish elapsed: ");
Serial.println(elapsed);
}
return true;
}
//TODO CHRIS add simulated sleep (set the time manually)
void processSerialInput(char* command) {
if (strcmp(command, "help") == 0) {
printHelpMenu();
} else if (strcmp(command, "diagnostics") == 0) {
Serial.println("Calling printDiagnosticsInfo...");
printDiagnosticsInfo();
} else if (strcmp(command, "resetlooptimers") == 0) {
Serial.println("Calling initLoopTimers...");
initLoopTimers();
} else if (strcmp(command, "connectmqtt") == 0) {
Serial.println("Calling initMQTT...");
initMQTT(nullptr);
} else if (strcmp(command, "connectwifi") == 0) {
Serial.println("Calling initWiFi...");
initWiFi(nullptr);
} else if (strcmp(command, "reinitwifi") == 0) {
Serial.println("Calling initialInitWiFi...");
initialInitWiFi(nullptr);
} else if (strcmp(command, "toggledebug") == 0) {
DEBUG = !DEBUG;
Serial.print("New DEBUG value: ");
Serial.println(boolToBooleanStr(DEBUG));
} else if (strcmp(command, "publishstatus") == 0) {
Serial.println("Calling publishStatusMessage...");
publishStatusMessage(nullptr);
} else if (strcmp(command, "ledsoff") == 0) {
Serial.println("Turning off LEDs...");
NEW_LED_MODE = LED_MODE_OFF;
} else if (strcmp(command, "ledsfull") == 0) {
Serial.println("Turning on LEDs (Full)...");
NEW_LED_MODE = LED_MODE_FULL;
} else if (strcmp(command, "ledsdim") == 0) {
Serial.println("Turning on LEDs (Dim)...");
NEW_LED_MODE = LED_MODE_DIM;
} else if (strcmp(command, "ledsflashfull") == 0) {
Serial.println("Turning on LEDs (Flash then Full)...");
NEW_LED_MODE = LED_MODE_QUICK_FLASH_THEN_FULL;
} else if (strcmp(command, "ledsflashdim") == 0) {
Serial.println("Turning on LEDs (Flash then Dim)...");
NEW_LED_MODE = LED_MODE_SLOW_FLASH_THEN_DIM;
} else if (strcmp(command, "ledsflashoff") == 0) {
Serial.println("Turning on LEDs (Flash then Off)...");
NEW_LED_MODE = LED_MODE_SLOW_FLASH_THEN_OFF;
}
}
void printHelpMenu() {
Serial.println();
Serial.println("===== Help / Supported Commands =====");
Serial.println();
Serial.println(" diagnostics");
Serial.println(" resetlooptimers");
Serial.println(" connectmqtt");
Serial.println(" connectwifi");
Serial.println(" reinitwifi");
Serial.println(" toggledebug");
Serial.println(" publishstatus");
Serial.println(" ledsoff");
Serial.println(" ledsfull");
Serial.println(" ledsdim");
Serial.println(" ledsflashfull");
Serial.println(" ledsflashdim");
Serial.println();
Serial.println("=====================================");
Serial.println();
}
void printDiagnosticsInfo() {
checkWifiInitStatus(nullptr); //Update WiFi status
Serial.println();
Serial.println("====== Diagnostics Information ======");
Serial.println();
//TODO CHRIS add sleep levels
Serial.print(" DEBUG flag: ");
Serial.println(boolToBooleanStr(DEBUG));
Serial.print(" WiFi connected: ");
Serial.println(boolToBooleanStr(wifiStatus == WL_CONNECTED));
Serial.print(" MQTT connected: ");
Serial.println(boolToBooleanStr(mqttClient.connected()));
Serial.print(" Active loop timers: ");
Serial.println(timer.size());
Serial.print(" Current LED mode: ");
Serial.println(CURRENT_LED_MODE);
Serial.print(" Current LED fade level: ");
Serial.println(ledsFadeCurrentLevel);
Serial.print(" Latest sequence number: ");
Serial.println(seqNo);
Serial.println();
Serial.println("=====================================");
Serial.println();
}
unsigned long getIdleTime() {
if (laserIdleStart <= 0)
return 0;
return (millis() - laserIdleStart) / 1000;
}
bool evaluateLaserIdleState(void*) {
if (laserIdleStart <= 0)
return true; //Keep the timer going in the event of a init race condition
unsigned long idleTime = getIdleTime();
if (!inLightSleep && (idleTime >= LIGHT_SLEEP_IDLE_TIME)) {
publishLogMessage("Forcing light sleep");
publishStatusMessage(nullptr);
lightSleepLaser();
}
if (!inDeepSleep && (idleTime >= DEEP_SLEEP_IDLE_TIME)) {
publishLogMessage("Forcing deep sleep");
publishStatusMessage(nullptr);
deepSleepLaser();
}
return true;
}
void lightSleepLaser() {
inLightSleep = true;
//NEW_LED_MODE = LED_MODE_SLOW_FLASH_THEN_DIM;
//TODO CHRIS UNTIL WE GET THE POWER SUPPLY HUMMING UNDER CONTROL
NEW_LED_MODE = LED_MODE_OFF;
//TODO CHRIS DEBUG
Serial.println("Light sleep");
}
void deepSleepLaser() {
inDeepSleep = true;
NEW_LED_MODE = LED_MODE_OFF;
//TODO CHRIS DEBUG
Serial.println("Deep sleep");
}
void wakeLaser() {
laserIdleStart = millis();
if (!inLightSleep && !inDeepSleep)
return;
//TODO CHRIS DEBUG
Serial.println("Waking laser");
publishLogMessage("Waking laser");
NEW_LED_MODE = LED_MODE_FULL;
inLightSleep = false;
inDeepSleep = false;
//TODO CHRIS DEBUG
Serial.println("Woke laser");
}
void ledTick() {
int currentMode;
int newMode;
int newLedsFadeCurrentLevel;
noInterrupts();
currentMode = CURRENT_LED_MODE;
newMode = NEW_LED_MODE;
if (newMode != LED_MODE_NA) {
//Set new mode states
switch (newMode) {
case LED_MODE_FULL:
ledsTargetConstantLevel = 0;
ledsFadeChange = 1;
break;
case LED_MODE_DIM:
ledsTargetConstantLevel = 210;
ledsFadeChange = 1;
break;
case LED_MODE_OFF:
ledsTargetConstantLevel = 255;
ledsFadeChange = 5;
break;
case LED_MODE_QUICK_FLASH_THEN_FULL:
ledsFadeChange = 5;
ledDimCyclesRemaining = 10;
break;
case LED_MODE_SLOW_FLASH_THEN_DIM:
ledsFadeChange = 1;
ledDimCyclesRemaining = 5;
break;
case LED_MODE_SLOW_FLASH_THEN_OFF:
ledsFadeChange = 1;
ledDimCyclesRemaining = 5;
break;
}
//Reset 'new' state
currentMode = CURRENT_LED_MODE = newMode;
newMode = NEW_LED_MODE = LED_MODE_NA;
}
if (currentMode == LED_MODE_QUICK_FLASH_THEN_FULL
|| currentMode == LED_MODE_SLOW_FLASH_THEN_DIM
|| currentMode == LED_MODE_SLOW_FLASH_THEN_OFF) {
if (ledsFadeCurrentLevel <= 0 || ledsFadeCurrentLevel >= 255) {
ledsFadeChange = -ledsFadeChange;
ledDimCyclesRemaining -= 1;
if (ledDimCyclesRemaining <= 0) {
if (currentMode == LED_MODE_SLOW_FLASH_THEN_DIM) {
NEW_LED_MODE = LED_MODE_DIM;
} else if (currentMode == LED_MODE_SLOW_FLASH_THEN_OFF) {
NEW_LED_MODE = LED_MODE_OFF;
} else {
NEW_LED_MODE = LED_MODE_FULL;
}
}
}
ledsFadeCurrentLevel += ledsFadeChange;
} else {
//fade towards the target
if (ledsTargetConstantLevel < ledsFadeCurrentLevel) {
ledsFadeCurrentLevel -= ledsFadeChange;
ledsFadeCurrentLevel = max(ledsFadeCurrentLevel, ledsTargetConstantLevel);
} else if (ledsTargetConstantLevel > ledsFadeCurrentLevel) {
ledsFadeCurrentLevel += ledsFadeChange;
ledsFadeCurrentLevel = min(ledsFadeCurrentLevel, ledsTargetConstantLevel);
}
}
if (ledsFadeCurrentLevel < 0) {
ledsFadeCurrentLevel = 0;
} else if (ledsFadeCurrentLevel > 255) {
ledsFadeCurrentLevel = 255;
}
newLedsFadeCurrentLevel = ledsFadeCurrentLevel; //local var
interrupts();
//PWM output the result:
byte newLevel = ledsLevelTable[newLedsFadeCurrentLevel];
analogWrite(LED_DRIVER_PIN, newLevel);
analogWrite(LED_BUILTIN, newLevel);
}
void fillLEDSineLevelTable() {
// set the range of values:
float maxValue = 255;
// iterate over the array and calculate the right value for it:
for (int l = 0; l <= maxValue; l++) {
// map input to a 0-179 range:
float angle = map(l, 0, maxValue, 0, 179);
float lightLevel = (sin((angle * PI / 180) + PI / 2) + 1) * 127.5;
ledsLevelTable[l] = lightLevel;
}
}