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;
boolean flow = false;
boolean flowChanged = false;
boolean flowOn = false;
boolean pump = false;
boolean pumpChanged = false;
boolean pumpOn = false;
boolean pumpRan = false;
boolean pumpEnabled = true;
boolean pumpEnabled = true; // controls pump duty cycle
// FSM
enum FSM {S0,S1,S2,S3};
@@ -124,7 +123,7 @@ void hotwater_loop () {
switch (state){
case S0:
if (flow && pumpEnabled) {
if (flowChanged && pumpEnabled) {
state = S1;
if (DEBUG) DEBUGSTATE(state)
DPRINT("pump start @ ");
@@ -141,7 +140,7 @@ void hotwater_loop () {
}
break;
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;
if (DEBUG) DEBUGSTATE(state)
//totalflow = Tflow;
@@ -168,9 +167,9 @@ void hotwater_loop () {
if (intflag1_FLOW) {
if (ticktime > flowstart_tc + FLOW_THLD) { // delay to avoid bounces generating more interrupts
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!
flow = false;
flowChanged = false;
flowstart_tc = 0; // reset
}
intflag1_FLOW = false; // clear interrupt.
@@ -181,15 +180,15 @@ void hotwater_loop () {
// 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!
DPRINTLN("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
flow = false;
flowChanged = false;
flowstop_tc = 0;
}
}
@@ -231,7 +230,7 @@ void hotwater_loop () {
Serial.print(state);
Serial.print(" ");
Serial.print(intflag1_FLOW);
Serial.print(flow);
Serial.print(flowChanged);
Serial.print("\t");
Serial.print(flowstart_tc);
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 int ticknum; // wifi checked when this is > wifiTick
extern boolean flow;
extern boolean flowChanged;
extern boolean flowOn;
extern boolean pump;
extern boolean pumpChanged;
extern boolean pumpOn;
// these are wifi timestamps
@@ -154,9 +154,9 @@ void mqtt_loop() {
*/
// record flow
if (flow) {
if (flowChanged) {
mqtt_connect();
if (flowOn) { // pin is LOW
if (flowOn) {
flowstarted = WiFi.getTime();
mqttClient.beginMessage(topic);
mqttClient.print("start flow event@: ");
@@ -164,8 +164,8 @@ void mqtt_loop() {
DPRINTLN();
mqttClient.print(flowstarted);
mqttClient.endMessage();
flow = false; // once is enough
} else { // pin is HIGH
flowChanged = false; // once is enough
} else {
flowstopped = WiFi.getTime();
mqttClient.beginMessage(topic);
mqttClient.print("stop flow event@: ");
@@ -179,9 +179,9 @@ void mqtt_loop() {
// record pump activity
if (pump) {
if (pumpChanged) {
mqtt_connect();
if (pumpOn) { // pin is LOW
if (pumpOn) {
pumpstarted = WiFi.getTime();
mqttClient.beginMessage(topic);
mqttClient.print("start pump event@: ");
@@ -189,7 +189,7 @@ void mqtt_loop() {
DPRINTLN();
mqttClient.print(pumpstarted);
mqttClient.endMessage();
} else { // pin is HIGH
} else {
pumpstopped = WiFi.getTime();
mqttClient.beginMessage(topic);
mqttClient.print("stop pump event@: ");
+1 -1
View File
@@ -37,6 +37,6 @@ void setup() {
}
void loop() {
mqtt_loop();
hotwater_loop();
mqtt_loop();
}