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

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

+ Calculate bailer temperature in OT_Boiler sub-class

File size: 4.4 KB
Line 
1#include <SPI.h>
2#include <Ethernet.h>
3#include <OT_Slave.h>
4
5//#define ACTIVITY_LED_PIN  LED_BUILTIN
6#define ACTIVITY_LED_PIN  9
7
8#define USE_ETHERNET  0
9
10#define CONSOLE(cmd)  do { if (Serial) Serial.cmd; } while (0)
11
12void  Sent_OT_Data (const OT_Action& data);
13
14byte  mac[] = { 0x02, 0xDE, 0xAD, 0xDE, 0xAD, 0x01 };
15byte  server[] = { 192, 168, 0, 22 };
16
17volatile long  loops = 0;
18volatile unsigned long msTicks = 0;
19
20volatile long  state = 0;
21volatile bool  _connected = false;
22
23class OT_Boiler : public OT_Slave {
24
25protected:
26        int             a2;
27
28public:
29        virtual float   GetBoilerTemperature ();
30        virtual float   GetReturnTemperature ();
31
32friend void loop();
33};
34
35OT_Boiler       boiler;
36
37EthernetClient  client;
38
39void setup()
40{
41  msTicks = millis();
42  digitalWrite(ACTIVITY_LED_PIN, LOW);  // Built-in LED off
43  pinMode(ACTIVITY_LED_PIN, OUTPUT);            // Built-in LED active
44  // Console
45  Serial.begin(19200);
46  CONSOLE(println("\nSystem starting..."));
47  // Setup OT
48  CONSOLE(print("OpenTherm.begin..."));
49  boiler.begin(Sent_OT_Data);
50  CONSOLE(println("done"));
51#if USE_ETHERNET
52  // Setup Ethernet
53  CONSOLE(print("Ethernet.begin..."));
54  Ethernet.begin(mac);
55  CONSOLE(println("done"));
56#else
57  CONSOLE(println("Ethernet is disabled!"));
58#endif
59}
60
61void loop()
62{
63  loops++;
64/*
65  Serial.print("[");
66  Serial.print(loops);
67  Serial.print("|");
68  Serial.print(state,HEX);
69  Serial.println("]");
70*/
71  msTicks = millis();
72#if USE_ETHERNET
73  maintainEthernet();
74#endif
75  boiler.maintain();
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) {
101                  float tBoiler = boiler.GetBoilerTemperature();
102                  Serial.print("Time: ");
103                  Serial.print(msTicks);
104                  Serial.print("\tPeriode: ");
105                  Serial.print(periode);
106                  Serial.print("\tLED: ");
107                  Serial.print(led);
108                  Serial.print("\tA2: ");
109                  Serial.print(boiler.a2);
110                  Serial.print("\tT(A2): ");
111                  Serial.println(tBoiler);
112          }
113          digitalWrite(ACTIVITY_LED_PIN, led);
114  }
115#endif
116}
117
118void maintainEthernet ()
119{
120  // Check for Ethernet hardware present
121  if (Ethernet.hardwareStatus() == EthernetNoHardware) {
122    return;
123  }
124  byte rc = Ethernet.maintain();
125  switch (rc) {
126    case 1: // renew failed
127      CONSOLE(println("Ethernet: renew failed!"));
128      break;
129    case 2: // renew success
130      CONSOLE(println("Ethernet: renew success!"));
131      break;
132    case 3: // rebind failed
133      CONSOLE(println("Ethernet: rebind failed!"));
134      break;
135    case 4: // rebind success 
136      CONSOLE(println("Ethernet: rebind success!"));
137      break;
138  }
139  if (client.connected()) {
140    if (!_connected) {
141      _connected = true;
142      CONSOLE(println("Ethernet: connected!")); 
143    }
144  } else {
145    if (_connected) {
146      _connected = false;
147      CONSOLE(println("Ethernet: connection lost!"));
148    }
149//    CONSOLE(println("Ethernet: restarting..."));
150    client.stop();
151//    CONSOLE(println("Ethernet: connecting..."));
152    client.connect(server, 23456); 
153  }
154}
155
156void    Sent_OT_Data (const OT_Action& data)
157{
158  state = OPENTHERM::getMode();
159  if (client.connected()) {
160    client.print("[");
161    client.print(loops);
162    client.print("|");
163    client.print(state,HEX);
164    client.print("]\t");
165/*
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);
173*/
174    client.print(data.GetRequestValue(),HEX);
175    client.print("\t");
176    client.print(data.GetResponseValue(),HEX);
177    client.println("");
178  }
179}
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.