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

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

Skip Ethernet-Handling if not present

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