forked from Yogui79/IntexPureSpa
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpa.ino
More file actions
1143 lines (1008 loc) · 31.7 KB
/
Spa.ino
File metadata and controls
1143 lines (1008 loc) · 31.7 KB
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
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
*
* Intex Purespa communication
*
*
* THIS SOFTWARE IS PROVIDED "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Pin connection
*
* LC12S Arduino ESP
* ___________________
* | o| GNG GND GND
* | o| CS D5 D18
* | o| SET D6 D19
* | o| TX D2 D16
* | o| RX D4 D17
* |__________________o| VCC 3.3V 3.3V
*
*/
/*******************************************************
*
* E N T E R Y O U R N E T W O R K I D
*
* A N D T H E C H A N N E L (HEX)
*
********************************************************/
#define USED_NETWORK_ID 0xFFFF
#define USED_CHANNEL 0x48
/*******************************************************
*
* S E L E C T Y O U R S P A M O D E L
*
********************************************************/
//#define _28458_28462_ // For spa model #28458 #28462 #28457(US) #28461(US)
//#define _28442_28440_ // For spa Model #28442 #28440
#if defined (_28458_28462_) && defined (_28442_28440_)
#error select only one SPA model
#endif
#if !defined (_28458_28462_) && !defined (_28442_28440_)
#error select a SPA model
#endif
/*******************************************************
*
* S E L E C T T H E P R O T O C O L Y O U U S E
*
********************************************************/
//#define _MY_SENSORS_
#define _MQTT_
#ifdef ESP32
const char* Myssid = "YourSSID";
const char* Mypassword = "YourPassword";
#endif
#if defined (_MY_SENSORS_) && defined (_MQTT_)
#error select between mysensors and MQTT
#endif
#ifdef _MQTT_
#include "EspMQTTClient.h"
EspMQTTClient client(
Myssid,
Mypassword,
"YourMQTT-Broker-IP", // MQTT Broker server ip
"NameMQTTBroker", // Can be omitted if not needed
"PasswordMQTTBroker", // Can be omitted if not needed
"IntexSpa", // Client name that uniquely identify your device
1883 // The MQTT port, default to 1883. this line can be omitted
);
#endif
//Uncomment following line to have more debug infos
//#define DEBUG_RECIEVED_DATA
//#define DEBUG_SEARCH_CHANNEL
#define DEBUG_SEND_COMMAND
#define DEBUG_PUMP_DATA
//#define DEBUG_CONTROLLER_DATA
//#define DEBUG_CONFIG
#define DEBUG_MQTT
//#define DEBUG_SEND_VALUE_TO_HOME_AUTOMATION_SW
#ifdef _MY_SENSORS_
//#include <MySensors.h>
#endif
bool FirstSend;
#include <arduino-timer.h>
#include "EEPROM.h"
#define EEPROM_SIZE 64
#if defined (__AVR__)
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 4); // RX, TX
// Output defines
#define DO_SET 6
#define DO_CS 5
#elif defined (ESP32)
#define mySerial Serial2
#define DO_SET 19
#define DO_CS 18
#include <WiFi.h>
#include <ESPmDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#endif
//Version and date
#define _VERSION "1.1.1"
#define _BUILD_DATE_TIME "2021.07.31 21:27:53"
//Command
#define COMMAND_ON_OFF 0x0001
#define COMMAND_WATER_FILTER 0x0010
#define COMMAND_BUBBLE 0x0020
#define COMMAND_HEATER 0x0040
#define COMMAND_CELSIUS_FARENHEIT 0x0002
#define COMMAND_DECREASE 0x0008
#define COMMAND_INCREASE 0x0004
#ifdef _28458_28462_
#define COMMAND_WATER_JET 0x0100
#define COMMAND_SANITIZER 0x0080
#endif
// Status Byte number
#define BYTE_STATUS_STATUS 2
#define BYTE_STATUS_COMMAND 4
#define BYTE_ACTUAL_TEMPERATURE 5
#define BYTE_SETPOINT_TEMPERATURE 7
#define BYTE_SETPOINT_TIME_SANITIZER 8
#define BYTE_SETPOINT_TIME_FILTER 12
#define BYTE_ERROR 14
//Value Pump bytes
#define VALUE_CONTROLLER_ON 0x01
#define VALUE_BUBBLE_ON 0x10
#define VALUE_HEATER_STANDBY 0x02
#define VALUE_HEATER_ON 0x04
#define VALUE_WATER_FILTER_ON 0x08
#ifdef _28458_28462_
#define VALUE_WATER_JET_ON 0x80
#define VALUE_SANITIZER_ON 0x20
#endif
#define VALUE_FARENHEIT 0x01
#define VALUE_TEMP_WINDOWS_OPEN 0x02
#define VALUE_COMMAND_RECIVED 0x80
// Commande bytes
#define BYTE_CONTROLLER_LOADING 3
// Value Controller bytes
//varibles for datamanagement
#define SIZE_CONTROLLER_DATA 8
unsigned char DataController[SIZE_CONTROLLER_DATA +2];
//Id for mysensors and for memo
uint16_t MemValueSended[40];
#define ID_POWER_ON 1
#define ID_BUBBLE_ON 2
#define ID_HEATER_ON 3
#define ID_HEATER_STATE 4
#define ID_WATER_FILTER_ON 5
#define ID_WATER_FILTER_TIME 6
#define ID_VALUE_WATER_JET_ON 7
#define ID_SANITIZER_ON 8
#define ID_SANITIZER_TIME 9
#define ID_FARENHEIT 15
#define ID_SETPOINT_TEMPERATURE 20
#define ID_ACTUAL_TEMPERATURE 21
#define ID_TARGET_TEMPERATURE 22
#define ID_TARGET_FILTER_TIME 23
#define ID_TARGET_SANITIZER_TIME 24
#define ID_ERROR_CODE 35
#define ID_COM_PUMP 36
#define SIZE_PUMP_DATA 17
unsigned char Data[SIZE_PUMP_DATA +2];
uint16_t DataCounter=0;
//internal variables
auto t = timer_create_default(); // create a timer with default settings
bool FinishPumpMessage;
bool FinishControllerMessage;
char ControllerLoadingState;
uint16_t state;
uint16_t CommandToSend =0x0000;
bool CommandRecived;
long LastTimeReciveData;
bool ErrorCommunicationWithPump;
long LastTimeSendData;
long LastTimeReciveDataCheckChannel;
uint8_t ActualSearchChannel;
uint16_t SearchChannelDataCount;
uint8_t UsedChannel;
uint8_t FirstCommandChar;
uint16_t ChannelChangeOk;
bool FarenheitCelsius;
uint8_t ActualSetpointTemperarue;
uint8_t TargetSetpointTemperarue;
bool ChangeTargetSetpointTemperarue;
bool ChangeSetpointRecirculationTime;
uint8_t ActualSetpointRecirculationTime;
uint8_t TargetSetpointRecirculationTime;
bool SwitchOffRecirculation;
bool StateRecirculation;
#ifdef _28458_28462_
bool ChangeSetpointSanitizerTime;
uint8_t ActualSetpointSanitizerTime;
uint8_t TargetSetpointSanitizerTime;
bool SwitchOffSanitizer;
bool StateSanitizer;
#endif
//Setup
void setup() {
mySerial.begin(9600);
Serial.begin(115200);
Serial.println(F("----------------------------------------------------------------------------------------------------------------------"));
Serial.println(F(""));
Serial.print (F(" Intex Purespa communication sketch version "));
Serial.print (_VERSION);
Serial.print (F(" build on : "));
Serial.println (_BUILD_DATE_TIME);
Serial.println(F(""));
Serial.println(F("----------------------------------------------------------------------------------------------------------------------"));
#ifdef ESP32
Serial.println("Booting");
WiFi.mode(WIFI_STA);
WiFi.begin(Myssid, Mypassword);
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("Connection Failed! Rebooting...");
delay(5000);
ESP.restart();
}
ArduinoOTA
.onStart([]() {
String type;
if (ArduinoOTA.getCommand() == U_FLASH)
type = "sketch";
else // U_SPIFFS
type = "filesystem";
// NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
Serial.println("Start updating " + type);
})
.onEnd([]() {
Serial.println("\nEnd");
})
.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
})
.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
else if (error == OTA_END_ERROR) Serial.println("End Failed");
});
ArduinoOTA.begin();
Serial.println("Ready");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
#endif
#if defined (_MQTT_) && defined (DEBUG_MQTT)
client.enableDebuggingMessages(); // Enable debugging messages sent to serial output
#endif
pinMode(DO_SET, OUTPUT); // SET
pinMode(DO_CS, OUTPUT); // CS
delay(1000);
digitalWrite(DO_CS, LOW);
digitalWrite(DO_SET, LOW);
delay(100);
//read channel inside EERPOM
#ifdef ESP32
if (!EEPROM.begin(EEPROM_SIZE))
{
Serial.println("failed to initialise EEPROM"); delay(1000000);
}
#endif
uint8_t DefaultChannel = (USED_CHANNEL & 0x00FF);
#if defined (_28458_28462_)
uint8_t DefaultFirstCommandChar = USED_CHANNEL ;
#elif defined (_28442_28440_)
uint8_t DefaultFirstCommandChar = (USED_CHANNEL & 0x00FF) + 0x7F;
#endif
UsedChannel = EEPROM.read(17)< 128? EEPROM.read(17):DefaultChannel ;
FirstCommandChar = EEPROM.read(18)< 256? EEPROM.read(18):DefaultFirstCommandChar;
Serial.print (F("Used Channel read from EEPROM 0x"));
Serial.println(UsedChannel,HEX);
Serial.print (F("Used first command read from EEPROM 0x"));
Serial.println(FirstCommandChar,HEX);
// Configure LC12s if a channel is known
if (UsedChannel){
SetSettings(UsedChannel);
}
else{ //else search the used channel
Serial.println (F("No channel saved in the EEPROM"));
while (!SearchChannel());
}
//configure send alive timer
#ifdef __AVR__
t.every(800, SendLifeFct, (void*)0); // only the alf time due to softwareserial
#endif
#ifdef ESP32
t.every(1600, SendLifeFct);
#endif
LastTimeReciveData = LastTimeSendData= millis();
}// void setup()
void loop() {
#ifdef DEBUG_RECIEVED_DATA
char res[5];
#endif
//update timer
t.tick();
#ifdef __AVR__
mySerial.listen();
#endif
#ifdef ESP32
ArduinoOTA.handle();
#endif
if (!FinishPumpMessage)
{
#ifdef __AVR__
//in case of overflow
if ( mySerial.overflow() )
{
state = 0;
DataCounter=0;
while ( mySerial.available())
mySerial.read();
}
else
#endif
{
if (mySerial.available() )
{
unsigned char c = mySerial.read();
ReadData(c);
#ifdef DEBUG_RECIEVED_DATA
sprintf(&res[0],"%02X",c);
if (c == FirstCommandChar){
Serial.println(" ");
}
Serial.print(res);
Serial.print(" ");
#endif
}
}
}
//Manage pump message
if (FinishPumpMessage)
{
LastTimeReciveData = millis();
#ifdef _MQTT_
if (client.isConnected())
#endif
{
DataManagement();
SendCommandManagement (&CommandToSend);
SendTemperatureSetpoint();
SendSpecialCommand(COMMAND_WATER_FILTER, &ChangeSetpointRecirculationTime, SwitchOffRecirculation,TargetSetpointRecirculationTime, ActualSetpointRecirculationTime);
#ifdef _28458_28462_
SendSpecialCommand(COMMAND_SANITIZER, &ChangeSetpointSanitizerTime, SwitchOffSanitizer,TargetSetpointSanitizerTime, ActualSetpointSanitizerTime);
#endif
state =0;
FirstSend = true;
FinishPumpMessage = false;
}
}
//Manage Controller message
if (FinishControllerMessage)
{
#ifdef DEBUG_CONTROLLER_DATA
Serial.print("Debug Controller data : ");
char res2[5];
for (uint8_t i =0; i<SIZE_CONTROLLER_DATA;i++){
sprintf(&res2[0],"%02X",DataController[i]);
Serial.print(res2);
Serial.print(" ");
}
Serial.println(" ");
#endif
ControllerLoadingState = DataController[BYTE_CONTROLLER_LOADING];
FinishControllerMessage = false;
}
#ifdef _MY_SENSORS_
MySensorsCommandManagement();
#endif
#ifdef _MQTT_
client.loop();
if ( !client.isConnected()
&& !FirstSend
){
LastTimeReciveData = millis();
}
#endif
// check communication pump timeout
#ifdef _28442_28440_
if ( (ChannelChangeOk > 14)
|| FirstSend
)
#endif
{
if (millis() - LastTimeReciveData > 5000)
{
SendValue("IntexSpa/Communication with pump", false,ID_COM_PUMP);
ErrorCommunicationWithPump = true;
#ifdef _28442_28440_
if ( FirstCommandChar - UsedChannel> 0x80 ){
Serial.println (F("Rebase channel to first command char"));
UsedChannel = FirstCommandChar-0x80;
SetSettings(UsedChannel);
EEPROM.write(17, UsedChannel);
#ifdef ESP32
EEPROM.commit();
#endif
}
#endif
}
else{
SendValue("IntexSpa/Communication with pump", true,ID_COM_PUMP);
ErrorCommunicationWithPump = false;
}
}
//Try to find the new channel in case of pump change channel
if ( !FirstSend
&& (millis() - LastTimeReciveData > 6000 )
#ifdef _MQTT_
&& client.isConnected()
#endif
){
#if defined (_28458_28462_)
if (ChannelChangeOk == 1){
ActualSearchChannel =UsedChannel+1;
}
#elif defined (_28442_28440_)
if (ChannelChangeOk == 1){
FirstCommandChar = FirstCommandChar -6;
}
if (ChannelChangeOk >0 && ChannelChangeOk < 15){
LastTimeReciveData = millis();
ChannelChangeOk++;
FirstCommandChar ++;
EEPROM.write(18, FirstCommandChar);
#ifdef ESP32
EEPROM.commit();
#endif
Serial.print (F("Try next first Command char 0x"));
Serial.println (FirstCommandChar,HEX);
return;
}
else if (ChannelChangeOk > 14)
ActualSearchChannel =UsedChannel+1;
#endif
else{
ActualSearchChannel =UsedChannel -10;
}
UsedChannel=0;
ChannelChangeOk = 0;
Serial.println (F("Look like channel change since last boot"));
#ifdef _MQTT_
client.publish("IntexSpa/Notification", "Search the device");
#endif
while (!UsedChannel && (millis() - LastTimeReciveData < 420000 ))
SearchChannel();
#ifdef _MQTT_
if (UsedChannel)
client.publish("IntexSpa/Notification", "Device found");
#endif
}
} //void loop()
//Managed data recived from the LC12s
void ReadData (unsigned char c)
{
switch (state)
{
case 0: //wait header
{
DataCounter=0;
memset (&Data,0,sizeof(Data));
memset (&DataController,0,sizeof(DataController));
if (c == FirstCommandChar)
{
Data[DataCounter] =c;
DataCounter++;
state++;
}
break;
}
//wait all other data
case 1:
{
Data[DataCounter] =c;
DataCounter++;
//it was a message from controller
if (c == FirstCommandChar && DataCounter< SIZE_PUMP_DATA && DataCounter > SIZE_CONTROLLER_DATA )
{
memcpy (&DataController,&Data, 8);
//Calclulate an check Checksum
uint16_t crc_out = calc_crc((char*) DataController,SIZE_CONTROLLER_DATA -2);
//Checksum ok
if ( (Data[SIZE_CONTROLLER_DATA-2] == ((crc_out & 0xFF00) >> 8 ))
&& (Data[SIZE_CONTROLLER_DATA-1] == (crc_out & 0x00FF))
)
{
FinishControllerMessage = true;
}
else
{
FinishControllerMessage = false;
}
DataCounter = 0;
Data[DataCounter] =c;
DataCounter++;
}
// wait until full pump message is recived
if (DataCounter> SIZE_PUMP_DATA-1)
{
//Calclulate an check Checksum
uint16_t crc_out = calc_crc((char*)Data,SIZE_PUMP_DATA -2);
//Checksum ok
if ( (Data[SIZE_PUMP_DATA-2] == ((crc_out & 0xFF00) >> 8 ))
&& (Data[SIZE_PUMP_DATA-1] == (crc_out & 0x00FF))
)
{
state =0;
FinishPumpMessage = true;
}
else{ // Wrong Cheksum
state =0;
}
}
break;
}
}
}
//manage information comming from the home automation sofware over MQTT
#ifdef _MQTT_
void onConnectionEstablished()
{
//manage command power on/off
client.subscribe("IntexSpa/Cmd Power on off", [](const String & payload) {
if (payload== "1" || payload== "0"){
CommandToSend |= COMMAND_ON_OFF ;
LastTimeSendData = millis();
}
});
//Target Filter time
client.subscribe("IntexSpa/Cmd water filter time", [](const String & payload) {
if( (payload== "2" || payload == "4" || payload == "6") ){
ChangeSetpointRecirculationTime = StateRecirculation;
TargetSetpointRecirculationTime = payload.toInt();
}
});
//manage command water filter on/off
client.subscribe("IntexSpa/Cmd water filter on off", [](const String & payload) {
if (payload== "1" && (TargetSetpointRecirculationTime== 2 || TargetSetpointRecirculationTime == 4 || TargetSetpointRecirculationTime == 6)){
ChangeSetpointRecirculationTime = true;
SwitchOffRecirculation = false;
}
else if(payload== "0"){
ChangeSetpointRecirculationTime = true;
SwitchOffRecirculation = true;
}
});
//manage command Bubble on/off
client.subscribe("IntexSpa/Cmd bubble on off", [](const String & payload) {
if (payload== "1" || payload== "0"){
CommandToSend |= COMMAND_BUBBLE;
LastTimeSendData = millis();
}
});
//manage command heater on/off
client.subscribe("IntexSpa/Cmd heater on off", [](const String & payload) {
if (payload== "1" || payload== "0"){
CommandToSend |= COMMAND_HEATER;
LastTimeSendData = millis();
}
});
//manage command Farenheit/Celsius
client.subscribe("IntexSpa/Cmd Farenheit Celsius", [](const String & payload) {
if (payload== "1" || payload== "0"){
CommandToSend |= COMMAND_CELSIUS_FARENHEIT;
LastTimeSendData = millis();
}
});
//manage command decrease
client.subscribe("IntexSpa/Cmd decrease", [](const String & payload) {
if (payload== "1")
{
LastTimeSendData = millis();
TargetSetpointTemperarue = TargetSetpointTemperarue -1;
ChangeTargetSetpointTemperarue = true;
}
});
//manage command increase
client.subscribe("IntexSpa/Cmd increase", [](const String & payload) {
if (payload== "1"){
LastTimeSendData = millis();
TargetSetpointTemperarue = TargetSetpointTemperarue +1;
ChangeTargetSetpointTemperarue = true;
}
});
//manage command temperature setpoint
client.subscribe("IntexSpa/Cmd Temperature Setpoint", [](const String & payload) {
TargetSetpointTemperarue = payload.toInt();
ChangeTargetSetpointTemperarue = true;
});
// reset
client.subscribe("IntexSpa/Cmd Reset ESP", [](const String & payload) {
if (payload== "reset"){
ESP.restart();
}
});
#ifdef _28458_28462_
//manage command water jet on off
client.subscribe("IntexSpa/Cmd water jet on off", [](const String & payload) {
if (payload== "1" || payload== "0"){
CommandToSend |= COMMAND_WATER_JET;
LastTimeSendData = millis();
}
});
//Target Sanitizer time
client.subscribe("IntexSpa/Cmd Sanitizer time", [](const String & payload) {
if( (payload== "3" || payload == "5" || payload == "8") ){
ChangeSetpointSanitizerTime = StateSanitizer;
TargetSetpointSanitizerTime = payload.toInt();
}
});
//manage command sanitizer on/off
client.subscribe("IntexSpa/Cmd sanitizer on off", [](const String & payload) {
if (payload== "1" && (TargetSetpointSanitizerTime== 3 || TargetSetpointSanitizerTime == 5 || TargetSetpointSanitizerTime == 8)){
ChangeSetpointSanitizerTime = true;
SwitchOffSanitizer = false;
}
else if(payload== "0"){
ChangeSetpointSanitizerTime = true;
SwitchOffSanitizer = true;
}
});
#endif
}
#endif
// manage the data recived from the pump and send it to the home autmation software over MQTT
void DataManagement (){
#ifdef DEBUG_PUMP_DATA
char res[5];
Serial.print("Debug Pump data : ");
for (uint8_t i =0; i<SIZE_PUMP_DATA;i++){
sprintf(&res[0],"%02X",Data[i]);
Serial.print(res);
Serial.print(" ");
}
Serial.println(" ");
#endif
// Send power on
SendValue("IntexSpa/Power on", (bool)(Data[BYTE_STATUS_COMMAND] & VALUE_CONTROLLER_ON),ID_POWER_ON);
//Send Bubble on
SendValue("IntexSpa/Bubble on", (bool)(Data[BYTE_STATUS_COMMAND] & VALUE_BUBBLE_ON),ID_BUBBLE_ON);
//Send heater on
SendValue("IntexSpa/heater on", (bool)((Data[BYTE_STATUS_COMMAND] & VALUE_HEATER_ON) || (Data[BYTE_STATUS_COMMAND] & VALUE_HEATER_STANDBY)),ID_HEATER_ON);
//Send heater State
uint16_t HeaterState= 0;
if (Data[BYTE_STATUS_COMMAND] & VALUE_HEATER_STANDBY){
HeaterState = 1; // state standby
}else if ((Data[BYTE_STATUS_COMMAND] & VALUE_HEATER_ON) ){
HeaterState = 2; // state heater on
}
SendValue("IntexSpa/heater state", HeaterState ,ID_HEATER_STATE);
//Send water filter on
StateRecirculation =(bool)(Data[BYTE_STATUS_COMMAND] & VALUE_WATER_FILTER_ON);
SendValue("IntexSpa/filter on", (bool)(Data[BYTE_STATUS_COMMAND] & VALUE_WATER_FILTER_ON),ID_WATER_FILTER_ON);
//Send actual filter setup time
ActualSetpointRecirculationTime = Data[BYTE_SETPOINT_TIME_FILTER];
SendValue("IntexSpa/filter setup time", Data[BYTE_SETPOINT_TIME_FILTER],ID_WATER_FILTER_TIME);
if (!ChangeSetpointRecirculationTime && ActualSetpointRecirculationTime)
{
TargetSetpointRecirculationTime =ActualSetpointRecirculationTime;
SendValue("IntexSpa/Cmd water filter time", Data[BYTE_SETPOINT_TIME_FILTER],ID_TARGET_FILTER_TIME);
}
#ifdef _28458_28462_
//Send water jet on
SendValue("IntexSpa/Water jet on", (bool)(Data[BYTE_STATUS_COMMAND] & VALUE_WATER_JET_ON),ID_VALUE_WATER_JET_ON);
//Send water sanitizer on
StateSanitizer =(bool)(Data[BYTE_STATUS_COMMAND] & VALUE_SANITIZER_ON);
SendValue("IntexSpa/Sanitizer on", (bool)(Data[BYTE_STATUS_COMMAND] & VALUE_SANITIZER_ON),ID_SANITIZER_ON);
//Send actual filter setup time
ActualSetpointSanitizerTime = Data[BYTE_SETPOINT_TIME_SANITIZER];
SendValue("IntexSpa/Sanitizer setup time", Data[BYTE_SETPOINT_TIME_SANITIZER],ID_SANITIZER_TIME);
if (!ChangeSetpointSanitizerTime && ActualSetpointSanitizerTime)
{
TargetSetpointSanitizerTime =ActualSetpointSanitizerTime;
SendValue("IntexSpa/Cmd Sanitizer time", Data[BYTE_SETPOINT_TIME_SANITIZER],ID_TARGET_SANITIZER_TIME);
}
#endif
//Send Farenheit selected
FarenheitCelsius = (bool)(Data[BYTE_STATUS_STATUS] & VALUE_FARENHEIT);
SendValue("IntexSpa/Farenheit Celsius", (bool)(Data[BYTE_STATUS_STATUS] & VALUE_FARENHEIT),ID_FARENHEIT);
//Send temperature setpoint
ActualSetpointTemperarue = Data[BYTE_SETPOINT_TEMPERATURE];
SendValue("IntexSpa/Temperature Setpoint", Data[BYTE_SETPOINT_TEMPERATURE],ID_SETPOINT_TEMPERATURE);
if (!ChangeTargetSetpointTemperarue)
{
TargetSetpointTemperarue =ActualSetpointTemperarue;
SendValue("IntexSpa/Cmd Temperature Setpoint", Data[BYTE_SETPOINT_TEMPERATURE],ID_TARGET_TEMPERATURE);
}
//Send actual temperature
SendValue("IntexSpa/Actual Temperature", Data[BYTE_ACTUAL_TEMPERATURE],ID_ACTUAL_TEMPERATURE);
//send Error number
SendValue("IntexSpa/Error Number", Data[BYTE_ERROR],ID_ERROR_CODE);
//Manage Command recived
if (Data[BYTE_STATUS_STATUS] & VALUE_COMMAND_RECIVED){
CommandRecived = true;
}
else{
CommandRecived = false;
}
}
//manage send temperature to pump
void SendTemperatureSetpoint(){
//nothing to do
if (!ChangeTargetSetpointTemperarue)
return;
// command is pendling actualy nothing else to do
if ( (CommandToSend & COMMAND_INCREASE)
|| (CommandToSend & COMMAND_DECREASE)
)
return;
// setpoint is done
if ( TargetSetpointTemperarue == ActualSetpointTemperarue
|| (TargetSetpointTemperarue > 40 && !FarenheitCelsius)
|| (TargetSetpointTemperarue < 10 && !FarenheitCelsius)
|| (TargetSetpointTemperarue > 50 && FarenheitCelsius)
|| (TargetSetpointTemperarue < 104 && FarenheitCelsius)
){
ChangeTargetSetpointTemperarue = false;
return;
}
//temperature need to be increased -> send increase command
if (TargetSetpointTemperarue > ActualSetpointTemperarue)
{
LastTimeSendData = millis();
CommandToSend |= COMMAND_INCREASE;
}
//temperature need to be decreased -> send decrease command
if (TargetSetpointTemperarue < ActualSetpointTemperarue)
{
LastTimeSendData = millis();
CommandToSend |= COMMAND_DECREASE;
}
}
//function to send Recirculation and sanizer command
void SendSpecialCommand( uint16_t Command, bool *CommandChange, bool CommandOff,uint8_t SetupValue ,uint8_t ActualValue ){
if ( !Command
|| !*CommandChange
)
return;
//nothing to do
if (!CommandChange)
{
CommandOff =false;
return;
}
// command is pendling actualy nothing else to do
if (CommandToSend & Command)
{
return;
}
// Sepoint is done
if ( ((SetupValue == ActualValue)&& !CommandOff)
|| ((ActualValue == 0x00) && CommandOff)
)
{
*CommandChange = false;
return;
}
// send command
LastTimeSendData = millis();
CommandToSend |= Command;
}
#ifdef _MY_SENSORS_
void MySensorsCommandManagement(){
}
#endif
//manage the command to the pump; send until it recived
void SendCommandManagement (uint16_t *Command){
if (!Command || !*Command)
return;
if( CommandRecived
|| (millis() -LastTimeSendData> 2000) // timeout
)
{
*Command = 0x0000;
return;
}
SendCommand(*Command);
}
//Send commant to recive all info in case the controller is not close to pump (E81 on controller or off)
bool SendLifeFct(void *)
{
#ifdef _MQTT_
if (client.isConnected() && !ErrorCommunicationWithPump)
#endif
{
if (!CommandToSend)
SendCommand(0x0000);
}
return true;
}
//Send command to pump
void SendCommand(uint16_t Command){
// Sample :C7 00 00 80 00 00 4D 2B
unsigned char SendData[10];
#ifdef DEBUG_SEND_COMMAND
char res[5];
#endif
SendData[0] = FirstCommandChar;
SendData[1] = (Command & 0xFF00) >> 8;;
SendData[2] = Command & 0x00FF;
SendData[3] = ControllerLoadingState;
SendData[4] = 0x00;
SendData[5] = 0x00;
//checksum
uint16_t crc_out = calc_crc((char*) SendData,6);
SendData[6] = (crc_out & 0xFF00) >> 8;
SendData[7] = crc_out & 0x00FFF;
#ifdef DEBUG_SEND_COMMAND
Serial.print("Debug Send command : ");
#endif
for (uint8_t i =0; i<SIZE_CONTROLLER_DATA;i++){
#ifdef DEBUG_SEND_COMMAND
sprintf(&res[0],"%02X",SendData[i]);
Serial.print(res);
Serial.print(" ");
#endif
//Wreite commans to LC12s Serial
mySerial.write(SendData[i]);
}
#ifdef DEBUG_SEND_COMMAND
Serial.println(" ");
#endif
}
//search channel
bool SearchChannel(){
#ifdef DEBUG_SEARCH_CHANNEL
char res[5];
#endif
#ifdef __AVR__
//in case of overflow
if ( mySerial.overflow() )
{
state = 0;
DataCounter=0;
while ( mySerial.available())
mySerial.read();
}
else
#endif
{
if (millis() - LastTimeReciveDataCheckChannel >1000)
{
SearchChannelDataCount =0;
if (ActualSearchChannel<128){
SetSettings(ActualSearchChannel++);
}else{
ActualSearchChannel=0;
SetSettings(ActualSearchChannel++);
}
}
if (mySerial.available() )
{
unsigned char c = mySerial.read();
LastTimeReciveDataCheckChannel = millis();
SearchChannelDataCount++;
#ifdef DEBUG_SEARCH_CHANNEL
sprintf(&res[0],"%02X",c);
if (c==0xAA){
Serial.println(F(""));
Serial.print(F("Debug search Chanel : "));
}
Serial.print(res);
Serial.print(F(" "));
#endif
if (SearchChannelDataCount >1500){
ChannelChangeOk ++;
UsedChannel = ActualSearchChannel-1; // due to autoamtic add inside the search function
LastTimeReciveData = LastTimeSendData= millis();
#if defined (_28458_28462_)
FirstCommandChar = UsedChannel;
#elif defined (_28442_28440_)
FirstCommandChar = UsedChannel + 0x7F;
#endif
// Write chanel to eeprom
if (UsedChannel){