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

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

Fake active detection and buildin LED support

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