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

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

Prototype for network based monitoring

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