source: trunk/resources/arduino-opentherm/examples/gateway/gateway.ino

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

Initial setup

File size: 1.9 KB
RevLine 
[1]1#include <opentherm.h>
2
3#define THERMOSTAT_IN 2
4#define THERMOSTAT_OUT 4
5#define BOILER_IN 3
6#define BOILER_OUT 5
7
8OpenthermData message;
9
10void setup() {
11  pinMode(THERMOSTAT_IN, INPUT);
12  digitalWrite(THERMOSTAT_OUT, HIGH);
13  pinMode(THERMOSTAT_OUT, OUTPUT); // low output = high current, high output = low current
14  pinMode(BOILER_IN, INPUT);
15  digitalWrite(BOILER_OUT, HIGH);
16  pinMode(BOILER_OUT, OUTPUT); // low output = high voltage, high output = low voltage
17
18  Serial.begin(19200);
19}
20
21#define MODE_LISTEN_MASTER 0
22#define MODE_LISTEN_SLAVE 1
23
24int mode = 0;
25
26/**
27 * Loop will act as man in the middle connected between Opentherm boiler and Opentherm thermostat.
28 * It will listen for requests from thermostat, forward them to boiler and then wait for response from boiler and forward it to thermostat.
29 * Requests and response are logged to Serial on the way through the gateway.
30 */
31void loop() {
32  if (mode == MODE_LISTEN_MASTER) {
33    if (OPENTHERM::isSent() || OPENTHERM::isIdle() || OPENTHERM::isError()) {
34      OPENTHERM::listen(THERMOSTAT_IN);
35    }
36    else if (OPENTHERM::getMessage(message)) {
37      Serial.print("-> ");
38      OPENTHERM::printToSerial(message);
39      Serial.println();
40      OPENTHERM::send(BOILER_OUT, message); // forward message to boiler
41      mode = MODE_LISTEN_SLAVE;
42    }
43  }
44  else if (mode == MODE_LISTEN_SLAVE) {
45    if (OPENTHERM::isSent()) {
46      OPENTHERM::listen(BOILER_IN, 800); // response need to be send back by boiler within 800ms
47    }
48    else if (OPENTHERM::getMessage(message)) {
49      Serial.print("<- ");
50      OPENTHERM::printToSerial(message);
51      Serial.println();
52      Serial.println();
53      OPENTHERM::send(THERMOSTAT_OUT, message); // send message back to thermostat
54      mode = MODE_LISTEN_MASTER;
55    }
56    else if (OPENTHERM::isError()) {
57      mode = MODE_LISTEN_MASTER;
58      Serial.println("<- Timeout");
59      Serial.println();
60    }
61  }
62}
Note: See TracBrowser for help on using the repository browser.