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

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

Changed: Using of serial is optional and non-blocking

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