-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathECODAN_Bridge.ino
1238 lines (1085 loc) · 48.9 KB
/
ECODAN_Bridge.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
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
// -- Supported Hardware -- //
/* As sold Witty ESP8266 based / Core 3.1.2 / Flash 4MB (1MB FS / 1MB OTA - 16KB Cache/48KB IRAM not shared) */
/* ESP32 AtomS3 Lite (ESP32S3 Dev Module) / Core 3.0.7 / Flash 4M with SPIFFS (1.2MB APP / 1.5MB SPIFFS) */
/* ESP32 Ethernet WT32-ETH01 / Core 3.0.7 / Flash 4MB (1.2MB APP / 1.5MB SPIFFS) */
#if defined(ESP8266) || defined(ESP32) // ESP32 or ESP8266 Compatiability
#include <FS.h> // Define File System First
#include <LittleFS.h>
#ifdef ESP8266
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <ESP8266WebServer.h>
#include <SoftwareSerial.h>
#endif
#ifdef ESP32
#include <WiFi.h>
#include <WebServer.h>
#include <ESPmDNS.h>
#endif
#ifdef ARDUINO_WT32_ETH01
#include <ETH.h>
#include <Arduino.h>
#endif
#include <WiFiManager.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>
#include <ESPTelnet.h>
#include "Ecodan.h"
#include "Melcloud.h"
String FirmwareVersion = "6.1.2";
#ifdef ESP8266 // Define the Witty ESP8266 Serial Pins
#define HEATPUMP_STREAM SwSerial1
#define MEL_STREAM SwSerial2
#define SERIAL_CONFIG SWSERIAL_8E1
int LDR = A0;
#define MEL_RxPin 3
int Activity_LED = 2;
int Reset_Button = 4;
#define MEL_TxPin 1
int Green_RGB_LED = 12;
int Blue_RGB_LED = 13;
#define FTCCable_RxPin 14
int Red_RGB_LED = 15;
#define FTCCable_TxPin 16
#endif
#ifdef ESP32 // Define the M5Stack Serial Pins
#define HEATPUMP_STREAM Serial1
#define MEL_STREAM Serial2
#define SERIAL_CONFIG SERIAL_8E1
#ifdef ARDUINO_M5STACK_ATOMS3
#include <FastLED.h>
#define FASTLED_FORCE_NAMESPACE
#define FASTLED_INTERNAL
#define NUM_LEDS 1
#define DATA_PIN 35
CRGB leds[NUM_LEDS];
int Reset_Button = 41;
#define FTCCable_RxPin 2
#define FTCCable_TxPin 1
#define FTCProxy_RxPin 38
#define FTCProxy_TxPin 39
#define MEL_RxPin 8
#define MEL_TxPin 7
#endif
#ifdef ARDUINO_WT32_ETH01
#define FTCCable_RxPin 4
#define FTCCable_TxPin 2
#define MEL_RxPin 14
#define MEL_TxPin 12
#ifndef ETH_PHY_TYPE
#define ETH_PHY_TYPE ETH_PHY_LAN8720
#define ETH_PHY_ADDR 0
#define ETH_PHY_MDC 23
#define ETH_PHY_MDIO 18
#define ETH_PHY_POWER -1
#define ETH_CLK_MODE ETH_CLOCK_GPIO0_IN
#endif
#endif
#endif
#define Heartbeat_Range 99 // Heatbeat Max value
int Heart_Value = 0; // Heatbeat ID
unsigned long SERIAL_BAUD = 2400;
bool shouldSaveConfig = false;
const int deviceId_max_length = 15;
const int hostname_max_length = 200;
const int port_max_length = 10;
const int user_max_length = 30;
const int password_max_length = 50;
const int basetopic_max_length = 30;
// The extra parameters to be configured (can be either global or just in the setup)
// After connecting, parameter.getValue() will get you the configured value
// id/name placeholder/prompt default length
// Here you can pre-set the settings for the MQTT connection. The settings can later be changed via Wifi Manager.
struct MqttSettings {
// These are the placeholder objects for the custom fields
char deviceId[deviceId_max_length] = "000000000000";
char wm_device_id_identifier[10] = "device_id";
// Client 1
char hostname[hostname_max_length] = "homeassistant.local";
char user[user_max_length] = "Username";
char password[password_max_length] = "Password";
char port[port_max_length] = "1883";
char baseTopic[basetopic_max_length] = "Ecodan/ASHP";
char wm_mqtt_hostname_identifier[14] = "mqtt_hostname";
char wm_mqtt_user_identifier[10] = "mqtt_user";
char wm_mqtt_password_identifier[14] = "mqtt_password";
char wm_mqtt_port_identifier[10] = "mqtt_port";
char wm_mqtt_basetopic_identifier[15] = "mqtt_basetopic";
// Client 2
char hostname2[hostname_max_length] = "IPorDNS";
char user2[user_max_length] = "Username";
char password2[password_max_length] = "Password";
char port2[port_max_length] = "1883";
char baseTopic2[basetopic_max_length] = "Ecodan/ASHP";
char wm_mqtt2_hostname_identifier[15] = "mqtt2_hostname";
char wm_mqtt2_user_identifier[11] = "mqtt2_user";
char wm_mqtt2_password_identifier[15] = "mqtt2_password";
char wm_mqtt2_port_identifier[11] = "mqtt2_port";
char wm_mqtt2_basetopic_identifier[16] = "mqtt2_basetopic";
};
struct UnitSettings {
float UnitSize = 8.5;
float GlycolStrength = 3.9;
char unitsize_identifier[9] = "unitsize";
char glycol_identifier[7] = "glycol";
};
MqttSettings mqttSettings;
UnitSettings unitSettings;
ECODAN HeatPump;
MELCLOUD MELCloud;
#ifdef ESP8266
SoftwareSerial SwSerial1;
SoftwareSerial SwSerial2;
#endif
WiFiClient NetworkClient1;
WiFiClient NetworkClient2;
//WiFiClientSecure NetworkClient; // Encryption Support
PubSubClient MQTTClient1(NetworkClient1);
PubSubClient MQTTClient2(NetworkClient2);
ESPTelnet TelnetServer;
WiFiManager wifiManager;
// Delcare Global Scope for Non-Blocking, always active Portal with "TEMP" placeholder, real values populated later from filesystem
WiFiManagerParameter custom_mqtt_server("server", "<b>Required</b> Primary MQTT Server (IP Address or DNS)", "TEMP", hostname_max_length);
WiFiManagerParameter custom_mqtt_user("user", "Primary MQTT Username", "TEMP", user_max_length);
WiFiManagerParameter custom_mqtt_pass("pass", "Primary MQTT Password", "TEMP", password_max_length);
WiFiManagerParameter custom_mqtt_port("port", "Primary MQTT Server Port (Default: 1883)", "TEMP", port_max_length);
WiFiManagerParameter custom_mqtt_basetopic("basetopic", "Primary MQTT Base Topic (Default: Ecodan/ASHP)<br><font size='0.8em'>Modify if you have multiple heat pumps connecting to the same MQTT server</font>", "TEMP", basetopic_max_length);
WiFiManagerParameter custom_mqtt2_server("server2", "<hr><b>Optional</b> Secondary MQTT Server<br><font size='0.8em'>You can send data to a second MQTT broker, <b>leave default or blank if not in use</b></font>", "TEMP", hostname_max_length);
WiFiManagerParameter custom_mqtt2_user("user2", "Secondary MQTT Username", "TEMP", user_max_length);
WiFiManagerParameter custom_mqtt2_pass("pass2", "Secondary MQTT Password", "TEMP", password_max_length);
WiFiManagerParameter custom_mqtt2_port("port2", "Secondary MQTT Server Port", "TEMP", port_max_length);
WiFiManagerParameter custom_mqtt2_basetopic("basetopic2", "Secondary MQTT Base Topic", "TEMP", basetopic_max_length);
WiFiManagerParameter custom_device_id("device_id", "<hr>Device ID<br><font size='0.8em'>Only modify if upgrading or changing hardware, copy your previous device ID over</font>", "TEMP", deviceId_max_length);
#include "TimerCallBack.h"
#include "Debug.h"
#include "MQTTDiscovery.h"
#include "MQTTConfig.h"
void HeatPumpQueryStateEngine(void);
void HeatPumpWriteStateEngine(void);
void MELCloudQueryReplyEngine(void);
void HeatPumpKeepAlive(void);
void Zone1Report(void);
void Zone2Report(void);
void HotWaterReport(void);
void SystemReport(void);
void ConfigurationReport(void);
void AdvancedReport(void);
void AdvancedTwoReport(void);
void EnergyReport(void);
void StatusReport(void);
void FastPublish(void);
TimerCallBack HeatPumpQuery1(400, HeatPumpQueryStateEngine); // Set to 400ms (Safe), 320-350ms best time between messages
TimerCallBack HeatPumpQuery2(30000, HeatPumpKeepAlive); // Set to 20-30s for heat pump query frequency
TimerCallBack HeatPumpQuery3(30000, handleMQTTState); // Re-connect attempt timer if MQTT is not online
TimerCallBack HeatPumpQuery4(30000, handleMQTT2State); // Re-connect attempt timer if MQTT Stream 2 is not online
TimerCallBack HeatPumpQuery5(500, HeatPumpWriteStateEngine); // Set to 500ms (Safe), 320-350ms best time between messages
TimerCallBack HeatPumpQuery6(2000, FastPublish); // Publish some reports at a faster rate
unsigned long looppreviousMicros = 0; // variable for comparing millis counter
unsigned long ftcpreviousMillis = 0; // variable for comparing millis counter
unsigned long wifipreviousMillis = 0; // variable for comparing millis counter
unsigned long ftcconpreviousMillis = 0; // variable for comparing millis counter
int FTCLoopSpeed, CPULoopSpeed; // variable for holding loop time in ms
bool WiFiOneShot = true;
bool CableConnected = true;
bool WiFiConnectedLastLoop = false;
extern int cmd_queue_length;
extern int cmd_queue_position;
extern bool WriteInProgress;
byte NormalHWBoostOperating = 0;
byte PreHWBoostSvrCtrlMode = 0;
#ifdef ARDUINO_WT32_ETH01
static bool eth_connected = false;
#endif
void setup() {
WiFi.mode(WIFI_STA); // explicitly set mode, esp defaults to STA+AP
DEBUGPORT.begin(DEBUGBAUD); // Start Debug
HEATPUMP_STREAM.begin(SERIAL_BAUD, SERIAL_CONFIG, FTCCable_RxPin, FTCCable_TxPin); // Rx, Tx
HeatPump.SetStream(&HEATPUMP_STREAM);
MEL_STREAM.begin(SERIAL_BAUD, SERIAL_CONFIG, MEL_RxPin, MEL_TxPin); // Rx, Tx
MELCloud.SetStream(&MEL_STREAM);
#ifdef ARDUINO_WT32_ETH01
Network.onEvent(onEvent);
ETH.begin();
#endif
#ifndef ARDUINO_WT32_ETH01
pinMode(Reset_Button, INPUT); // Pushbutton on other modules
#endif
// -- Lights for ESP8266 and ESP32 -- //
#ifdef ARDUINO_M5STACK_ATOMS3 // Define the M5Stack LED
FastLED.addLeds<WS2812, DATA_PIN, GRB>(leds, NUM_LEDS); // ESP32 M5 Stack Atom S3
#endif
#ifdef ESP8266 // Define the Witty ESP8266 Ports
pinMode(Activity_LED, OUTPUT); // ESP8266 Onboard LED
pinMode(LDR, INPUT); // LDR
pinMode(Red_RGB_LED, OUTPUT); // Red (RGB) LED
pinMode(Green_RGB_LED, OUTPUT); // Green (RGB) LED
pinMode(Blue_RGB_LED, OUTPUT); // Blue (RGB) LED
digitalWrite(Activity_LED, HIGH); // Set On (Inverted)
digitalWrite(Red_RGB_LED, LOW); // Set Off
digitalWrite(Green_RGB_LED, LOW); // Set Off
digitalWrite(Blue_RGB_LED, LOW); // Set Off
#endif
readSettingsFromConfig();
initializeWifiManager();
if (shouldSaveConfig) {
saveConfig();
}
setupTelnet();
startTelnet();
MQTTClient1.setBufferSize(2048); // Increase MQTT Buffer Size
MQTTClient2.setBufferSize(2048); // Increase MQTT Buffer Size
RecalculateMQTTTopics();
RecalculateMQTT2Topics();
initializeMQTTClient1();
MQTTClient1.setCallback(MQTTonData);
initializeMQTTClient2();
MQTTClient2.setCallback(MQTTonData);
wifiManager.startWebPortal();
MDNS.begin("heatpump");
MDNS.addService("http", "tcp", 80);
HeatPump.Status.Write_To_Ecodan_OK = false;
HeatPumpKeepAlive();
}
void loop() {
// -- Loop Start -- //
looppreviousMicros = micros(); // Loop Speed Check
// -- Process Handlers -- //
HeatPumpQuery1.Process();
HeatPumpQuery2.Process();
HeatPumpQuery3.Process();
HeatPumpQuery4.Process();
HeatPumpQuery5.Process();
HeatPumpQuery6.Process();
MELCloudQueryReplyEngine();
MQTTClient1.loop();
MQTTClient2.loop();
TelnetServer.loop();
HeatPump.Process();
MELCloud.Process();
wifiManager.process();
// -- Config Saver -- //
if (shouldSaveConfig) { saveConfig(); } // Handles WiFiManager Settings Changes
// -- Heat Pump Write Command Handler -- //
if (HeatPump.Status.Write_To_Ecodan_OK && WriteInProgress) { // A write command is executing
DEBUG_PRINTLN(F("Write OK!")); // Pause normal processsing until complete
HeatPump.Status.Write_To_Ecodan_OK = false; // Set back to false
WriteInProgress = false; // Set back to false
if (cmd_queue_length > cmd_queue_position) {
cmd_queue_position++; // Increment the position
} else {
cmd_queue_position = 1; // All commands written, reset
cmd_queue_length = 0;
} // Dequeue the last message that was written
if (MQTTReconnect() || MQTT2Reconnect()) { PublishAllReports(); } // Publish update to the MQTT Topics
}
// -- WiFi Status Handler -- //
if (WiFi.status() != WL_CONNECTED && !wifiManager.getConfigPortalActive()) {
if (WiFiOneShot) {
wifipreviousMillis = millis();
WiFiOneShot = false;
#ifdef ESP8266 // Define the Witty ESP8266 Ports
digitalWrite(Blue_RGB_LED, LOW); // Turn the Blue LED Off
digitalWrite(Green_RGB_LED, LOW); // Turn the Green LED Off
digitalWrite(Red_RGB_LED, HIGH); // Turn the Red LED On
#endif
#ifdef ARDUINO_M5STACK_ATOMS3 // Define the M5Stack LED
leds[0] = CRGB::Red; // Turn the Red LED On
FastLED.setBrightness(255);
FastLED.show();
#endif
} // Oneshot to start the timer
if (millis() - wifipreviousMillis >= 300000) {
#ifdef ESP8266 // Define the Witty ESP8266 Ports
digitalWrite(Red_RGB_LED, HIGH); // Flash the Red LED
delay(500);
digitalWrite(Red_RGB_LED, LOW);
delay(500);
digitalWrite(Red_RGB_LED, HIGH);
delay(500);
digitalWrite(Red_RGB_LED, LOW);
delay(500);
digitalWrite(Red_RGB_LED, HIGH);
ESP.reset();
#endif
#ifdef ARDUINO_M5STACK_ATOMS3 // Define the M5Stack LED
leds[0] = CRGB::Red; // Flash the Red LED
FastLED.setBrightness(255);
FastLED.show();
delay(500);
FastLED.setBrightness(0);
FastLED.show();
delay(500);
FastLED.setBrightness(255);
FastLED.show();
delay(500);
FastLED.setBrightness(0);
FastLED.show();
delay(500);
FastLED.setBrightness(255);
FastLED.show();
ESP.restart();
#endif
} // Wait for 5 mins to try reconnects then force restart
WiFiConnectedLastLoop = false;
} else if (WiFi.status() != WL_CONNECTED && wifiManager.getConfigPortalActive()) {
#ifdef ESP8266 // Define the Witty ESP8266 Ports
digitalWrite(Blue_RGB_LED, HIGH); // Turn the Blue LED On
analogWrite(Green_RGB_LED, LOW); // Green LED on, 25% brightness
digitalWrite(Red_RGB_LED, LOW); // Turn the Red LED Off
#endif
#ifdef ARDUINO_M5STACK_ATOMS3 // Define the M5Stack LED
leds[0] = CRGB::Blue;
FastLED.setBrightness(255); // LED on, reduced brightness
FastLED.show();
#endif
WiFiConnectedLastLoop = false;
} else { // WiFi is connected
if (!WiFiConnectedLastLoop) { // Used to update LEDs only on transition of state
#ifdef ESP8266 // Define the Witty ESP8266 Ports
digitalWrite(Blue_RGB_LED, LOW); // Turn the Blue LED Off
analogWrite(Green_RGB_LED, 30); // Green LED on, 25% brightness
digitalWrite(Red_RGB_LED, LOW); // Turn the Red LED Off
#endif
#ifdef ARDUINO_M5STACK_ATOMS3 // Define the M5Stack LED
leds[0] = CRGB::Green;
FastLED.setBrightness(100); // LED on, reduced brightness
FastLED.show();
#endif
}
WiFiConnectedLastLoop = true;
}
// -- Push Button Action Handler -- //
#ifndef ARDUINO_WT32_ETH01
if (digitalRead(Reset_Button) == LOW) { // Inverted (Button Pushed is LOW)
HeatPump.SetSvrControlMode(0, HeatPump.Status.ProhibitDHW, HeatPump.Status.ProhibitHeatingZ1, HeatPump.Status.ProhibitCoolingZ1, HeatPump.Status.ProhibitHeatingZ2, HeatPump.Status.ProhibitCoolingZ2); // Exit SCM leaving state
#ifdef ESP8266 // Define the Witty ESP8266 Ports
digitalWrite(Red_RGB_LED, HIGH); // Flash the Red LED
delay(500);
digitalWrite(Red_RGB_LED, LOW);
delay(500);
digitalWrite(Red_RGB_LED, HIGH);
delay(500);
digitalWrite(Red_RGB_LED, LOW);
delay(500);
digitalWrite(Red_RGB_LED, HIGH);
delay(500);
ESP.reset();
#endif
#ifdef ARDUINO_M5STACK_ATOMS3 // Define the M5Stack LED
leds[0] = CRGB::Red; // Flash the Red LED
FastLED.show();
FastLED.setBrightness(255);
FastLED.show();
delay(500);
FastLED.setBrightness(0);
FastLED.show();
delay(500);
FastLED.setBrightness(255);
FastLED.show();
delay(500);
FastLED.setBrightness(0);
FastLED.show();
delay(500);
FastLED.setBrightness(255);
FastLED.show();
delay(500);
ESP.restart(); // No button on ETH
#endif
if (digitalRead(Reset_Button) == LOW) { // If still pressed after flashing seq - reset
#ifdef ESP8266
digitalWrite(Red_RGB_LED, LOW);
digitalWrite(Blue_RGB_LED, HIGH);
delay(500);
#endif
#ifdef ARDUINO_M5STACK_ATOMS3 // Define the M5Stack LED
leds[0] = CRGB::Blue;
FastLED.show();
FastLED.setBrightness(255);
FastLED.show();
#endif
delay(500);
wifiManager.resetSettings(); // Clear settings
}
#ifdef ESP8266
ESP.reset(); // Define the Witty ESP8266 Ports
#endif
#ifdef ESP32 // ESP32 Action
ESP.restart(); // No button on ETH
#endif
}
#endif
// -- Normal DHW Boost Handler (Enter SCM > Remove DHW Prohibit > Remain or Exit SCM) -- //
if ((HeatPump.Status.LastSystemOperationMode == 1 || HeatPump.Status.LastSystemOperationMode == 6) && HeatPump.Status.SystemOperationMode != 1 && NormalHWBoostOperating == 1) {
HeatPump.SetSvrControlMode(PreHWBoostSvrCtrlMode, 1, HeatPump.Status.ProhibitHeatingZ1, HeatPump.Status.ProhibitCoolingZ1, HeatPump.Status.ProhibitHeatingZ2, HeatPump.Status.ProhibitCoolingZ2); // Enable the Prohibit and Return Server Control Mode to the previous state when the System Operation Mode changes from Hot Water to anything else
WriteInProgress = true; // Wait For OK
NormalHWBoostOperating = 0; // Don't enter again
}
// -- CPU Loop Time End -- //
CPULoopSpeed = micros() - looppreviousMicros; // Loop Speed End Monitor
}
void HeatPumpKeepAlive(void) {
if (!HeatPump.HeatPumpConnected()) {
DEBUG_PRINTLN(F("Heat Pump Disconnected"));
#ifdef ARDUINO_M5STACK_ATOMS3
// Swap to the other pins and test the connection
if (CableConnected) {
DEBUG_PRINTLN(F("Trying to connect via Proxy Circuit Board"));
HEATPUMP_STREAM.begin(SERIAL_BAUD, SERIAL_CONFIG, FTCProxy_RxPin, FTCProxy_TxPin); // Rx, Tx
HeatPump.SetStream(&HEATPUMP_STREAM);
CableConnected = false;
} else {
DEBUG_PRINTLN(F("Trying to connect via Cable"));
HEATPUMP_STREAM.begin(SERIAL_BAUD, SERIAL_CONFIG, FTCCable_RxPin, FTCCable_TxPin); // Rx, Tx
HeatPump.SetStream(&HEATPUMP_STREAM);
CableConnected = true;
}
#endif
}
ftcpreviousMillis = millis();
HeatPump.TriggerStatusStateMachine();
}
void HeatPumpQueryStateEngine(void) {
HeatPump.StatusStateMachine(); // Full Read trigged by CurrentMessage
// Call Once Full Update is complete
if (HeatPump.UpdateComplete()) {
DEBUG_PRINTLN(F("Update Complete"));
FTCLoopSpeed = millis() - ftcpreviousMillis; // Loop Speed End
if (HeatPump.Status.FTCVersion == 0) { HeatPump.GetFTCVersion(); }
if ((MQTTReconnect() || MQTT2Reconnect()) && (HeatPump.Status.FTCVersion != 0)) { PublishAllReports(); }
}
}
void HeatPumpWriteStateEngine(void) {
HeatPump.WriteStateMachine(); // Full Read trigged by CurrentMessage
}
void MELCloudQueryReplyEngine(void) {
if (MELCloud.Status.ReplyNow) {
MELCloud.ReplyStatus(MELCloud.Status.ActiveMessage);
MELCloud.Status.ReplyNow = false;
if (MELCloud.Status.ActiveMessage == 0x32 || MELCloud.Status.ActiveMessage == 0x33 || MELCloud.Status.ActiveMessage == 0x34 || MELCloud.Status.ActiveMessage == 0x35) { // The writes
HeatPump.WriteMELCloudCMD(MELCloud.Status.ActiveMessage);
}
} else if ((MELCloud.Status.ConnectRequest) && (HeatPump.Status.FTCVersion != 0)) {
MELCloud.Connect(); // Reply to the connect request
MELCloud.Status.ConnectRequest = false;
} else if (MELCloud.Status.MELRequest1) {
MELCloud.MELNegotiate1(); // Reply to the connect request
MELCloud.Status.MELRequest1 = false;
} else if (MELCloud.Status.MELRequest2) {
MELCloud.MELNegotiate2(); // Reply to the connect request
MELCloud.Status.MELRequest2 = false;
}
}
void MQTTonDisconnect(void* response) {
DEBUG_PRINTLN(F("MQTT Disconnect"));
}
void MQTTonData(char* topic, byte* payload, unsigned int length) {
payload[length] = 0;
String Topic = topic;
String Payload = (char*)payload;
DEBUG_PRINT(F("\nReceived MQTT Message on topic "));
DEBUG_PRINT(Topic.c_str());
DEBUG_PRINT(F(" with Payload "));
DEBUG_PRINTLN(Payload.c_str());
// Curve or Temp Independent Thermostat Setting
// Heating & Cooling Zone 1 Commands
if ((Topic == MQTTCommandZone1NoModeSetpoint) || (Topic == MQTTCommand2Zone1NoModeSetpoint)) {
MQTTWriteReceived("MQTT Set Zone1 Temperature Setpoint", 6);
HeatPump.SetZoneTempSetpoint(Payload.toFloat(), HeatPump.Status.HeatingControlModeZ1, ZONE1);
HeatPump.Status.Zone1TemperatureSetpoint = Payload.toFloat();
}
// Flow Setpoint Commands
// Heating & Cooling Zone 1 Commands
if ((Topic == MQTTCommandZone1FlowSetpoint) || (Topic == MQTTCommand2Zone1FlowSetpoint)) {
MQTTWriteReceived("MQTT Set Zone1 Flow Setpoint", 6);
HeatPump.SetFlowSetpoint(Payload.toFloat(), HeatPump.Status.HeatingControlModeZ1, ZONE1);
HeatPump.Status.Zone1FlowTemperatureSetpoint = Payload.toFloat();
}
// Thermostat Setpoint
// Heating & Cooling Zone 2 Commands
if ((Topic == MQTTCommandZone2NoModeSetpoint) || (Topic == MQTTCommand2Zone2NoModeSetpoint)) {
MQTTWriteReceived("MQTT Set Zone2 Temperature Setpoint", 6);
HeatPump.SetZoneTempSetpoint(Payload.toFloat(), HeatPump.Status.HeatingControlModeZ2, ZONE2);
HeatPump.Status.Zone2TemperatureSetpoint = Payload.toFloat();
}
// Flow Setpoint Commands
// Heating & Cooling Zone 2 Commands
if ((Topic == MQTTCommandZone2FlowSetpoint) || (Topic == MQTTCommand2Zone2FlowSetpoint)) {
MQTTWriteReceived("MQTT Set Zone2 Flow Setpoint", 6);
HeatPump.SetFlowSetpoint(Payload.toFloat(), HeatPump.Status.HeatingControlModeZ2, ZONE2);
HeatPump.Status.Zone2FlowTemperatureSetpoint = Payload.toFloat();
}
// Prohibits for Server Control Mode
if ((Topic == MQTTCommandZone1ProhibitHeating) || (Topic == MQTTCommand2Zone1ProhibitHeating)) {
MQTTWriteReceived("MQTT Zone 1 Prohibit Heating", 16);
HeatPump.SetProhibits(TX_MESSAGE_SETTING_HEAT_Z1_INH_Flag, Payload.toInt());
HeatPump.Status.ProhibitHeatingZ1 = Payload.toInt();
}
if ((Topic == MQTTCommandZone1ProhibitCooling) || (Topic == MQTTCommand2Zone1ProhibitCooling)) {
MQTTWriteReceived("MQTT Zone 1 Prohibit Cooling", 16);
HeatPump.SetProhibits(TX_MESSAGE_SETTING_COOL_Z1_INH_Flag, Payload.toInt());
HeatPump.Status.ProhibitCoolingZ1 = Payload.toInt();
}
if ((Topic == MQTTCommandZone2ProhibitHeating) || (Topic == MQTTCommand2Zone2ProhibitHeating)) {
MQTTWriteReceived("MQTT Zone 2 Prohibit Heating", 16);
HeatPump.SetProhibits(TX_MESSAGE_SETTING_HEAT_Z2_INH_Flag, Payload.toInt());
HeatPump.Status.ProhibitHeatingZ2 = Payload.toInt();
}
if ((Topic == MQTTCommandZone2ProhibitCooling) || (Topic == MQTTCommand2Zone2ProhibitCooling)) {
MQTTWriteReceived("MQTT Zone 2 Prohibit Cooling", 16);
HeatPump.SetProhibits(TX_MESSAGE_SETTING_COOL_Z2_INH_Flag, Payload.toInt());
HeatPump.Status.ProhibitCoolingZ2 = Payload.toInt();
}
if ((Topic == MQTTCommandHotwaterProhibit) || (Topic == MQTTCommand2HotwaterProhibit)) {
MQTTWriteReceived("MQTT DHW Prohibit", 16);
HeatPump.SetProhibits(TX_MESSAGE_SETTING_DHW_INH_Flag, Payload.toInt());
HeatPump.Status.ProhibitDHW = Payload.toInt();
}
// Other Commands
if ((Topic == MQTTCommandHotwaterMode) || (Topic == MQTTCommand2HotwaterMode)) {
MQTTWriteReceived("MQTT Set HW Mode", 15);
HeatPump.SetDHWMode(&Payload);
}
if ((Topic == MQTTCommandHotwaterBoost) || (Topic == MQTTCommand2HotwaterBoost)) {
MQTTWriteReceived("MQTT Set HW Boost", 16);
HeatPump.ForceDHW(Payload.toInt());
HeatPump.Status.HotWaterBoostActive = Payload.toInt();
}
if ((Topic == MQTTCommandHotwaterNormalBoost) || (Topic == MQTTCommand2HotwaterNormalBoost)) {
MQTTWriteReceived("MQTT Normal DHW Boost Run", 16);
PreHWBoostSvrCtrlMode = HeatPump.Status.SvrControlMode; // Take the Server Control Mode when Entering Boost
HeatPump.SetSvrControlMode(Payload.toInt(), 1 - Payload.toInt(), HeatPump.Status.ProhibitHeatingZ1, HeatPump.Status.ProhibitCoolingZ1, HeatPump.Status.ProhibitHeatingZ2, HeatPump.Status.ProhibitCoolingZ2);
if (PreHWBoostSvrCtrlMode == 0) { HeatPump.Status.SvrControlMode = Payload.toInt(); } // Show Server Control Mode is now On
HeatPump.Status.ProhibitDHW = 1 - Payload.toInt(); // Hot Water Boost is Inverse
NormalHWBoostOperating = Payload.toInt(); // Hot Water Boost Operating is Active
}
if ((Topic == MQTTCommandSystemHolidayMode) || (Topic == MQTTCommand2SystemHolidayMode)) {
MQTTWriteReceived("MQTT Set Holiday Mode", 16);
HeatPump.SetHolidayMode(Payload.toInt());
HeatPump.Status.HolidayModeActive = Payload.toInt();
}
if ((Topic == MQTTCommandHotwaterSetpoint) || (Topic == MQTTCommand2HotwaterSetpoint)) {
MQTTWriteReceived("MQTT Set HW Setpoint", 6);
HeatPump.SetHotWaterSetpoint(Payload.toInt());
HeatPump.Status.HotWaterSetpoint = Payload.toInt();
}
if ((Topic == MQTTCommandZone1HeatingMode) || (Topic == MQTTCommand2Zone1HeatingMode)) {
MQTTWriteReceived("MQTT Set Heating Mode Zone 1", 4);
if (Payload == String("Heating Temperature")) {
HeatPump.SetHeatingControlMode(HEATING_CONTROL_MODE_ZONE_TEMP, SET_HEATING_CONTROL_MODE_Z1);
HeatPump.Status.HeatingControlModeZ1 = HEATING_CONTROL_MODE_ZONE_TEMP;
} else if (Payload == String("Heating Flow")) {
HeatPump.SetHeatingControlMode(HEATING_CONTROL_MODE_FLOW_TEMP, SET_HEATING_CONTROL_MODE_Z1);
HeatPump.Status.HeatingControlModeZ1 = HEATING_CONTROL_MODE_FLOW_TEMP;
} else if (Payload == String("Heating Compensation")) {
HeatPump.SetHeatingControlMode(HEATING_CONTROL_MODE_COMPENSATION, SET_HEATING_CONTROL_MODE_Z1);
HeatPump.Status.HeatingControlModeZ1 = HEATING_CONTROL_MODE_COMPENSATION;
} else if (Payload == String("Cooling Temperature")) {
HeatPump.SetHeatingControlMode(HEATING_CONTROL_MODE_COOL_ZONE_TEMP, SET_HEATING_CONTROL_MODE_Z1);
HeatPump.Status.HeatingControlModeZ1 = HEATING_CONTROL_MODE_COOL_ZONE_TEMP;
} else if (Payload == String("Cooling Flow")) {
HeatPump.SetHeatingControlMode(HEATING_CONTROL_MODE_COOL_FLOW_TEMP, SET_HEATING_CONTROL_MODE_Z1);
HeatPump.Status.HeatingControlModeZ1 = HEATING_CONTROL_MODE_COOL_FLOW_TEMP;
} else if (Payload == String("Dry Up")) {
HeatPump.SetHeatingControlMode(HEATING_CONTROL_MODE_DRY_UP, SET_HEATING_CONTROL_MODE_Z1);
HeatPump.Status.HeatingControlModeZ1 = HEATING_CONTROL_MODE_DRY_UP;
}
}
if ((Topic == MQTTCommandZone2HeatingMode) || (Topic == MQTTCommand2Zone2HeatingMode)) {
MQTTWriteReceived("MQTT Set Heating Mode Zone 2", 4);
if (Payload == String("Heating Temperature")) {
HeatPump.SetHeatingControlMode(HEATING_CONTROL_MODE_ZONE_TEMP, SET_HEATING_CONTROL_MODE_Z2);
HeatPump.Status.HeatingControlModeZ2 = HEATING_CONTROL_MODE_ZONE_TEMP;
} else if (Payload == String("Heating Flow")) {
HeatPump.SetHeatingControlMode(HEATING_CONTROL_MODE_FLOW_TEMP, SET_HEATING_CONTROL_MODE_Z2);
HeatPump.Status.HeatingControlModeZ2 = HEATING_CONTROL_MODE_FLOW_TEMP;
} else if (Payload == String("Heating Compensation")) {
HeatPump.SetHeatingControlMode(HEATING_CONTROL_MODE_COMPENSATION, SET_HEATING_CONTROL_MODE_Z2);
HeatPump.Status.HeatingControlModeZ2 = HEATING_CONTROL_MODE_COMPENSATION;
} else if (Payload == String("Cooling Temperature")) {
HeatPump.SetHeatingControlMode(HEATING_CONTROL_MODE_COOL_ZONE_TEMP, SET_HEATING_CONTROL_MODE_Z2);
HeatPump.Status.HeatingControlModeZ2 = HEATING_CONTROL_MODE_COOL_ZONE_TEMP;
} else if (Payload == String("Cooling Flow")) {
HeatPump.SetHeatingControlMode(HEATING_CONTROL_MODE_COOL_FLOW_TEMP, SET_HEATING_CONTROL_MODE_Z2);
HeatPump.Status.HeatingControlModeZ2 = HEATING_CONTROL_MODE_COOL_FLOW_TEMP;
} else if (Payload == String("Dry Up")) {
HeatPump.SetHeatingControlMode(HEATING_CONTROL_MODE_DRY_UP, SET_HEATING_CONTROL_MODE_Z2);
HeatPump.Status.HeatingControlModeZ2 = HEATING_CONTROL_MODE_DRY_UP;
}
}
if ((Topic == MQTTCommandSystemSvrMode) || (Topic == MQTTCommand2SystemSvrMode)) {
MQTTWriteReceived("MQTT Server Control Mode", 17);
HeatPump.SetSvrControlMode(Payload.toInt(), HeatPump.Status.ProhibitDHW, HeatPump.Status.ProhibitHeatingZ1, HeatPump.Status.ProhibitCoolingZ1, HeatPump.Status.ProhibitHeatingZ2, HeatPump.Status.ProhibitCoolingZ2);
HeatPump.Status.SvrControlMode = Payload.toInt();
}
if ((Topic == MQTTCommandSystemPower) || (Topic == MQTTCommand2SystemPower)) {
MQTTWriteReceived("MQTT Set System Power Mode", 15);
if (Payload == String("On")) {
HeatPump.SetSystemPowerMode(SYSTEM_POWER_MODE_ON);
HeatPump.Status.SystemPowerMode = SYSTEM_POWER_MODE_ON;
} else if (Payload == String("Standby")) {
HeatPump.SetSystemPowerMode(SYSTEM_POWER_MODE_STANDBY);
HeatPump.Status.SystemPowerMode = SYSTEM_POWER_MODE_STANDBY;
}
}
if ((Topic == MQTTCommandSystemUnitSize) || (Topic == MQTTCommand2SystemUnitSize)) {
MQTTWriteReceived("MQTT Set Unit Size", 15);
unitSettings.UnitSize = Payload.toFloat();
shouldSaveConfig = true; // Write the data to JSON file so if device reboots it is saved
}
if ((Topic == MQTTCommandSystemGlycol) || (Topic == MQTTCommand2SystemGlycol)) {
MQTTWriteReceived("MQTT Set Glycol Strength", 15);
if (Payload == String("0%")) {
unitSettings.GlycolStrength = 4.18;
} else if (Payload == String("10%")) {
unitSettings.GlycolStrength = 4.12;
} else if (Payload == String("20%")) {
unitSettings.GlycolStrength = 4.07;
} else if (Payload == String("30%")) {
unitSettings.GlycolStrength = 3.9;
}
shouldSaveConfig = true; // Write the data to JSON file so if device reboots it is saved
}
}
void Zone1Report(void) {
JsonDocument doc;
char Buffer[512];
doc[F("Temperature")] = HeatPump.Status.Zone1Temperature;
doc[F("Setpoint")] = HeatPump.Status.Zone1TemperatureSetpoint;
doc[F("HeatingControlMode")] = HeatingControlModeString[HeatPump.Status.HeatingControlModeZ1];
doc[F("FSP")] = round2(HeatPump.Status.Zone1FlowTemperatureSetpoint);
if ((HeatPump.Status.Zone2Temperature == 0) && (HeatPump.Status.SystemOperationMode == 2 || HeatPump.Status.SystemOperationMode == 3 || HeatPump.Status.SystemOperationMode == 7)) {
doc[F("TwoZone_Z1Working")] = 1;
} else {
doc[F("TwoZone_Z1Working")] = HeatPump.Status.TwoZone_Z1Working;
}
doc[F("ProhibitHeating")] = HeatPump.Status.ProhibitHeatingZ1;
doc[F("ProhibitCooling")] = HeatPump.Status.ProhibitCoolingZ1;
doc[F("FlowTemp")] = HeatPump.Status.Zone1FlowTemperature;
doc[F("ReturnTemp")] = HeatPump.Status.Zone1ReturnTemperature;
doc[F("HB_ID")] = Heart_Value;
serializeJson(doc, Buffer);
MQTTClient1.publish(MQTT_STATUS_ZONE1.c_str(), Buffer, false);
MQTTClient2.publish(MQTT_2_STATUS_ZONE1.c_str(), Buffer, false);
}
void Zone2Report(void) {
JsonDocument doc;
char Buffer[512];
doc[F("Temperature")] = HeatPump.Status.Zone2Temperature;
doc[F("Setpoint")] = HeatPump.Status.Zone2TemperatureSetpoint;
doc[F("HeatingControlMode")] = HeatingControlModeString[HeatPump.Status.HeatingControlModeZ2];
doc[F("FSP")] = round2(HeatPump.Status.Zone2FlowTemperatureSetpoint);
doc[F("TwoZone_Z2Working")] = HeatPump.Status.TwoZone_Z2Working;
doc[F("ProhibitHeating")] = HeatPump.Status.ProhibitHeatingZ2;
doc[F("ProhibitCooling")] = HeatPump.Status.ProhibitCoolingZ2;
doc[F("FlowTemp")] = HeatPump.Status.Zone2FlowTemperature;
doc[F("ReturnTemp")] = HeatPump.Status.Zone2ReturnTemperature;
doc[F("HB_ID")] = Heart_Value;
serializeJson(doc, Buffer);
MQTTClient1.publish(MQTT_STATUS_ZONE2.c_str(), Buffer, false);
MQTTClient2.publish(MQTT_2_STATUS_ZONE2.c_str(), Buffer, false);
}
void HotWaterReport(void) {
JsonDocument doc;
char Buffer[1024];
doc[F("Temperature")] = HeatPump.Status.HotWaterTemperature;
doc[F("TempTHW5A")] = HeatPump.Status.HotWaterTemperatureTHW5A;
doc[F("Setpoint")] = HeatPump.Status.HotWaterSetpoint;
doc[F("HotWaterBoostActive")] = HeatPump.Status.HotWaterBoostActive;
doc[F("HotWaterEcoBoostActive")] = NormalHWBoostOperating;
doc[F("ProhibitDHW")] = HeatPump.Status.ProhibitDHW;
doc[F("DHWActive")] = HeatPump.Status.DHWActive;
doc[F("HotWaterControlMode")] = HotWaterControlModeString[HeatPump.Status.HotWaterControlMode];
doc[F("LegionellaSetpoint")] = HeatPump.Status.LegionellaSetpoint;
doc[F("HotWaterMaxTDrop")] = HeatPump.Status.HotWaterMaximumTempDrop;
doc[F("HotWaterPhase")] = DHWPhaseString[HeatPump.Status.DHWHeatSourcePhase];
doc[F("HB_ID")] = Heart_Value;
serializeJson(doc, Buffer);
MQTTClient1.publish(MQTT_STATUS_HOTWATER.c_str(), Buffer, false);
MQTTClient2.publish(MQTT_2_STATUS_HOTWATER.c_str(), Buffer, false);
}
void SystemReport(void) {
JsonDocument doc;
char Buffer[1024];
float HeatOutputPower, CoolOutputPower, UnitSizeFactor, Instant_CoP;
double OutputPower = (((float)HeatPump.Status.PrimaryFlowRate / 60) * (float)HeatPump.Status.HeaterDeltaT * unitSettings.GlycolStrength); // Approx Heat Capacity of Fluid in Use
// Unit Size Factoring
if (unitSettings.UnitSize == 4.0) {
UnitSizeFactor = 0.4;
} else if (unitSettings.UnitSize == 5.0) {
UnitSizeFactor = 0.6;
} else if (unitSettings.UnitSize == 7.5) {
UnitSizeFactor = 0.95;
} else if (unitSettings.UnitSize == 8.0) {
UnitSizeFactor = 1.0;
} else if ((unitSettings.UnitSize == 6.0) || (unitSettings.UnitSize == 8.5)) { // 6kW is limited 8.5 unit, only maximum power is capped
UnitSizeFactor = 1.1;
} else if (unitSettings.UnitSize == 10.0) {
UnitSizeFactor = 1.3;
} else if ((unitSettings.UnitSize == 11.2) || (unitSettings.UnitSize == 14.0)) { // 11.2kW is limited 14kW unit, only maximum power is capped
UnitSizeFactor = 1.6;
} else if (unitSettings.UnitSize == 12.0) {
UnitSizeFactor = 1.7;
}
double EstInputPower = (((((((float)HeatPump.Status.CompressorFrequency * 2) * ((float)HeatPump.Status.HeaterOutputFlowTemperature * 0.8)) / 1000) / 2) - HeatPump.Status.InputPower) * ((HeatPump.Status.InputPower + 1) - HeatPump.Status.InputPower) / ((HeatPump.Status.InputPower + 1) - HeatPump.Status.InputPower) + HeatPump.Status.InputPower) * UnitSizeFactor;
if (EstInputPower == 0 && (HeatPump.Status.ImmersionActive == 1 || HeatPump.Status.Booster1Active == 1 || HeatPump.Status.Booster2Active == 1)) { EstInputPower = HeatPump.Status.InputPower; } // Account for Immersion or Booster Instead of HP
if (OutputPower < 0) {
HeatOutputPower = 0;
CoolOutputPower = fabsf(OutputPower);
} else {
if (OutputPower == 0 && (HeatPump.Status.ImmersionActive == 1 || HeatPump.Status.Booster1Active == 1 || HeatPump.Status.Booster2Active == 1)) { HeatOutputPower = HeatPump.Status.OutputPower; } // Account for Immersion or Booster Instead of HP
else {
HeatOutputPower = OutputPower;
}
CoolOutputPower = 0;
}
// Instant CoP measurement from computed estimates
if (OutputPower > 0 && EstInputPower > 0) {
Instant_CoP = OutputPower / EstInputPower;
} else {
Instant_CoP = 0;
}
doc[F("HeaterFlow")] = HeatPump.Status.HeaterOutputFlowTemperature;
doc[F("HeaterReturn")] = HeatPump.Status.HeaterReturnFlowTemperature;
doc[F("FlowReturnDeltaT")] = HeatPump.Status.HeaterDeltaT;
doc[F("OutsideTemp")] = HeatPump.Status.OutsideTemperature;
doc[F("Defrost")] = DefrostModeString[HeatPump.Status.Defrost];
doc[F("InputPower")] = HeatPump.Status.InputPower;
doc[F("HeaterPower")] = HeatPump.Status.OutputPower;
doc[F("EstInputPower")] = round2(EstInputPower);
doc[F("EstHeatOutputPower")] = round2(HeatOutputPower);
doc[F("EstCoolOutputPower")] = round2(CoolOutputPower);
doc[F("Instant_CoP")] = round2(Instant_CoP);
doc[F("Compressor")] = HeatPump.Status.CompressorFrequency;
doc[F("SystemPower")] = SystemPowerModeString[HeatPump.Status.SystemPowerMode];
if (HeatPump.Status.Defrost == 2) {
doc[F("SystemOperationMode")] = "Defrosting";
} else {
doc[F("SystemOperationMode")] = SystemOperationModeString[HeatPump.Status.SystemOperationMode];
}
doc[F("HolidayMode")] = HeatPump.Status.HolidayModeActive;
doc[F("FlowRate")] = HeatPump.Status.PrimaryFlowRate;
doc[F("RunHours")] = HeatPump.Status.RunHours;
doc[F("HB_ID")] = Heart_Value;
serializeJson(doc, Buffer);
MQTTClient1.publish(MQTT_STATUS_SYSTEM.c_str(), Buffer, false);
MQTTClient2.publish(MQTT_2_STATUS_SYSTEM.c_str(), Buffer, false);
}
void AdvancedReport(void) {
JsonDocument doc;
char Buffer[1024];
doc[F("FlowTMax")] = HeatPump.Status.FlowTempMax;
doc[F("FlowTMin")] = HeatPump.Status.FlowTempMin;
doc[F("BoilerFlow")] = HeatPump.Status.ExternalBoilerFlowTemperature;
doc[F("BoilerReturn")] = HeatPump.Status.ExternalBoilerReturnTemperature;
doc[F("MixingTemp")] = HeatPump.Status.MixingTemperature;
doc[F("MixingStep")] = HeatPump.Status.MixingStep;
doc[F("Immersion")] = OFF_ON_String[HeatPump.Status.ImmersionActive];
doc[F("Booster")] = OFF_ON_String[HeatPump.Status.Booster1Active];
doc[F("Booster2")] = OFF_ON_String[HeatPump.Status.Booster2Active];
doc[F("ThreeWayValve")] = HeatPump.Status.ThreeWayValve;
doc[F("PrimaryWaterPump")] = OFF_ON_String[HeatPump.Status.PrimaryWaterPump];
doc[F("RefrigeTemp")] = HeatPump.Status.RefrigeTemp;
doc[F("CondensingTemp")] = HeatPump.Status.CondensingTemp;
doc[F("HeatingActive")] = HeatingRunningBinary[HeatPump.Status.SystemOperationMode];
doc[F("CoolingActive")] = CoolingRunningBinary[HeatPump.Status.SystemOperationMode];
doc[F("HB_ID")] = Heart_Value;
serializeJson(doc, Buffer);
MQTTClient1.publish(MQTT_STATUS_ADVANCED.c_str(), Buffer, false);
MQTTClient2.publish(MQTT_2_STATUS_ADVANCED.c_str(), Buffer, false);
}
void EnergyReport(void) {
JsonDocument doc;
char Buffer[512];
float heat_cop, cool_cop, dhw_cop, ctotal, dtotal, total_cop;
// A check for errors before calculating CoP
if ((HeatPump.Status.DeliveredHeatingEnergy == 0) && (HeatPump.Status.ConsumedHeatingEnergy > 0)) {
HeatPump.Status.ConsumedHeatingEnergy = 0; // Re-write
}
if ((HeatPump.Status.DeliveredCoolingEnergy == 0) && (HeatPump.Status.ConsumedCoolingEnergy > 0)) {
HeatPump.Status.ConsumedCoolingEnergy = 0; // Re-write
}
if ((HeatPump.Status.DeliveredHotWaterEnergy == 0) && (HeatPump.Status.ConsumedHotWaterEnergy > 0)) {
HeatPump.Status.ConsumedHotWaterEnergy = 0; // Re-write
}
// CoP Calculations to avoid divide by 0 occuring
if (HeatPump.Status.ConsumedHeatingEnergy > 0) {
heat_cop = HeatPump.Status.DeliveredHeatingEnergy / HeatPump.Status.ConsumedHeatingEnergy;
} else {
heat_cop = 0;
}
if (HeatPump.Status.ConsumedCoolingEnergy > 0) {
cool_cop = HeatPump.Status.DeliveredCoolingEnergy / HeatPump.Status.ConsumedCoolingEnergy;
} else {
cool_cop = 0;
}
if (HeatPump.Status.ConsumedHotWaterEnergy > 0) {
dhw_cop = (HeatPump.Status.DeliveredHotWaterEnergy / HeatPump.Status.ConsumedHotWaterEnergy);
} else {
dhw_cop = 0;
}
// CoP Totals
ctotal = (HeatPump.Status.ConsumedHeatingEnergy + HeatPump.Status.ConsumedCoolingEnergy + HeatPump.Status.ConsumedHotWaterEnergy);
dtotal = (HeatPump.Status.DeliveredHeatingEnergy + HeatPump.Status.DeliveredCoolingEnergy + HeatPump.Status.DeliveredHotWaterEnergy);
if (ctotal != 0) {
total_cop = dtotal / ctotal;
} else {
total_cop = 0;
}
// Write into the JSON with 2dp rounding
doc[F("CHEAT")] = round2(HeatPump.Status.ConsumedHeatingEnergy);
doc[F("CCOOL")] = round2(HeatPump.Status.ConsumedCoolingEnergy);
doc[F("CDHW")] = round2(HeatPump.Status.ConsumedHotWaterEnergy);
doc[F("DHEAT")] = round2(HeatPump.Status.DeliveredHeatingEnergy);
doc[F("DCOOL")] = round2(HeatPump.Status.DeliveredCoolingEnergy);
doc[F("DDHW")] = round2(HeatPump.Status.DeliveredHotWaterEnergy);
doc[F("CTOTAL")] = round2(ctotal);
doc[F("DTOTAL")] = round2(dtotal);
doc[F("HEAT_CoP")] = round2(heat_cop);
doc[F("COOL_CoP")] = round2(cool_cop);
doc[F("DHW_CoP")] = round2(dhw_cop);
doc[F("TOTAL_CoP")] = round2(total_cop);
doc[F("HB_ID")] = Heart_Value;
serializeJson(doc, Buffer);
MQTTClient1.publish(MQTT_STATUS_ENERGY.c_str(), Buffer, false);
MQTTClient2.publish(MQTT_2_STATUS_ENERGY.c_str(), Buffer, false);
}
void AdvancedTwoReport(void) {
JsonDocument doc;
char Buffer[1024];
int ErrorCode = ((String(HeatPump.Status.ErrCode1, HEX)).toInt() * 100) + (String(HeatPump.Status.ErrCode2, HEX)).toInt();
doc[F("SvrControlMode")] = HeatPump.Status.SvrControlMode;
doc[F("WaterPump2")] = OFF_ON_String[HeatPump.Status.WaterPump2];
doc[F("WaterPump4")] = OFF_ON_String[HeatPump.Status.WaterPump4];
if (!HeatPump.Status.Simple2Zone) {
doc[F("WaterPump3")] = OFF_ON_String[HeatPump.Status.WaterPump3a];
} else {
doc[F("WaterPump3")] = OFF_ON_String[HeatPump.Status.WaterPump3b];
}
doc[F("WaterPump13")] = OFF_ON_String[HeatPump.Status.WaterPump13];
doc[F("ThreeWayValve2")] = HeatPump.Status.ThreeWayValve2;
doc[F("RefrigeFltCode")] = RefrigeFltCodeString[HeatPump.Status.RefrigeFltCode];
if (ErrorCode == 8000) {
doc[F("ErrCode")] = String("Normal");
} else {
doc[F("ErrCode")] = ErrorCode;
}