-
Notifications
You must be signed in to change notification settings - Fork 2
/
opentherm-interface.ino
250 lines (210 loc) · 7.1 KB
/
opentherm-interface.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
// Firmware for an OpenTherm/plus interface
//
#include <Arduino.h>
#include "opentherm.h"
#define OT_BUS_IN 13 //D7
#define OT_BUS_OUT 5 //D1
#define LED 2 //D4
#define MAX_ALLOWED_OT_TIMEOUTS 10
bool requested_ch_enabled = false;
float requested_ch_set_point = 0.0;
char command_buffer[20];
unsigned int received_bytes = 0;
unsigned int debug_level = 0;
unsigned int ot_communication_error_count = 0;
struct {
bool fault = false;
bool ch_on = false;
bool dhw_on = false;
bool flame_on = false;
float modulation_level = 0;
float boiler_flow_water_temp = 0;
float max_ch_set_point = 0;
float ch_water_pressure = 0;
} boiler_state;
unsigned int message_loop_index = 0;
#define MESSAGE_LOOP_LENGTH 7
OpenthermData message_loop_list[MESSAGE_LOOP_LENGTH] = {
{OT_MSGTYPE_READ_DATA, OT_MSGID_STATUS, 0, 0},
{OT_MSGTYPE_READ_DATA, OT_MSGID_SLAVE_CONFIG, 0, 0},
{OT_MSGTYPE_READ_DATA, OT_MSGID_MODULATION_LEVEL, 0, 0},
{OT_MSGTYPE_READ_DATA, OT_MSGID_CH_WATER_PRESSURE, 0, 0},
{OT_MSGTYPE_READ_DATA, OT_MSGID_BOILER_FLOW_WATER_TEMP, 0, 0},
{OT_MSGTYPE_READ_DATA, OT_MSGID_MAX_CH_SETPOINT, 0, 0},
{OT_MSGTYPE_WRITE_DATA, OT_MSGID_CH_SETPOINT, 0, 0},
};
void setup()
{
pinMode(LED, OUTPUT);
digitalWrite(LED, HIGH);
pinMode(OT_BUS_IN, INPUT);
digitalWrite(OT_BUS_IN, HIGH);
digitalWrite(OT_BUS_OUT, HIGH);
pinMode(OT_BUS_OUT, OUTPUT);
Serial.begin(115200);
delay(3000);
Serial.print("Hello, I am an OpenTherm/plus interface");
}
unsigned int build_master_status_message()
{
bool enable_domestic_hot_water = true;
bool enable_cooling = true;
bool enable_outside_temperature_compensation = false;
bool enable_central_heating_channel_2 = false;
unsigned int data = requested_ch_enabled |
(enable_domestic_hot_water << 1) |
(enable_cooling << 2) |
(enable_outside_temperature_compensation << 3) |
(enable_central_heating_channel_2 << 4);
return data << 8;
}
OpenthermData get_next_ot_message_to_send()
{
OpenthermData next_message = message_loop_list[message_loop_index];
if (next_message.id == OT_MSGID_STATUS) {
next_message.u16(build_master_status_message());
} else if (next_message.id == OT_MSGID_CH_SETPOINT) {
next_message.f88(requested_ch_set_point);
}
return next_message;
}
void print_status()
{
Serial.print("== ch_on:");
Serial.println(boiler_state.ch_on);
Serial.print("== flame_on:");
Serial.println(boiler_state.flame_on);
Serial.print("== modulation_level:");
Serial.println(boiler_state.modulation_level);
Serial.print("== boiler_flow_water_temp:");
Serial.println(boiler_state.boiler_flow_water_temp);
if(debug_level > 0) {
Serial.print("== fault:");
Serial.println(boiler_state.fault);
Serial.print("== dhw_on:");
Serial.println(boiler_state.dhw_on);
Serial.print("== max_ch_set_point:");
Serial.println(boiler_state.max_ch_set_point);
Serial.print("== ch_water_pressure:");
Serial.println(boiler_state.ch_water_pressure);
}
Serial.println();
}
void handle_ot_response(OpenthermData response)
{
message_loop_index = (message_loop_index + 1) % MESSAGE_LOOP_LENGTH;
switch (response.id) {
case OT_MSGID_STATUS:
boiler_state.fault = bitRead(response.valueLB, 0);
boiler_state.ch_on = bitRead(response.valueLB, 1);
boiler_state.dhw_on = bitRead(response.valueLB, 2);
boiler_state.flame_on = bitRead(response.valueLB, 3);
break;
case OT_MSGID_MODULATION_LEVEL:
boiler_state.modulation_level = response.f88();
break;
case OT_MSGID_BOILER_FLOW_WATER_TEMP:
boiler_state.boiler_flow_water_temp = response.f88();
break;
case OT_MSGID_CH_WATER_PRESSURE:
boiler_state.ch_water_pressure = response.f88();
break;
case OT_MSGID_MAX_CH_SETPOINT:
boiler_state.max_ch_set_point = response.f88();
break;
}
if (debug_level > 1) {
Serial.print("<< ");
OPENTHERM::printToSerial(response);
Serial.println();
}
if (message_loop_index == 0) {
print_status();
}
delay(110);
}
void handle_ot_conversation()
{
OpenthermData received_ot_message{};
if (OPENTHERM::isIdle()) {
digitalWrite(LED, LOW);
OpenthermData message_to_send = get_next_ot_message_to_send();
if (debug_level > 1) {
Serial.print(">> ");
OPENTHERM::printToSerial(message_to_send);
Serial.println();
}
OPENTHERM::send(OT_BUS_OUT, message_to_send);
} else if (OPENTHERM::isSent()) {
OPENTHERM::listen(OT_BUS_IN, 300);
} else if (OPENTHERM::getMessage(received_ot_message)) {
OPENTHERM::stop();
digitalWrite(LED, HIGH);
ot_communication_error_count = 0;
handle_ot_response(received_ot_message);
} else if (OPENTHERM::isError()) {
OPENTHERM::stop();
ot_communication_error_count++;
if (ot_communication_error_count > MAX_ALLOWED_OT_TIMEOUTS) {
digitalWrite(LED, HIGH);
ot_communication_error_count = MAX_ALLOWED_OT_TIMEOUTS;
Serial.println("E: Too many consecutive OT response timeouts");
delay(5000);
}
if (debug_level > 0) Serial.println("W: Timeout waiting for OT slave response");
digitalWrite(LED, HIGH);
}
}
void parse_received_command()
{
char command[8];
float param = 0.0;
int parsed_count = sscanf(command_buffer, "%s %f", command, ¶m);
if (parsed_count < 1) goto exit_parse_error;
if (strncmp(command, "on", 2) == 0) {
if (parsed_count != 2 || param > 100 || param < 0) {
goto exit_parse_error;
}
requested_ch_enabled = true;
requested_ch_set_point = param;
Serial.print("==> Heating ON. Set point:");
Serial.println(param);
return;
} else if (strncmp(command_buffer, "off", 3) == 0) {
if (parsed_count != 1) goto exit_parse_error;
requested_ch_enabled = false;
Serial.println("==> Heating OFF");
return;
} else if (strncmp(command_buffer, "debug", 5) == 0) {
if (parsed_count != 2 || param > 2 || param < 0) goto exit_parse_error;
debug_level = (int) param;
Serial.print("==> Debug level ");
Serial.println(debug_level);
return;
}
exit_parse_error:
Serial.println("E: Invalid command or arguments");
}
void check_for_serial_data()
{
if (Serial.available() <= 0) {
return;
}
char incoming_byte = Serial.read();
if (received_bytes >= sizeof(command_buffer)) {
Serial.println("E: Command overflow");
received_bytes = 0;
}
if (incoming_byte == '\r') {
command_buffer[received_bytes++] = '\0';
parse_received_command();
received_bytes = 0;
} else {
command_buffer[received_bytes++] = incoming_byte;
}
}
void loop()
{
check_for_serial_data();
handle_ot_conversation();
}