-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathPool-Controller.ino
executable file
·801 lines (731 loc) · 30.1 KB
/
Pool-Controller.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
// Automated Pool Controller
// v2.3
// by Jerad Jacob
// Refer to README.md for additional information and latest updates
// https://github.com/Here-Be-Dragons/Pool-Controller
// This #include statement was automatically added by the Particle IDE.
#include <photon-thermistor.h>
// This #include statement was automatically added by the Particle IDE.
#include <Adafruit_SSD1306.h> //OLED
SYSTEM_MODE(SEMI_AUTOMATIC); //Prevent blocking on setup()
SYSTEM_THREAD(ENABLED); //Prevents blocking code when offline.
////
//Start of user configurations
////
// Update the below array with the settings from
// the pump for output to the Screen, SmartThings, and
// estimating turnover/energy use. This doesn't actually
// change the speeds on the pump, it's just bookkeeping.
// speedRPM[0] must be 0 to turn the pump off
// 1200RPM is the minimum speed for chlorinator flow sensor
// 2750RPM is the recommended speed for 9 solar panels (~45GPM)
// 3450RPM for pool cleaning
uint16_t speedRPM[8] = {0,600,1200,1800,2300,2750,3000,3450};
// Manually trigger each of the above speeds and record their
// energy usage (Watts) below for accurate consumption tracking
uint16_t energyConsum[8] = {5,100,200,400,800,1100,1400,1700};
// Estimate total gallons of flow per minute for each speed
uint16_t flowCalc[8] = {0,18,35,53,68,81,88,102};
// Default length (in seconds) to run manual Overrides
// Applied if no time is specified in override
uint16_t defaultOverride = 3600;
// Time Zone offset
int16_t timeZone = -5;
// Calibration for light sensor.
// 30 is a reasonable value if the box is tightly sealed.
uint16_t dispMinBrightness = 100;
// Set the speed the pump will run for manual pool cleaning
uint8_t cleanSpeed = 8;
// Set the minimum speed the pump will run when solar is active
uint8_t minSolarSpeed = 6;
// Speed activation times in HHMM format. If two speeds have the same
// time entered, the higher speed takes precidence. Leave off any
// preceeding zeros, as they will prevent a proper match.
/////
//Winter Schedule
/////
uint16_t aSpeed1[] = {0, 1700}; // 0 RPM
uint16_t aSpeed2[] = { }; // 600 RPM
uint16_t aSpeed3[] = { 900, 1100, 1400 }; // 1200 RPM
uint16_t aSpeed4[] = { }; // 1800 RPM
uint16_t aSpeed5[] = { }; // 2300 RPM
uint16_t aSpeed6[] = { 1000, 1300 }; // 2750 RPM
uint16_t aSpeed7[] = { }; // 3000 RPM
uint16_t aSpeed8[] = { }; // 3450 RPM
/*
/////
//Summer Schedule
/////
uint16_t aSpeed1[] = {0, 1900}; // 0 RPM
uint16_t aSpeed2[] = { }; // 600 RPM
uint16_t aSpeed3[] = { 900, 1100, 1400 }; // 1200 RPM
uint16_t aSpeed4[] = { }; // 1800 RPM
uint16_t aSpeed5[] = { }; // 2300 RPM
uint16_t aSpeed6[] = { 1000, 1300 }; // 2750 RPM
uint16_t aSpeed7[] = { }; // 3000 RPM
uint16_t aSpeed8[] = { }; // 3450 RPM
*/
// Button resistances (found via manual probing, see test-buttons.ino)
// Only needs to be within +/- 15 of actual measured value to account for noise
uint16_t buttonResist1 = 2435;
uint16_t buttonResist2 = 2790;
uint16_t buttonResist3 = 3360;
uint16_t buttonResist4 = 3725;
// Voltage divider resistor values for thermistors. These need to be reasonably exact
uint16_t rPool = 10090;
uint16_t rSolar = 10040;
uint16_t rRoof = 10030;
////
//End of user configurations
////
// This value persists permanently on the Photon even after flashes/reboots
STARTUP(WiFi.selectAntenna(ANT_EXTERNAL)); // Use external antenna
//STARTUP(WiFi.selectAntenna(ANT_INTERNAL)); // Use internal antenna
time_t currentEpochTime = 0; //The current Epoch time set at the beginning of each loop in getTimes()
uint16_t currentTime = 0; //Friendly time converted from currentEpochTime via convertTime(), 10:00 PM is referenced as 2200
uint16_t previousTime = 0; //The time as of the last loop, set at the bottom of loop()
char publishString[40]; //Temporary string to use for Particle.publish of WhTally
uint16_t currentSpeed = 0; //The current motor speed setting number (1-8)
uint16_t overrideSpeed; //Stores the override speed when set manually
//uint16_t lastChange; //Time the speed was last changed via scheduler (not currently used)
uint16_t scheduledSpeed = 0; //What speed the schedule says you should be at
uint8_t manualOverride = 0; //0 for scheduled, 1 for override with schedule, 2 for indefinite override. Changes via user intervention
time_t overrideStarted; //This is set to currentEpochTime when a manual override is triggered
time_t overrideEnds = 0; //This is set to currentEpochTime + overrideLength when a manual override is triggered
uint32_t overrideLength = 0; //Recieved override length in seconds.
uint8_t lastDay; //Used for onceADay()
uint8_t lastMinute; //Used for onceAMinute()
//Variables for Photoresistor
bool isBright = 1; //Tracks whether the screen should be lit or not.
//Variables for buttons
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 25; // Adjust for jitter (if you get a false reading increase this number)
uint8_t buttonState = 0; // the current reading from the input pin
uint8_t lastButtonState = 0; // the previous reading from the input pin
uint8_t lastActionedButton = 0; // the last button actioned
//Variables for Temperature Sensors
float poolTempF;
float solarTempF;
float roofTempF;
float ambTempF;
//Variables for Solar Heating
uint8_t solarControl = 2; // Manual control of solar. 0 = override to off, 1 = override to on, 2 = Solar Controller
bool autoOverride = 1; // Changes when solar kicks on. 0 = scheduled speed, 1 = solar min. speed override
bool solarRequest; // Request from Hayward controller
//Variables for reporting
time_t previousDataPublish; //Webhook publish tracking (Epoch)
double WhTally = 0; //Daily count of Wh consumption, to upload to Google Docs for tracking
double gallonTally = 0; //Daily count of Gallons pumped, to upload to Google Docs for tracking
String sSpeed;
String sWattage;
String sFlow;
String sSolar;
String sOverride;
String sTempSolar;
String sTempPool;
String sTempRoof;
String sTempAmb;
String sButton;
String sResist;
String sIllum;
String sOverrideEnds;
String sValuesST;
String sSolarST;
//Variables for Display
uint8_t displayIndex = 0;
time_t oledOverrideUntil = 0;
uint8_t oledOverrideIndex = 0;
uint8_t oledOverrideTimer = 0;
// Assign pins to relays
// D0 reserved for SDA of OLED
// D1 reserved for SCL of OLED
#define pPumpRelay1 D2
#define pPumpRelay2 D3
#define pPumpRelay3 D4
#define pRelay4 D5
#define pSolarRelay1 D6
#define OLED_RESET WKP //Not sure what this is for, not connected
#define pLED D7
#define pButtons A1
#define pIllum A0
#define pTempSolar A2
#define pTempPool A3
#define pTempRoof A4
Adafruit_SSD1306 oled(OLED_RESET);
// For an NTC (negative temperature coefficient) thermistor only!
// CONSTRUCTOR PARAMETERS:
// 1. pin: Photon pin
// 2. seriesResistor: The resistance value of the fixed resistor (based on your hardware setup)
// 3. adcMax: The maximum analog-to-digital convert value returned by analogRead (Photon is 4095 NOT the typical Arduino 1023!)
// 4. thermistorNominal: Resistance at nominal temperature (will be documented with the thermistor, usually "10k")
// 5. temperatureNominal: Temperature for nominal resistance in celcius (will be supplied with the thermistor, assume 25 if not stated)
// 6. bCoef: Beta coefficient of the thermistor; usually 3435 or 3950 (will be documented with the thermistor)
// 7. samples: Number of analog samples to average (for smoothing)
// 8. sampleDelay: Milliseconds between analog samples (for smoothing)
Thermistor solarTherm = Thermistor(pTempSolar, rSolar, 4095, 10800, 25, 3950, 3, 5); //5 8
Thermistor poolTherm = Thermistor(pTempPool, rPool, 4095, 10900, 25, 3850, 3, 5);
Thermistor roofTherm = Thermistor(pTempRoof, rRoof, 4095, 10000, 25, 3921, 3, 5);
void setup() {
pinMode(pPumpRelay1, OUTPUT); // Pump Relay 1
pinMode(pPumpRelay2, OUTPUT); // Pump Relay 2
pinMode(pPumpRelay3, OUTPUT); // Pump Relay 3
pinMode(pRelay4, OUTPUT); // Solar Actuator Relay
//pinMode(pSolarRelay1, INPUT); // Solar panel on pulls this up to 3.3v.
pinMode(pLED, OUTPUT);//DEBUG
oled.begin(SSD1306_SWITCHCAPVCC, 0x3C); // Initialize with the I2C addr 0x3D (for the 128x64 screen)
Particle.function("mOverride", mOverride); // Listen for a manual override via remote user input for pump
Particle.function("solarCmd", solarCmd); // Listen for a manual override via remote user input for solar
Particle.variable("pumpSpeed", sSpeed);
Particle.variable("pumpWattage", sWattage);
Particle.variable("waterFlow", sFlow);
Particle.variable("solarAct", sSolar);
Particle.variable("override", sOverride);
Particle.variable("tempSolar", sTempSolar);
Particle.variable("tempPool", sTempPool);
Particle.variable("tempRoof", sTempRoof);
Particle.variable("valuesST", sValuesST); // Values to export to SmartThings Device Type
Particle.variable("solarST", sSolarST); // Values to export to SmartThings Device Type
// I have a weather station that is already sending Webhooks to DarkSky.net.
// This just listens for the response and parses out local ambient air temp.
// The weather station project is here:
Particle.subscribe("290023001047343339383037/hook-response/weather/", gotWeatherData, MY_DEVICES);
Particle.connect(); // Needed in SEMI_AUTOMATIC
for (int x = 10; x > 0; x--){ // Give it x seconds to stabilize the RTC and get a time from NTP
oled.clearDisplay(); // Clear Screen
oled.drawRect(0,0,128,64, WHITE);
oled.drawRect(1,1,127,63, WHITE);
oled.setTextColor(WHITE);
oled.setCursor(5,5);
oled.setTextSize(1);
oled.println("Pool-Controller v2.2");
oled.print(" by Jerad Jacob");
oled.setCursor(30,50);
oled.print("Startup: ");
oled.print(x);
oled.display();
delay(1000);
}
Time.setDSTOffset(1);
Time.zone(timeZone);
onceADay();
getTimes(); // Set up initial times
returnToSchedule(); // Find what the current speed should be
}
void loop() {
catchButtonPresses();
updateDisplay();
// True once every day
if (lastDay != Time.day()) {
onceADay();
}
if (lastMinute != Time.minute()) {
onceAMinute();
}
checkIllum();
getTimes();
//Check for expired manual Override
if( manualOverride == 1 ) {
if (overrideEnds <= currentEpochTime) {
returnToSchedule();
}
}
//Activate new speed if there is one
setPumpSpeed();
//Check solar state and set transformer
setSolar();
}
//Finds the current time each loop
void getTimes(){
//Gets Time-zone adjusted Epoch Time from the cloud, and converted HHMM comparison time
currentEpochTime = Time.now();
currentTime = convertTime(currentEpochTime);
}
void onceADay(){
//Manual DST logic
if( Time.month() < 3 || (Time.month() == 3 && Time.day() <=13) || (Time.month() == 11 && Time.day() >=6) || Time.month() > 11 ){
Time.endDST();
}else{
Time.beginDST();
}
lastDay = Time.day();
}
void onceAMinute(){
//Check for new scheduled speed
scheduledSpeed = findScheduledSpeed(currentTime);
getTemps();
WhTally += (double) ( energyConsum[currentSpeed-1] / 60 ); // Add 1 minute worth of kWh
gallonTally += (double) ( flowCalc[currentSpeed-1] ); // Add 1 minute worth of water flow
trackData();
lastMinute = Time.minute();
}
uint16_t convertTime(uint32_t testTime){
//Converts Epoch to HHMM
uint16_t returnTime;
returnTime = Time.hour(testTime) * 100 + Time.minute(testTime);
return returnTime;
}
uint16_t findScheduledSpeed(uint16_t atTime){ // Find the Scheduled Speed
uint16_t x = scheduledSpeed;
for (uint16_t i=0; i < sizeof(aSpeed1) / sizeof(uint16_t); i++) {
if ( aSpeed1[i] == atTime ) {
x = 1;
}
}
for (uint16_t i=0; i < sizeof(aSpeed2) / sizeof(uint16_t); i++) {
if ( aSpeed2[i] == atTime ) {
x = 2;
}
}
for (uint16_t i=0; i < sizeof(aSpeed3) / sizeof(uint16_t); i++) {
if ( aSpeed3[i] == atTime ) {
x = 3;
}
}
for (uint16_t i=0; i < sizeof(aSpeed4) / sizeof(uint16_t); i++) {
if ( aSpeed4[i] == atTime ) {
x = 4;
}
}
for (uint16_t i=0; i < sizeof(aSpeed5) / sizeof(uint16_t); i++) {
if ( aSpeed5[i] == atTime ) {
x = 5;
}
}
for (uint16_t i=0; i < sizeof(aSpeed6) / sizeof(uint16_t); i++) {
if ( aSpeed6[i] == atTime ) {
x = 6;
}
}
for (uint16_t i=0; i < sizeof(aSpeed7) / sizeof(uint16_t); i++) {
if ( aSpeed7[i] == atTime ) {
x = 7;
}
}
for (uint16_t i=0; i < sizeof(aSpeed8) / sizeof(uint16_t); i++) {
if ( aSpeed1[8] == atTime ) {
x = 8;
}
}
return x;
}
void setPumpSpeed() {
uint8_t newSpeed;
newSpeed = scheduledSpeed;
//Set Manual Override Speed to newSpeed if active
if( manualOverride != 0 ){
newSpeed = overrideSpeed;
}
//If auto override is active, set a minimum speed
if( autoOverride == 1 && newSpeed < minSolarSpeed ){
newSpeed = minSolarSpeed;
}
switch (newSpeed) {
case 1:
digitalWrite(pPumpRelay1, LOW );
digitalWrite(pPumpRelay2, LOW );
digitalWrite(pPumpRelay3, LOW );
break;
case 2:
digitalWrite(pPumpRelay1, HIGH);
digitalWrite(pPumpRelay2, LOW );
digitalWrite(pPumpRelay3, LOW );
break;
case 3:
digitalWrite(pPumpRelay1, LOW );
digitalWrite(pPumpRelay2, HIGH);
digitalWrite(pPumpRelay3, LOW );
break;
case 4:
digitalWrite(pPumpRelay1, HIGH);
digitalWrite(pPumpRelay2, HIGH);
digitalWrite(pPumpRelay3, LOW );
break;
case 5:
digitalWrite(pPumpRelay1, LOW );
digitalWrite(pPumpRelay2, LOW );
digitalWrite(pPumpRelay3, HIGH);
break;
case 6:
digitalWrite(pPumpRelay1, HIGH);
digitalWrite(pPumpRelay2, LOW );
digitalWrite(pPumpRelay3, HIGH);
break;
case 7:
digitalWrite(pPumpRelay1, LOW );
digitalWrite(pPumpRelay2, HIGH);
digitalWrite(pPumpRelay3, HIGH);
break;
case 8:
digitalWrite(pPumpRelay1, HIGH);
digitalWrite(pPumpRelay2, HIGH);
digitalWrite(pPumpRelay3, HIGH);
break;
//This condition should never happen, but default to Speed 1 if it does
default:
digitalWrite(pPumpRelay1, LOW );
digitalWrite(pPumpRelay2, LOW );
digitalWrite(pPumpRelay3, LOW );
returnToSchedule(); //Try a schedule reset
break;
}
currentSpeed = newSpeed; //Update current speed value
}
void setSolar() {
//Check Solar Controller for request to heat. Applies in setPumpSpeed()
solarRequest = digitalRead(pSolarRelay1);
if (solarControl == 0) { // Manual Override: Disable Solar
digitalWrite(pRelay4, LOW);
autoOverride = 0;
} else if (solarControl == 1) { // Manual Override: Enable Solar
digitalWrite(pRelay4, HIGH);
autoOverride = 1;
} else { // System follows ext. controller
if (solarRequest == 1){
digitalWrite(pRelay4, HIGH);
autoOverride = 1;
} else {
digitalWrite(pRelay4, LOW);
autoOverride = 0;
}
}
}
int mOverride(String command) { //Manual Trigger (SmartThings, button press, etc)
char strBuffer[63] = ""; //Max length of the Function String is 63 characters
command.toCharArray(strBuffer, 63);
overrideSpeed = atoi(strtok(strBuffer, "~"));
overrideLength = atoi(strtok(NULL, "~"));
manualOverride = atoi(strtok(NULL, "~"));
if (overrideSpeed == 0 && manualOverride == 1) { //Add the specified time to existing override
Particle.publish("Pool Debug", "adding 60 minutes to override");
overrideEnds = overrideEnds + overrideLength;
}
if (overrideSpeed <= 8 && overrideSpeed >= 1){ //These are direct speeds
if (manualOverride == 1) {
overrideStarted = currentEpochTime;
if (overrideLength == 0 ){ //Use default override length
overrideEnds = currentEpochTime + defaultOverride;
}else{
overrideEnds = currentEpochTime + overrideLength;
}
}
setPumpSpeed();
}
if( manualOverride == 0 ){ //0 is a "return to schedule"
returnToSchedule();
}
// DEBUG
String sSpeedRecieved = String(overrideSpeed);
String sTimeRecieved = String(overrideLength);
String sOverride = String(manualOverride);
Particle.publish("Speed_Recieved", sSpeedRecieved + ", " + sTimeRecieved + ", " + sOverride);
// END DEBUG
overrideLength = 0; //Reset overrideLength
trackData();
return speedRPM[currentSpeed-1];
}
int solarCmd(String command) { //Manual Trigger for Solar
solarControl = atoi(command);
setSolar();
trackData();
return poolTempF;
}
void returnToSchedule() {
scheduledSpeed = 0;
uint32_t testTimeEpoch = currentEpochTime;
uint16_t testTime;
while( scheduledSpeed == 0 ) {
testTime = convertTime(testTimeEpoch);
scheduledSpeed = findScheduledSpeed(testTime);
testTimeEpoch-=60;
}
manualOverride = 0;
overrideSpeed = 0;
overrideStarted = 0;
delay(2000);
}
// Prevent OLED burn-in by turning off the screen when
// the door is shut.
void checkIllum(){
uint16_t currentIllum = analogRead(pIllum);
if (currentIllum > dispMinBrightness) {
isBright = 1;
} else {
isBright = 0;
}
sIllum = String(currentIllum);
}
void getTemps(){
if (currentSpeed != 1) {
solarTempF = solarTherm.readTempF();
poolTempF = poolTherm.readTempF();
}
roofTempF = roofTherm.readTempF();
sTempSolar = String(solarTempF);
sTempPool = String(poolTempF);
sTempRoof = String(roofTempF);
}
void catchButtonPresses() {
// read the state of the switch into a local variable:
uint16_t reading = analogRead(pButtons);
sResist = String(reading);
uint8_t tmpButtonState = 0;
if (reading > (buttonResist1 - 15) && reading < (buttonResist1 + 15)) {
tmpButtonState = 1;
} else if (reading > buttonResist2 - 15 && reading < buttonResist2 + 15) {
tmpButtonState = 2;
} else if (reading > buttonResist3 - 15 && reading < buttonResist3 + 15) {
tmpButtonState = 3;
} else if (reading > buttonResist4 - 15 && reading < buttonResist4 + 15) {
tmpButtonState = 4;
} else {
tmpButtonState = 0; //No button is pressed;
}
// If the switch changed (due to noise OR pressing)
if (tmpButtonState != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// Assume this state is legitimate, so set it as buttonState
buttonState = tmpButtonState;
}
if (buttonState != lastActionedButton) {
switch (buttonState) {
case 1: //Display
digitalWrite(pLED, HIGH);
sButton = String("BUTTON 1: Display");
displayIndex++;
displayIndex %= 4;
break;
case 2: //Stop
digitalWrite(pLED, HIGH);
sButton = String("BUTTON 2: Stop");
if (currentSpeed != 1 || (currentSpeed == 1 && oledOverrideUntil == 0)) {
mOverride("1");
oledOverrideIndex = 1;
oledOverrideTimer = 5;
} else if (currentSpeed == 1 || oledOverrideUntil != 0) {
overrideEnds = overrideEnds + defaultOverride;
oledOverrideIndex = 1;
oledOverrideTimer = 5;
}
break;
case 3: //Clean (Speed 8)
digitalWrite(pLED, HIGH);
sButton = String("BUTTON 3: Clean");
if (currentSpeed != cleanSpeed || (currentSpeed == cleanSpeed && oledOverrideUntil == 0)) {
String i = String(cleanSpeed);
mOverride(i);
oledOverrideIndex = 2;
oledOverrideTimer = 5;
} else if (currentSpeed == cleanSpeed || oledOverrideUntil != 0) {
overrideEnds = overrideEnds + defaultOverride;
oledOverrideIndex = 2;
oledOverrideTimer = 5;
}
break;
case 4: //Resume
digitalWrite(pLED, HIGH);
sButton = String("BUTTON 4: Resume");
overrideEnds = 0;
oledOverrideIndex = 3;
oledOverrideTimer = 2;
break;
default:
digitalWrite(pLED, LOW);
sButton = String("---");
break;
}
lastActionedButton = buttonState;
}
lastButtonState = tmpButtonState;
}
void trackData(){
if (currentTime == 0) { //runs once at midnight
sprintf(publishString, "%.5f", WhTally); // Convert double WhTally to char[40] for Particle.publish()
Particle.publish("24Hr_kWh", publishString);
sprintf(publishString, "%.5f", gallonTally); // Convert double gallonTally to char[40] for Particle.publish()
Particle.publish("24Hr_gal", publishString);
WhTally = 0;
gallonTally = 0;
}
//previousTime = currentTime;
sSpeed = String(speedRPM[currentSpeed - 1]);
sWattage = String(energyConsum[currentSpeed - 1]);
sFlow = String(flowCalc[currentSpeed - 1]);
sSolar = String(autoOverride);
sOverride = String(manualOverride);
sTempSolar = String(solarTempF);
sTempPool = String(poolTempF);
sTempRoof = String(roofTempF);
sTempAmb = String(ambTempF);
sOverrideEnds = String(convertTime(overrideEnds));
sValuesST = sOverride + "~" + sOverrideEnds + "~" + String(currentSpeed) + "~" + sSpeed + "~" + sWattage + "~" + sFlow;
sSolarST = String(solarControl) + "~" + String(autoOverride) + "~" + sTempPool + "~" + sTempRoof + "~" + sTempSolar + "~" + sTempAmb;
}
void updateDisplay(){ //128x64
oled.clearDisplay();
if (isBright == 1) { //Only display if light sensor activates it
switch(displayIndex){
case 1: //Debug 1
oled.setCursor(0,0);
oled.setTextSize(1);
oled.setTextColor(WHITE);
oled.print("Time: ");
oled.println(currentTime);
oled.print("Current Speed: ");
oled.println(currentSpeed);
oled.print("Wh: ");
oled.println(WhTally);
if( manualOverride ) {
oled.println("MANUAL OVERRIDE:");
oled.print("Timer: ");
oled.println(overrideEnds - currentEpochTime);
oled.print("Scheduled Speed: ");
oled.println(scheduledSpeed);
}
if( autoOverride ) {
oled.println("AUTO OVERRIDE");
oled.print("Scheduled Speed: ");
oled.println(scheduledSpeed);
}
break;
case 2: //Debug reporting
oled.setCursor(0,0);
oled.setTextSize(1);
oled.setTextColor(WHITE);
oled.println("REPORT DEBUGGING PG.1");
oled.drawLine(0, 8, 127, 8, WHITE);
oled.setCursor(0,10);
oled.print("Spd: ");
oled.println(sSpeed);
oled.print("Wattage: ");
oled.println(sWattage);
oled.print("Flow: ");
oled.println(sFlow);
oled.print("SolarReq: ");
oled.println(sSolar);
oled.print("ManualOver: ");
oled.println(sOverride);
oled.print("illum: ");
oled.println(sIllum);
break;
case 3: //Temps
oled.setCursor(30,0);
oled.setTextSize(2);
oled.setTextColor(WHITE);
oled.println("Temps");
oled.setCursor(0,25);
oled.setTextSize(1);
oled.print("Solar Temp: ");
oled.println(sTempSolar);
oled.print("Pool Temp: ");
oled.println(sTempPool);
oled.print("Roof Temp: ");
oled.println(sTempRoof);
break;
default:
oled.drawRect(80,0,48,55, WHITE);
oled.drawRect(81,1,46,53, WHITE);
oled.setTextSize(6);
oled.setTextColor(WHITE);
oled.setCursor(88,6);
oled.print(currentSpeed);
oled.setTextSize(1);
oled.setTextColor(WHITE);
oled.setCursor(80,57);
oled.print(speedRPM[currentSpeed-1]);
oled.print(" RPM");
oled.setCursor(0,0);
oled.print("Time: ");
oled.println(currentTime);
oled.print("Wh: ");
oled.println(WhTally);
oled.print("Scheduled: ");
oled.println(scheduledSpeed);
if( manualOverride ) {
oled.println("M. OVERRIDE:");
oled.print("Spd: ");
oled.println(overrideSpeed);
oled.print("Timer: ");
oled.println(overrideEnds - currentEpochTime);
}
if( autoOverride ) oled.println("A. OVERRIDE");
break;
}
// Handle screen override logic
if (oledOverrideTimer != 0) {
oledOverrideUntil = currentEpochTime + oledOverrideTimer;
oledOverrideTimer = 0;
}
if (currentEpochTime <= oledOverrideUntil) {
switch(oledOverrideIndex){
case 1: //Speed Override
oled.clearDisplay();
oled.drawLine(0, 0, 127, 0, WHITE);
oled.drawLine(0, 1, 127, 1, WHITE);
oled.setCursor(40,10);
oled.setTextSize(2);
oled.setTextColor(WHITE);
oled.print("STOP");
oled.setCursor(1,27);
oled.setTextSize(1);
oled.print("Override Timer: ");
oled.println(overrideEnds - currentEpochTime);
oled.setCursor(1,44);
oled.print("Press \"Clean\" again");
oled.setCursor(1,50);
oled.print("to increase time...");
oled.drawLine(0, 62, 127, 62, WHITE);
oled.drawLine(0, 63, 127, 63, WHITE);
break;
case 2: //Speed Override
oled.clearDisplay();
oled.drawLine(0, 0, 127, 0, WHITE);
oled.drawLine(0, 1, 127, 1, WHITE);
oled.setCursor(15,10);
oled.setTextSize(2);
oled.setTextColor(WHITE);
oled.print("CLEANING");
oled.setCursor(1,27);
oled.setTextSize(1);
oled.print("Override Timer: ");
oled.println(overrideEnds - currentEpochTime);
oled.setCursor(1,44);
oled.print("Press \"Clean\" again");
oled.setCursor(1,50);
oled.print("to increase time...");
oled.drawLine(0, 62, 127, 62, WHITE);
oled.drawLine(0, 63, 127, 63, WHITE);
break;
case 3: //Speed Override
oled.clearDisplay();
oled.drawLine(0, 0, 127, 0, WHITE);
oled.drawLine(0, 1, 127, 1, WHITE);
oled.setCursor(12,10);
oled.setTextSize(2);
oled.setTextColor(WHITE);
oled.print("Returning ");
oled.setCursor(0,27);
oled.print(" to ");
oled.setCursor(0,44);
oled.print(" Schedule ");
oled.drawLine(0, 62, 127, 62, WHITE);
oled.drawLine(0, 63, 127, 63, WHITE);
break;
default:
//no display override
break;
}
} else {
oledOverrideUntil = 0;
}
}
oled.display();
}
void gotWeatherData(const char *name, const char *data) {
String str = String(data);
char strBuffer[400] = "";
str.toCharArray(strBuffer, 400);
ambTempF = atof(strtok(strBuffer, "~"));
}