#include #include #include //#define ACTIVITY_LED_PIN LED_BUILTIN #define ACTIVITY_LED_PIN 9 #define USE_ETHERNET 0 #define CONSOLE(cmd) do { if (Serial) Serial.cmd; } while (0) void Sent_OT_Data (const OT_Action& data); byte mac[] = { 0x02, 0xDE, 0xAD, 0xDE, 0xAD, 0x01 }; byte server[] = { 192, 168, 0, 22 }; volatile long loops = 0; volatile unsigned long msTicks = 0; volatile long state = 0; volatile bool _connected = false; OT_Slave boiler; EthernetClient client; void setup() { msTicks = millis(); digitalWrite(ACTIVITY_LED_PIN, LOW); // Built-in LED off pinMode(ACTIVITY_LED_PIN, OUTPUT); // Built-in LED active // Console Serial.begin(19200); CONSOLE(println("\nSystem starting...")); // Setup OT CONSOLE(print("OpenTherm.begin...")); boiler.begin(Sent_OT_Data); CONSOLE(println("done")); #if USE_ETHERNET // Setup Ethernet CONSOLE(print("Ethernet.begin...")); Ethernet.begin(mac); CONSOLE(println("done")); #else CONSOLE(println("Ethernet is disabled!")); #endif } void loop() { loops++; /* Serial.print("["); Serial.print(loops); Serial.print("|"); Serial.print(state,HEX); Serial.println("]"); */ msTicks = millis(); #if USE_ETHERNET maintainEthernet(); #endif boiler.maintain(); #ifdef _NEVER_ unsigned long base = boiler.IsActive() ? 500 : 3000; unsigned long periode = msTicks / base; int led = (periode & 1) ? HIGH : LOW; if (Serial) { static unsigned long prev = 0; if (prev != periode) { prev = periode; Serial.print("Time: "); Serial.print(msTicks); Serial.print("\tPeriode: "); Serial.print(periode); Serial.print("\tLED: "); Serial.println(led); } } digitalWrite(ACTIVITY_LED_PIN, led); //digitalWrite(ACTIVITY_LED_PIN, boiler.IsActive() ? HIGH : LOW); #else unsigned long periode = msTicks / (boiler.IsActive() ? 500 : 2000); static unsigned long prev = 0; int a2 = analogRead(A2); // read the input pin #ifdef _R9_22K_ const double m = 0.1309; const double b = -9.4535; #elif defined(_R9_15K_1_) const double m = 0.1746; const double b = -13.164; #else const double m = 0.1848; const double b = (-12.936 - 5.5); #endif double ta2 = m*double(a2)+b; if (prev != periode) { prev = periode; int led = (periode & 1) ? HIGH : LOW; if (Serial) { Serial.print("Time: "); Serial.print(msTicks); Serial.print("\tPeriode: "); Serial.print(periode); Serial.print("\tLED: "); Serial.print(led); Serial.print("\tA2: "); Serial.print(a2); Serial.print("\tT(A2): "); Serial.println(ta2); } digitalWrite(ACTIVITY_LED_PIN, led); } #endif } void maintainEthernet () { // Check for Ethernet hardware present if (Ethernet.hardwareStatus() == EthernetNoHardware) { return; } byte rc = Ethernet.maintain(); switch (rc) { case 1: // renew failed CONSOLE(println("Ethernet: renew failed!")); break; case 2: // renew success CONSOLE(println("Ethernet: renew success!")); break; case 3: // rebind failed CONSOLE(println("Ethernet: rebind failed!")); break; case 4: // rebind success CONSOLE(println("Ethernet: rebind success!")); break; } if (client.connected()) { if (!_connected) { _connected = true; CONSOLE(println("Ethernet: connected!")); } } else { if (_connected) { _connected = false; CONSOLE(println("Ethernet: connection lost!")); } // CONSOLE(println("Ethernet: restarting...")); client.stop(); // CONSOLE(println("Ethernet: connecting...")); client.connect(server, 23456); } } void Sent_OT_Data (const OT_Action& data) { state = OPENTHERM::getMode(); if (client.connected()) { client.print("["); client.print(loops); client.print("|"); client.print(state,HEX); client.print("]\t"); /* client.print(data.type,HEX); client.print(":"); client.print(data.id,HEX); client.print(":"); client.print(data.valueHB,HEX); client.print(":"); client.print(data.valueLB,HEX); */ client.print(data.GetRequestValue(),HEX); client.print("\t"); client.print(data.GetResponseValue(),HEX); client.println(""); } }