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

Last change on this file since 7 was 7, checked in by markus, 7 years ago
  • Moving class OT_V2R2_Slave to library "ot_slave_2"
  • Modified sketch OT_boiler for first combined prototype OT + ETH


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