forked from UlrichMai/MaiPureSpaController
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMaiPureSpaController.ino
950 lines (829 loc) · 29.5 KB
/
MaiPureSpaController.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
/*
* MaiPureSpaController.ino
* the pool controller sketch
*
* Created on: 2020-05-16
* Author: Ulrich Mai
*
* Updated on: 2021-01-23
* Author: James Gibbard
*/
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <ESP8266WebServer.h>
#include <ArduinoOTA.h>
#include <ArduinoQueue.h>
#include <PubSubClient.h>
#define MQTT_CLIENT "Hot_Tub_Controller" // mqtt client_id
#define MQTT_SERVER "192.168.0.2" // mqtt server
#define MQTT_PORT 1883 // mqtt port
#define MQTT_TOPIC "home/hot_tub_controller" // mqtt topic
#define MQTT_USER "ha-sonoff" // mqtt user
#define VERSION "\n\n-------------- Intex Spa WiFi Controller v1.01pOTA --------------"
boolean OTAupdate = false; // (Do not Change)
boolean requestRestart = false; // (Do not Change)
int kUpdFreq = 1; // Update frequency in Mintes to check for mqtt connection
int kRetries = 50; // WiFi retry count. Increase if not connecting to router.
unsigned long TTasks; // (Do not Change)
WiFiClient wifiClient;
PubSubClient mqttClient(wifiClient, MQTT_SERVER, MQTT_PORT);
char hostname[] = "HotTub_XXXXXX";
void generateHostname() {
uint8_t mac[WL_MAC_ADDR_LENGTH];
WiFi.macAddress(mac);
snprintf(hostname, 11, "HotTub_%02X%02X%02X", mac[3], mac[4], mac[5]);
}
/******************************************************************************************/
// Software SPI Client catching Intex pool controller input
// GPIO Pins
const int CLK = D7;
const int LAT = D6;
const int DAT_IN = D5;
const int DAT_OUT = D0; //emulate button press
const int BUZ = D2; //out=buzzer beep, low=shut off; in=buzzer works normal,
#define GPIO_IN ((volatile uint32_t*) 0x60000318) //GPIO Read Register
#define GPIO_OUT ((volatile uint32_t*) 0x60000300) //GPIO Write Register
// 16-Bit Shift Register,
volatile uint8_t clkCount = 0;
volatile uint16_t buf = 0;
bool flushSync = false; // True when display reset, used to sync buffer
char digit[5] = "????";
int curTempTmp; // current temperature candidate
bool curTempTmpValid = false; // current temperature candidate is valid / has not timed out
int curTemp = 20; // current temperature
int setTemp = 38; // target temperature
int lstTemp; // last valid display reading
bool BuzzerEnabled = false; // (Do not Change) Internal buzzer state
bool mqttLastBuzzerState = false; // (Do not Change) Internal buzzer state
bool powerState = false; // (Do not Change) Internal power state
bool pumpState = false; // (Do not Change) Internal pump state
bool bubbleState = false; // (Do not Change) Internal bubble state
bool heaterState = false; // (Do not Change) Internal heater state
bool heatingState = false; // (Do not Change) Internal heating state
int targetTemp = setTemp; // (Do not Change) Internal target temp state
int currentTemp = curTemp; // (Do not Change) Internal current temp state
enum ButtonT {
BTN_POWER = 0,
BTN_UP = 1,
BTN_DOWN = 2,
BTN_FILTER = 3,
BTN_HEATER = 4,
BTN_BUBBLE = 5,
BTN_FC = 6
};
volatile bool btnRequest[7] = {};
volatile uint8_t btnCount[7] = {};
const int btnCycles = 10;
bool btnPulse = false;
// Prototypen
void writeButton(ButtonT button);
void handleButton(ButtonT button);
void handleWebButton(ButtonT button);
//Just clock the data bits into the buffer
ICACHE_RAM_ATTR void SPI_handleClock() {
clkCount++;
buf = buf << 1; // Shift buffer along
if (bitRead(*GPIO_IN, DAT_IN) == 1) bitSet(buf, 0); // Flip data bit in buffer if needed.
}
ICACHE_RAM_ATTR void SPI_handleLatch() {
if (!simulateButtonPress()) {
if (flushSync) {
if (clkCount == 16) {
// Valid if 16 clock cycles detected since last latch
if (false);
else if (bitRead(buf, 6) == 0) readSegment(0);
else if (bitRead(buf, 5) == 0) readSegment(1);
else if (bitRead(buf, 11) == 0) readSegment(2);
else if (bitRead(buf, 2) == 0) readSegment(3);
else if (bitRead(buf, 14) == 0) readLEDStates();
}
// reset buffer
flushSync = false;
buf = 0;
clkCount = 0;
} else {
if ((buf | 0xF00) == 0xFFFF) { //If idle, we can use to mark a sync.
flushSync = true;
buf = 0;
clkCount = 0;
}
}
}
}
ICACHE_RAM_ATTR void readSegment(int seg) {
uint16_t d = buf & 13976; // mask and keep segment bits
if (d == 16) digit[seg]='0';
else if (d == 9368) digit[seg]='1';
else if (d == 520) digit[seg]='2';
else if (d == 136) digit[seg]='3';
else if (d == 9344) digit[seg]='4';
else if (d == 4224) digit[seg]='5';
else if (d == 4096) digit[seg]='6';
else if (d == 1176) digit[seg]='7';
else if (d == 0) digit[seg]='8';
else if (d == 128) digit[seg]='9';
else if (d == 1152) digit[seg]='9';
else if (d == 4624) digit[seg]='C';
else if (d == 5632) digit[seg]='F';
else if (d == 4608) digit[seg]='E';
else if (d == 13976) digit[seg]=' '; //blank
// if this is the last digit, decide what temp value this is
if (seg == 3) classifyTemperature();
}
const int reqCycles = 90;
int dispCycles = reqCycles; //non blank display cycles
ICACHE_RAM_ATTR void classifyTemperature() {
if (digit[0] != ' ') { // non blank display
// remember the last valid temp reading
lstTemp = atoi(digit);
if (--dispCycles < 0) {
// temperature, that is not followed by an empty display for 90 (>82) cycles, is the current temperature
if (validTempValue(curTempTmp) && (curTempTmpValid && curTemp != curTempTmp)) {
curTemp = curTempTmp;
Serial.println("Current Temp?: " + String(curTemp));
current_temperature_changed_event(curTemp);
}
dispCycles = reqCycles;
curTempTmp = lstTemp;
curTempTmpValid = true;
}
}
else { // blank display during blinking
// temperature before an blank display is the target temperature
if (validTempValue(lstTemp) && (setTemp != lstTemp)) {
setTemp = lstTemp;
Serial.println("Target Temp?: " + String(setTemp));
target_temperature_changed_event(setTemp);
}
dispCycles = reqCycles;
curTempTmpValid = false;
}
}
ICACHE_RAM_ATTR bool validTempValue(int value) {
return (value >= 20 && value <= 40);
}
enum Led {
LED_POWER =0,
LED_BUBBLE =1,
LED_HEATER_GREEN =2,
LED_HEATER_RED =3,
LED_FILTER =4
};
uint8_t ledStates;
ICACHE_RAM_ATTR void readLEDStates() {
uint8_t ls = ledStates;
ledStates = 0;
if (bitRead(buf, 0 ) == 0) bitSet(ledStates, LED_POWER);
if (bitRead(buf, 12) == 0) bitSet(ledStates, LED_FILTER);
if (bitRead(buf, 10) == 0) bitSet(ledStates, LED_BUBBLE);
if (bitRead(buf, 9 ) == 0) bitSet(ledStates, LED_HEATER_GREEN);
if (bitRead(buf, 7 ) == 0) bitSet(ledStates, LED_HEATER_RED);
if (ls != ledStates) {
if (bitRead(ledStates, LED_POWER) != bitRead(ls, LED_POWER)) {
Serial.println("LED_POWER_STATE_CHANGED");
controller_power_state_changed_event();
}
if (bitRead(ledStates, LED_FILTER) != bitRead(ls, LED_FILTER)) {
Serial.println("LED_FILTER_STATE_CHANGED");
controller_pump_state_changed_event();
}
if (bitRead(ledStates, LED_BUBBLE) != bitRead(ls, LED_BUBBLE)) {
Serial.println("LED_BUBBLE_STATE_CHANGED");
controller_bubble_state_changed_event();
}
if ((bitRead(ledStates, LED_HEATER_GREEN) || bitRead(ledStates, LED_HEATER_RED)) != (bitRead(ls, LED_HEATER_GREEN) || bitRead(ls, LED_HEATER_RED))) {
Serial.println("LED_HEATER_STATE_CHANGED");
controller_heater_state_changed_event();
}
if (bitRead(ledStates, LED_HEATER_RED) != bitRead(ls, LED_HEATER_RED)) {
Serial.println("LED_HEATING_STATE_CHANGED");
controller_heating_state_changed_event();
}
}
}
ICACHE_RAM_ATTR bool simulateButtonPress() {
// called at each latch
if (btnPulse) {
// reset pulse
digitalWrite(DAT_OUT, 1);
btnPulse = false;
}
uint16_t b = buf | 0x0100; // mask buzzer
int button = -1;
if (b == 0xFFFF) return false ; // no button
else if (b == 0xFFFD) button = BTN_FILTER; //65533
else if (b == 0x7FFF) button = BTN_HEATER; //32767
else if (b == 0xEFFF) button = BTN_UP; //61439
else if (b == 0xFF7F) button = BTN_DOWN; //65407
else if (b == 0xFBFF) button = BTN_POWER; //64511
else if (b == 0xFFF7) button = BTN_BUBBLE; //65527
else if (b == 0xDFFF) button = BTN_FC;
else return false;
if (btnRequest[button]) {
if (--btnCount[button] <= 0) {
btnRequest[button] = false;
}
// start button pulse (until next latch)
digitalWrite(DAT_OUT, 0);
btnPulse = true;
}
return true;
}
ICACHE_RAM_ATTR void writeButton(ButtonT button) {
btnRequest[button] = true;
btnCount[button] = btnCycles;
};
void switchBuzz(bool bOn) {
BuzzerEnabled = bOn;
if (bOn) {
pinMode(BUZ, INPUT);
} else {
pinMode(BUZ, OUTPUT);
digitalWrite(BUZ, 0);
};
};
void buzzSignal(int nBeep) {
pinMode(BUZ, OUTPUT);
digitalWrite(BUZ, 0);
for (;nBeep>0;nBeep--) {
digitalWrite(BUZ, 1);
delay(200);
digitalWrite(BUZ, 0);
delay(200);
}
switchBuzz(BuzzerEnabled);
};
void initial_publish() {
mqttLog("Publishing initial values . . .");
mqttPublish("power_state", (powerState ? "on":"off"));
mqttPublish("buzzer_state", (BuzzerEnabled ? "on":"off"));
mqttPublish("pump_state", (powerState ? "on":"off"));
mqttPublish("bubble_state", (bubbleState ? "on":"off"));
mqttPublish("heater_state", (heaterState ? "on":"off"));
mqttPublish("heating_state", (heatingState ? "on":"off"));
}
void SPI_begin() {
mqttLog("SPI: Configuring shift register . . .");
pinMode(CLK, INPUT);
pinMode(LAT, INPUT);
pinMode(DAT_IN, INPUT);
pinMode(DAT_OUT, OUTPUT);
digitalWrite(DAT_OUT, 1); //default high
switchBuzz(BuzzerEnabled);
mqttLog("SPI: Attaching handlers to interrupts . . .");
attachInterrupt(digitalPinToInterrupt(CLK), SPI_handleClock, RISING);
attachInterrupt(digitalPinToInterrupt(LAT), SPI_handleLatch, RISING);
}
void SPI_end() {
mqttLog("SPI: Detaching handlers from interrupts . . .");
detachInterrupt(digitalPinToInterrupt(CLK));
detachInterrupt(digitalPinToInterrupt(LAT));
}
//---------------------------------------------------------------
// highlevel pool controller methods
//---------------------------------------------------------------
typedef enum {
EVT_POWER_STATE_CHANGED,
EVT_PUMP_STATE_CHANGED,
EVT_BUBBLE_STATE_CHANGED,
EVT_HEATER_STATE_CHANGED,
EVT_HEATING_STATE_CHANGED,
EVT_CURRENT_TEMPERATURE_CHANGED,
EVT_TARGET_TEMPERATURE_CHANGED
} EventType;
typedef struct {
EventType evt;
bool state;
int temp;
} EventT;
ArduinoQueue<EventT> EventQueue(200);
//***********************************
// Temperature Events
//***********************************
// CURRENT TEMP value changed event
ICACHE_RAM_ATTR void current_temperature_changed_event(int temp) {
EventT e;
e.evt = EVT_CURRENT_TEMPERATURE_CHANGED;
e.state = false;
e.temp = temp;
EventQueue.enqueue(e);
}
// TARGET TEMP value changed event
ICACHE_RAM_ATTR void target_temperature_changed_event(int temp) {
// called inside ISR, be fast!
EventT e;
e.evt = EVT_TARGET_TEMPERATURE_CHANGED;
e.state = false;
e.temp = temp;
EventQueue.enqueue(e);
}
//***********************************
// Controller Events
//***********************************
// POWER state changed event
ICACHE_RAM_ATTR void controller_power_state_changed_event() {
EventT e;
e.evt = EVT_POWER_STATE_CHANGED;
e.state = controller_power_state_get();
e.temp = 0.0;
EventQueue.enqueue(e);
}
// PUMP state changed event
ICACHE_RAM_ATTR void controller_pump_state_changed_event() {
EventT e;
e.evt = EVT_PUMP_STATE_CHANGED;
e.state = controller_pump_state_get();
e.temp = 0.0;
EventQueue.enqueue(e);
}
// BUBBLE state changed event
ICACHE_RAM_ATTR void controller_bubble_state_changed_event() {
EventT e;
e.evt = EVT_BUBBLE_STATE_CHANGED;
e.state = controller_bubble_state_get();
e.temp = 0.0;
EventQueue.enqueue(e);
}
// HEATER state changed event
ICACHE_RAM_ATTR void controller_heater_state_changed_event() {
EventT e;
e.evt = EVT_HEATER_STATE_CHANGED;
e.state = controller_heater_state_get();
e.temp = 0.0;
EventQueue.enqueue(e);
}
// HEATING state changed event
ICACHE_RAM_ATTR void controller_heating_state_changed_event() {
EventT e;
e.evt = EVT_HEATING_STATE_CHANGED;
e.state = controller_heating_state_get();
e.temp = 0.0;
EventQueue.enqueue(e);
}
//***********************************
// Controller Getters
//***********************************
// POWER get state
ICACHE_RAM_ATTR bool controller_power_state_get() {
return (bitRead(ledStates, LED_POWER) == 1);
}
// PUMP get state
ICACHE_RAM_ATTR bool controller_pump_state_get() {
return (bitRead(ledStates, LED_FILTER) == 1);
}
// BUBBLE get state
ICACHE_RAM_ATTR bool controller_bubble_state_get() {
return (bitRead(ledStates, LED_BUBBLE) == 1);
}
// HEATER get state
ICACHE_RAM_ATTR bool controller_heater_state_get() {
return (bitRead(ledStates, LED_HEATER_GREEN) == 1 || bitRead(ledStates, LED_HEATER_RED) == 1);
}
// HEATING get state
ICACHE_RAM_ATTR bool controller_heating_state_get() {
return (bitRead(ledStates, LED_HEATER_RED) == 1);
}
// CURRENT TEMP get value
ICACHE_RAM_ATTR int controller_current_temperature_get() {
return curTemp;
}
// TARGET TEMP get value
ICACHE_RAM_ATTR int controller_target_temperature_get() {
return setTemp;
}
//***********************************
// Controller Setters
//***********************************
// POWER set state
ICACHE_RAM_ATTR void controller_power_state_set(bool newValue) {
int retry = 1;
while (controller_power_state_get() != newValue) {
writeButton(BTN_POWER);
delay(500);
if (--retry < 0) return;
}
}
// PUMP set state
ICACHE_RAM_ATTR void controller_pump_state_set(bool newValue) {
int retry = 1;
while (controller_pump_state_get() != newValue) {
writeButton(BTN_FILTER);
delay(500);
if (--retry < 0) return;
}
}
// BUBBLE set state
ICACHE_RAM_ATTR void controller_bubble_state_set(bool newValue) {
int retry = 1;
while (controller_bubble_state_get() != newValue) {
writeButton(BTN_BUBBLE);
delay(500);
if (--retry < 0) return;
}
}
// HEATER set state
ICACHE_RAM_ATTR void controller_heater_state_set(bool newValue) {
int retry = 1;
while (controller_heater_state_get() != newValue) {
writeButton(BTN_HEATER);
delay(500);
if (--retry < 0) return;
}
}
// TARGET TEMP set value
ICACHE_RAM_ATTR void controller_target_temperature_set(int newValue) {
int retry = 20;
// only works if power is on
if (controller_power_state_get()) {
while (controller_target_temperature_get() != newValue) {
if (controller_target_temperature_get() > newValue) {
writeButton(BTN_DOWN);
} else {
writeButton(BTN_UP);
}
delay(500);
if (--retry < 0) return;
}
}
}
// TARGET TEMP increase value
ICACHE_RAM_ATTR void controller_target_temperature_increase() {
int retry = 1;
int tempNow = controller_target_temperature_get();
// only works if power is on
if (controller_power_state_get()) {
while (controller_target_temperature_get() != (tempNow + 1)) {
writeButton(BTN_UP);
delay(600);
if (--retry < 0) return;
}
}
}
// TARGET TEMP decrease value
ICACHE_RAM_ATTR void controller_target_temperature_decrease() {
int retry = 1;
int tempNow = controller_target_temperature_get();
// only works if power is on
if (controller_power_state_get()) {
while (controller_target_temperature_get() != (tempNow - 1)) {
writeButton(BTN_DOWN);
delay(600);
if (--retry < 0) return;
}
}
}
//***********************************
// Controller Button Handler
//***********************************
void handleBuzz(bool bOn) {
switchBuzz( bOn );
buzzSignal( (bOn ? 2 : 1) );
}
void handleButton(ButtonT button) {
switch (button) {
case BTN_POWER:
controller_power_state_set(!controller_power_state_get());
break;
case BTN_FILTER:
controller_pump_state_set(!controller_pump_state_get());
break;
case BTN_HEATER:
controller_heater_state_set(!controller_heater_state_get());
break;
case BTN_BUBBLE:
controller_bubble_state_set(!controller_bubble_state_get());
break;
case BTN_UP:
controller_target_temperature_increase();
break;
case BTN_DOWN:
controller_target_temperature_decrease();
break;
}
}
//***********************************
// Controller Event Loop - process
//***********************************
void controller_loop() {
//handle events from controller like user interaction, current temp changes, heater changes
if (mqttLastBuzzerState != BuzzerEnabled) {
mqttLog("EVT_BUZZER_STATE_CHANGED: " + String(mqttLastBuzzerState ? "on":"off") + " -> " + String(BuzzerEnabled ? "on":"off"));
mqttPublish("buzzer_state", String(BuzzerEnabled ? "on":"off"));
mqttLastBuzzerState = BuzzerEnabled;
}
if (!EventQueue.isEmpty()) {
EventT e = EventQueue.dequeue();
switch (e.evt) {
case EVT_POWER_STATE_CHANGED:
if (powerState != e.state) {
mqttLog("EVT_POWER_STATE_CHANGED: " + String(powerState ? "on":"off") + " -> " + String(e.state ? "on":"off"));
powerState = e.state;
mqttPublish("power_state", String(powerState ? "on":"off"));
}
break;
case EVT_PUMP_STATE_CHANGED:
if (pumpState != e.state) {
mqttLog("EVT_PUMP_STATE_CHANGED: " + String(pumpState ? "on":"off") + " -> " + String(e.state ? "on":"off"));
pumpState = e.state;
mqttPublish("pump_state", String(pumpState ? "on":"off"));
}
break;
case EVT_BUBBLE_STATE_CHANGED:
if (bubbleState != e.state) {
mqttLog("EVT_BUBBLE_STATE_CHANGED: " + String(bubbleState ? "on":"off") + " -> " + String(e.state ? "on":"off"));
bubbleState = e.state;
mqttPublish("bubble_state", String(bubbleState ? "on":"off"));
}
break;
case EVT_HEATER_STATE_CHANGED:
if (heaterState != e.state) {
mqttLog("EVT_HEATER_STATE_CHANGED: " + String(heaterState ? "on":"off") + " -> " + String(e.state ? "on":"off"));
heaterState = e.state;
mqttPublish("heater_state", String(heaterState ? "on":"off"));
}
break;
case EVT_HEATING_STATE_CHANGED:
if (heatingState != e.state) {
mqttLog("EVT_HEATING_STATE_CHANGED: " + String(heatingState ? "on":"off") + " -> " + String(e.state ? "on":"off"));
heatingState = e.state;
mqttPublish("heating_state", String(heatingState ? "on":"off"));
}
break;
case EVT_CURRENT_TEMPERATURE_CHANGED:
if (currentTemp != e.temp && validTempValue(e.temp)) {
mqttLog("EVT_CURRENT_TEMPERATURE_CHANGED: " + String(currentTemp) + " -> " + String(e.temp));
currentTemp = int(e.temp);
mqttPublish("current_temperature", String(currentTemp));
}
break;
case EVT_TARGET_TEMPERATURE_CHANGED:
if (targetTemp != e.temp && validTempValue(e.temp)) {
mqttLog("EVT_TARGET_TEMPERATURE_CHANGED: " + String(targetTemp) + " -> " + String(e.temp));
targetTemp = int(e.temp);
mqttPublish("target_temperature", String(targetTemp));
}
break;
}
}
}
// Private.h contains the ssid and password as a temporary measure until a config page is added
#include "Private.h"
//---------------------------------------------------------------
// Webserver
ESP8266WebServer server(80);
void handleNotFound() {
String message = "File Not Found\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET) ? "GET" : "POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for (uint8_t i = 0; i < server.args(); i++) {
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}
server.send(200, "text/plain", message);
}
void returnToStatus()
{
delay(500);
server.sendHeader("Location", "/",true);
server.send(302, "text/plain","");
server.client().stop();
}
const char htmlTemplate[] =
"<!DOCTYPE html>"
"<html lang='en'>"
"<head>"
" <title>%s</title>"
" <metax http-equiv='refresh' content='5' />"
" <meta charset='utf-8' />"
" <meta name='viewport' content='width=device-width,initial-scale=1'/>"
" <link href=\"https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css\" rel=\"stylesheet\" integrity=\"sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1\" crossorigin=\"anonymous\" />"
"</head>"
"<body>"
" <h1>Intex Hot Tub Controller</h1>"
"<ul class=\"list-group\">"
" <li class=\"list-group-item\">Water temperature: <span class=\"badge bg-secondary\">%d</span></li>"
" <li class=\"list-group-item\">Target temperature: <span class=\"badge bg-secondary\">%d</span></li>"
" <li class=\"list-group-item\">"
" <a class=\"btn btn-primary\" role=\"button\" href='/up'>INCREASE</a>"
" <a class=\"btn btn-primary\" role=\"button\" href='/down'>DECREASE</a>"
" </li>"
" <li class=\"list-group-item\">Power <a class=\"btn btn-primary\" role=\"button\" href='/power'> %s </a></li>"
" <li class=\"list-group-item\">Filter <a class=\"btn btn-primary\" role=\"button\" href='/filter'> %s </a></li>"
" <li class=\"list-group-item\">Heater <a class=\"btn btn-primary\" role=\"button\" href='/heater'> %s </a></li>"
" <li class=\"list-group-item\">Bubble <a class=\"btn btn-primary\" role=\"button\" href='/bubble'> %s </a></li>"
" <li class=\"list-group-item\">Buzzer <a class=\"btn btn-primary\" role=\"button\" href='/buzz%s'> %s </a></li>"
" <li class=\"list-group-item\"><a class=\"btn btn-primary\" role=\"button\" href='/'>Refresh</a></li>"
"</ul>"
"</body>"
"</html>";
char message[3000];
void handleStatus() {
snprintf(message, 3000, htmlTemplate,
hostname,
curTemp,
setTemp,
controller_power_state_get() ? "ON" : "OFF",
controller_pump_state_get() ? "ON" : "OFF",
controller_heater_state_get() ? "ON" : "OFF",
controller_bubble_state_get() ? "ON" : "OFF",
(BuzzerEnabled?"0":"1"), (BuzzerEnabled?"ON":"OFF")
);
server.sendHeader("Cache-Control", "no-cache, no-store, must-revalidate");
server.sendHeader("Pragma", "no-cache");
server.sendHeader("Expires", "-1");
server.send(200, "text/html", message);
Serial.printf("strlen(message): %d\n",strlen(message));
};
void handleWebButton(ButtonT button) {
handleButton(button);
returnToStatus();
};
void handleWebBuzz(bool bOn) {
handleBuzz(bOn);
returnToStatus();
};
void server_setup() {
server.on("/", handleStatus);
server.on("/reboot", []() {
server.send(200, "text/plain", "Rebooting...");
delay(1);
ESP.reset();
});
server.on("/power", []() {handleWebButton(BTN_POWER);});
server.on("/up", []() {handleWebButton(BTN_UP);});
server.on("/down", []() {handleWebButton(BTN_DOWN);});
server.on("/filter", []() {handleWebButton(BTN_FILTER);});
server.on("/heater", []() {handleWebButton(BTN_HEATER);});
server.on("/bubble", []() {handleWebButton(BTN_BUBBLE);});
server.on("/buzz0", []() {handleWebBuzz(0);});
server.on("/buzz1", []() {handleWebBuzz(1);});
server.onNotFound(handleNotFound);
server.begin();
}
//******************************************************************************************
void ota_setup() {
ArduinoOTA.setHostname(hostname);
ArduinoOTA.setPassword(passwordOTA);
ArduinoOTA.onStart([]() {
OTAupdate = true;
SPI_end(); // disable interrupts, otherwise OTA is failing
mqttLog("OTA Update Initiated . . .");
});
ArduinoOTA.onEnd([]() {
mqttLog("\nOTA Update Ended . . .");
ESP.restart();
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
});
ArduinoOTA.onError([](ota_error_t error) {
OTAupdate = false;
Serial.printf("OTA Error [%u] ", error);
String otaErr = "";
if (error == OTA_AUTH_ERROR) otaErr = ". . . . . . . . . . . . . . . Auth Failed";
else if (error == OTA_BEGIN_ERROR) otaErr = ". . . . . . . . . . . . . . . Begin Failed";
else if (error == OTA_CONNECT_ERROR) otaErr = ". . . . . . . . . . . . . . . Connect Failed";
else if (error == OTA_RECEIVE_ERROR) otaErr = ". . . . . . . . . . . . . . . Receive Failed";
else if (error == OTA_END_ERROR) otaErr = ". . . . . . . . . . . . . . . End Failed";
mqttLog(otaErr);
});
ArduinoOTA.begin();
}
//******************************************************************************************
void callback(const MQTT::Publish& pub) {
if (pub.topic() == MQTT_TOPIC"/power") {
if (pub.payload_string() == "on") controller_power_state_set(true);
else if (pub.payload_string() == "off") controller_power_state_set(false);
}
else if (pub.topic() == MQTT_TOPIC"/pump") {
if (pub.payload_string() == "on") controller_pump_state_set(true);
else if (pub.payload_string() == "off") controller_pump_state_set(false);
}
else if (pub.topic() == MQTT_TOPIC"/heater") {
if (pub.payload_string() == "on") controller_heater_state_set(true);
else if (pub.payload_string() == "off") controller_heater_state_set(false);
}
else if (pub.topic() == MQTT_TOPIC"/bubble") {
if (pub.payload_string() == "on") controller_bubble_state_set(true);
else if (pub.payload_string() == "off") controller_bubble_state_set(false);
}
else if (pub.topic() == MQTT_TOPIC"/target_temp") {
int new_temp = atoi((char *)pub.payload());
if ((validTempValue(new_temp)) && (new_temp != targetTemp)) {
controller_target_temperature_set(new_temp);
}
}
else {
if (pub.payload_string() == "reset") {
requestRestart = true;
}
}
}
//******************************************************************************************
void setup() {
buzzSignal(1); //start setup
Serial.begin(115200);
Serial.setRxBufferSize(32);
Serial.setDebugOutput(false);
Serial.println('start setup');
generateHostname();
// Initalise the WiFi Connection
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
WiFi.hostname(hostname);
Serial.println(VERSION);
Serial.print("\nUnit ID: ");
Serial.print("esp8266-");
Serial.print(ESP.getChipId(), HEX);
Serial.print("\nConnecting to "); Serial.print(ssid); Serial.print(" Wifi");
while ((WiFi.status() != WL_CONNECTED) && kRetries --) {
delay(500);
Serial.print(" .");
}
if (WiFi.status() == WL_CONNECTED) {
Serial.println(" DONE");
Serial.print("IP Address is: "); Serial.println(WiFi.localIP());
Serial.print("Hostname is: "); Serial.println(hostname);
Serial.print("Connecting to "); Serial.print(MQTT_SERVER); Serial.print(" Broker . .");
delay(500);
while (!mqttClient.connect(MQTT::Connect(MQTT_CLIENT).set_keepalive(90).set_auth(MQTT_USER, MQTT_PASS)) && kRetries --) {
Serial.print(" .");
delay(1000);
}
if(mqttClient.connected()) {
Serial.println(" DONE");
Serial.println("\n---------------------------- Logs ----------------------------");
Serial.println();
mqttClient.subscribe(MQTT_TOPIC"/#");
mqttClient.set_callback(callback);
buzzSignal(1); //connected
ota_setup();
server_setup();
initial_publish();
SPI_begin();
}
else {
Serial.println(" FAILED!");
Serial.println("\n----------------------------------------------------------------");
Serial.println();
}
}
else {
Serial.println(" WiFi FAILED!");
Serial.println("\n----------------------------------------------------------------");
Serial.println();
}
}
void loop() {
ArduinoOTA.handle();
if (OTAupdate == false) {
mqttClient.loop();
yield();
timedTasks();
yield();
server.handleClient();
yield();
controller_loop();
yield();
if (requestRestart) {
ESP.restart();
}
}
}
void checkConnection() {
if (WiFi.status() == WL_CONNECTED) {
if (mqttClient.connected()) {
mqttLog("mqtt broker connection . . . . . . . . . . OK");
}
else {
Serial.println("mqtt broker connection . . . . . . . . . . LOST");
requestRestart = true;
}
}
else {
Serial.println("WiFi connection . . . . . . . . . . LOST");
requestRestart = true;
}
}
void timedTasks() {
if ((millis() > TTasks + (kUpdFreq*60000)) || (millis() < TTasks)) {
TTasks = millis();
checkConnection();
}
}
void mqttLog(String text) {
Serial.println(text);
if (mqttClient.connected()) {
mqttClient.publish(MQTT::Publish(MQTT_TOPIC"/log", text).set_retain().set_qos(1));
}
}
void mqttPublish(String topic, String text) {
if (mqttClient.connected()) {
mqttClient.publish(MQTT::Publish(MQTT_TOPIC"/" + topic, text).set_retain().set_qos(1));
}
}