From a4f385710420077e3bf2816dc84c9f228e0f89d1 Mon Sep 17 00:00:00 2001 From: ken Date: Sun, 27 Apr 2025 20:13:33 -0700 Subject: [PATCH] timing tweak --- src/HotWaterController.cpp | 77 +++++++++++++++++++------------------- src/MQTTClientPump.cpp | 35 +++++++---------- src/main.cpp | 2 +- 3 files changed, 52 insertions(+), 62 deletions(-) diff --git a/src/HotWaterController.cpp b/src/HotWaterController.cpp index 3bb1faf..16c5bf2 100644 --- a/src/HotWaterController.cpp +++ b/src/HotWaterController.cpp @@ -96,43 +96,6 @@ void hotwater_setup() { // loop() is called repeatedly void hotwater_loop () { - - switch (state){ - case S0: //wait for flow threshold before turning pump on - if (flowChanged && pumpEnabled) { - state = S1; - if (FSM_DEBUG) DEBUGSTATE(state) - DPRINT("pump start @ "); - DPRINTLN(ticktime); - pumpOn = true; - pumpEnabled = false; - pumpstart_tc = ticktime; - } - break; - case S1: //wait for pump off - if (!pumpOn) { - state = S2; - if (FSM_DEBUG) DEBUGSTATE(state) - } - break; - case S2: //wait for flow to stop - if (!flowChanged) { // wait for flow to stop; don't need to keep pumping! - state = S3; - if (FSM_DEBUG) DEBUGSTATE(state) - //totalflow = Tflow; - } - break; - case S3: //wait for hysteresis time before cycling again - if (pumpEnabled) { // limit duty cycle - state = S0; - if (FSM_DEBUG) DEBUGSTATE(state) - } - break; - } // FSM - - - - // process interrupt if (intflag_FLOW) { if (FLOWING){ @@ -144,19 +107,20 @@ void hotwater_loop () { DPRINTLN("flow interrupt"); } + if (millis()+DEBOUNCE > FLOW_eventtime){ // this is a debounce flowChanged = true; FLOW_eventtime = ULONG_MAX; } // record flowstart time if ((flowChanged == true) && (FLOWING) && (flowstart_tc == 0)) { - flowstart_tc = ticktime; // only set flowstarted time once! + flowstart_tc = ticktime; DPRINTLN("flow start"); } // flow start if ((flowChanged == true) && (flowstart_tc > 0)){ - if(ticktime > flowstart_tc){ + if(ticktime > flowstart_tc + TWOSECONDS){ // flow state changes 2 sec after flowstopped flowChanged = false; reportFlowChange = true; flowOn = true; @@ -192,6 +156,7 @@ void hotwater_loop () { flowstart_tc = 0; pumpstart_tc = 0; pumpstop_tc = ticktime; + reportPumpChange = true; } } @@ -231,4 +196,38 @@ void hotwater_loop () { } } + switch (state){ + case S0: //wait for flow threshold before turning pump on + if (flowOn && pumpEnabled) { + state = S1; + if (FSM_DEBUG) DEBUGSTATE(state) + DPRINT("pump start @ "); + DPRINTLN(ticktime); + pumpOn = true; + pumpEnabled = false; + pumpstart_tc = ticktime; + reportPumpChange = true; + } + break; + case S1: //wait for pump off + if (!pumpOn) { + state = S2; + if (FSM_DEBUG) DEBUGSTATE(state) + } + break; + case S2: //wait for flow to stop + if (!flowOn) { // wait for flow to stop; don't need to keep pumping! + state = S3; + if (FSM_DEBUG) DEBUGSTATE(state) + //totalflow = Tflow; + } + break; + case S3: //wait for hysteresis time before cycling again + if (pumpEnabled) { // limit duty cycle + state = S0; + if (FSM_DEBUG) DEBUGSTATE(state) + } + break; + } // FSM + } // end loop() diff --git a/src/MQTTClientPump.cpp b/src/MQTTClientPump.cpp index 4c9e06c..5ba68d4 100644 --- a/src/MQTTClientPump.cpp +++ b/src/MQTTClientPump.cpp @@ -134,52 +134,43 @@ void mqtt_loop() { // record flow if (reportFlowChange) { mqtt_connect(); + mqttClient.beginMessage(topic); if (flowOn) { flowstarted = WiFi.getTime(); - mqttClient.beginMessage(topic); mqttClient.print("start flow event@: "); - DPRINT(flowstarted); - DPRINTLN(); - mqttClient.print(flowstarted); - mqttClient.endMessage(); - reportFlowChange = false; // do once + DPRINTLN(flowstarted); + mqttClient.print(flowstarted); } else { flowstopped = WiFi.getTime(); - mqttClient.beginMessage(topic); mqttClient.print("stop flow event@: "); - DPRINT(flowstopped); - DPRINTLN(); + DPRINTLN(flowstopped); mqttClient.print(flowstopped); - mqttClient.endMessage(); - reportFlowChange = false; // do once } + mqttClient.endMessage(); mqttClient.stop(); + reportFlowChange = false; // do once } // record pump activity - if (pumpChanged) { + if (reportPumpChange) { mqtt_connect(); + mqttClient.beginMessage(topic); if (pumpOn) { pumpstarted = WiFi.getTime(); - mqttClient.beginMessage(topic); mqttClient.print("start pump event@: "); - DPRINT(pumpstarted); - DPRINTLN(); + DPRINTLN(pumpstarted); mqttClient.print(pumpstarted); - mqttClient.endMessage(); - pumpChanged = false; // once is enough } else { pumpstopped = WiFi.getTime(); mqttClient.beginMessage(topic); mqttClient.print("stop pump event@: "); - DPRINT(pumpstopped); - DPRINTLN(); + DPRINTLN(pumpstopped); mqttClient.print(pumpstopped); - mqttClient.endMessage(); - pumpChanged = false; // once is enough - } + } + mqttClient.endMessage(); mqttClient.stop(); + reportPumpChange = false; // do once } diff --git a/src/main.cpp b/src/main.cpp index a0af26a..31ebd3d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -14,7 +14,7 @@ void hotwater_loop(); volatile boolean intflag_FLOW = false; // external GPIO interrupt (event) volatile unsigned long ticktime = 0; volatile unsigned int ticknum = 0; -volatile unsigned long FLOW_eventtime = 0; +volatile unsigned long FLOW_eventtime = ULONG_MAX; unsigned long FLOW_starttime; unsigned long FLOW_stoptime;