source: trunk/Arduino/projects/OT_boiler/OT_boiler.ino @ 21

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

+ Calculate bailer temperature in OT_Boiler sub-class

File size: 4.4 KB
RevLine 
[8]1#include <SPI.h>
2#include <Ethernet.h>
[11]3#include <OT_Slave.h>
[8]4
[19]5//#define ACTIVITY_LED_PIN  LED_BUILTIN
6#define ACTIVITY_LED_PIN  9
7
8#define USE_ETHERNET  0
9
[10]10#define CONSOLE(cmd)  do { if (Serial) Serial.cmd; } while (0)
11
[11]12void  Sent_OT_Data (const OT_Action& data);
[8]13
14byte  mac[] = { 0x02, 0xDE, 0xAD, 0xDE, 0xAD, 0x01 };
15byte  server[] = { 192, 168, 0, 22 };
[19]16
[8]17volatile long  loops = 0;
[19]18volatile unsigned long msTicks = 0;
19
[8]20volatile long  state = 0;
21volatile bool  _connected = false;
22
[21]23class OT_Boiler : public OT_Slave {
[8]24
[21]25protected:
26        int             a2;
27
28public:
29        virtual float   GetBoilerTemperature ();
30        virtual float   GetReturnTemperature ();
31
32friend void loop();
33};
34
35OT_Boiler       boiler;
36
[19]37EthernetClient  client;
[8]38
39void setup()
40{
[19]41  msTicks = millis();
42  digitalWrite(ACTIVITY_LED_PIN, LOW);  // Built-in LED off
43  pinMode(ACTIVITY_LED_PIN, OUTPUT);            // Built-in LED active
[8]44  // Console
45  Serial.begin(19200);
[10]46  CONSOLE(println("\nSystem starting..."));
[8]47  // Setup OT
[19]48  CONSOLE(print("OpenTherm.begin..."));
[11]49  boiler.begin(Sent_OT_Data);
[10]50  CONSOLE(println("done"));
[19]51#if USE_ETHERNET
[8]52  // Setup Ethernet
[10]53  CONSOLE(print("Ethernet.begin..."));
[8]54  Ethernet.begin(mac);
[10]55  CONSOLE(println("done"));
[19]56#else
57  CONSOLE(println("Ethernet is disabled!"));
58#endif
[8]59}
60
61void loop()
62{
[9]63  loops++;
[8]64/*
65  Serial.print("[");
[9]66  Serial.print(loops);
[8]67  Serial.print("|");
68  Serial.print(state,HEX);
69  Serial.println("]");
70*/
[19]71  msTicks = millis();
72#if USE_ETHERNET
[8]73  maintainEthernet();
[19]74#endif
[11]75  boiler.maintain();
[19]76#ifdef _NEVER_
77  unsigned long base = boiler.IsActive() ? 500 : 3000;
78  unsigned long periode = msTicks / base;
79  int led = (periode & 1) ? HIGH : LOW;
80  if (Serial) {
81          static unsigned long prev = 0;
82          if (prev != periode) {
83                  prev = periode;
84                  Serial.print("Time: ");
85                  Serial.print(msTicks);
86                  Serial.print("\tPeriode: ");
87                  Serial.print(periode);
88                  Serial.print("\tLED: ");
89                  Serial.println(led);
90          }
91  }
92  digitalWrite(ACTIVITY_LED_PIN, led);
93  //digitalWrite(ACTIVITY_LED_PIN, boiler.IsActive() ? HIGH : LOW);
94#else
95  unsigned long periode = msTicks / (boiler.IsActive() ? 500 : 2000);
96  static unsigned long prev = 0;
97  if (prev != periode) {
98          prev = periode;
99          int led = (periode & 1) ? HIGH : LOW;
100          if (Serial) {
[21]101                  float tBoiler = boiler.GetBoilerTemperature();
[19]102                  Serial.print("Time: ");
103                  Serial.print(msTicks);
104                  Serial.print("\tPeriode: ");
105                  Serial.print(periode);
106                  Serial.print("\tLED: ");
107                  Serial.print(led);
[21]108                  Serial.print("\tA2: ");
109                  Serial.print(boiler.a2);
110                  Serial.print("\tT(A2): ");
111                  Serial.println(tBoiler);
[19]112          }
113          digitalWrite(ACTIVITY_LED_PIN, led);
114  }
115#endif
[8]116}
117
118void maintainEthernet ()
119{
[14]120  // Check for Ethernet hardware present
121  if (Ethernet.hardwareStatus() == EthernetNoHardware) {
122    return;
123  }
[8]124  byte rc = Ethernet.maintain();
125  switch (rc) {
126    case 1: // renew failed
[10]127      CONSOLE(println("Ethernet: renew failed!"));
[8]128      break;
129    case 2: // renew success
[10]130      CONSOLE(println("Ethernet: renew success!"));
[8]131      break;
132    case 3: // rebind failed
[10]133      CONSOLE(println("Ethernet: rebind failed!"));
[8]134      break;
135    case 4: // rebind success 
[10]136      CONSOLE(println("Ethernet: rebind success!"));
[8]137      break;
138  }
139  if (client.connected()) {
140    if (!_connected) {
141      _connected = true;
[10]142      CONSOLE(println("Ethernet: connected!")); 
[8]143    }
144  } else {
145    if (_connected) {
146      _connected = false;
[10]147      CONSOLE(println("Ethernet: connection lost!"));
[8]148    }
[10]149//    CONSOLE(println("Ethernet: restarting..."));
[8]150    client.stop();
[10]151//    CONSOLE(println("Ethernet: connecting..."));
[8]152    client.connect(server, 23456); 
153  }
154}
155
[11]156void    Sent_OT_Data (const OT_Action& data)
[8]157{
158  state = OPENTHERM::getMode();
[9]159  if (client.connected()) {
160    client.print("[");
161    client.print(loops);
162    client.print("|");
163    client.print(state,HEX);
164    client.print("]\t");
[11]165/*
[9]166    client.print(data.type,HEX);
167    client.print(":");
168    client.print(data.id,HEX);
169    client.print(":");
170    client.print(data.valueHB,HEX);
171    client.print(":");
172    client.print(data.valueLB,HEX);
[11]173*/
[12]174    client.print(data.GetRequestValue(),HEX);
[11]175    client.print("\t");
[12]176    client.print(data.GetResponseValue(),HEX);
[9]177    client.println("");
178  }
[8]179}
[21]180
181float OT_Boiler::GetBoilerTemperature ()
182{
183        a2 = analogRead(A2);   // read the input pin
184        const double m = 0.1848;
185        const double b = -17.4056;
186        tBoiler = double(m*double(a2)+b);
187        return tBoiler;
188}
189
190float OT_Boiler::GetReturnTemperature ()
191{
192        return OT_Slave::GetReturnTemperature ();
193}
Note: See TracBrowser for help on using the repository browser.