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

Last change on this file since 25 was 25, checked in by markus, 7 years ago
  • Adjust temerature calculation
File size: 4.7 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  int   a3;
28  int   a4;
29
30  virtual double AD2Temperature (int);
31
32public:
33        virtual float   GetBoilerTemperature ();
34        virtual float   GetReturnTemperature ();
35
36friend void loop();
37};
38
39OT_Boiler       boiler;
40
41EthernetClient  client;
42
43void setup()
44{
45  msTicks = millis();
46  digitalWrite(ACTIVITY_LED_PIN, LOW);  // Built-in LED off
47  pinMode(ACTIVITY_LED_PIN, OUTPUT);            // Built-in LED active
48  // Console
49  Serial.begin(19200);
50  CONSOLE(println("\nSystem starting..."));
51  // Setup OT
52  CONSOLE(print("OpenTherm.begin..."));
53  boiler.begin(Sent_OT_Data);
54  CONSOLE(println("done"));
55#if USE_ETHERNET
56  // Setup Ethernet
57  CONSOLE(print("Ethernet.begin..."));
58  Ethernet.begin(mac);
59  CONSOLE(println("done"));
60#else
61  CONSOLE(println("Ethernet is disabled!"));
62#endif
63}
64
65void loop()
66{
67  loops++;
68/*
69  Serial.print("[");
70  Serial.print(loops);
71  Serial.print("|");
72  Serial.print(state,HEX);
73  Serial.println("]");
74*/
75  msTicks = millis();
76#if USE_ETHERNET
77  maintainEthernet();
78#endif
79  boiler.maintain();
80#ifdef _NEVER_
81  unsigned long base = boiler.IsActive() ? 500 : 3000;
82  unsigned long periode = msTicks / base;
83  int led = (periode & 1) ? HIGH : LOW;
84  if (Serial) {
85          static unsigned long prev = 0;
86          if (prev != periode) {
87                  prev = periode;
88                  Serial.print("Time: ");
89                  Serial.print(msTicks);
90                  Serial.print("\tPeriode: ");
91                  Serial.print(periode);
92                  Serial.print("\tLED: ");
93                  Serial.println(led);
94          }
95  }
96  digitalWrite(ACTIVITY_LED_PIN, led);
97  //digitalWrite(ACTIVITY_LED_PIN, boiler.IsActive() ? HIGH : LOW);
98#else
99  unsigned long periode = msTicks / (boiler.IsActive() ? 500 : 2000);
100  static unsigned long prev = 0;
101  if (prev != periode) {
102          prev = periode;
103          int led = (periode & 1) ? HIGH : LOW;
104          if (Serial) {
105                  Serial.print("Time: ");
106                  Serial.print(msTicks);
107                  Serial.print("\tPeriode: ");
108                  Serial.print(periode);
109                  Serial.print("\tLED: ");
110                  Serial.print(led);
111      float tBoiler = boiler.GetBoilerTemperature();
112                  Serial.print("\tA2: ");
113                  Serial.print(boiler.a2);
114                  Serial.print("\tT(A2): ");
115                  Serial.print(tBoiler);
116      float tReturn = boiler.GetReturnTemperature();
117      Serial.print("\tA3: ");
118      Serial.print(boiler.a3);
119      Serial.print("\tT(A3): ");
120      Serial.println(tReturn);
121          }
122          digitalWrite(ACTIVITY_LED_PIN, led);
123  }
124#endif
125}
126
127void maintainEthernet ()
128{
129  // Check for Ethernet hardware present
130  if (Ethernet.hardwareStatus() == EthernetNoHardware) {
131    return;
132  }
133  byte rc = Ethernet.maintain();
134  switch (rc) {
135    case 1: // renew failed
136      CONSOLE(println("Ethernet: renew failed!"));
137      break;
138    case 2: // renew success
139      CONSOLE(println("Ethernet: renew success!"));
140      break;
141    case 3: // rebind failed
142      CONSOLE(println("Ethernet: rebind failed!"));
143      break;
144    case 4: // rebind success 
145      CONSOLE(println("Ethernet: rebind success!"));
146      break;
147  }
148  if (client.connected()) {
149    if (!_connected) {
150      _connected = true;
151      CONSOLE(println("Ethernet: connected!")); 
152    }
153  } else {
154    if (_connected) {
155      _connected = false;
156      CONSOLE(println("Ethernet: connection lost!"));
157    }
158//    CONSOLE(println("Ethernet: restarting..."));
159    client.stop();
160//    CONSOLE(println("Ethernet: connecting..."));
161    client.connect(server, 23456); 
162  }
163}
164
165void    Sent_OT_Data (const OT_Action& data)
166{
167  state = OPENTHERM::getMode();
168  if (client.connected()) {
169    client.print("[");
170    client.print(loops);
171    client.print("|");
172    client.print(state,HEX);
173    client.print("]\t");
174/*
175    client.print(data.type,HEX);
176    client.print(":");
177    client.print(data.id,HEX);
178    client.print(":");
179    client.print(data.valueHB,HEX);
180    client.print(":");
181    client.print(data.valueLB,HEX);
182*/
183    client.print(data.GetRequestValue(),HEX);
184    client.print("\t");
185    client.print(data.GetResponseValue(),HEX);
186    client.println("");
187  }
188}
189
190double OT_Boiler::AD2Temperature (int x)
191{
192  const double m = 0.2059;
193  const double b = -27.951;
194  return m*double(x)+b;
195}
196
197float OT_Boiler::GetBoilerTemperature ()
198{
199        a2 = analogRead(A2);   // read the input pin
200        tBoiler = AD2Temperature(a2);
201        return tBoiler;
202}
203
204float OT_Boiler::GetReturnTemperature ()
205{
206  a3 = analogRead(A3);   // read the input pin
207  tReturn = AD2Temperature(a3);
208  return tReturn;
209}
Note: See TracBrowser for help on using the repository browser.