-
Notifications
You must be signed in to change notification settings - Fork 3
/
OpenTherm.cpp
573 lines (473 loc) · 18.1 KB
/
OpenTherm.cpp
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
/*
OpenTherm.cpp - OpenTherm Communication Library For Arduino, ESP8266
Copyright 2018, Ihor Melnyk
Update by raf1000 in reference to Brink Renovent HR
*/
#include "OpenTherm.h"
OpenTherm::OpenTherm(int inPin, int outPin, bool isSlave):
status(OpenThermStatus::NOT_INITIALIZED),
inPin(inPin),
outPin(outPin),
isSlave(isSlave),
response(0),
responseStatus(OpenThermResponseStatus::NONE),
responseTimestamp(0),
handleInterruptCallback(NULL),
processResponseCallback(NULL)
{
}
void OpenTherm::begin(void(*handleInterruptCallback)(void), void(*processResponseCallback)(unsigned long, OpenThermResponseStatus))
{
pinMode(inPin, INPUT);
pinMode(outPin, OUTPUT);
if (handleInterruptCallback != NULL) {
this->handleInterruptCallback = handleInterruptCallback;
attachInterrupt(digitalPinToInterrupt(inPin), handleInterruptCallback, CHANGE);
}
activateBoiler();
status = OpenThermStatus::READY;
this->processResponseCallback = processResponseCallback;
}
void OpenTherm::begin(void(*handleInterruptCallback)(void))
{
begin(handleInterruptCallback, NULL);
}
bool ICACHE_RAM_ATTR OpenTherm::isReady()
{
return status == OpenThermStatus::READY;
}
int ICACHE_RAM_ATTR OpenTherm::readState() {
return digitalRead(inPin);
}
void OpenTherm::setActiveState() {
digitalWrite(outPin, LOW);
}
void OpenTherm::setIdleState() {
digitalWrite(outPin, HIGH);
}
void OpenTherm::activateBoiler() {
setIdleState();
delay(1000);
}
void OpenTherm::sendBit(bool high) {
if (high) setActiveState(); else setIdleState();
delayMicroseconds(500);
if (high) setIdleState(); else setActiveState();
delayMicroseconds(500);
}
bool OpenTherm::sendRequestAync(unsigned long request)
{
//Serial.println("Request: " + String(request, HEX));
noInterrupts();
const bool ready = isReady();
interrupts();
if (!ready)
return false;
status = OpenThermStatus::REQUEST_SENDING;
response = 0;
responseStatus = OpenThermResponseStatus::NONE;
sendBit(HIGH); //start bit
for (int i = 31; i >= 0; i--) {
sendBit(bitRead(request, i)); //bitRead - standardowa biblioteka Arduino
}
sendBit(HIGH); //stop bit
setIdleState();
status = OpenThermStatus::RESPONSE_WAITING;
responseTimestamp = micros();
return true;
}
unsigned long OpenTherm::sendRequest(unsigned long request)
{
if (!sendRequestAync(request)) return 0; //wyslij ramkę i podaj zero jesli nie udalo sie wysłac ramki
while (!isReady()) {
process();
yield();
}
return response;
}
bool OpenTherm::sendResponse(unsigned long request)
{
status = OpenThermStatus::REQUEST_SENDING;
response = 0;
responseStatus = OpenThermResponseStatus::NONE;
sendBit(HIGH); //start bit
for (int i = 31; i >= 0; i--) {
sendBit(bitRead(request, i));
}
sendBit(HIGH); //stop bit
setIdleState();
status = OpenThermStatus::READY;
return true;
}
OpenThermResponseStatus OpenTherm::getLastResponseStatus()
{
return responseStatus;
}
void ICACHE_RAM_ATTR OpenTherm::handleInterrupt()
{
if (isReady())
{
if (isSlave && readState() == HIGH) {
status = OpenThermStatus::RESPONSE_WAITING;
}
else {
return;
}
}
unsigned long newTs = micros();
if (status == OpenThermStatus::RESPONSE_WAITING) {
if (readState() == HIGH) {
status = OpenThermStatus::RESPONSE_START_BIT;
responseTimestamp = newTs;
}
else {
status = OpenThermStatus::RESPONSE_INVALID;
responseTimestamp = newTs;
}
}
else if (status == OpenThermStatus::RESPONSE_START_BIT) {
if ((newTs - responseTimestamp < 750) && readState() == LOW) {
status = OpenThermStatus::RESPONSE_RECEIVING;
responseTimestamp = newTs;
responseBitIndex = 0;
}
else {
status = OpenThermStatus::RESPONSE_INVALID;
responseTimestamp = newTs;
}
}
else if (status == OpenThermStatus::RESPONSE_RECEIVING) {
if ((newTs - responseTimestamp) > 750) {
if (responseBitIndex < 32) {
response = (response << 1) | !readState();
responseTimestamp = newTs;
responseBitIndex++;
}
else { //stop bit
status = OpenThermStatus::RESPONSE_READY;
responseTimestamp = newTs;
}
}
}
}
void OpenTherm::process()
{
noInterrupts();
OpenThermStatus st = status;
unsigned long ts = responseTimestamp;
interrupts();
if (st == OpenThermStatus::READY) return;
unsigned long newTs = micros();
if (st != OpenThermStatus::NOT_INITIALIZED && (newTs - ts) > 1000000) {
status = OpenThermStatus::READY;
responseStatus = OpenThermResponseStatus::TIMEOUT;
if (processResponseCallback != NULL) {
processResponseCallback(response, responseStatus);
}
}
else if (st == OpenThermStatus::RESPONSE_INVALID) {
status = OpenThermStatus::DELAY;
responseStatus = OpenThermResponseStatus::INVALID;
if (processResponseCallback != NULL) {
processResponseCallback(response, responseStatus);
}
}
else if (st == OpenThermStatus::RESPONSE_READY) {
status = OpenThermStatus::DELAY;
responseStatus = (isSlave ? isValidRequest(response) : isValidResponse(response)) ? OpenThermResponseStatus::SUCCESS : OpenThermResponseStatus::INVALID;
if (processResponseCallback != NULL) {
processResponseCallback(response, responseStatus);
}
}
else if (st == OpenThermStatus::DELAY) {
if ((newTs - ts) > 100000) {
status = OpenThermStatus::READY;
}
}
}
bool OpenTherm::parity(unsigned long frame) //odd parity
{
byte p = 0;
while (frame > 0)
{
if (frame & 1) p++;
frame = frame >> 1;
}
return (p & 1);
}
OpenThermMessageType OpenTherm::getMessageType(unsigned long message)
{
OpenThermMessageType msg_type = static_cast<OpenThermMessageType>((message >> 28) & 7);
return msg_type;
}
OpenThermMessageID OpenTherm::getDataID(unsigned long frame)
{
return (OpenThermMessageID)((frame >> 16) & 0xFF);
}
unsigned long OpenTherm::buildRequest(OpenThermMessageType type, OpenThermMessageID id, unsigned int data)
{
unsigned long request = data;
if (type == OpenThermMessageType::WRITE_DATA) {
request |= 1ul << 28;
}
request |= ((unsigned long)id) << 16;
if (parity(request)) request |= (1ul << 31);
return request;
}
unsigned long OpenTherm::buildResponse(OpenThermMessageType type, OpenThermMessageID id, unsigned int data)
{
unsigned long response = data;
response |= type << 28;
response |= ((unsigned long)id) << 16;
if (parity(response)) response |= (1ul << 31);
return response;
}
bool OpenTherm::isValidResponse(unsigned long response)
{
if (parity(response)) return false;
byte msgType = (response << 1) >> 29;
return msgType == READ_ACK || msgType == WRITE_ACK;
}
bool OpenTherm::isValidRequest(unsigned long request)
{
if (parity(request)) return false;
byte msgType = (request << 1) >> 29;
return msgType == READ_DATA || msgType == WRITE_DATA;
}
void OpenTherm::end() {
if (this->handleInterruptCallback != NULL) {
detachInterrupt(digitalPinToInterrupt(inPin));
}
}
const char *OpenTherm::statusToString(OpenThermResponseStatus status)
{
switch (status) {
case NONE: return "NONE";
case SUCCESS: return "SUCCESS";
case INVALID: return "INVALID";
case TIMEOUT: return "TIMEOUT";
default: return "UNKNOWN";
}
}
const char *OpenTherm::messageTypeToString(OpenThermMessageType message_type)
{
switch (message_type) {
case READ_DATA: return "READ_DATA";
case WRITE_DATA: return "WRITE_DATA";
case INVALID_DATA: return "INVALID_DATA";
case RESERVED: return "RESERVED";
case READ_ACK: return "READ_ACK";
case WRITE_ACK: return "WRITE_ACK";
case DATA_INVALID: return "DATA_INVALID";
case UNKNOWN_DATA_ID: return "UNKNOWN_DATA_ID";
default: return "UNKNOWN";
}
}
//building requests
unsigned long OpenTherm::buildSetBoilerStatusRequest(bool enableCentralHeating, bool enableHotWater, bool enableCooling, bool enableOutsideTemperatureCompensation, bool enableCentralHeating2) {
unsigned int data = enableCentralHeating | (enableHotWater << 1) | (enableCooling << 2) | (enableOutsideTemperatureCompensation << 3) | (enableCentralHeating2 << 4);
data <<= 8;
return buildRequest(OpenThermMessageType::READ_DATA, OpenThermMessageID::Status, data);
}
unsigned long OpenTherm::buildSetBoilerTemperatureRequest(float temperature) {
unsigned int data = temperatureToData(temperature);
return buildRequest(OpenThermMessageType::WRITE_DATA, OpenThermMessageID::TSet, data);
}
unsigned long OpenTherm::buildGetBoilerTemperatureRequest() {
return buildRequest(OpenThermMessageType::READ_DATA, OpenThermMessageID::Tboiler, 0);
}
//parsing responses
bool OpenTherm::isFault(unsigned long response) {
return response & 0x1;
}
bool OpenTherm::isCentralHeatingActive(unsigned long response) {
return response & 0x2;
}
bool OpenTherm::isHotWaterActive(unsigned long response) {
return response & 0x4;
}
bool OpenTherm::isFlameOn(unsigned long response) {
return response & 0x8;
}
bool OpenTherm::isCoolingActive(unsigned long response) {
return response & 0x10;
}
bool OpenTherm::isDiagnostic(unsigned long response) {
return response & 0x40;
}
uint16_t OpenTherm::getUInt(const unsigned long response) const {
const uint16_t u88 = response & 0xffff; // int16_t - 16 bits type maximum value is 2^16, or 65535. 0xffff= 0b1111111111111111 =>all bits are 1
return u88;
}
// Brink Renovent HR
uint8_t OpenTherm::getU8 (const unsigned long response) const {
const uint8_t u8 = response & 0xff;
return u8;
}
float OpenTherm::getFloat(const unsigned long response) const {
const uint16_t u88 = getUInt(response);
const float f = (u88 & 0x8000) ? -(0x10000L - u88) / 256.0f : u88 / 256.0f;
return f;
}
unsigned int OpenTherm::temperatureToData(float temperature) {
if (temperature < 0) temperature = 0;
if (temperature > 100) temperature = 100;
unsigned int data = (unsigned int)(temperature * 256);
return data;
}
//basic requests
unsigned long OpenTherm::setBoilerStatus(bool enableCentralHeating, bool enableHotWater, bool enableCooling, bool enableOutsideTemperatureCompensation, bool enableCentralHeating2) {
return sendRequest(buildSetBoilerStatusRequest(enableCentralHeating, enableHotWater, enableCooling, enableOutsideTemperatureCompensation, enableCentralHeating2));
}
bool OpenTherm::setBoilerTemperature(float temperature) {
unsigned long response = sendRequest(buildSetBoilerTemperatureRequest(temperature));
return isValidResponse(response);
}
float OpenTherm::getBoilerTemperature() {
unsigned long response = sendRequest(buildGetBoilerTemperatureRequest());
return isValidResponse(response) ? getFloat(response) : 0;
}
float OpenTherm::getReturnTemperature() {
unsigned long response = sendRequest(buildRequest(OpenThermRequestType::READ, OpenThermMessageID::Tret, 0));
return isValidResponse(response) ? getFloat(response) : 0;
}
bool OpenTherm::setDHWSetpoint(float temperature) {
unsigned int data = temperatureToData(temperature);
unsigned long response = sendRequest(buildRequest(OpenThermMessageType::WRITE_DATA, OpenThermMessageID::TdhwSet, data));
return isValidResponse(response);
}
float OpenTherm::getDHWTemperature() {
unsigned long response = sendRequest(buildRequest(OpenThermMessageType::READ_DATA, OpenThermMessageID::Tdhw, 0));
return isValidResponse(response) ? getFloat(response) : 0;
}
float OpenTherm::getModulation() {
unsigned long response = sendRequest(buildRequest(OpenThermRequestType::READ, OpenThermMessageID::RelModLevel, 0));
return isValidResponse(response) ? getFloat(response) : 0;
}
float OpenTherm::getPressure() {
unsigned long response = sendRequest(buildRequest(OpenThermRequestType::READ, OpenThermMessageID::CHPressure, 0));
return isValidResponse(response) ? getFloat(response) : 0;
}
unsigned char OpenTherm::getFault() {
return ((sendRequest(buildRequest(OpenThermRequestType::READ, OpenThermMessageID::ASFflags, 0)) >> 8) & 0xff);
}
// Brink Renovent HR
uint8_t OpenTherm::getBrinkTSP(BrinkTSPindex index) {
if (index > 72 || index < 0) return 0;
unsigned int TSPdata = (unsigned int)index << 8;
unsigned long response = sendRequest(buildRequest(OpenThermRequestType::READ, OpenThermMessageID::VentTSPEntry, TSPdata) );
return isValidResponse(response) ? getU8(response) : 0;
}
bool OpenTherm::setBrinkTSP(BrinkTSPindex index, uint8_t value) {
if (index > 28 || index < 0) return 0;
if (value < 0) value = 0;
if (value > 255) value = 255;
unsigned int TSPdata = value;
TSPdata |= (unsigned int)index << 8;
unsigned long response = sendRequest(buildRequest(OpenThermRequestType::WRITE_DATA, OpenThermMessageID::VentTSPEntry, TSPdata) );
return isValidResponse(response);
}
uint16_t OpenTherm::getBrink2TSP(BrinkTSPindex first_index) {
if ( first_index > 72 || first_index < 0 ) return 0;
unsigned int TSPdata = (unsigned int)first_index << 8;
unsigned long response = sendRequest(buildRequest(OpenThermRequestType::READ, OpenThermMessageID::VentTSPEntry, TSPdata) );
if ( isValidResponse(response) ) { response = getU8(response);}
else { return 0; }
TSPdata = ( (unsigned int)first_index + 1 ) << 8;
unsigned long response2 = sendRequest(buildRequest(OpenThermRequestType::READ, OpenThermMessageID::VentTSPEntry, TSPdata) );
return isValidResponse(response2) ? getUInt(response |= response2 << 8) : 0;
}
bool OpenTherm::setBrink2TSP(BrinkTSPindex first_index, uint16_t value) {
if (first_index > 4 || first_index < 0) return 0; //limit range of paramteres against wrong usage of thhe method
if (value < 0) value = 0;
if (value > 400) value = 400;
if (value <=255)
{
unsigned int TSPdata = value;
TSPdata |= (unsigned int)first_index << 8;
unsigned long response = sendRequest(buildRequest(OpenThermRequestType::WRITE_DATA, OpenThermMessageID::VentTSPEntry, TSPdata) );
return isValidResponse(response);
}
if (value > 255)
{
unsigned int TSPdata = value - 256;
TSPdata |= (unsigned int)first_index << 8;
unsigned long response1 = sendRequest(buildRequest(OpenThermRequestType::WRITE_DATA, OpenThermMessageID::VentTSPEntry, TSPdata) );
delay(200);
TSPdata = 1;
TSPdata |= ((unsigned int)first_index + 1) << 8;
unsigned long response2 = sendRequest(buildRequest(OpenThermRequestType::WRITE_DATA, OpenThermMessageID::VentTSPEntry, TSPdata) );
return ( isValidResponse(response1) && isValidResponse(response2) );
}
}
unsigned int OpenTherm::getVentRPM(OpenThermMessageID id) {
if ( id != VentRPMexhaust && id != VentRPMsupply ) return 0;
unsigned long response = sendRequest(buildRequest(OpenThermRequestType::READ, OpenThermMessageID::VentTsupplyin, 0));
return isValidResponse(response) ? getUInt(response) : 0;
}
uint8_t OpenTherm::getVentFaultCode() {
unsigned long response = sendRequest(buildRequest(OpenThermRequestType::READ, OpenThermMessageID::VentFaultBufferEntry, 0));
return isValidResponse(response) ? getU8(response) : 0;
}
unsigned int OpenTherm::getVentilation() {
unsigned long response = sendRequest(buildRequest(OpenThermRequestType::READ, OpenThermMessageID::VentNomVent, 0));
return isValidResponse(response) ? getUInt(response) : 0;
}
/** Setting the nominal ventilation, clamps to the range 0-100
* Some ventilation systems have special values for nominal value 0,1,2,3
*/
unsigned int OpenTherm::setVentilation(unsigned int nominal_value) {
if (nominal_value < 0) nominal_value = 0;
if (nominal_value > 100) nominal_value = 100;
unsigned long response = sendRequest(buildRequest(OpenThermRequestType::WRITE, OpenThermMessageID::VentNomVentSet, nominal_value));
return isValidResponse(response) ? getUInt(response) : 0;
}
/**
* Temperature getting functions for Vent systems
* Often respond with temperature '80' when the sensors aren't available
*/
float OpenTherm::getVentSupplyInTemperature() {
unsigned long response = sendRequest(buildRequest(OpenThermRequestType::READ, OpenThermMessageID::VentTsupplyin, 0));
return isValidResponse(response) ? getFloat(response) : 0;
}
float OpenTherm::getVentSupplyOutTemperature() {
unsigned long response = sendRequest(buildRequest(OpenThermRequestType::READ, OpenThermMessageID::VentTsupplyout, 0));
return isValidResponse(response) ? getFloat(response) : 0;
}
float OpenTherm::getVentExhaustInTemperature() {
unsigned long response = sendRequest(
buildRequest(OpenThermRequestType::READ, OpenThermMessageID::VentTexhaustin, 0));
return isValidResponse(response) ? getFloat(response) : 0;
}
float OpenTherm::getVentExhaustOutTemperature() {
unsigned long response = sendRequest(buildRequest(OpenThermRequestType::READ, OpenThermMessageID::VentTexhaustout, 0));
return isValidResponse(response) ? getFloat(response) : 0;
}
bool OpenTherm::getFaultIndication() { //LB0: 0=no Fault
unsigned long response = sendRequest(buildRequest(OpenThermRequestType::READ, OpenThermMessageID::VentStatus,0));
return isValidResponse(response)? (getUInt(response) & 0x1) : 0; // 0x1 = 0b0000000001, check bit on zero position
}
bool OpenTherm::getVentilationMode() { // LB1: 1=Active
unsigned long response = sendRequest(buildRequest(OpenThermRequestType::READ, OpenThermMessageID::VentStatus,0));
return isValidResponse(response) ? (getUInt(response) & 0x2) >> 1: 0; // 0x2 = 0b0000000010
}
bool OpenTherm::getBypassStatus() { // LB2: 0=Closed
unsigned long response = sendRequest(buildRequest(OpenThermRequestType::READ, OpenThermMessageID::VentStatus,0));
return isValidResponse(response) ? (getUInt(response) & 0x4) >> 2: 0; // 0x4 = 0b0000000100
}
bool OpenTherm::getBypassAutomaticStatus() { //LB3: 0=manual
unsigned long response = sendRequest(buildRequest(OpenThermRequestType::READ, OpenThermMessageID::VentStatus,0));
return isValidResponse(response) ? (getUInt(response) & 0x8) >> 3: 0; // 0x8 = 0b0000001000
}
bool OpenTherm::getDiagnosticIndication() { //LB6 0=no diagnostic (wymiana filtra)
unsigned long response = sendRequest(buildRequest(OpenThermRequestType::READ, OpenThermMessageID::VentStatus,0));
return isValidResponse(response)? (getUInt(response) & 0x20) >> 5: 0; // 0x20 = 0b000100000
}
bool OpenTherm::getBypassPosition() { // HB1: 0=Closed
unsigned long response = sendRequest(buildRequest(OpenThermRequestType::READ, OpenThermMessageID::VentStatus,0));
return isValidResponse(response) ? (getUInt(response) & 0x200) >> 9: 0; // 0x200 = 0b00000001000000000
}
bool OpenTherm::getBypassMode() { // HB2:
unsigned long response = sendRequest(buildRequest(OpenThermRequestType::READ, OpenThermMessageID::VentStatus,0));
return isValidResponse(response) ? (getUInt(response) & 0x400) >> 10: 0; // 0x200 = 0b00000010000000000
}