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

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

Update: OT_Data conversion

File size: 2.6 KB
Line 
1#include <SPI.h>
2#include <Ethernet.h>
3#include <OT_Slave.h>
4
5#define CONSOLE(cmd)  do { if (Serial) Serial.cmd; } while (0)
6
7void  Sent_OT_Data (const OT_Action& data);
8
9byte  mac[] = { 0x02, 0xDE, 0xAD, 0xDE, 0xAD, 0x01 };
10byte  server[] = { 192, 168, 0, 22 };
11volatile long  loops = 0;
12volatile long  state = 0;
13volatile bool  _connected = false;
14
15OT_Slave boiler;
16
17EthernetClient client;
18
19void setup()
20{
21  // Console
22  Serial.begin(19200);
23  CONSOLE(println("\nSystem starting..."));
24  // Setup OT
25  CONSOLE(print("OpenTherm.setup..."));
26  boiler.begin(Sent_OT_Data);
27  CONSOLE(println("done"));
28  // Setup Ethernet
29  CONSOLE(print("Ethernet.begin..."));
30  Ethernet.begin(mac);
31  CONSOLE(println("done"));
32  // Check for Ethernet hardware present
33/*  Not needed anymore
34  if (Ethernet.hardwareStatus() == EthernetNoHardware) {
35    CONSOLE(println("Ethernet shield was not found.  Sorry, can't run without hardware. :("));
36    while (true) {
37      delay(1); // do nothing, no point running without Ethernet hardware
38    }
39  }
40*/
41}
42
43void loop()
44{
45  loops++;
46/*
47  Serial.print("[");
48  Serial.print(loops);
49  Serial.print("|");
50  Serial.print(state,HEX);
51  Serial.println("]");
52*/
53  maintainEthernet();
54  boiler.maintain();
55/*
56  delay(1000);
57*/
58}
59
60void maintainEthernet ()
61{
62  byte rc = Ethernet.maintain();
63  switch (rc) {
64    case 1: // renew failed
65      CONSOLE(println("Ethernet: renew failed!"));
66      break;
67    case 2: // renew success
68      CONSOLE(println("Ethernet: renew success!"));
69      break;
70    case 3: // rebind failed
71      CONSOLE(println("Ethernet: rebind failed!"));
72      break;
73    case 4: // rebind success 
74      CONSOLE(println("Ethernet: rebind success!"));
75      break;
76  }
77  if (client.connected()) {
78    if (!_connected) {
79      _connected = true;
80      CONSOLE(println("Ethernet: connected!")); 
81    }
82  } else {
83    if (_connected) {
84      _connected = false;
85      CONSOLE(println("Ethernet: connection lost!"));
86    }
87//    CONSOLE(println("Ethernet: restarting..."));
88    client.stop();
89//    CONSOLE(println("Ethernet: connecting..."));
90    client.connect(server, 23456); 
91  }
92}
93
94void    Sent_OT_Data (const OT_Action& data)
95{
96  state = OPENTHERM::getMode();
97  if (client.connected()) {
98    client.print("[");
99    client.print(loops);
100    client.print("|");
101    client.print(state,HEX);
102    client.print("]\t");
103/*
104    client.print(data.type,HEX);
105    client.print(":");
106    client.print(data.id,HEX);
107    client.print(":");
108    client.print(data.valueHB,HEX);
109    client.print(":");
110    client.print(data.valueLB,HEX);
111*/
112    client.print(data.GetRequestValue(),HEX);
113    client.print("\t");
114    client.print(data.GetResponseValue(),HEX);
115    client.println("");
116  }
117}
Note: See TracBrowser for help on using the repository browser.