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

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

Prototype for network based monitoring

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