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

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

Setup project structure

File size: 3.6 KB
Line 
1#include <SPI.h>
2#include <Ethernet.h>
3#include <opentherm.h>
4
5#define THERMOSTAT_IN 2
6#define THERMOSTAT_OUT  4
7
8byte  mac[] = { 0x02, 0xDE, 0xAD, 0xDE, 0xAD, 0x01 };
9byte  server[] = { 192, 168, 0, 22 };
10volatile long  loops = 0;
11volatile long  state = 0;
12volatile bool  _connected = false;
13
14OpenthermData message;
15
16EthernetClient client;
17
18void setup()
19{
20  pinMode(THERMOSTAT_IN, INPUT);
21  digitalWrite(THERMOSTAT_OUT, HIGH);
22  pinMode(THERMOSTAT_OUT, OUTPUT); // low output = high current, high output = low current
23
24  // Console
25  // Serial.begin(9600);
26  Serial.begin(19200);
27/*
28  while (!Serial) {
29    ; // wait for serial port to connect. Needed for native USB port only
30  }
31*/
32  Serial.println("\nSystem starting...");
33  // Setup Ethernet
34  Serial.print("Ethernet.begin...");
35  Ethernet.begin(mac);
36  Serial.println("done");
37  // Check for Ethernet hardware present
38  if (Ethernet.hardwareStatus() == EthernetNoHardware) {
39    Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware. :(");
40    while (true) {
41      delay(1); // do nothing, no point running without Ethernet hardware
42    }
43  }
44/*
45  if (Ethernet.linkStatus() == LinkOFF) {
46    Serial.println("Ethernet cable is not connected.");
47  }
48*/
49}
50
51void loop()
52{
53  Serial.print("[");
54  Serial.print(++loops);
55  Serial.print("|");
56  Serial.print(state,HEX);
57  Serial.println("]");
58  maintainEthernet();
59  if (OPENTHERM::getMessage(message)) {
60    Serial.print("-> ");
61    OPENTHERM::printToSerial(message);
62    Serial.println();
63    Sent_OT_Data(message);
64
65    delay(100); // Opentherm defines delay between request and response
66    message.type = OT_MSGTYPE_UNKNOWN_DATAID; // construct fake response
67    message.valueHB = 0;
68    message.valueLB = 0;
69    OPENTHERM::send(THERMOSTAT_OUT, message, listenAfterResponse); // send some response and wait for antoher request from thermostat set by callback
70  } else {
71    if (OPENTHERM::isSent() || OPENTHERM::isIdle() || OPENTHERM::isError()) { // wait for request from thermostat
72      OPENTHERM::listen(THERMOSTAT_IN, -1);
73    }
74    OpenthermData dummy;
75    dummy.type = OT_MSGTYPE_UNKNOWN_DATAID;
76    dummy.id = OT_MSGID_STATUS;
77    dummy.valueHB = (loops >> 8) & 0xFF;
78    dummy.valueLB = loops & 0xFF;
79    Sent_OT_Data(dummy);
80  }
81  delay(1000);
82}
83
84void maintainEthernet ()
85{
86  byte rc = Ethernet.maintain();
87  switch (rc) {
88    case 1: // renew failed
89      Serial.println("Ethernet: renew failed!");
90      break;
91    case 2: // renew success
92      Serial.println("Ethernet: renew success!");
93      break;
94    case 3: // rebind failed
95      Serial.println("Ethernet: rebind failed!");
96      break;
97    case 4: // rebind success 
98      Serial.println("Ethernet: rebind success!");
99      break;
100  }
101  if (client.connected()) {
102    if (!_connected) {
103      _connected = true;
104      Serial.println("Ethernet: connected!"); 
105    }
106  } else {
107    if (_connected) {
108      _connected = false;
109      Serial.println("Ethernet: connection lost!"); 
110    }
111    Serial.println("Ethernet: restarting...");
112    client.stop();
113    Serial.println("Ethernet: connecting...");
114    client.connect(server, 23456); 
115  }
116}
117
118void    Sent_OT_Data (OpenthermData& data)
119{
120  state = OPENTHERM::getMode();
121    if (client.connected()) {
122      client.print("[");
123      client.print(loops);
124      client.print("|");
125      client.print(state,HEX);
126      client.print("]\t");
127      client.print(data.type,HEX);
128      client.print(data.id,HEX);
129      client.print(data.valueHB,HEX);
130      client.print(data.valueLB,HEX);
131      client.println("");
132    }
133}
134
135void  listenAfterResponse () {
136  Serial.print("<- ");
137  OPENTHERM::printToSerial(message);
138  Serial.println();
139  Serial.println();
140  OPENTHERM::listen(THERMOSTAT_IN, -1);
141}
Note: See TracBrowser for help on using the repository browser.