improving separation of mqtt and pump control

This commit is contained in:
ken
2025-01-13 08:51:18 -08:00
parent 949778ee8a
commit 89f2043edf
3 changed files with 21 additions and 22 deletions
+11 -12
View File
@@ -81,12 +81,11 @@ unsigned long pumpstart_tc = 0;
unsigned long pumpstop_tc = 0; unsigned long pumpstop_tc = 0;
boolean flow = false; boolean flowChanged = false;
boolean flowOn = false; boolean flowOn = false;
boolean pump = false; boolean pumpChanged = false;
boolean pumpOn = false; boolean pumpOn = false;
boolean pumpRan = false; boolean pumpEnabled = true; // controls pump duty cycle
boolean pumpEnabled = true;
// FSM // FSM
enum FSM {S0,S1,S2,S3}; enum FSM {S0,S1,S2,S3};
@@ -124,7 +123,7 @@ void hotwater_loop () {
switch (state){ switch (state){
case S0: case S0:
if (flow && pumpEnabled) { if (flowChanged && pumpEnabled) {
state = S1; state = S1;
if (DEBUG) DEBUGSTATE(state) if (DEBUG) DEBUGSTATE(state)
DPRINT("pump start @ "); DPRINT("pump start @ ");
@@ -141,7 +140,7 @@ void hotwater_loop () {
} }
break; break;
case S2: case S2:
if (!flow) { // wait for flow to stop; don't need to keep pumping! if (!flowChanged) { // wait for flow to stop; don't need to keep pumping!
state = S3; state = S3;
if (DEBUG) DEBUGSTATE(state) if (DEBUG) DEBUGSTATE(state)
//totalflow = Tflow; //totalflow = Tflow;
@@ -168,9 +167,9 @@ void hotwater_loop () {
if (intflag1_FLOW) { if (intflag1_FLOW) {
if (ticktime > flowstart_tc + FLOW_THLD) { // delay to avoid bounces generating more interrupts if (ticktime > flowstart_tc + FLOW_THLD) { // delay to avoid bounces generating more interrupts
if (FLOWING) { if (FLOWING) {
flow = true; // set flow state 2000mS after interrupt IF still flowing flowChanged = true; // set flow state 2000mS after interrupt IF still flowing
} else { // flow didn't last longer than the threshold! } else { // flow didn't last longer than the threshold!
flow = false; flowChanged = false;
flowstart_tc = 0; // reset flowstart_tc = 0; // reset
} }
intflag1_FLOW = false; // clear interrupt. intflag1_FLOW = false; // clear interrupt.
@@ -181,15 +180,15 @@ void hotwater_loop () {
// record flowstop time // record flowstop time
if ((flow == true) && (NOTFLOWING) && (flowstop_tc == 0)) { if ((flowChanged == true) && (NOTFLOWING) && (flowstop_tc == 0)) {
flowstop_tc = ticktime; // only set flowstopped time once! flowstop_tc = ticktime; // only set flowstopped time once!
DPRINTLN("flow stop"); DPRINTLN("flow stop");
} }
// flow stop // flow stop
if ((flow == true) && (flowstop_tc > 0)){ if ((flowChanged == true) && (flowstop_tc > 0)){
if (ticktime > flowstop_tc + 20){ // flow state changes 2 sec after flowstopped if (ticktime > flowstop_tc + 20){ // flow state changes 2 sec after flowstopped
flow = false; flowChanged = false;
flowstop_tc = 0; flowstop_tc = 0;
} }
} }
@@ -231,7 +230,7 @@ void hotwater_loop () {
Serial.print(state); Serial.print(state);
Serial.print(" "); Serial.print(" ");
Serial.print(intflag1_FLOW); Serial.print(intflag1_FLOW);
Serial.print(flow); Serial.print(flowChanged);
Serial.print("\t"); Serial.print("\t");
Serial.print(flowstart_tc); Serial.print(flowstart_tc);
Serial.print("\t"); Serial.print("\t");
+9 -9
View File
@@ -78,9 +78,9 @@ const char subtopic[] = "arduino/state";
volatile extern unsigned long ticktime; // timer interrupt volatile extern unsigned long ticktime; // timer interrupt
volatile extern unsigned int ticknum; // wifi checked when this is > wifiTick volatile extern unsigned int ticknum; // wifi checked when this is > wifiTick
extern boolean flow; extern boolean flowChanged;
extern boolean flowOn; extern boolean flowOn;
extern boolean pump; extern boolean pumpChanged;
extern boolean pumpOn; extern boolean pumpOn;
// these are wifi timestamps // these are wifi timestamps
@@ -154,9 +154,9 @@ void mqtt_loop() {
*/ */
// record flow // record flow
if (flow) { if (flowChanged) {
mqtt_connect(); mqtt_connect();
if (flowOn) { // pin is LOW if (flowOn) {
flowstarted = WiFi.getTime(); flowstarted = WiFi.getTime();
mqttClient.beginMessage(topic); mqttClient.beginMessage(topic);
mqttClient.print("start flow event@: "); mqttClient.print("start flow event@: ");
@@ -164,8 +164,8 @@ void mqtt_loop() {
DPRINTLN(); DPRINTLN();
mqttClient.print(flowstarted); mqttClient.print(flowstarted);
mqttClient.endMessage(); mqttClient.endMessage();
flow = false; // once is enough flowChanged = false; // once is enough
} else { // pin is HIGH } else {
flowstopped = WiFi.getTime(); flowstopped = WiFi.getTime();
mqttClient.beginMessage(topic); mqttClient.beginMessage(topic);
mqttClient.print("stop flow event@: "); mqttClient.print("stop flow event@: ");
@@ -179,9 +179,9 @@ void mqtt_loop() {
// record pump activity // record pump activity
if (pump) { if (pumpChanged) {
mqtt_connect(); mqtt_connect();
if (pumpOn) { // pin is LOW if (pumpOn) {
pumpstarted = WiFi.getTime(); pumpstarted = WiFi.getTime();
mqttClient.beginMessage(topic); mqttClient.beginMessage(topic);
mqttClient.print("start pump event@: "); mqttClient.print("start pump event@: ");
@@ -189,7 +189,7 @@ void mqtt_loop() {
DPRINTLN(); DPRINTLN();
mqttClient.print(pumpstarted); mqttClient.print(pumpstarted);
mqttClient.endMessage(); mqttClient.endMessage();
} else { // pin is HIGH } else {
pumpstopped = WiFi.getTime(); pumpstopped = WiFi.getTime();
mqttClient.beginMessage(topic); mqttClient.beginMessage(topic);
mqttClient.print("stop pump event@: "); mqttClient.print("stop pump event@: ");
+1 -1
View File
@@ -37,6 +37,6 @@ void setup() {
} }
void loop() { void loop() {
mqtt_loop();
hotwater_loop(); hotwater_loop();
mqtt_loop();
} }