forked from arendst/Tasmota
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxsns_85_opentherm_protocol.ino
422 lines (375 loc) · 17.3 KB
/
xsns_85_opentherm_protocol.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
#include "OpenTherm.h"
#ifdef USE_OPENTHERM
// Temperature tolerance. If temperature setpoint difference is less than the value,
// OT (1)(Control setpoint) command will be skipped
#define OPENTHERM_BOILER_SETPOINT_TOLERANCE 1.0
typedef union {
uint8_t m_flags;
struct
{
uint8_t notSupported : 1; // If set, boiler does not support this command
uint8_t supported : 1; // Set if at least one response were successfull
uint8_t retryCount : 2; // Retry counter before notSupported flag being set
};
} OpenThermParamFlags;
typedef union {
float m_float;
uint8_t m_u8;
uint16_t m_u16;
unsigned long m_ul;
bool m_bool;
} ResponseStorage;
typedef struct OpenThermCommandT
{
const char *m_command_name;
uint8_t m_command_code;
OpenThermParamFlags m_flags;
ResponseStorage m_results[2];
unsigned long (*m_ot_make_request)(OpenThermCommandT *self, OT_BOILER_STATUS_T *boilerStatus);
void (*m_ot_parse_response)(OpenThermCommandT *self, OT_BOILER_STATUS_T *boilerStatus, unsigned long response);
void (*m_ot_appent_telemetry)(OpenThermCommandT *self);
} OpenThermCommand;
OpenThermCommand sns_opentherm_commands[] = {
{// Get/Set Slave Status Flags
.m_command_name = "SLAVE",
.m_command_code = 0,
// OpenTherm ID(0) should never go into the notSupported state due to some connectivity issues
// otherwice it may lose boiler control
.m_flags = {.supported = 1},
.m_results = {{.m_u8 = 0}, {.m_u8 = 0}},
.m_ot_make_request = sns_opentherm_set_slave_flags,
.m_ot_parse_response = sns_opentherm_parse_slave_flags,
.m_ot_appent_telemetry = sns_opentherm_tele_slave_flags},
{// Set boiler temperature
.m_command_name = "BTMP",
.m_command_code = 0,
// OpenTherm ID(1) also should never go into the notSupported state due to some connectivity issues
.m_flags = {.supported = 1},
.m_results = {{.m_u8 = 0}, {.m_u8 = 0}},
.m_ot_make_request = sns_opentherm_set_boiler_temperature,
.m_ot_parse_response = sns_opentherm_parse_set_boiler_temperature,
.m_ot_appent_telemetry = sns_opentherm_tele_boiler_temperature},
{// Set Hot Water temperature
.m_command_name = "HWTMP",
.m_command_code = 0,
// OpenTherm ID(56) may not be supported
.m_flags = 0,
.m_results = {{.m_u8 = 0}, {.m_u8 = 0}},
.m_ot_make_request = sns_opentherm_set_boiler_dhw_temperature,
.m_ot_parse_response = sns_opentherm_parse_boiler_dhw_temperature,
.m_ot_appent_telemetry = sns_opentherm_tele_boiler_dhw_temperature},
{// Read Application-specific fault flags and OEM fault code
.m_command_name = "ASFF",
.m_command_code = 0,
.m_flags = 0,
.m_results = {{.m_u8 = 0}, {.m_u8 = 0}},
.m_ot_make_request = sns_opentherm_get_flags,
.m_ot_parse_response = sns_opentherm_parse_flags,
.m_ot_appent_telemetry = sns_opentherm_tele_flags},
{// Read An OEM-specific diagnostic/service code
.m_command_name = "OEMD",
.m_command_code = 0,
.m_flags = 0,
.m_results = {{.m_u8 = 0}, {.m_u8 = 0}},
.m_ot_make_request = sns_opentherm_get_oem_diag,
.m_ot_parse_response = sns_opentherm_parse_oem_diag,
.m_ot_appent_telemetry = sns_opentherm_tele_oem_diag},
{// Read Flame modulation
.m_command_name = "FLM",
.m_command_code = (uint8_t)OpenThermMessageID::RelModLevel,
.m_flags = 0,
.m_results = {{.m_u8 = 0}, {.m_u8 = 0}},
.m_ot_make_request = sns_opentherm_get_generic_float,
.m_ot_parse_response = sns_opentherm_parse_flame_modulation,
.m_ot_appent_telemetry = sns_opentherm_tele_generic_float},
{// Read Boiler Temperature
.m_command_name = "TB",
.m_command_code = (uint8_t)OpenThermMessageID::Tboiler,
.m_flags = 0,
.m_results = {{.m_u8 = 0}, {.m_u8 = 0}},
.m_ot_make_request = sns_opentherm_get_generic_float,
.m_ot_parse_response = sns_opentherm_parse_boiler_temperature,
.m_ot_appent_telemetry = sns_opentherm_tele_generic_float},
{// Read DHW temperature
.m_command_name = "TDHW",
.m_command_code = (uint8_t)OpenThermMessageID::Tdhw,
.m_flags = 0,
.m_results = {{.m_u8 = 0}, {.m_u8 = 0}},
.m_ot_make_request = sns_opentherm_get_generic_float,
.m_ot_parse_response = sns_opentherm_parse_generic_float,
.m_ot_appent_telemetry = sns_opentherm_tele_generic_float},
{// Read Outside temperature
.m_command_name = "TOUT",
.m_command_code = (uint8_t)OpenThermMessageID::Toutside,
.m_flags = 0,
.m_results = {{.m_u8 = 0}, {.m_u8 = 0}},
.m_ot_make_request = sns_opentherm_get_generic_float,
.m_ot_parse_response = sns_opentherm_parse_generic_float,
.m_ot_appent_telemetry = sns_opentherm_tele_generic_float},
{// Read Return water temperature
.m_command_name = "TRET",
.m_command_code = (uint8_t)OpenThermMessageID::Tret,
.m_flags = 0,
.m_results = {{.m_u8 = 0}, {.m_u8 = 0}},
.m_ot_make_request = sns_opentherm_get_generic_float,
.m_ot_parse_response = sns_opentherm_parse_generic_float,
.m_ot_appent_telemetry = sns_opentherm_tele_generic_float},
{// Read DHW setpoint
.m_command_name = "DHWS",
.m_command_code = (uint8_t)OpenThermMessageID::TdhwSet,
.m_flags = 0,
.m_results = {{.m_u8 = 0}, {.m_u8 = 0}},
.m_ot_make_request = sns_opentherm_get_generic_float,
.m_ot_parse_response = sns_opentherm_parse_dhw_setpoint,
.m_ot_appent_telemetry = sns_opentherm_tele_generic_float},
{// Read max CH water setpoint
.m_command_name = "TMAX",
.m_command_code = (uint8_t)OpenThermMessageID::MaxTSet,
.m_flags = 0,
.m_results = {{.m_u8 = 0}, {.m_u8 = 0}},
.m_ot_make_request = sns_opentherm_get_generic_float,
.m_ot_parse_response = sns_opentherm_parse_generic_float,
.m_ot_appent_telemetry = sns_opentherm_tele_generic_float},
};
/////////////////////////////////// Process Slave Status Flags & Control //////////////////////////////////////////////////
unsigned long sns_opentherm_set_slave_flags(struct OpenThermCommandT *self, struct OT_BOILER_STATUS_T *status)
{
bool centralHeatingIsOn = status->m_enableCentralHeating;
if (status->m_useDiagnosticIndicationAsHeatRequest) {
centralHeatingIsOn |= OpenTherm::isDiagnostic(status->m_slave_raw_status);
}
if (self->m_results[1].m_bool != centralHeatingIsOn) {
AddLog_P2(LOG_LEVEL_INFO,
PSTR("[OTH]: Central Heating transitioning from %s to %s"),
self->m_results[1].m_bool ? "on" : "off",
status->m_enableCentralHeating ? "on" : "off");
}
self->m_results[1].m_bool = centralHeatingIsOn;
unsigned int data = centralHeatingIsOn |
(status->m_enableHotWater << 1) |
(status->m_enableCooling << 2) |
(status->m_enableOutsideTemperatureCompensation << 3) |
(status->m_enableCentralHeating2 << 4);
data <<= 8;
return OpenTherm::buildRequest(OpenThermRequestType::READ, OpenThermMessageID::Status, data);
}
void sns_opentherm_parse_slave_flags(struct OpenThermCommandT *self, struct OT_BOILER_STATUS_T *boilerStatus, unsigned long response)
{
boilerStatus->m_slave_raw_status = response;
self->m_results[0].m_ul = response;
}
#define OT_FLAG_TO_ON_OFF(status, flag) ((((status) & (flag)) != 0) ? 1 : 0)
void sns_opentherm_tele_slave_flags(struct OpenThermCommandT *self)
{
unsigned long st = self->m_results[0].m_ul;
ResponseAppend_P(PSTR("{\"FAULT\":%d,\"CH\":%d,\"DHW\":%d,\"FL\":%d,\"COOL\":%d,\"CH2\":%d,\"DIAG\":%d,\"RAW\":%lu}"),
OT_FLAG_TO_ON_OFF(st, 0x01),
OT_FLAG_TO_ON_OFF(st, 0x02),
OT_FLAG_TO_ON_OFF(st, 0x04),
OT_FLAG_TO_ON_OFF(st, 0x08),
OT_FLAG_TO_ON_OFF(st, 0x10),
OT_FLAG_TO_ON_OFF(st, 0x20),
OT_FLAG_TO_ON_OFF(st, 0x40),
st);
}
/////////////////////////////////// Set Boiler Temperature //////////////////////////////////////////////////
unsigned long sns_opentherm_set_boiler_temperature(struct OpenThermCommandT *self, struct OT_BOILER_STATUS_T *status)
{
// Assuming some boilers might write setpoint temperature into the Flash memory
// Having PID controlled appliance may produce a lot of small fluctuations in the setpoint value
// wearing out Boiler flash memory.
float diff = abs(status->m_boilerSetpoint - self->m_results[0].m_float);
// Ignore small changes in the boiler setpoint temperature
if (diff < OPENTHERM_BOILER_SETPOINT_TOLERANCE)
{
return -1;
}
AddLog_P2(LOG_LEVEL_INFO,
PSTR("[OTH]: Setting Boiler Temp. Old: %d, New: %d"),
(int)self->m_results[0].m_float,
(int)status->m_boilerSetpoint);
self->m_results[0].m_float = status->m_boilerSetpoint;
unsigned int data = OpenTherm::temperatureToData(status->m_boilerSetpoint);
return OpenTherm::buildRequest(OpenThermMessageType::WRITE_DATA, OpenThermMessageID::TSet, data);
}
void sns_opentherm_parse_set_boiler_temperature(struct OpenThermCommandT *self, struct OT_BOILER_STATUS_T *boilerStatus, unsigned long response)
{
self->m_results[1].m_float = OpenTherm::getFloat(response);
}
void sns_opentherm_tele_boiler_temperature(struct OpenThermCommandT *self)
{
char requested[FLOATSZ];
dtostrfd(self->m_results[0].m_float, Settings.flag2.temperature_resolution, requested);
char actual[FLOATSZ];
dtostrfd(self->m_results[1].m_float, Settings.flag2.temperature_resolution, actual);
// indicate fault if tepmerature demand and actual setpoint are greater then tolerance
bool isFault = abs(self->m_results[1].m_float - self->m_results[0].m_float) > OPENTHERM_BOILER_SETPOINT_TOLERANCE;
ResponseAppend_P(PSTR("{\"FAULT\":%d,\"REQ\":%s,\"ACT\": %s}"),
(int)isFault,
requested,
actual);
}
/////////////////////////////////// Set Domestic Hot Water Temperature //////////////////////////////////////////////////
unsigned long sns_opentherm_set_boiler_dhw_temperature(struct OpenThermCommandT *self, struct OT_BOILER_STATUS_T *status)
{
// The same consideration as for the boiler temperature
float diff = abs(status->m_hotWaterSetpoint - self->m_results[0].m_float);
// Ignore small changes in the boiler setpoint temperature
if (diff < OPENTHERM_BOILER_SETPOINT_TOLERANCE)
{
return -1;
}
AddLog_P2(LOG_LEVEL_INFO,
PSTR("[OTH]: Setting Hot Water Temp. Old: %d, New: %d"),
(int)self->m_results[0].m_float,
(int)status->m_hotWaterSetpoint);
self->m_results[0].m_float = status->m_hotWaterSetpoint;
unsigned int data = OpenTherm::temperatureToData(status->m_hotWaterSetpoint);
return OpenTherm::buildRequest(OpenThermMessageType::WRITE_DATA, OpenThermMessageID::TdhwSet, data);
}
void sns_opentherm_parse_boiler_dhw_temperature(struct OpenThermCommandT *self, struct OT_BOILER_STATUS_T *boilerStatus, unsigned long response)
{
self->m_results[1].m_float = OpenTherm::getFloat(response);
}
void sns_opentherm_tele_boiler_dhw_temperature(struct OpenThermCommandT *self)
{
char requested[FLOATSZ];
dtostrfd(self->m_results[0].m_float, Settings.flag2.temperature_resolution, requested);
char actual[FLOATSZ];
dtostrfd(self->m_results[1].m_float, Settings.flag2.temperature_resolution, actual);
ResponseAppend_P(PSTR("{\"REQ\":%s,\"ACT\": %s}"),
requested,
actual);
}
/////////////////////////////////// App Specific Fault Flags //////////////////////////////////////////////////
unsigned long sns_opentherm_get_flags(struct OpenThermCommandT *self, struct OT_BOILER_STATUS_T *)
{
return OpenTherm::buildRequest(OpenThermRequestType::READ, OpenThermMessageID::ASFflags, 0);
}
void sns_opentherm_parse_flags(struct OpenThermCommandT *self, struct OT_BOILER_STATUS_T *boilerStatus, unsigned long response)
{
uint8_t fault_code = (response >> 8) & 0xFF;
uint8_t oem_fault_code = response & 0xFF;
boilerStatus->m_fault_code = fault_code;
boilerStatus->m_oem_fault_code = fault_code;
self->m_results[0].m_u8 = fault_code;
self->m_results[1].m_u8 = oem_fault_code;
}
void sns_opentherm_tele_flags(struct OpenThermCommandT *self)
{
ResponseAppend_P(PSTR("{\"FC\":%d,\"OFC\":%d}"),
(int)self->m_results[0].m_u8,
(int)self->m_results[1].m_u8);
}
/////////////////////////////////// OEM Diag Code //////////////////////////////////////////////////
unsigned long sns_opentherm_get_oem_diag(struct OpenThermCommandT *self, struct OT_BOILER_STATUS_T *)
{
return OpenTherm::buildRequest(OpenThermRequestType::READ, OpenThermMessageID::OEMDiagnosticCode, 0);
}
void sns_opentherm_parse_oem_diag(struct OpenThermCommandT *self, struct OT_BOILER_STATUS_T *boilerStatus, unsigned long response)
{
uint16_t diag_code = (uint16_t)response & 0xFFFF;
boilerStatus->m_oem_diag_code = diag_code;
self->m_results[0].m_u16 = diag_code;
}
void sns_opentherm_tele_oem_diag(struct OpenThermCommandT *self)
{
ResponseAppend_P(PSTR("%d"), (int)self->m_results[0].m_u16);
}
/////////////////////////////////// Generic Single Float /////////////////////////////////////////////////
unsigned long sns_opentherm_get_generic_float(struct OpenThermCommandT *self, struct OT_BOILER_STATUS_T *)
{
return OpenTherm::buildRequest(OpenThermRequestType::READ, (OpenThermMessageID)self->m_command_code, 0);
}
void sns_opentherm_parse_generic_float(struct OpenThermCommandT *self, struct OT_BOILER_STATUS_T *boilerStatus, unsigned long response)
{
self->m_results[0].m_float = OpenTherm::getFloat(response);
}
void sns_opentherm_tele_generic_float(struct OpenThermCommandT *self)
{
char str[FLOATSZ];
dtostrfd(self->m_results[0].m_float, Settings.flag2.temperature_resolution, str);
ResponseAppend_P(PSTR("%s"), str);
}
/////////////////////////////////// Specific Floats Rerports to the /////////////////////////////////////////////////
void sns_opentherm_parse_dhw_setpoint(struct OpenThermCommandT *self, struct OT_BOILER_STATUS_T *boilerStatus, unsigned long response)
{
self->m_results[0].m_float = OpenTherm::getFloat(response);
boilerStatus->m_hotWaterSetpoint_read = self->m_results[0].m_float;
}
void sns_opentherm_parse_flame_modulation(struct OpenThermCommandT *self, struct OT_BOILER_STATUS_T *boilerStatus, unsigned long response)
{
self->m_results[0].m_float = OpenTherm::getFloat(response);
boilerStatus->m_flame_modulation_read = self->m_results[0].m_float;
}
void sns_opentherm_parse_boiler_temperature(struct OpenThermCommandT *self, struct OT_BOILER_STATUS_T *boilerStatus, unsigned long response)
{
self->m_results[0].m_float = OpenTherm::getFloat(response);
boilerStatus->m_boiler_temperature_read = self->m_results[0].m_float;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define SNS_OT_COMMANDS_COUNT (sizeof(sns_opentherm_commands) / sizeof(OpenThermCommand))
int sns_opentherm_current_command = SNS_OT_COMMANDS_COUNT;
unsigned long sns_opentherm_get_next_request(struct OT_BOILER_STATUS_T *boilerStatus)
{
// get next and loop the command
if (++sns_opentherm_current_command >= SNS_OT_COMMANDS_COUNT)
{
sns_opentherm_current_command = 0;
}
struct OpenThermCommandT *cmd = &sns_opentherm_commands[sns_opentherm_current_command];
// Return error if command known as not supported
if (cmd->m_flags.notSupported)
{
return -1;
}
// Retrurn OT compatible request
return cmd->m_ot_make_request(cmd, boilerStatus);
}
void sns_opentherm_check_retry_request()
{
if (sns_opentherm_current_command >= SNS_OT_COMMANDS_COUNT)
{
return;
}
struct OpenThermCommandT *cmd = &sns_opentherm_commands[sns_opentherm_current_command];
bool canRetry = ++cmd->m_flags.retryCount < 3;
// In case of last retry and if this command never respond successfully, set notSupported flag
if (!canRetry && !cmd->m_flags.supported)
{
cmd->m_flags.notSupported = true;
AddLog_P2(LOG_LEVEL_ERROR,
PSTR("[OTH]: command %s is not supported by the boiler. Last status: %s"),
cmd->m_command_name,
sns_ot_master->statusToString(sns_ot_master->getLastResponseStatus()));
}
}
void sns_opentherm_process_success_response(struct OT_BOILER_STATUS_T *boilerStatus, unsigned long response)
{
if (sns_opentherm_current_command >= SNS_OT_COMMANDS_COUNT)
{
return;
}
struct OpenThermCommandT *cmd = &sns_opentherm_commands[sns_opentherm_current_command];
// mark command as supported
cmd->m_flags.supported = true;
cmd->m_ot_parse_response(cmd, boilerStatus, response);
}
void sns_opentherm_dump_telemetry()
{
bool add_coma = false;
for (int i = 0; i < SNS_OT_COMMANDS_COUNT; ++i)
{
struct OpenThermCommandT *cmd = &sns_opentherm_commands[i];
if (!cmd->m_flags.supported)
{
continue;
}
ResponseAppend_P(PSTR("%s\"%s\":"), add_coma ? "," : "", cmd->m_command_name);
cmd->m_ot_appent_telemetry(cmd);
add_coma = true;
}
}
#endif