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
RevLine 
[8]1#include <SPI.h>
2#include <Ethernet.h>
[11]3#include <OT_Slave.h>
[8]4
[31]5//#define ACTIVITY_LED_PIN      LED_BUILTIN
6#define ACTIVITY_LED_PIN        9
7#define PWM_PIN         10
[19]8
[31]9#define USE_ETHERNET    0
[19]10
[10]11#define CONSOLE(cmd)  do { if (Serial) Serial.cmd; } while (0)
12
[31]13void    Sent_OT_Data (const OT_Action& data);
[8]14
[31]15byte    mac[] = { 0x02, 0xDE, 0xAD, 0xDE, 0xAD, 0x01 };
16byte    server[] = { 192, 168, 0, 22 };
[19]17
[8]18volatile long  loops = 0;
[19]19volatile unsigned long msTicks = 0;
20
[8]21volatile long  state = 0;
22volatile bool  _connected = false;
23
[21]24class OT_Boiler : public OT_Slave {
[8]25
[21]26protected:
[31]27        long            a2;
28        long            a3;
29        long            a4;
30        long            a5;
31        long            cnt;
32        long            cntVal;
[21]33
[31]34        long            pwm;
[25]35
[31]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
[21]45public:
[31]46        OT_Boiler ();
47
48        virtual void    maintain ();
49
50        virtual bool    IsActive ();
51
52        virtual void    SetRequestTemperature (float);
[21]53        virtual float   GetBoilerTemperature ();
54        virtual float   GetReturnTemperature ();
55
[31]56        friend void     loop ();
[21]57};
58
59OT_Boiler       boiler;
60
[19]61EthernetClient  client;
[8]62
[31]63void    setup()
[8]64{
[31]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"));
[19]77#if USE_ETHERNET
[31]78        // Setup Ethernet
79        CONSOLE(print("Ethernet.begin..."));
80        Ethernet.begin(mac);
81        CONSOLE(println("done"));
[19]82#else
[31]83        CONSOLE(println("Ethernet is disabled!"));
[19]84#endif
[8]85}
86
[31]87void    loop()
[8]88{
[31]89        loops++;
[8]90/*
[31]91        Serial.print("[");
92        Serial.print(loops);
93        Serial.print("|");
94        Serial.print(state,HEX);
95        Serial.println("]");
[8]96*/
[31]97        msTicks = millis();
[19]98#if USE_ETHERNET
[31]99        maintainEthernet();
[19]100#endif
[31]101        boiler.maintain();
[19]102#ifdef _NEVER_
[31]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;
[19]108#else
[31]109  static unsigned long limit = 0;
110  if (!limit) {
111    unsigned long interval = boiler.IsActive() ? 500 : 2000;
112    limit = ((msTicks / interval) + 1 ) * interval;
[19]113  }
[31]114  static unsigned long periode = 0;
115  if (msTicks >= limit) {
116    limit = 0;
117    periode += 1;   
[19]118#endif
[31]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        }
[8]162}
163
[31]164void    maintainEthernet ()
[8]165{
[31]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        }
[8]200}
201
[11]202void    Sent_OT_Data (const OT_Action& data)
[8]203{
[31]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");
[11]211/*
[31]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);
[11]219*/
[31]220                client.print(data.GetRequestValue(),HEX);
221                client.print("\t");
222                client.print(data.GetResponseValue(),HEX);
223                client.println("");
224        }
[8]225}
[21]226
[31]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)
[25]238{
239}
240
[31]241void    OT_Boiler::maintain ()
[21]242{
[31]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();
[25]317        tBoiler = AD2Temperature(a2);
[21]318        return tBoiler;
319}
320
[31]321float   OT_Boiler::GetReturnTemperature ()
[21]322{
[31]323        CalcData();
324        tReturn = AD2Temperature(a3);
325        return tReturn;
[21]326}
Note: See TracBrowser for help on using the repository browser.