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
RevLine 
[8]1#include <SPI.h>
2#include <Ethernet.h>
[11]3#include <OT_Slave.h>
[8]4
[19]5//#define ACTIVITY_LED_PIN  LED_BUILTIN
6#define ACTIVITY_LED_PIN  9
7
8#define USE_ETHERNET  0
9
[10]10#define CONSOLE(cmd)  do { if (Serial) Serial.cmd; } while (0)
11
[11]12void  Sent_OT_Data (const OT_Action& data);
[8]13
14byte  mac[] = { 0x02, 0xDE, 0xAD, 0xDE, 0xAD, 0x01 };
15byte  server[] = { 192, 168, 0, 22 };
[19]16
[8]17volatile long  loops = 0;
[19]18volatile unsigned long msTicks = 0;
19
[8]20volatile long  state = 0;
21volatile bool  _connected = false;
22
[21]23class OT_Boiler : public OT_Slave {
[8]24
[21]25protected:
26        int             a2;
[25]27  int   a3;
28  int   a4;
[29]29  int   a5;
[21]30
[25]31  virtual double AD2Temperature (int);
32
[21]33public:
34        virtual float   GetBoilerTemperature ();
35        virtual float   GetReturnTemperature ();
36
37friend void loop();
38};
39
40OT_Boiler       boiler;
41
[19]42EthernetClient  client;
[8]43
44void setup()
45{
[19]46  msTicks = millis();
47  digitalWrite(ACTIVITY_LED_PIN, LOW);  // Built-in LED off
48  pinMode(ACTIVITY_LED_PIN, OUTPUT);            // Built-in LED active
[8]49  // Console
50  Serial.begin(19200);
[10]51  CONSOLE(println("\nSystem starting..."));
[8]52  // Setup OT
[19]53  CONSOLE(print("OpenTherm.begin..."));
[11]54  boiler.begin(Sent_OT_Data);
[10]55  CONSOLE(println("done"));
[19]56#if USE_ETHERNET
[8]57  // Setup Ethernet
[10]58  CONSOLE(print("Ethernet.begin..."));
[8]59  Ethernet.begin(mac);
[10]60  CONSOLE(println("done"));
[19]61#else
62  CONSOLE(println("Ethernet is disabled!"));
63#endif
[8]64}
65
66void loop()
67{
[9]68  loops++;
[8]69/*
70  Serial.print("[");
[9]71  Serial.print(loops);
[8]72  Serial.print("|");
73  Serial.print(state,HEX);
74  Serial.println("]");
75*/
[19]76  msTicks = millis();
77#if USE_ETHERNET
[8]78  maintainEthernet();
[19]79#endif
[11]80  boiler.maintain();
[19]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
[29]100  unsigned long periode = msTicks / (boiler.IsActive() ? 500 : 1000);
[19]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);
[25]112      float tBoiler = boiler.GetBoilerTemperature();
[21]113                  Serial.print("\tA2: ");
114                  Serial.print(boiler.a2);
115                  Serial.print("\tT(A2): ");
[25]116                  Serial.print(tBoiler);
117      float tReturn = boiler.GetReturnTemperature();
118      Serial.print("\tA3: ");
119      Serial.print(boiler.a3);
120      Serial.print("\tT(A3): ");
[29]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);
[19]128          }
129          digitalWrite(ACTIVITY_LED_PIN, led);
130  }
131#endif
[8]132}
133
134void maintainEthernet ()
135{
[14]136  // Check for Ethernet hardware present
137  if (Ethernet.hardwareStatus() == EthernetNoHardware) {
138    return;
139  }
[8]140  byte rc = Ethernet.maintain();
141  switch (rc) {
142    case 1: // renew failed
[10]143      CONSOLE(println("Ethernet: renew failed!"));
[8]144      break;
145    case 2: // renew success
[10]146      CONSOLE(println("Ethernet: renew success!"));
[8]147      break;
148    case 3: // rebind failed
[10]149      CONSOLE(println("Ethernet: rebind failed!"));
[8]150      break;
151    case 4: // rebind success 
[10]152      CONSOLE(println("Ethernet: rebind success!"));
[8]153      break;
154  }
155  if (client.connected()) {
156    if (!_connected) {
157      _connected = true;
[10]158      CONSOLE(println("Ethernet: connected!")); 
[8]159    }
160  } else {
161    if (_connected) {
162      _connected = false;
[10]163      CONSOLE(println("Ethernet: connection lost!"));
[8]164    }
[10]165//    CONSOLE(println("Ethernet: restarting..."));
[8]166    client.stop();
[10]167//    CONSOLE(println("Ethernet: connecting..."));
[8]168    client.connect(server, 23456); 
169  }
170}
171
[11]172void    Sent_OT_Data (const OT_Action& data)
[8]173{
174  state = OPENTHERM::getMode();
[9]175  if (client.connected()) {
176    client.print("[");
177    client.print(loops);
178    client.print("|");
179    client.print(state,HEX);
180    client.print("]\t");
[11]181/*
[9]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);
[11]189*/
[12]190    client.print(data.GetRequestValue(),HEX);
[11]191    client.print("\t");
[12]192    client.print(data.GetResponseValue(),HEX);
[9]193    client.println("");
194  }
[8]195}
[21]196
[25]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
[21]204float OT_Boiler::GetBoilerTemperature ()
205{
206        a2 = analogRead(A2);   // read the input pin
[25]207        tBoiler = AD2Temperature(a2);
[21]208        return tBoiler;
209}
210
211float OT_Boiler::GetReturnTemperature ()
212{
[25]213  a3 = analogRead(A3);   // read the input pin
214  tReturn = AD2Temperature(a3);
215  return tReturn;
[21]216}
Note: See TracBrowser for help on using the repository browser.