#include #include #include #include //#define ACTIVITY_LED_PIN LED_BUILTIN #define ACTIVITY_LED_PIN 9 #define PWM_PIN 10 #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; class OT_Boiler : public OT_Slave { protected: SMA a2; SMA a3; SMA a4; SMA a5; SMA a2Trend; bool a2Rising; // Boiler (VC-506E) active detection float qRequest; float qCurrent; // debug only ??? int pwmRequest; int pwmCurrent; void ReadData (); static double Func_T_of_A (int a, int pin) { return Func_T_of_A(float(a), pin); } static double Func_T_of_A (float a, int pin); static double Func_Q789_of_T (double t); static double Func_PWM_of_Q789 (double q); public: OT_Boiler (); virtual void maintain (); virtual void periodic (); virtual bool IsActive (); virtual void SetRequestTemperature (float); virtual float GetBoilerTemperature (); virtual float GetReturnTemperature (); friend void loop (); }; OT_Boiler boiler; EthernetClient client; void setup() { msTicks = millis(); digitalWrite(ACTIVITY_LED_PIN, LOW); // Activity LED off pinMode(ACTIVITY_LED_PIN, OUTPUT); // Activity LED active pinMode(PWM_PIN, OUTPUT); // PWM output // Console // Serial.begin(19200); Serial.begin(115200); 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(); static unsigned long limit = 0; if (!limit) { unsigned long interval = boiler.IsActive() ? 500 : 2000; limit = ((msTicks / interval) + 1 ) * interval; } static unsigned long periode = 0; if (msTicks >= limit) { limit = 0; periode += 1; int led = (periode & 1) ? HIGH : LOW; boiler.periodic(); if (Serial) { Serial.print("Time: "); Serial.print(msTicks); Serial.print("\tPeriode: "); Serial.print(periode); Serial.print("\tA2: "); Serial.print(boiler.a2.mean()); Serial.print("\tA2.Trend: "); Serial.print(boiler.a2Trend.slope()); Serial.print("\tA2.Rising: "); Serial.print(boiler.a2Rising); Serial.print("\tT(A2): "); Serial.print(boiler.GetBoilerTemperature()); Serial.print("\tA3: "); Serial.print(boiler.a3.mean()); Serial.print("\tT(A3): "); Serial.print(boiler.GetReturnTemperature()); Serial.print("\tA4: "); Serial.print(boiler.a4.mean()); Serial.print("\tA5: "); Serial.print(boiler.a5.mean()); Serial.print("\tPWM: "); Serial.print(boiler.pwmCurrent); Serial.print("\tReq: "); Serial.print(boiler.tRequest); Serial.print("\tQ(A): "); Serial.println(boiler.qCurrent); } digitalWrite(ACTIVITY_LED_PIN, led); } } 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(""); } } void OT_Boiler::ReadData () { a2.push(analogRead(A2)); a3.push(analogRead(A3)); a4.push(analogRead(A4)); a5.push(analogRead(A5)); } double OT_Boiler::Func_T_of_A (float a, int pin) { double x(a); // 'Messung.xlsx'::Messung-3.1.3M const double a0(-19.234); const double a1(0.1816); return a0 + x * a1; } double OT_Boiler::Func_Q789_of_T (double t) { // 'Heizung.xlsx'::'789-Calc' const double a0(0.4283); const double a1(0.0009); const double a2(0.00006); const double a3(-0.0000003); return a0 + t * (a1 + t * (a2 + t * a3)); } double OT_Boiler::Func_PWM_of_Q789 (double q) { // 'Heizung.xlsx'::'789-Mess' const double a0(-0.9097); const double a1(164.44); return a0 + q * a1; } OT_Boiler::OT_Boiler () : a2(13), a3(13), a4(13), a5(13), a2Trend(7), a2Rising(false), qRequest(0), qCurrent(0), pwmRequest(0), pwmCurrent(0) { analogRead(A2); analogRead(A3); analogRead(A4); analogRead(A5); } void OT_Boiler::maintain () { ReadData(); OT_Slave::maintain(); } void OT_Boiler::periodic () { a2Trend.push(a2.mean()); if (a2Rising) { // a2Rising = (a2Trend.slope() > (0.00025 * a2Trend.mean())); a2Rising = (a2Trend.slope() >= 0.0); } else { // a2Rising = a2Trend.loaded() && (a2Trend.slope() > (0.0025 * a2Trend.mean())); a2Rising = a2Trend.loaded() && (a2Trend.slope() > 0.1); } } bool OT_Boiler::IsActive () { return a2Rising; } void OT_Boiler::SetRequestTemperature (float t) { bool update = (t != tRequest); OT_Slave::SetRequestTemperature(t); qRequest = Func_Q789_of_T(tRequest); pwmRequest = Func_PWM_of_Q789(qRequest); qCurrent = a5/a4; int diff = round(100.0 * (qCurrent - qRequest)); if (diff) { if (update) { pwmCurrent = pwmRequest; } else { pwmCurrent += ((diff > 0) ? -1 : 1); } analogWrite(PWM_PIN, pwmCurrent); } } float OT_Boiler::GetBoilerTemperature () { tBoiler = Func_T_of_A(a2.mean(), A2); return tBoiler; } float OT_Boiler::GetReturnTemperature () { tReturn = Func_T_of_A(a3.mean(), A3); return tReturn; }