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

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

-SUB: Removed references/access to 'BUILDIN_LED' which isn't accessable with Ethernet shield

File size: 6.2 KB
Line 
1#include "OT_Slave.h"
2
3#ifndef _SIM_T_BOILER_
4#define _SIM_T_BOILER_  1
5#endif
6#ifndef _SIM_T_RET_
7#define _SIM_T_RET_     1
8#endif
9
10#define THERMOSTAT_IN   2
11#define THERMOSTAT_OUT  4
12
13uint32_t        OT_Action::OTD2uint32 (const OpenthermData& data)
14{
15        uint32_t val(data.type);
16        val = ((val << 8) | data.id);
17        val = ((val << 8) | data.valueHB);
18        val = ((val << 8) | data.valueLB);
19        return val;
20}
21
22OT_Action OT_Slave::action;
23
24OT_Slave::OT_Slave ()
25:
26  cbAction(0),
27  chEnable(false),
28  chActive(false),
29  chTemperature(0),
30  dhwEnable(false),
31  dhwActive(false),
32  dhwTemperature(0),
33  tBoiler(28.0),
34  tReturn(28.0)
35{
36}
37
38void  OT_Slave::begin (OT_Action_CB cb)
39{
40  pinMode(THERMOSTAT_IN, INPUT);
41  digitalWrite(THERMOSTAT_OUT, HIGH);
42  pinMode(THERMOSTAT_OUT, OUTPUT);      // low output = high current, high output = low current
43  cbAction = cb;
44}
45
46/**
47 * Loop will act as boiler (slave) connected to Opentherm thermostat.
48 * It will wait for requests from thermostat and response with unknown data id as a fake response.
49 */
50void  OT_Slave::maintain ()
51{
52  if (OPENTHERM::getMessage(action.request)) {
53    HandleRequest(action);
54  } else if (OPENTHERM::isSent() || OPENTHERM::isIdle() || OPENTHERM::isError()) { // wait for request from thermostat
55    OPENTHERM::listen(THERMOSTAT_IN, -1);
56  }
57}
58
59void  OT_Slave::HandleRequest (OT_Action& action) 
60{
61  if (Serial) {
62    Serial.print("-> ");
63    OPENTHERM::printToSerial(action.request);
64    Serial.println();
65  }
66  delay(100); // Opentherm defines delay between request and response
67  action.response = action.request;
68  switch (action.request.id) {
69    case OT_MSGID_STATUS: // 0  (Master and slave status flags) [M:ReadAck]
70      StatusReq(action);
71      break; 
72    case OT_MSGID_CH_SETPOINT:  // 1  (Control setpoint ie CH water temp. setpoint) [M:WriteAck]
73      action.response.type = OT_MSGTYPE_WRITE_ACK;
74      CH_Temperature(action.request.f88());
75      break;
76    case OT_MSGID_SLAVE_CONFIG: // 3  (Slave configuration flags) [M:ReadAck]
77      ConfigReq(action);
78      break; 
79    case OT_MSGID_FAULT_FLAGS:  // 5  (Application-specific fault flags / OEM fault code)
80      FaultReq(action);
81      break; 
82    case 14:  // 14 (Maximum relative modulation level setting) [M:WriteAck] 
83      action.response.type = OT_MSGTYPE_WRITE_ACK;
84      break;
85    case OT_MSGID_MODULATION_LEVEL: // 17 (Relative modulation level)  [M:ReadAck|DataInvalid]
86      action.response.type = OT_MSGTYPE_DATA_INVALID;
87      break;
88    case OT_MSGID_FEED_TEMP:  // 25 (Boiler water temperature)  [M:ReadAck|DataInvalid]
89#if _SIM_T_BOILER_
90      action.response.type = OT_MSGTYPE_READ_ACK;
91      action.response.f88(GetBoilerTemperature());
92#else
93      action.response.type = OT_MSGTYPE_DATA_INVALID;
94#endif
95      break;
96#if _SIM_T_RET_
97    case OT_MSGID_RETURN_WATER_TEMP: // 28 (Return water temperature)
98      action.response.type = OT_MSGTYPE_READ_ACK;
99      action.response.f88(GetReturnTemperature());
100      break;
101#endif
102    case OT_MSGID_DHW_SETPOINT: // 56 (DHW setpoint)
103      if (action.request.type == OT_MSGTYPE_READ_DATA) {
104        action.response.type = OT_MSGTYPE_READ_ACK;
105        action.response.f88(dhwTemperature);
106        break;
107      } else if (action.request.type == OT_MSGTYPE_WRITE_DATA) {
108        action.response.type = OT_MSGTYPE_WRITE_ACK;
109        DHW_Temperature(action.request.f88());
110      } else {
111        action.response.type = OT_MSGTYPE_UNKNOWN_DATAID; // construct fake response
112      }
113      break; 
114    case OT_MSGID_VERSION_SLAVE:  // 127  (Slave product version number and type)
115      action.response.type = OT_MSGTYPE_READ_ACK;
116      action.response.valueHB = 0;  //  product type
117      action.response.valueLB = 0;  //  product version
118      break; 
119    default:
120      action.response.type = OT_MSGTYPE_UNKNOWN_DATAID; // construct fake response
121      action.response.valueHB = 0;
122      action.response.valueLB = 0;
123  }
124  if (cbAction) {
125    (*cbAction)(action);
126  }
127  OPENTHERM::send(THERMOSTAT_OUT, action.response, OT_Slave::ListenAfterResponse); // send some response and wait for antoher request from thermostat set by callback
128}
129
130void  OT_Slave::ConfigReq (OT_Action& action)
131{
132  action.response.type = OT_MSGTYPE_READ_ACK;
133  action.response.valueHB = 0; // 0x11;  //  slave configuration:
134  action.response.valueLB = 0;  //  Slave MemberID code
135}
136
137void  OT_Slave::StatusReq (OT_Action& action)
138{
139  action.response.type = OT_MSGTYPE_READ_ACK;
140  CH_Enable(action.request.valueHB & 0x01);
141  DHW_Enable(action.request.valueHB & 0x02);
142  action.response.valueLB = 0;  //  Slave status
143  if (chActive) {
144    action.response.valueLB |= 0x02;
145  } else {
146    action.response.valueLB &= ~0x02;
147  }
148  if (dhwActive) {
149    action.response.valueLB |= 0x04;
150  } else {
151    action.response.valueLB &= ~0x04;
152  }
153}
154
155void  OT_Slave::FaultReq (OT_Action& action)
156{
157  action.response.type = OT_MSGTYPE_READ_ACK;
158  action.response.valueHB = 0;  //  Application-specific fault flags
159  action.response.valueLB = 0;  //  OEM fault code
160}
161
162bool  OT_Slave::CH_Enable (bool enable)
163{
164  chEnable = enable;
165  chActive = enable;
166  return chEnable;
167}
168
169bool  OT_Slave::CH_Disable ()
170{
171  chEnable = false;
172  chActive = false;
173  return chEnable;
174}
175
176void  OT_Slave::CH_Temperature (float temp)
177{
178  chTemperature = temp;
179}
180
181bool  OT_Slave::DHW_Enable (bool enable)
182{
183  dhwEnable = enable;
184  dhwActive = enable;
185  return dhwEnable;
186}
187
188bool  OT_Slave::DHW_Disable ()
189{
190  dhwEnable = false;
191  dhwActive = false;
192  return dhwEnable;
193}
194
195void  OT_Slave::DHW_Temperature (float temp)
196{
197  dhwTemperature = temp;
198}
199
200bool    OT_Slave::IsActive ()
201{
202        return (chActive || dhwActive);
203}
204
205float   OT_Slave::GetBoilerTemperature ()
206{
207        if (IsActive()) {
208                // increase
209                float tTarget = (chTemperature > dhwTemperature) ? chTemperature : dhwTemperature;
210                if (tBoiler < tTarget) {
211                        tBoiler = (tBoiler + 2.0 * tTarget) / 3.0;
212                }
213        } else {
214                // decrease
215                if (tBoiler > tReturn) {
216                        tBoiler = (tBoiler + 2.0 * tReturn) / 3.0;
217                }
218        }
219        return tBoiler; // FAKE NEWS !!!
220}
221
222extern volatile long  loops;
223
224float   OT_Slave::GetReturnTemperature ()
225{
226        float temp = 100.0 * (tReturn - 1);
227        temp += float((loops >> 4) % 100);
228        return temp / 100.0;
229}
230
231void  OT_Slave::ListenAfterResponse () 
232{
233  if (Serial) {
234    Serial.print("<- ");
235    OPENTHERM::printToSerial(action.response);
236    Serial.println();
237    Serial.println();
238  }
239  OPENTHERM::listen(THERMOSTAT_IN, -1);
240}
Note: See TracBrowser for help on using the repository browser.