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

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

Prototype OT and VC40 Adapter

File size: 6.6 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#define PWM_PIN         10
8
9#define USE_ETHERNET    0
10
11#define CONSOLE(cmd)  do { if (Serial) Serial.cmd; } while (0)
12
13void    Sent_OT_Data (const OT_Action& data);
14
15byte    mac[] = { 0x02, 0xDE, 0xAD, 0xDE, 0xAD, 0x01 };
16byte    server[] = { 192, 168, 0, 22 };
17
18volatile long  loops = 0;
19volatile unsigned long msTicks = 0;
20
21volatile long  state = 0;
22volatile bool  _connected = false;
23
24class OT_Boiler : public OT_Slave {
25
26protected:
27        long            a2;
28        long            a3;
29        long            a4;
30        long            a5;
31        long            cnt;
32        long            cntVal;
33
34        long            pwm;
35
36  long    a2Val;
37        long            a2Dir;          // Boiler (VC-40) active detection
38  bool  a2Rising;
39
40        void            ReadData ();
41        void            CalcData ();
42
43        virtual double  AD2Temperature (int);
44
45public:
46        OT_Boiler ();
47
48        virtual void    maintain ();
49
50        virtual bool    IsActive ();
51
52        virtual void    SetRequestTemperature (float);
53        virtual float   GetBoilerTemperature ();
54        virtual float   GetReturnTemperature ();
55
56        friend void     loop ();
57};
58
59OT_Boiler       boiler;
60
61EthernetClient  client;
62
63void    setup()
64{
65        msTicks = millis();
66        digitalWrite(ACTIVITY_LED_PIN, LOW);    // Activity LED off
67        pinMode(ACTIVITY_LED_PIN, OUTPUT);      // Activity LED active
68        pinMode(PWM_PIN, OUTPUT);               // PWM output
69        // Console
70//      Serial.begin(19200);
71        Serial.begin(115200);
72        CONSOLE(println("\nSystem starting..."));
73        // Setup OT
74        CONSOLE(print("OpenTherm.begin..."));
75        boiler.begin(Sent_OT_Data);
76        CONSOLE(println("done"));
77#if USE_ETHERNET
78        // Setup Ethernet
79        CONSOLE(print("Ethernet.begin..."));
80        Ethernet.begin(mac);
81        CONSOLE(println("done"));
82#else
83        CONSOLE(println("Ethernet is disabled!"));
84#endif
85}
86
87void    loop()
88{
89        loops++;
90/*
91        Serial.print("[");
92        Serial.print(loops);
93        Serial.print("|");
94        Serial.print(state,HEX);
95        Serial.println("]");
96*/
97        msTicks = millis();
98#if USE_ETHERNET
99        maintainEthernet();
100#endif
101        boiler.maintain();
102#ifdef _NEVER_
103//  unsigned long periode = msTicks / (boiler.IsActive() ? 500 : 2000);
104        unsigned long periode = msTicks / (boiler.IsActive() ? 2000 : 2000);
105        static unsigned long prev = 0;
106        if (prev != periode) {
107                prev = periode;
108#else
109  static unsigned long limit = 0;
110  if (!limit) {
111    unsigned long interval = boiler.IsActive() ? 500 : 2000;
112    limit = ((msTicks / interval) + 1 ) * interval;
113  }
114  static unsigned long periode = 0;
115  if (msTicks >= limit) {
116    limit = 0;
117    periode += 1;   
118#endif
119                int led = (periode & 1) ? HIGH : LOW;
120                if (Serial) {
121                        boiler.CalcData();
122                        Serial.print("Time: ");
123                        Serial.print(msTicks);
124                        Serial.print("\tPeriode: ");
125                        Serial.print(periode);
126/*
127                        Serial.print("\tLED: ");
128                        Serial.print(led);
129*/
130                        Serial.print("\tCnt: ");
131                        Serial.print(boiler.cntVal);
132                        float tBoiler = boiler.GetBoilerTemperature();
133                        Serial.print("\tA2: ");
134                        Serial.print(boiler.a2);
135                        Serial.print("\tT(A2): ");
136                        Serial.print(tBoiler);
137                        Serial.print("\tDir(A2): ");
138                        Serial.print(boiler.a2Dir);
139      Serial.print("\tRising(A2): ");
140      Serial.print(boiler.a2Rising);
141                        float tReturn = boiler.GetReturnTemperature();
142                        Serial.print("\tA3: ");
143                        Serial.print(boiler.a3);
144                        Serial.print("\tT(A3): ");
145                        Serial.print(tReturn);
146                        Serial.print("\tA4: ");
147                        Serial.print(boiler.a4);
148                        Serial.print("\tA5: ");
149                        Serial.print(boiler.a5);
150                        Serial.print("\tPWM: ");
151//                      long _pwm = (msTicks / 20000) % 256;
152//                      long _pwm = long(0.9097+164.44*(0.4283+boiler.tRequest*(0.0009+boiler.tRequest*(0.00006-boiler.tRequest*0.0000003))));
153//                      analogWrite(PWM_PIN, _pwm);
154                        Serial.print(boiler.pwm);
155                        Serial.print("\tReq: ");
156                        Serial.print(boiler.tRequest);
157                        Serial.print("\tQ(A): ");
158                        Serial.println(double(boiler.a5)/double(boiler.a4));
159                }
160                digitalWrite(ACTIVITY_LED_PIN, led);
161        }
162}
163
164void    maintainEthernet ()
165{
166        // Check for Ethernet hardware present
167        if (Ethernet.hardwareStatus() == EthernetNoHardware) {
168                return;
169        }
170        byte rc = Ethernet.maintain();
171        switch (rc) {
172                case 1: // renew failed
173                        CONSOLE(println("Ethernet: renew failed!"));
174                        break;
175                case 2: // renew success
176                        CONSOLE(println("Ethernet: renew success!"));
177                        break;
178                case 3: // rebind failed
179                        CONSOLE(println("Ethernet: rebind failed!"));
180                        break;
181                case 4: // rebind success 
182                        CONSOLE(println("Ethernet: rebind success!"));
183                        break;
184        }
185        if (client.connected()) {
186                if (!_connected) {
187                        _connected = true;
188                        CONSOLE(println("Ethernet: connected!")); 
189                }
190        } else {
191                if (_connected) {
192                        _connected = false;
193                        CONSOLE(println("Ethernet: connection lost!"));
194                }
195//              CONSOLE(println("Ethernet: restarting..."));
196                client.stop();
197//              CONSOLE(println("Ethernet: connecting..."));
198                client.connect(server, 23456); 
199        }
200}
201
202void    Sent_OT_Data (const OT_Action& data)
203{
204        state = OPENTHERM::getMode();
205        if (client.connected()) {
206                client.print("[");
207                client.print(loops);
208                client.print("|");
209                client.print(state,HEX);
210                client.print("]\t");
211/*
212                client.print(data.type,HEX);
213                client.print(":");
214                client.print(data.id,HEX);
215                client.print(":");
216                client.print(data.valueHB,HEX);
217                client.print(":");
218                client.print(data.valueLB,HEX);
219*/
220                client.print(data.GetRequestValue(),HEX);
221                client.print("\t");
222                client.print(data.GetResponseValue(),HEX);
223                client.println("");
224        }
225}
226
227OT_Boiler::OT_Boiler ()
228:
229        a2(0),
230        a3(0),
231        a4(0),
232        a5(0),
233        cnt(0),
234        cntVal(0),
235  a2Val(0),
236        a2Dir(0),
237  a2Rising(false)
238{
239}
240
241void    OT_Boiler::maintain ()
242{
243        ReadData();
244        OT_Slave::maintain();
245}
246
247void    OT_Boiler::ReadData ()
248{
249        long a2Prev = a2Val;
250  a2Val = analogRead(A2);
251        a2 += a2Val;
252        a3 += analogRead(A3);
253        a4 += analogRead(A4);
254        a5 += analogRead(A5);
255        cnt++;
256        // Calc A2 direction...
257        if (a2Val != a2Prev) {
258                if (a2Val < a2Prev) {
259                        if (a2Dir > 0) {
260                                a2Dir = -1;
261                        } else {
262                                a2Dir -= 1;
263        if (a2Dir < -5) {
264          a2Rising = false;
265        }
266                        }
267                } else {
268                        if (a2Dir < 0) {
269                                a2Dir = 1;
270                        } else {
271                                a2Dir += 1;
272        if (a2Dir > 5) {
273          a2Rising = true;
274        }
275                        }
276                }
277        }
278}
279
280void    OT_Boiler::CalcData ()
281{
282        if (cnt > 1) {
283                cntVal = cnt;
284                a2 /= cnt;
285                a3 /= cnt;
286                a4 /= cnt;
287                a5 /= cnt;
288                cnt = 1;
289        }
290}
291
292double  OT_Boiler::AD2Temperature (int x)
293{
294        const double m = 0.1816;
295        const double b = -19.234;
296        return m*double(x)+b;
297}
298
299bool    OT_Boiler::IsActive ()
300{
301        return a2Rising;
302}
303
304void    OT_Boiler::SetRequestTemperature (float t)
305{
306        bool update(t != tRequest);
307        OT_Slave::SetRequestTemperature(t);
308        if (update) {
309                pwm = long(0.9097+164.44*(0.4283+boiler.tRequest*(0.0009+boiler.tRequest*(0.00006-boiler.tRequest*0.0000003))));
310                analogWrite(PWM_PIN, pwm);
311        }
312}
313
314float   OT_Boiler::GetBoilerTemperature ()
315{
316        CalcData();
317        tBoiler = AD2Temperature(a2);
318        return tBoiler;
319}
320
321float   OT_Boiler::GetReturnTemperature ()
322{
323        CalcData();
324        tReturn = AD2Temperature(a3);
325        return tReturn;
326}
Note: See TracBrowser for help on using the repository browser.