-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathremeha-counters.php
298 lines (265 loc) · 15.6 KB
/
remeha-counters.php
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
<style>
body {
font-family: monaco, monospace;
font-size: 0.7em;
// font-family: arial,sans-serif;
// font-size: small;
text-align: left;
}
h1 {
font-size: 20px
}
h2 {
font-size: 14px;
}
h3 {
font-size: 10px;
}
</style>
<?php
// Uncomment to report Errors for Debug purposes
// error_reporting(E_ALL);
require('remeha_functions.php');
// remeha.ini file Variables
//
$ini_array = parse_ini_file("remeha.ini");
$ESPIPAddress = $ini_array['ESPIPAddress'];
$ESPPort = $ini_array['ESPPort'];
$retries = $ini_array['retries'];
$nanosleeptime = $ini_array['nanosleeptime'];
$echo_flag = "1";
$newline = "<br />";
$phpver = phpversion();
$remeha_counter1 = hex2bin($ini_array['remeha_counter1']);
$remeha_counter2 = hex2bin($ini_array['remeha_counter2']);
$remeha_counter3 = hex2bin($ini_array['remeha_counter3']);
$remeha_counter4 = hex2bin($ini_array['remeha_counter4']);
$fp = connect_to_esp($ESPIPAddress, $ESPPort, $retries, $newline);
if (!$fp)
{
exit("Unable to establish connection to $ESPIPAddress:$ESPPort$newline");
}
else
{
stream_set_timeout($fp, 5);
// Collect Counter Info
conditional_echo(str_repeat("=", 80) . "$newline", $echo_flag);
conditional_echo("PHP version: $phpver$newline", $echo_flag);
conditional_echo("Connected to $ESPIPAddress:$ESPPort$newline", $echo_flag);
conditional_echo("Sending request...$newline", $echo_flag);
fwrite($fp,$remeha_counter1, 10);
$data_counter1 = "";
$data_counter1 = bin2hex(fread($fp, 52));
$data_counter1U = strtoupper($data_counter1);
conditional_echo("Counter Data-1 read: $data_counter1U$newline", $echo_flag);
usleep($nanosleeptime);
fwrite($fp,$remeha_counter2, 10);
$data_counter2 = "";
$data_counter2 = bin2hex(fread($fp, 52));
$data_counter2U = strtoupper($data_counter2);
conditional_echo("Counter Data-2 read: $data_counter2U$newline", $echo_flag);
usleep($nanosleeptime);
fwrite($fp,$remeha_counter3, 10);
$data_counter3="";
$data_counter3=bin2hex(fread($fp, 52));
$data_counter3U = strtoupper($data_counter3);
conditional_echo("Counter Data-3 read: $data_counter3U$newline", $echo_flag);
usleep($nanosleeptime);
fwrite($fp,$remeha_counter4, 10);
$data_counter4="";
$data_counter4=bin2hex(fread($fp, 52));
$data_counter4U = strtoupper($data_counter4);
conditional_echo("Counter Data-4 read: $data_counter4U$newline", $echo_flag);
$output = counter_data_dump($data_counter1, $data_counter2, $data_counter3, $data_counter4, $echo_flag, $newline);
fclose($fp);
}
// Time to 'Work the COUNTER Data'
//
function counter_data_dump($data_counter1, $data_counter2, $data_counter3, $data_counter4, $echo_flag, $newline)
{
// Manipulate data & Do a CRC Check
$decode_cnt1 = str_split($data_counter1, 2);
$hexstr_cnt1 = str_split($data_counter1, 52);
$hexstrPayload_cnt1 = substr($data_counter1, 2, 44);
$hexstrCRC_cnt1 = substr($data_counter1, 46, 4);
$crcCalc_cnt1 = crc16_modbus($hexstrPayload_cnt1);
$decode_cnt2 = str_split($data_counter2, 2);
$hexstr_cnt2 = str_split($data_counter2, 52);
$hexstrPayload_cnt2 = substr($data_counter2, 2, 44);
$hexstrCRC_cnt2 = substr($data_counter2, 46, 4);
$crcCalc_cnt2 = crc16_modbus($hexstrPayload_cnt2);
$decode_cnt3 = str_split($data_counter3, 2);
$hexstr_cnt3 = str_split($data_counter3, 52);
$hexstrPayload_cnt3 = substr($data_counter3, 2, 44);
$hexstrCRC_cnt3 = substr($data_counter3, 46, 4);
$crcCalc_cnt3 = crc16_modbus($hexstrPayload_cnt3);
$decode_cnt4 = str_split($data_counter4, 2);
$hexstr_cnt4 = str_split($data_counter4, 52);
$hexstrPayload_cnt4 = substr($data_counter4, 2, 44);
$hexstrCRC_cnt4 = substr($data_counter4, 46, 4);
$crcCalc_cnt4 = crc16_modbus($hexstrPayload_cnt4);
// Concatenate Counter data to work with
$concat_counter = substr($hexstrPayload_cnt1, 12, 32).substr($hexstrPayload_cnt2, 12, 32).substr($hexstrPayload_cnt3, 12, 32).substr($hexstrPayload_cnt4, 12, 32);
$decode_counter = str_split($concat_counter, 2);
// Write the contents to the file
$ini_array = parse_ini_file("remeha.ini");
$log_data = $ini_array['log_data'];
$path = $ini_array['path_to_logs'];
$filename = $ini_array['counter_data_log'];
$file = "$path$filename";
date_default_timezone_set('Europe/Amsterdam');
$date = date_create();
if (($hexstrCRC_cnt1 == $crcCalc_cnt1) && ($hexstrCRC_cnt2 == $crcCalc_cnt2) && ($hexstrCRC_cnt3 == $crcCalc_cnt3) && ($hexstrCRC_cnt4 == $crcCalc_cnt4))
{
conditional_echo("Data Integrity Good - CRCs Compute OK$newline", $echo_flag);
if ($log_data == 2)
{
$datatowrite = date_format($date, 'Y-m-d H:i:s') . ' | 02 ' . $hexstrPayload_cnt1 . ' ' . $hexstrCRC_cnt1 . ' ' .'03 | ' . '02 ' . $hexstrPayload_cnt2 . ' ' . $hexstrCRC_cnt2 . ' ' .'03 | ' . '02 ' . $hexstrPayload_cnt3 . ' ' . $hexstrCRC_cnt3 . ' ' .'03 | ' . '02 ' . $hexstrPayload_cnt4 . ' ' . $hexstrCRC_cnt4 . ' ' .'03 |' . "\n";
file_put_contents($file, $datatowrite, FILE_APPEND);
conditional_echo("Data written to log: $file$newline", $echo_flag);
}
conditional_echo(str_repeat("=", 80) . "$newline", $echo_flag);
}
else
{
if (($log_data == 1) || ($log_data == 2))
{
$datatowrite = '**** CRC Error **** | ' . date_format($date, 'Y-m-d H:i:s') . ' | 02 ' . $hexstrPayload_cnt1 . ' ' . $hexstrCRC_cnt1 . ' ' .'03 | ' . '02 ' . $hexstrPayload_cnt2 . ' ' . $hexstrCRC_cnt2 . ' ' .'03 | ' . '02 ' . $hexstrPayload_cnt3 . ' ' . $hexstrCRC_cnt3 . ' ' .'03 | ' . '02 ' . $hexstrPayload_cnt4 . ' ' . $hexstrCRC_cnt4 . ' ' .'03 |' . "\n";
file_put_contents($file, $datatowrite, FILE_APPEND);
conditional_echo("Data written to log: $file$newline", $echo_flag);
}
conditional_echo("$newline", $echo_flag);
conditional_echo("************** CRC ERROR!!!! ***********$newline", $echo_flag);
return; # Don't continue with updating Counter data
}
// Counter Info
$pumphours_ch_dhw = $decode_counter["0"];
$pumphours_ch_dhw .= $decode_counter["1"];
$threewayvalvehours = $decode_counter["2"];
$threewayvalvehours .= $decode_counter["3"];
$hours_ch_dhw = $decode_counter["4"];
$hours_ch_dhw .= $decode_counter["5"];
$hours_dhw = $decode_counter["6"];
$hours_dhw .= $decode_counter["7"];
$powerhours_ch_dhw = $decode_counter["8"];
$powerhours_ch_dhw .= $decode_counter["9"];
$pumpstarts_ch_dhw = $decode_counter["10"];
$pumpstarts_ch_dhw .= $decode_counter["11"];
$nr_threewayvalvecycles = $decode_counter["12"];
$nr_threewayvalvecycles .= $decode_counter["13"];
$burnerstarts_dhw = $decode_counter["14"];
$burnerstarts_dhw .= $decode_counter["15"];
$tot_burnerstarts_ch_dhw = $decode_counter["16"];
$tot_burnerstarts_ch_dhw .= $decode_counter["17"];
$failed_burnerstarts = $decode_counter["18"];
$failed_burnerstarts .= $decode_counter["19"];
$nr_flame_loss = $decode_counter["20"];
$nr_flame_loss .= $decode_counter["21"];
// END Counter Info
//Convert Hex2Dec
$pumphours_ch_dhw = hexdec($pumphours_ch_dhw)*2;
$threewayvalvehours = hexdec($threewayvalvehours)*2;
$hours_ch_dhw = hexdec($hours_ch_dhw)*2;
$hours_dhw = hexdec($hours_dhw)*1;
$powerhours_ch_dhw = hexdec($powerhours_ch_dhw)*2;
$pumpstarts_ch_dhw = hexdec($pumpstarts_ch_dhw)*8;
$nr_threewayvalvecycles = hexdec($nr_threewayvalvecycles)*8;
$burnerstarts_dhw = hexdec($burnerstarts_dhw)*8;
$tot_burnerstarts_ch_dhw = hexdec($tot_burnerstarts_ch_dhw)*8;
$failed_burnerstarts = hexdec($failed_burnerstarts)*1;
$nr_flame_loss = hexdec($nr_flame_loss)*1;
// END Convert Hex2Dec
// START Display Counters
echo "Counters Received: " . date_format($date, 'Y-m-d H:i:s') . "$newline";
echo str_repeat("=", 80) . "$newline";
echo "Hours run pump CH+DHW: $pumphours_ch_dhw hours$newline";
echo "Hours run 3-way valve DHW: $threewayvalvehours hours$newline";
echo "Hours run CH+DHW: $hours_ch_dhw hours$newline";
echo "Hours run DHW: $hours_dhw hours$newline";
echo "Power Supply available hours: $powerhours_ch_dhw hours$newline";
echo "Pump starts CH+DHW: $pumpstarts_ch_dhw starts$newline";
echo "Number of 3-way valve cycles: $nr_threewayvalvecycles cycles$newline";
echo "Burner Starts DHW: $burnerstarts_dhw starts$newline";
echo "Total Burner Starts CH+DHW: $tot_burnerstarts_ch_dhw starts$newline";
echo "Failed burner starts: $failed_burnerstarts starts$newline";
echo "Number of flame loss: $nr_flame_loss times$newline";
echo str_repeat("=", 80) . "$newline";
// END Display Counters
// Update Domoticz Devices with collected values
// DomoticZ Device ID's
$pumphours_ch_dhwIDX = $ini_array['pumphours_ch_dhwIDX'];
$threewayvalvehoursIDX = $ini_array['threewayvalvehoursIDX'];
$hours_ch_dhwIDX = $ini_array['hours_ch_dhwIDX'];
$hours_dhwIDX = $ini_array['hours_dhwIDX'];
$powerhours_ch_dhwIDX = $ini_array['powerhours_ch_dhwIDX'];
$pumpstarts_ch_dhwIDX = $ini_array['pumpstarts_ch_dhwIDX'];
$nr_threewayvalvecyclesIDX = $ini_array['nr_threewayvalvecyclesIDX'];
$burnerstarts_dhwIDX = $ini_array['burnerstarts_dhwIDX'];
$tot_burnerstarts_ch_dhwIDX = $ini_array['tot_burnerstarts_ch_dhwIDX'];
$failed_burnerstartsIDX = $ini_array['failed_burnerstartsIDX'];
$nr_flame_lossIDX = $ini_array['nr_flame_lossIDX'];
// END Device ID's
// Set variables for cURL updates & call udevice function to update
$DOMOIPAddress = $ini_array['DOMOIPAddress'];
$DOMOPort = $ini_array['DOMOPort'];
$Username = $ini_array['Username'];
$Password = $ini_array['Password'];
$DOMOUpdate = $ini_array['DOMOUpdate'];
$DOMOUpdateInterval = $ini_array['DOMOUpdateInterval'];
$url = "http://$Username:$Password@$DOMOIPAddress:$DOMOPort/json.htm?type=devices&filter=all&order=ID";
$json_string = file_get_contents($url);
$parsed_json = json_decode($json_string, true);
$DOMOdevices_lastupdate = array_lookup($parsed_json, $pumphours_ch_dhwIDX, "LastUpdate");
$now = date('Y-m-d H:i:s');
$time_diff_mins = number_format((strtotime($now) - strtotime($DOMOdevices_lastupdate))/60, 2);
echo "Last Update:$DOMOdevices_lastupdate Time Now:$now Elapsed:$time_diff_mins<br />";
if ($time_diff_mins > $DOMOUpdateInterval) {$DOMOUpdateAll = 1;}
else {$DOMOUpdateAll = $ini_array['DOMOUpdateAll'];}
// Set variables for cURL updates & call udevice function to update
If ($DOMOUpdateAll == 1)
{
$DOMOpumphours_ch_dhw = udevice($pumphours_ch_dhwIDX, 0, $pumphours_ch_dhw, $DOMOIPAddress, $DOMOPort, $Username, $Password, $DOMOUpdate);
$DOMOthreewayvalvehours = udevice($threewayvalvehoursIDX, 0, $threewayvalvehours, $DOMOIPAddress, $DOMOPort, $Username, $Password, $DOMOUpdate);
$DOMOhours_ch_dhw = udevice($hours_ch_dhwIDX, 0, $hours_ch_dhw, $DOMOIPAddress, $DOMOPort, $Username, $Password, $DOMOUpdate);
$DOMOhours_dhw = udevice($hours_dhwIDX, 0, $hours_dhw, $DOMOIPAddress, $DOMOPort, $Username, $Password, $DOMOUpdate);
$DOMOpowerhours_ch_dhw = udevice($powerhours_ch_dhwIDX, 0, $powerhours_ch_dhw, $DOMOIPAddress, $DOMOPort, $Username, $Password, $DOMOUpdate);
$DOMOpumpstarts_ch_dhw = udevice($pumpstarts_ch_dhwIDX, 0, $pumpstarts_ch_dhw, $DOMOIPAddress, $DOMOPort, $Username, $Password, $DOMOUpdate);
$DOMOnr_threewayvalvecycles = udevice($nr_threewayvalvecyclesIDX, 0, $nr_threewayvalvecycles, $DOMOIPAddress, $DOMOPort, $Username, $Password, $DOMOUpdate);
$DOMOburnerstarts_dhw = udevice($burnerstarts_dhwIDX, 0, $burnerstarts_dhw, $DOMOIPAddress, $DOMOPort, $Username, $Password, $DOMOUpdate);
$DOMOtot_burnerstarts_ch_dhw = udevice($tot_burnerstarts_ch_dhwIDX, 0, $tot_burnerstarts_ch_dhw, $DOMOIPAddress, $DOMOPort, $Username, $Password, $DOMOUpdate);
$DOMOfailed_burnerstarts = udevice($failed_burnerstartsIDX, 0, $failed_burnerstarts, $DOMOIPAddress, $DOMOPort, $Username, $Password, $DOMOUpdate);
$DOMOnr_flame_loss = udevice($nr_flame_lossIDX, 0, $nr_flame_loss, $DOMOIPAddress, $DOMOPort, $Username, $Password, $DOMOUpdate);
echo "Update All: Domoticz Update:$DOMOUpdate Update All:$DOMOUpdateAll$newline";
echo str_repeat("=", 80) . "$newline";
}
else
{
$DOMOType = "Data"; // Lookup the 'Data' devices
$DOMOpumphours_ch_dhw_array = preg_replace('/[^0-9.]+/', '', array_lookup($parsed_json, $pumphours_ch_dhwIDX, $DOMOType));
If ($DOMOpumphours_ch_dhw_array != $pumphours_ch_dhw) {$DOMOpumphours_ch_dhw = udevice($pumphours_ch_dhwIDX, 0, $pumphours_ch_dhw, $DOMOIPAddress, $DOMOPort, $Username, $Password, $DOMOUpdate);}
$DOMOthreewayvalvehours_array = preg_replace('/[^0-9.]+/', '', array_lookup($parsed_json, $threewayvalvehoursIDX, $DOMOType));
If ($DOMOthreewayvalvehours_array != $threewayvalvehours) {$DOMOthreewayvalvehours = udevice($threewayvalvehoursIDX, 0, $threewayvalvehours, $DOMOIPAddress, $DOMOPort, $Username, $Password, $DOMOUpdate);}
$DOMOhours_ch_dhw_array = preg_replace('/[^0-9.]+/', '', array_lookup($parsed_json, $hours_ch_dhwIDX, $DOMOType));
If ($DOMOhours_ch_dhw_array != $hours_ch_dhw) {$DOMOhours_ch_dhw = udevice($hours_ch_dhwIDX, 0, $hours_ch_dhw, $DOMOIPAddress, $DOMOPort, $Username, $Password, $DOMOUpdate);}
$DOMOhours_dhw_array = preg_replace('/[^0-9.]+/', '', array_lookup($parsed_json, $hours_dhwIDX, $DOMOType));
If ($DOMOhours_dhw_array != $hours_dhw) {$DOMOhours_dhw = udevice($hours_dhwIDX, 0, $hours_dhw, $DOMOIPAddress, $DOMOPort, $Username, $Password, $DOMOUpdate);}
$DOMOpowerhours_ch_dhw_array = preg_replace('/[^0-9.]+/', '', array_lookup($parsed_json, $powerhours_ch_dhwIDX, $DOMOType));
If ($DOMOpowerhours_ch_dhw_array != $powerhours_ch_dhw) {$DOMOpowerhours_ch_dhw = udevice($powerhours_ch_dhwIDX, 0, $powerhours_ch_dhw, $DOMOIPAddress, $DOMOPort, $Username, $Password, $DOMOUpdate);}
$DOMOpumpstarts_ch_dhw_array = preg_replace('/[^0-9.]+/', '', array_lookup($parsed_json, $pumpstarts_ch_dhwIDX, $DOMOType));
If ($DOMOpumpstarts_ch_dhw_array != $pumpstarts_ch_dhw) {$DOMOpumpstarts_ch_dhw = udevice($pumpstarts_ch_dhwIDX, 0, $pumpstarts_ch_dhw, $DOMOIPAddress, $DOMOPort, $Username, $Password, $DOMOUpdate);}
$DOMOnr_threewayvalvecycles_array = preg_replace('/[^0-9.]+/', '', array_lookup($parsed_json, $nr_threewayvalvecyclesIDX, $DOMOType));
If ($DOMOnr_threewayvalvecycles_array != $nr_threewayvalvecycles) {$DOMOnr_threewayvalvecycles = udevice($nr_threewayvalvecyclesIDX, 0, $nr_threewayvalvecycles, $DOMOIPAddress, $DOMOPort, $Username, $Password, $DOMOUpdate);}
$DOMOburnerstarts_dhw_array = preg_replace('/[^0-9.]+/', '', array_lookup($parsed_json, $burnerstarts_dhwIDX, $DOMOType));
If ($DOMOburnerstarts_dhw_array != $burnerstarts_dhw) {$DOMOburnerstarts_dhw = udevice($burnerstarts_dhwIDX, 0, $burnerstarts_dhw, $DOMOIPAddress, $DOMOPort, $Username, $Password, $DOMOUpdate);}
$DOMOtot_burnerstarts_ch_dhw_array = preg_replace('/[^0-9.]+/', '', array_lookup($parsed_json, $tot_burnerstarts_ch_dhwIDX, $DOMOType));
If ($DOMOtot_burnerstarts_ch_dhw_array != $tot_burnerstarts_ch_dhw) {$DOMOtot_burnerstarts_ch_dhw = udevice($tot_burnerstarts_ch_dhwIDX, 0, $tot_burnerstarts_ch_dhw, $DOMOIPAddress, $DOMOPort, $Username, $Password, $DOMOUpdate);}
$DOMOfailed_burnerstarts_array = preg_replace('/[^0-9.]+/', '', array_lookup($parsed_json, $failed_burnerstartsIDX, $DOMOType));
If ($DOMOfailed_burnerstarts_array != $failed_burnerstarts) {$DOMOfailed_burnerstarts = udevice($failed_burnerstartsIDX, 0, $failed_burnerstarts, $DOMOIPAddress, $DOMOPort, $Username, $Password, $DOMOUpdate);}
$DOMOnr_flame_loss_array = preg_replace('/[^0-9.]+/', '', array_lookup($parsed_json, $nr_flame_lossIDX, $DOMOType));
If ($DOMOnr_flame_loss_array != $nr_flame_loss) {$DOMOnr_flame_loss = udevice($nr_flame_lossIDX, 0, $nr_flame_loss, $DOMOIPAddress, $DOMOPort, $Username, $Password, $DOMOUpdate);}
echo "Update Changes Only: Domoticz Update:$DOMOUpdate Update All:$DOMOUpdateAll$newline";
echo str_repeat("=", 80) . "$newline";
}
// END set variables for cURL updates
}
?>