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
| #!/usr/bin/php
<?php
//***********************************
$output = shell_exec("sudo /usr/local/bin/mbus-serial-request-data -d -b 2400 /dev/ttyUSB-MBUS 81"); //usb drv RPi- "m-busadress 81"
$xmloutput=substr($output,strpos($output,'<MBusData>'));
$xmloutput = new SimpleXMLElement($xmloutput);
$counterID=$xmloutput->SlaveInformation->Id;
$heatValue=$xmloutput->DataRecord[1]->Value; //Unit 100 Wh (or 0.1 kWh)
$coolValue=$xmloutput->DataRecord[2]->Value; //Unit 100 Wh (or 0.1 kWh)
$ontimeheatValue=$xmloutput->DataRecord[6]->Value; //Unit hours
$ontimecoolValue=$xmloutput->DataRecord[7]->Value; //Unit hours
$tempoutValue=$xmloutput->DataRecord[8]->Value; //Unit K or C
$tempreturnValue=$xmloutput->DataRecord[9]->Value; //Unit K or C
$tempdiffValue=$xmloutput->DataRecord[10]->Value; //Unit K or C
$powerinstValue=$xmloutput->DataRecord[11]->Value; //Unit 100 W (or 0.1 kW)
$powermaxValue=$xmloutput->DataRecord[12]->Value; //Unit 100 W (or 0.1 kW)
$flowinstValue=$xmloutput->DataRecord[13]->Value; //Unit 1000 m3/h or liter/h
$flowmaxValue=$xmloutput->DataRecord[14]->Value; //Unit 1000 m3/h or liter/h
$heatenergymonthValue=$xmloutput->DataRecord[17]->Value; //Unit 100 Wh (or 0.1 kWh)
$coolenergymonthValue=$xmloutput->DataRecord[18]->Value; //Unit 100 Wh (or 0.1 kWh)
$DomoticzIP="http://127.0.0.1:8080"; // Enter withour a trailing /! For example "http://127.0.0.1:8080"
$IDXTflow=125; //in Domoticz: type temperature, name WP Kamstrup Ta
$IDXTreturn=126; //in Domoticz: type temperature, name WP Kamstrup Tr
$IDXTdiff=127; //in Domoticz: type temperature, name WP Kamstrup Tdiff
$IDXHeat=133; //in Domoticz: type counter, energy generated, divide 1 kWh, name WP Heat E
$IDXCool=132; //in Domoticz: type counter, energy generated, divide 1 kWh, name WP Cool E
$IDXFlow=130; //in Domoticz: type waterflow, name WP Flow
$IDXPower=131; //in Domoticz: type verbruik / elektra, name WP Vermogen
$IDXWP_COP=135; //in Domoticz: type custom, name WP COP
$IDXWP_Power_E=136; //Index number of P electrical power (in Watts) in Domoticz
$IDXWP_Energy_E=137; //Index number of WP electrical energy (in kWh) in Domoticz
$IDXWP_Day_COP=138; //Index number of WP Day COP. Type Counter.
$IDXHP_Status=97; //Index number of HP Status.
$IDXAC_Energy_E=143; //Index number of AC Energy. Type Electric Instant plus Counter.
$IDXVE_Energy_E=197; //Index number of Ventilation Energy. Type Electric Instant plus Counter.
$IDXQO_Energy_E=198; //Index number of Quooker Energy. Type Electric Instant plus Counter.
$IDXIH_Energy_E=199; //Index number of Immersion Heater. Type Electric Instant plus Counter.
$IDXPV_Energy_E=146; //Index number of PV Energy. Type P1 smart meter
//// Retrieving heat pump consumption data from ABB energy meter
// Retrieving energy (in 0.01 kWh) and remove first 8 characters.
$WP_Energy_E = substr(shell_exec("/mbrtu/mbrtu -d /dev/ttyUSB-RS485 -P -a2 -f3 -tuint64 -n4 -r0x5000"), 8);
usleep(250000);
// Retrieving power (in 0.01 W), remove first 8 characters, convert to integer and divide by 100 to get Watts.
$WP_Power_E = ((int)substr(shell_exec("/mbrtu/mbrtu -d /dev/ttyUSB-RS485 -P -a2 -f3 -tint32 -n2 -r0x5B14"), 8))/100;
usleep(250000);
//// Retrieving Air Conditioning consumption data from ABB energy meter
// Retrieving energy (in 0.01 kWh) and remove first 8 characters, convert to integer and multiply by 10 to get Wh.
$AC_Energy_E = ((int)substr(shell_exec("/mbrtu/mbrtu -d /dev/ttyUSB-RS485 -P -a3 -f3 -tuint64 -n4 -r0x5000"), 8))*10;
usleep(250000);
// Retrieving power (in 0.01 W), remove first 8 characters, convert to integer and divide by 100 to get Watts.
$AC_Power_E = ((int)substr(shell_exec("/mbrtu/mbrtu -d /dev/ttyUSB-RS485 -P -a3 -f3 -tint32 -n2 -r0x5B14"), 8))/100;
usleep(250000);
//// Retrieving Ventilation System consumption data from ABB energy meter
// Retrieving energy (in 0.01 kWh) and remove first 8 characters, convert to integer and multiply by 10 to get Wh.
$VE_Energy_E = ((int)substr(shell_exec("/mbrtu/mbrtu -d /dev/ttyUSB-RS485 -P -a4 -f3 -tuint64 -n4 -r0x5000"), 8))*10;
usleep(250000);
// Retrieving power (in 0.01 W), remove first 8 characters, convert to integer and divide by 100 to get Watts.
$VE_Power_E = ((int)substr(shell_exec("/mbrtu/mbrtu -d /dev/ttyUSB-RS485 -P -a4 -f3 -tint32 -n2 -r0x5B14"), 8))/100;
usleep(250000);
//// Retrieving Quooker consumption data from ABB energy meter
// Retrieving energy (in 0.01 kWh) and remove first 8 characters, convert to integer and multiply by 10 to get Wh.
$QO_Energy_E = ((int)substr(shell_exec("/mbrtu/mbrtu -d /dev/ttyUSB-RS485 -P -a6 -f3 -tuint64 -n4 -r0x5000"), 8))*10;
usleep(250000);
// Retrieving power (in 0.01 W), remove first 8 characters, convert to integer and divide by 100 to get Watts.
$QO_Power_E = ((int)substr(shell_exec("/mbrtu/mbrtu -d /dev/ttyUSB-RS485 -P -a6 -f3 -tint32 -n2 -r0x5B14"), 8))/100;
usleep(250000);
//// Retrieving Immersion Heater consumption data from ABB energy meter
// Retrieving energy (in 0.01 kWh) and remove first 8 characters, convert to integer and multiply by 10 to get Wh.
$IH_Energy_E = ((int)substr(shell_exec("/mbrtu/mbrtu -d /dev/ttyUSB-RS485 -P -a7 -f3 -tuint64 -n4 -r0x5000"), 8))*10;
usleep(250000);
// Retrieving power (in 0.01 W), remove first 8 characters, convert to integer and divide by 100 to get Watts.
$IH_Power_E = ((int)substr(shell_exec("/mbrtu/mbrtu -d /dev/ttyUSB-RS485 -P -a7 -f3 -tint32 -n2 -r0x5B14"), 8))/100;
usleep(250000);
//// Retrieving PV data from ABB energy meter
// Retrieving energy consumption (in 0.01 kWh) and remove first 8 characters, convert to integer and multiply by 10 to get Wh.
$PVusage = ((int)substr(shell_exec("/mbrtu/mbrtu -d /dev/ttyUSB-RS485 -P -a8 -f3 -tuint64 -n4 -r0x5000"), 8))*10;
usleep(250000);
// Retrieving energy production (in 0.01 kWh) and remove first 8 characters, convert to integer and multiply by 10 to get Wh.
$PVreturn = ((int)substr(shell_exec("/mbrtu/mbrtu -d /dev/ttyUSB-RS485 -P -a8 -f3 -tuint64 -n4 -r0x5004"), 8))*10;
usleep(250000);
// Retrieving power (in 0.01 W), remove first 8 characters, convert to integer and divide by 100 to get Watts.
$PVcons = ((int)substr(shell_exec("/mbrtu/mbrtu -d /dev/ttyUSB-RS485 -P -a8 -f3 -tint32 -n2 -r0x5B14"), 8))/100;
usleep(250000);
// PVPower is signed, if it is negative, change it to production
if ($PVcons < 0) { // There is no consumption, but production
$PVprod = -$PVcons;
$PVcons = 0;
} else {
$PVprod = 0;
}
// Function to get variable Data from a device in Domoticz
function dt($idx,$name){
global $DomoticzIP;
$json_string = file_get_contents($DomoticzIP."/json.htm?type=devices&rid=".$idx);
$parsed_json = json_decode($json_string, true);
$parsed_json = $parsed_json['result'][0];
$data = $parsed_json['Data'];
print $name." = ".$data."\n";
usleep(100000);
return $data;
}
// Retrieving HP Status from Domoticz
$HP_Status = dt($IDXHP_Status,"HP Status");
// Invert instant power reported by Kamstrup when in Cooling mode (power reported as negative)
if ($HP_Status == "Cooling") { // HP produces negative power
$powerinstValue = $powerinstValue*-1; // Invert value
}
// Calculate WP COP
if ($WP_Power_E > 100) { // Larger than 100W, to prevent calculation without compressor running
$WP_COP = round($powerinstValue*100/$WP_Power_E, 2); // Divide watt by watt, rounded at 2 decimals
} else {
$WP_COP = 0;
}
// Reduce extreme values which ruin graphs:
if ($WP_COP < 0) {
$WP_COP = 0;
}
if (($WP_COP > 9.9) && ($HP_Status == "Heating")){
$WP_COP = 9.9;
}
if (($WP_COP > 5.9) && (($HP_Status == "DHW") || ($HP_Status == "Legionella"))){
$WP_COP = 5.9;
}
if ($WP_COP > 19.9) {
$WP_COP = 19.9;
}
//Function to send to Domoticz
function ud($idx,$nvalue,$svalue){
global $DomoticzIP;
print " --- UPDATE ".$idx." ".$nvalue." ".$svalue."\n";
file_get_contents($DomoticzIP."/json.htm?type=command¶m=udevice&idx=".$idx.'&nvalue='.$nvalue.'&svalue='.$svalue);
usleep(250000);
}
//Function Electricty instant and counter to send to Domoticz
function ue($idx,$power,$energy){
global $DomoticzIP;
print " --- UPDATE ".$idx." ".$power." ".$energy."\n";
file_get_contents($DomoticzIP."/json.htm?type=command¶m=udevice&idx=".$idx.'&nvalue=0&svalue='.$power.';'.$energy);
usleep(250000);
}
//Function P1 meter to send to Domoticz
function up($idx,$usage1,$usage2,$return1,$return2,$cons,$prod){
global $DomoticzIP;
print " --- UPDATE ".$idx." ".$usage1." ".$usage2." ".$return1." ".$return2." ".$cons." ".$prod."\n";
file_get_contents($DomoticzIP."/json.htm?type=command¶m=udevice&idx=".$idx.'&nvalue=0&svalue='.$usage1.';'.$usage2.';'.$return1.';'.$return2.';'.$cons.';'.$prod);
usleep(250000);
}
// Tflow (C) to Domoticz (kamstrup Reg 8)
ud($IDXTflow,0,$tempoutValue/100);
// Treturn (C) to Domoticz (kamstrup Reg 9)
ud($IDXTreturn,0,$tempreturnValue/100);
// Tdiff (C) to Domoticz (kamstrup Reg 10)
ud($IDXTdiff,0,$tempdiffValue/100);
// Instant Heat (kWh) to Domoticz (kamstrup Reg 1)
ud($IDXHeat,0,$heatValue);
// Instant Cool (kWh) Domoticz (kamstrup Reg 2)
ud($IDXCool,0,$coolValue);
// Instant flow (ltr/min) to Domoticz (kamstrup Reg 13)
ud($IDXFlow,0,round($flowinstValue/60, 2)); // Value rounded at 2 decimals
// Instant Power (watt) to Domoticz (kamstrup Reg 11)
ud($IDXPower,0,$powerinstValue*100);
// Instant COP to Domoticz
ud($IDXWP_COP,0,$WP_COP);
// WP E-Power setting to Domoticz
ud($IDXWP_Power_E,0,$WP_Power_E);
// WP kWh meter setting to Domoticz
ud($IDXWP_Energy_E,0,$WP_Energy_E);
// AC Power+Energy setting to Domoticz
ue($IDXAC_Energy_E,$AC_Power_E,$AC_Energy_E);
// Ventilation Power+Energy setting to Domoticz
ue($IDXVE_Energy_E,$VE_Power_E,$VE_Energy_E);
// Quooker Power+Energy setting to Domoticz
ue($IDXQO_Energy_E,$QO_Power_E,$QO_Energy_E);
// Immersion Heater Power+Energy setting to Domoticz
ue($IDXIH_Energy_E,$IH_Power_E,$IH_Energy_E);
// PV Power+Energy setting to Domoticz P1 meter
up($IDXPV_Energy_E,$PVusage,0,$PVreturn,0,$PVcons,$PVprod);
?> |