source: trunk/Arduino/libraries/ot_slave_2/ot_slave_2.cpp @ 7

Last change on this file since 7 was 7, checked in by markus, 7 years ago
  • Moving class OT_V2R2_Slave to library "ot_slave_2"
  • Modified sketch OT_boiler for first combined prototype OT + ETH


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