source: trunk/Arduino/libraries/OT_Slave/OT_Slave.cpp @ 11

Last change on this file since 11 was 11, checked in by markus, 7 years ago

Redesign: OT_V2R2_Slave -> OT_Slave

File size: 5.0 KB
Line 
1#include "OT_Slave.h"
2
3#define THERMOSTAT_IN 2
4#define THERMOSTAT_OUT 4
5
6// OpenthermData  OT_Slave::message;
7
8OT_Action OT_Slave::action;
9
10OT_Slave::OT_Slave ()
11:
12  cbAction(0),
13  chEnable(false),
14  chActive(false),
15  chTemperature(0),
16  dhwEnable(false),
17  dhwActive(false),
18  dhwTemperature(0)
19{
20}
21
22void  OT_Slave::begin (OT_Action_CB cb)
23{
24  pinMode(THERMOSTAT_IN, INPUT);
25  digitalWrite(THERMOSTAT_OUT, HIGH);
26  pinMode(THERMOSTAT_OUT, OUTPUT); // low output = high current, high output = low current
27  cbAction = cb;
28}
29
30/**
31 * Loop will act as boiler (slave) connected to Opentherm thermostat.
32 * It will wait for requests from thermostat and response with unknown data id as a fake response.
33 */
34void  OT_Slave::maintain ()
35{
36  if (OPENTHERM::getMessage(action.request)) {
37    HandleRequest(action);
38  } else if (OPENTHERM::isSent() || OPENTHERM::isIdle() || OPENTHERM::isError()) { // wait for request from thermostat
39    OPENTHERM::listen(THERMOSTAT_IN, -1);
40  }
41}
42
43void  OT_Slave::HandleRequest (OT_Action& action) 
44{
45  if (Serial) {
46    Serial.print("-> ");
47    OPENTHERM::printToSerial(action.request);
48    Serial.println();
49  }
50  delay(100); // Opentherm defines delay between request and response
51  action.response = action.request;
52  switch (action.request.id) {
53    case OT_MSGID_STATUS: // 0  (Master and slave status flags) [M:ReadAck]
54      StatusReq(action);
55      break; 
56    case OT_MSGID_CH_SETPOINT:  // 1  (Control setpoint ie CH water temp. setpoint) [M:WriteAck]
57      action.response.type = OT_MSGTYPE_WRITE_ACK;
58      CH_Temperature(action.request.f88());
59      break;
60    case OT_MSGID_SLAVE_CONFIG: // 3  (Slave configuration flags) [M:ReadAck]
61      ConfigReq(action);
62      break; 
63    case OT_MSGID_FAULT_FLAGS:  // 5  (Application-specific fault flags / OEM fault code)
64      FaultReq(action);
65      break; 
66    case 14:  // 14 (Maximum relative modulation level setting) [M:WriteAck] 
67      action.response.type = OT_MSGTYPE_WRITE_ACK;
68      break;
69    case OT_MSGID_MODULATION_LEVEL: // 17 (Relative modulation level)  [M:ReadAck|DataInvalid]
70      action.response.type = OT_MSGTYPE_DATA_INVALID;
71      break;
72    case OT_MSGID_FEED_TEMP:  // 25 (Boiler temperature)  [M:ReadAck|DataInvalid]
73      action.response.type = OT_MSGTYPE_DATA_INVALID;
74      break;
75    case OT_MSGID_DHW_SETPOINT: // 56 (DHW setpoint)
76      if (action.request.type == OT_MSGTYPE_READ_DATA) {
77        action.response.type = OT_MSGTYPE_READ_ACK;
78        action.response.f88(dhwTemperature);
79        break;
80      } else if (action.request.type == OT_MSGTYPE_WRITE_DATA) {
81        action.response.type = OT_MSGTYPE_WRITE_ACK;
82        DHW_Temperature(action.request.f88());
83      } else {
84        action.response.type = OT_MSGTYPE_UNKNOWN_DATAID; // construct fake response
85      }
86      break; 
87    case OT_MSGID_VERSION_SLAVE:  // 127  (Slave product version number and type)
88      action.response.type = OT_MSGTYPE_READ_ACK;
89      action.response.valueHB = 0;  //  product type
90      action.response.valueLB = 0;  //  product version
91      break; 
92    default:
93      action.response.type = OT_MSGTYPE_UNKNOWN_DATAID; // construct fake response
94      action.response.valueHB = 0;
95      action.response.valueLB = 0;
96  }
97  if (cbAction) {
98    (*cbAction)(action);
99  }
100  OPENTHERM::send(THERMOSTAT_OUT, action.response, OT_Slave::ListenAfterResponse); // send some response and wait for antoher request from thermostat set by callback
101}
102
103void  OT_Slave::ConfigReq (OT_Action& action)
104{
105  action.response.type = OT_MSGTYPE_READ_ACK;
106  action.response.valueHB = 0; // 0x11;  //  slave configuration:
107  action.response.valueLB = 0;  //  Slave MemberID code
108}
109
110void  OT_Slave::StatusReq (OT_Action& action)
111{
112  action.response.type = OT_MSGTYPE_READ_ACK;
113  CH_Enable(action.request.valueHB & 0x01);
114  DHW_Enable(action.request.valueHB & 0x02);
115  action.response.valueLB = 0;  //  Slave status
116  if (chActive) {
117    action.response.valueLB |= 0x02;
118  } else {
119    action.response.valueLB &= ~0x02;
120  }
121  if (dhwActive) {
122    action.response.valueLB |= 0x04;
123  } else {
124    action.response.valueLB &= ~0x04;
125  }
126}
127
128void  OT_Slave::FaultReq (OT_Action& action)
129{
130  action.response.type = OT_MSGTYPE_READ_ACK;
131  action.response.valueHB = 0;  //  Application-specific fault flags
132  action.response.valueLB = 0;  //  OEM fault code
133}
134
135bool  OT_Slave::CH_Enable (bool enable)
136{
137  chEnable = enable;
138  chActive = enable;
139  return chEnable;
140}
141
142bool  OT_Slave::CH_Disable ()
143{
144  chEnable = false;
145  chActive = false;
146  return chEnable;
147}
148
149void  OT_Slave::CH_Temperature (float temp)
150{
151  chTemperature = temp;
152}
153
154bool  OT_Slave::DHW_Enable (bool enable)
155{
156  dhwEnable = enable;
157  dhwActive = enable;
158  return dhwEnable;
159}
160
161bool  OT_Slave::DHW_Disable ()
162{
163  dhwEnable = false;
164  dhwActive = false;
165  return dhwEnable;
166}
167
168void  OT_Slave::DHW_Temperature (float temp)
169{
170  dhwTemperature = temp;
171}
172
173void  OT_Slave::ListenAfterResponse () 
174{
175  if (Serial) {
176    Serial.print("<- ");
177    OPENTHERM::printToSerial(action.response);
178    Serial.println();
179    Serial.println();
180  }
181  OPENTHERM::listen(THERMOSTAT_IN, -1);
182}
Note: See TracBrowser for help on using the repository browser.