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

Last change on this file since 28 was 25, checked in by markus, 7 years ago
  • Adjust temerature calculation
File size: 4.7 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;
[21]29
[25]30  virtual double AD2Temperature (int);
31
[21]32public:
33        virtual float   GetBoilerTemperature ();
34        virtual float   GetReturnTemperature ();
35
36friend void loop();
37};
38
39OT_Boiler       boiler;
40
[19]41EthernetClient  client;
[8]42
43void setup()
44{
[19]45  msTicks = millis();
46  digitalWrite(ACTIVITY_LED_PIN, LOW);  // Built-in LED off
47  pinMode(ACTIVITY_LED_PIN, OUTPUT);            // Built-in LED active
[8]48  // Console
49  Serial.begin(19200);
[10]50  CONSOLE(println("\nSystem starting..."));
[8]51  // Setup OT
[19]52  CONSOLE(print("OpenTherm.begin..."));
[11]53  boiler.begin(Sent_OT_Data);
[10]54  CONSOLE(println("done"));
[19]55#if USE_ETHERNET
[8]56  // Setup Ethernet
[10]57  CONSOLE(print("Ethernet.begin..."));
[8]58  Ethernet.begin(mac);
[10]59  CONSOLE(println("done"));
[19]60#else
61  CONSOLE(println("Ethernet is disabled!"));
62#endif
[8]63}
64
65void loop()
66{
[9]67  loops++;
[8]68/*
69  Serial.print("[");
[9]70  Serial.print(loops);
[8]71  Serial.print("|");
72  Serial.print(state,HEX);
73  Serial.println("]");
74*/
[19]75  msTicks = millis();
76#if USE_ETHERNET
[8]77  maintainEthernet();
[19]78#endif
[11]79  boiler.maintain();
[19]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);
[25]111      float tBoiler = boiler.GetBoilerTemperature();
[21]112                  Serial.print("\tA2: ");
113                  Serial.print(boiler.a2);
114                  Serial.print("\tT(A2): ");
[25]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);
[19]121          }
122          digitalWrite(ACTIVITY_LED_PIN, led);
123  }
124#endif
[8]125}
126
127void maintainEthernet ()
128{
[14]129  // Check for Ethernet hardware present
130  if (Ethernet.hardwareStatus() == EthernetNoHardware) {
131    return;
132  }
[8]133  byte rc = Ethernet.maintain();
134  switch (rc) {
135    case 1: // renew failed
[10]136      CONSOLE(println("Ethernet: renew failed!"));
[8]137      break;
138    case 2: // renew success
[10]139      CONSOLE(println("Ethernet: renew success!"));
[8]140      break;
141    case 3: // rebind failed
[10]142      CONSOLE(println("Ethernet: rebind failed!"));
[8]143      break;
144    case 4: // rebind success 
[10]145      CONSOLE(println("Ethernet: rebind success!"));
[8]146      break;
147  }
148  if (client.connected()) {
149    if (!_connected) {
150      _connected = true;
[10]151      CONSOLE(println("Ethernet: connected!")); 
[8]152    }
153  } else {
154    if (_connected) {
155      _connected = false;
[10]156      CONSOLE(println("Ethernet: connection lost!"));
[8]157    }
[10]158//    CONSOLE(println("Ethernet: restarting..."));
[8]159    client.stop();
[10]160//    CONSOLE(println("Ethernet: connecting..."));
[8]161    client.connect(server, 23456); 
162  }
163}
164
[11]165void    Sent_OT_Data (const OT_Action& data)
[8]166{
167  state = OPENTHERM::getMode();
[9]168  if (client.connected()) {
169    client.print("[");
170    client.print(loops);
171    client.print("|");
172    client.print(state,HEX);
173    client.print("]\t");
[11]174/*
[9]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);
[11]182*/
[12]183    client.print(data.GetRequestValue(),HEX);
[11]184    client.print("\t");
[12]185    client.print(data.GetResponseValue(),HEX);
[9]186    client.println("");
187  }
[8]188}
[21]189
[25]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
[21]197float OT_Boiler::GetBoilerTemperature ()
198{
199        a2 = analogRead(A2);   // read the input pin
[25]200        tBoiler = AD2Temperature(a2);
[21]201        return tBoiler;
202}
203
204float OT_Boiler::GetReturnTemperature ()
205{
[25]206  a3 = analogRead(A3);   // read the input pin
207  tReturn = AD2Temperature(a3);
208  return tReturn;
[21]209}
Note: See TracBrowser for help on using the repository browser.