Ignore:
Timestamp:
Jun 28, 2019, 1:07:05 AM (7 years ago)
Author:
markus
Message:
  • Moving class OT_V2R2_Slave to library "ot_slave_2"
  • Modified sketch OT_boiler for first combined prototype OT + ETH


File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Arduino/projects/OT_slave/OT_slave.ino

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