| 1 | #include "ot_slave_2.h" |
|---|
| 2 | |
|---|
| 3 | #define THERMOSTAT_IN 2 |
|---|
| 4 | #define THERMOSTAT_OUT 4 |
|---|
| 5 | |
|---|
| 6 | OpenthermData OT_V2R2_Slave::message; |
|---|
| 7 | |
|---|
| 8 | OT_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 | |
|---|
| 20 | void 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 | */ |
|---|
| 31 | void 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 | |
|---|
| 40 | void 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 | if (cbPostReq) { |
|---|
| 92 | (*cbPostReq)(message); |
|---|
| 93 | } |
|---|
| 94 | OPENTHERM::send(THERMOSTAT_OUT, message, OT_V2R2_Slave::ListenAfterResponse); // send some response and wait for antoher request from thermostat set by callback |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | void OT_V2R2_Slave::ConfigReq (OpenthermData& message) |
|---|
| 98 | { |
|---|
| 99 | message.type = OT_MSGTYPE_READ_ACK; |
|---|
| 100 | message.valueHB = 0; // 0x11; // slave configuration: |
|---|
| 101 | message.valueLB = 0; // Slave MemberID code |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | void OT_V2R2_Slave::StatusReq (OpenthermData& message) |
|---|
| 105 | { |
|---|
| 106 | message.type = OT_MSGTYPE_READ_ACK; |
|---|
| 107 | CH_Enable(message.valueHB & 0x01); |
|---|
| 108 | DHW_Enable(message.valueHB & 0x02); |
|---|
| 109 | message.valueLB = 0; // Slave status |
|---|
| 110 | if (chActive) { |
|---|
| 111 | message.valueLB |= 0x02; |
|---|
| 112 | } else { |
|---|
| 113 | message.valueLB &= ~0x02; |
|---|
| 114 | } |
|---|
| 115 | if (dhwActive) { |
|---|
| 116 | message.valueLB |= 0x04; |
|---|
| 117 | } else { |
|---|
| 118 | message.valueLB &= ~0x04; |
|---|
| 119 | } |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | void OT_V2R2_Slave::FaultReq (OpenthermData& message) |
|---|
| 123 | { |
|---|
| 124 | message.type = OT_MSGTYPE_READ_ACK; |
|---|
| 125 | message.valueHB = 0; // Application-specific fault flags |
|---|
| 126 | message.valueLB = 0; // OEM fault code |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | bool OT_V2R2_Slave::CH_Enable (bool enable) |
|---|
| 130 | { |
|---|
| 131 | chEnable = enable; |
|---|
| 132 | chActive = enable; |
|---|
| 133 | return chEnable; |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | bool OT_V2R2_Slave::CH_Disable () |
|---|
| 137 | { |
|---|
| 138 | chEnable = false; |
|---|
| 139 | chActive = false; |
|---|
| 140 | return chEnable; |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | void OT_V2R2_Slave::CH_Temperature (float temp) |
|---|
| 144 | { |
|---|
| 145 | chTemperature = temp; |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | bool OT_V2R2_Slave::DHW_Enable (bool enable) |
|---|
| 149 | { |
|---|
| 150 | dhwEnable = enable; |
|---|
| 151 | dhwActive = enable; |
|---|
| 152 | return dhwEnable; |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | bool OT_V2R2_Slave::DHW_Disable () |
|---|
| 156 | { |
|---|
| 157 | dhwEnable = false; |
|---|
| 158 | dhwActive = false; |
|---|
| 159 | return dhwEnable; |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | void OT_V2R2_Slave::DHW_Temperature (float temp) |
|---|
| 163 | { |
|---|
| 164 | dhwTemperature = temp; |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | void OT_V2R2_Slave::ListenAfterResponse () |
|---|
| 168 | { |
|---|
| 169 | Serial.print("<- "); |
|---|
| 170 | OPENTHERM::printToSerial(message); |
|---|
| 171 | Serial.println(); |
|---|
| 172 | Serial.println(); |
|---|
| 173 | OPENTHERM::listen(THERMOSTAT_IN, -1); |
|---|
| 174 | } |
|---|