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

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

Changed: Using of serial is optional and non-blocking

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