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

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

Adding measurement data and tools

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