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