Changeset 31 for trunk/Arduino/projects/OT_boiler/OT_boiler.ino
- Timestamp:
- Oct 17, 2019, 8:55:57 AM (7 years ago)
- File:
-
- 1 edited
-
trunk/Arduino/projects/OT_boiler/OT_boiler.ino (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/Arduino/projects/OT_boiler/OT_boiler.ino
r29 r31 3 3 #include <OT_Slave.h> 4 4 5 //#define ACTIVITY_LED_PIN LED_BUILTIN 6 #define ACTIVITY_LED_PIN 9 7 8 #define USE_ETHERNET 0 5 //#define ACTIVITY_LED_PIN LED_BUILTIN 6 #define ACTIVITY_LED_PIN 9 7 #define PWM_PIN 10 8 9 #define USE_ETHERNET 0 9 10 10 11 #define CONSOLE(cmd) do { if (Serial) Serial.cmd; } while (0) 11 12 12 void Sent_OT_Data (const OT_Action& data);13 14 byte mac[] = { 0x02, 0xDE, 0xAD, 0xDE, 0xAD, 0x01 };15 byte server[] = { 192, 168, 0, 22 };13 void Sent_OT_Data (const OT_Action& data); 14 15 byte mac[] = { 0x02, 0xDE, 0xAD, 0xDE, 0xAD, 0x01 }; 16 byte server[] = { 192, 168, 0, 22 }; 16 17 17 18 volatile long loops = 0; … … 24 25 25 26 protected: 26 int a2; 27 int a3; 28 int a4; 29 int a5; 30 31 virtual double AD2Temperature (int); 27 long a2; 28 long a3; 29 long a4; 30 long a5; 31 long cnt; 32 long cntVal; 33 34 long pwm; 35 36 long a2Val; 37 long a2Dir; // Boiler (VC-40) active detection 38 bool a2Rising; 39 40 void ReadData (); 41 void CalcData (); 42 43 virtual double AD2Temperature (int); 32 44 33 45 public: 46 OT_Boiler (); 47 48 virtual void maintain (); 49 50 virtual bool IsActive (); 51 52 virtual void SetRequestTemperature (float); 34 53 virtual float GetBoilerTemperature (); 35 54 virtual float GetReturnTemperature (); 36 55 37 friend void loop();56 friend void loop (); 38 57 }; 39 58 … … 42 61 EthernetClient client; 43 62 44 void setup() 45 { 46 msTicks = millis(); 47 digitalWrite(ACTIVITY_LED_PIN, LOW); // Built-in LED off 48 pinMode(ACTIVITY_LED_PIN, OUTPUT); // Built-in LED active 49 // Console 50 Serial.begin(19200); 51 CONSOLE(println("\nSystem starting...")); 52 // Setup OT 53 CONSOLE(print("OpenTherm.begin...")); 54 boiler.begin(Sent_OT_Data); 55 CONSOLE(println("done")); 63 void setup() 64 { 65 msTicks = millis(); 66 digitalWrite(ACTIVITY_LED_PIN, LOW); // Activity LED off 67 pinMode(ACTIVITY_LED_PIN, OUTPUT); // Activity LED active 68 pinMode(PWM_PIN, OUTPUT); // PWM output 69 // Console 70 // Serial.begin(19200); 71 Serial.begin(115200); 72 CONSOLE(println("\nSystem starting...")); 73 // Setup OT 74 CONSOLE(print("OpenTherm.begin...")); 75 boiler.begin(Sent_OT_Data); 76 CONSOLE(println("done")); 56 77 #if USE_ETHERNET 57 // Setup Ethernet58 CONSOLE(print("Ethernet.begin..."));59 Ethernet.begin(mac);60 CONSOLE(println("done"));78 // Setup Ethernet 79 CONSOLE(print("Ethernet.begin...")); 80 Ethernet.begin(mac); 81 CONSOLE(println("done")); 61 82 #else 62 CONSOLE(println("Ethernet is disabled!"));83 CONSOLE(println("Ethernet is disabled!")); 63 84 #endif 64 85 } 65 86 66 void loop()67 { 68 loops++;87 void loop() 88 { 89 loops++; 69 90 /* 70 Serial.print("[");71 Serial.print(loops);72 Serial.print("|");73 Serial.print(state,HEX);74 Serial.println("]");91 Serial.print("["); 92 Serial.print(loops); 93 Serial.print("|"); 94 Serial.print(state,HEX); 95 Serial.println("]"); 75 96 */ 76 msTicks = millis();97 msTicks = millis(); 77 98 #if USE_ETHERNET 78 maintainEthernet();99 maintainEthernet(); 79 100 #endif 80 boiler.maintain();101 boiler.maintain(); 81 102 #ifdef _NEVER_ 82 unsigned long base = boiler.IsActive() ? 500 : 3000; 83 unsigned long periode = msTicks / base; 84 int led = (periode & 1) ? HIGH : LOW; 85 if (Serial) { 86 static unsigned long prev = 0; 87 if (prev != periode) { 88 prev = periode; 89 Serial.print("Time: "); 90 Serial.print(msTicks); 91 Serial.print("\tPeriode: "); 92 Serial.print(periode); 93 Serial.print("\tLED: "); 94 Serial.println(led); 95 } 103 // unsigned long periode = msTicks / (boiler.IsActive() ? 500 : 2000); 104 unsigned long periode = msTicks / (boiler.IsActive() ? 2000 : 2000); 105 static unsigned long prev = 0; 106 if (prev != periode) { 107 prev = periode; 108 #else 109 static unsigned long limit = 0; 110 if (!limit) { 111 unsigned long interval = boiler.IsActive() ? 500 : 2000; 112 limit = ((msTicks / interval) + 1 ) * interval; 96 113 } 97 digitalWrite(ACTIVITY_LED_PIN, led); 98 //digitalWrite(ACTIVITY_LED_PIN, boiler.IsActive() ? HIGH : LOW); 99 #else 100 unsigned long periode = msTicks / (boiler.IsActive() ? 500 : 1000); 101 static unsigned long prev = 0; 102 if (prev != periode) { 103 prev = periode; 104 int led = (periode & 1) ? HIGH : LOW; 105 if (Serial) { 106 Serial.print("Time: "); 107 Serial.print(msTicks); 108 Serial.print("\tPeriode: "); 109 Serial.print(periode); 110 Serial.print("\tLED: "); 111 Serial.print(led); 112 float tBoiler = boiler.GetBoilerTemperature(); 113 Serial.print("\tA2: "); 114 Serial.print(boiler.a2); 115 Serial.print("\tT(A2): "); 116 Serial.print(tBoiler); 117 float tReturn = boiler.GetReturnTemperature(); 118 Serial.print("\tA3: "); 119 Serial.print(boiler.a3); 120 Serial.print("\tT(A3): "); 121 Serial.print(tReturn); 122 boiler.a4 = analogRead(A4); // read the input pin 123 Serial.print("\tA4: "); 124 Serial.print(boiler.a4); 125 boiler.a5 = analogRead(A5); // read the input pin 126 Serial.print("\tA5: "); 127 Serial.println(boiler.a5); 128 } 129 digitalWrite(ACTIVITY_LED_PIN, led); 130 } 114 static unsigned long periode = 0; 115 if (msTicks >= limit) { 116 limit = 0; 117 periode += 1; 131 118 #endif 132 } 133 134 void maintainEthernet () 135 { 136 // Check for Ethernet hardware present 137 if (Ethernet.hardwareStatus() == EthernetNoHardware) { 138 return; 139 } 140 byte rc = Ethernet.maintain(); 141 switch (rc) { 142 case 1: // renew failed 143 CONSOLE(println("Ethernet: renew failed!")); 144 break; 145 case 2: // renew success 146 CONSOLE(println("Ethernet: renew success!")); 147 break; 148 case 3: // rebind failed 149 CONSOLE(println("Ethernet: rebind failed!")); 150 break; 151 case 4: // rebind success 152 CONSOLE(println("Ethernet: rebind success!")); 153 break; 154 } 155 if (client.connected()) { 156 if (!_connected) { 157 _connected = true; 158 CONSOLE(println("Ethernet: connected!")); 159 } 160 } else { 161 if (_connected) { 162 _connected = false; 163 CONSOLE(println("Ethernet: connection lost!")); 164 } 165 // CONSOLE(println("Ethernet: restarting...")); 166 client.stop(); 167 // CONSOLE(println("Ethernet: connecting...")); 168 client.connect(server, 23456); 169 } 119 int led = (periode & 1) ? HIGH : LOW; 120 if (Serial) { 121 boiler.CalcData(); 122 Serial.print("Time: "); 123 Serial.print(msTicks); 124 Serial.print("\tPeriode: "); 125 Serial.print(periode); 126 /* 127 Serial.print("\tLED: "); 128 Serial.print(led); 129 */ 130 Serial.print("\tCnt: "); 131 Serial.print(boiler.cntVal); 132 float tBoiler = boiler.GetBoilerTemperature(); 133 Serial.print("\tA2: "); 134 Serial.print(boiler.a2); 135 Serial.print("\tT(A2): "); 136 Serial.print(tBoiler); 137 Serial.print("\tDir(A2): "); 138 Serial.print(boiler.a2Dir); 139 Serial.print("\tRising(A2): "); 140 Serial.print(boiler.a2Rising); 141 float tReturn = boiler.GetReturnTemperature(); 142 Serial.print("\tA3: "); 143 Serial.print(boiler.a3); 144 Serial.print("\tT(A3): "); 145 Serial.print(tReturn); 146 Serial.print("\tA4: "); 147 Serial.print(boiler.a4); 148 Serial.print("\tA5: "); 149 Serial.print(boiler.a5); 150 Serial.print("\tPWM: "); 151 // long _pwm = (msTicks / 20000) % 256; 152 // long _pwm = long(0.9097+164.44*(0.4283+boiler.tRequest*(0.0009+boiler.tRequest*(0.00006-boiler.tRequest*0.0000003)))); 153 // analogWrite(PWM_PIN, _pwm); 154 Serial.print(boiler.pwm); 155 Serial.print("\tReq: "); 156 Serial.print(boiler.tRequest); 157 Serial.print("\tQ(A): "); 158 Serial.println(double(boiler.a5)/double(boiler.a4)); 159 } 160 digitalWrite(ACTIVITY_LED_PIN, led); 161 } 162 } 163 164 void maintainEthernet () 165 { 166 // Check for Ethernet hardware present 167 if (Ethernet.hardwareStatus() == EthernetNoHardware) { 168 return; 169 } 170 byte rc = Ethernet.maintain(); 171 switch (rc) { 172 case 1: // renew failed 173 CONSOLE(println("Ethernet: renew failed!")); 174 break; 175 case 2: // renew success 176 CONSOLE(println("Ethernet: renew success!")); 177 break; 178 case 3: // rebind failed 179 CONSOLE(println("Ethernet: rebind failed!")); 180 break; 181 case 4: // rebind success 182 CONSOLE(println("Ethernet: rebind success!")); 183 break; 184 } 185 if (client.connected()) { 186 if (!_connected) { 187 _connected = true; 188 CONSOLE(println("Ethernet: connected!")); 189 } 190 } else { 191 if (_connected) { 192 _connected = false; 193 CONSOLE(println("Ethernet: connection lost!")); 194 } 195 // CONSOLE(println("Ethernet: restarting...")); 196 client.stop(); 197 // CONSOLE(println("Ethernet: connecting...")); 198 client.connect(server, 23456); 199 } 170 200 } 171 201 172 202 void Sent_OT_Data (const OT_Action& data) 173 203 { 174 state = OPENTHERM::getMode();175 if (client.connected()) {176 client.print("[");177 client.print(loops);178 client.print("|");179 client.print(state,HEX);180 client.print("]\t");204 state = OPENTHERM::getMode(); 205 if (client.connected()) { 206 client.print("["); 207 client.print(loops); 208 client.print("|"); 209 client.print(state,HEX); 210 client.print("]\t"); 181 211 /* 182 client.print(data.type,HEX);183 client.print(":");184 client.print(data.id,HEX);185 client.print(":");186 client.print(data.valueHB,HEX);187 client.print(":");188 client.print(data.valueLB,HEX);212 client.print(data.type,HEX); 213 client.print(":"); 214 client.print(data.id,HEX); 215 client.print(":"); 216 client.print(data.valueHB,HEX); 217 client.print(":"); 218 client.print(data.valueLB,HEX); 189 219 */ 190 client.print(data.GetRequestValue(),HEX); 191 client.print("\t"); 192 client.print(data.GetResponseValue(),HEX); 193 client.println(""); 194 } 195 } 196 197 double OT_Boiler::AD2Temperature (int x) 198 { 199 const double m = 0.2059; 200 const double b = -27.951; 201 return m*double(x)+b; 202 } 203 204 float OT_Boiler::GetBoilerTemperature () 205 { 206 a2 = analogRead(A2); // read the input pin 220 client.print(data.GetRequestValue(),HEX); 221 client.print("\t"); 222 client.print(data.GetResponseValue(),HEX); 223 client.println(""); 224 } 225 } 226 227 OT_Boiler::OT_Boiler () 228 : 229 a2(0), 230 a3(0), 231 a4(0), 232 a5(0), 233 cnt(0), 234 cntVal(0), 235 a2Val(0), 236 a2Dir(0), 237 a2Rising(false) 238 { 239 } 240 241 void OT_Boiler::maintain () 242 { 243 ReadData(); 244 OT_Slave::maintain(); 245 } 246 247 void OT_Boiler::ReadData () 248 { 249 long a2Prev = a2Val; 250 a2Val = analogRead(A2); 251 a2 += a2Val; 252 a3 += analogRead(A3); 253 a4 += analogRead(A4); 254 a5 += analogRead(A5); 255 cnt++; 256 // Calc A2 direction... 257 if (a2Val != a2Prev) { 258 if (a2Val < a2Prev) { 259 if (a2Dir > 0) { 260 a2Dir = -1; 261 } else { 262 a2Dir -= 1; 263 if (a2Dir < -5) { 264 a2Rising = false; 265 } 266 } 267 } else { 268 if (a2Dir < 0) { 269 a2Dir = 1; 270 } else { 271 a2Dir += 1; 272 if (a2Dir > 5) { 273 a2Rising = true; 274 } 275 } 276 } 277 } 278 } 279 280 void OT_Boiler::CalcData () 281 { 282 if (cnt > 1) { 283 cntVal = cnt; 284 a2 /= cnt; 285 a3 /= cnt; 286 a4 /= cnt; 287 a5 /= cnt; 288 cnt = 1; 289 } 290 } 291 292 double OT_Boiler::AD2Temperature (int x) 293 { 294 const double m = 0.1816; 295 const double b = -19.234; 296 return m*double(x)+b; 297 } 298 299 bool OT_Boiler::IsActive () 300 { 301 return a2Rising; 302 } 303 304 void OT_Boiler::SetRequestTemperature (float t) 305 { 306 bool update(t != tRequest); 307 OT_Slave::SetRequestTemperature(t); 308 if (update) { 309 pwm = long(0.9097+164.44*(0.4283+boiler.tRequest*(0.0009+boiler.tRequest*(0.00006-boiler.tRequest*0.0000003)))); 310 analogWrite(PWM_PIN, pwm); 311 } 312 } 313 314 float OT_Boiler::GetBoilerTemperature () 315 { 316 CalcData(); 207 317 tBoiler = AD2Temperature(a2); 208 318 return tBoiler; 209 319 } 210 320 211 float OT_Boiler::GetReturnTemperature ()212 { 213 a3 = analogRead(A3); // read the input pin 214 tReturn = AD2Temperature(a3);215 return tReturn;216 } 321 float OT_Boiler::GetReturnTemperature () 322 { 323 CalcData(); 324 tReturn = AD2Temperature(a3); 325 return tReturn; 326 }
Note: See TracChangeset
for help on using the changeset viewer.
![(please configure the [header_logo] section in trac.ini)](/trac/Paradigma/chrome/site/paradigma.png)