-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathfunctions.h
152 lines (143 loc) · 4.81 KB
/
functions.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/**************************************************************
General Functions
**************************************************************/
/*
getCurrentTime() -
- return the time as 'HH:MM:SS'
*/
String getCurrentTime() {
String extraZeroH = "", extraZeroM = "", extraZeroS = "";
if (hour() < 10) extraZeroH = '0';
if (minute() < 10) extraZeroM = '0';
if (second() < 10) extraZeroS = '0';
return String(extraZeroH + hour()) + ':' + extraZeroM + minute() + ':' + extraZeroS + second();
}
/*
getCurrentDate() -
- return the date as 'DD:MM:YY'
*/
String getCurrentDate() {
return String(day()) + '-' + monthShortStr(month()) + '-' + (year() - 2000);
}
/*
curDateTime() -
- return the dat & time as String 'DD:MM:YYYY HH:MM:SS'
*/
String curDateTime() {
return getCurrentDate() + String(" ") + getCurrentTime();
}
/*
printTask() - usage 'printTask(String(abc));'
- outputs to Serial or Terminal or both depending what is defined in settings
*/
void printTask(String a, String b) {
#if defined(OUTPUT_SERIAL)
Serial.print(a + String("="));
Serial.println(b);
#endif
#if defined(OUTPUT_TERMINAL)
terminal.print(a + String("="));
terminal.println(b);
terminal.flush();
#endif
}
/**************************************************************
Flow Sensor
**************************************************************/
/*
pulseCounter() -
- attached to an interrupt counting the pulses from the sensor then used to calculate the rate
*/
void pulseCounter() {
pulseCount++;
}
/*
flowSensor() -
- uses interrupt on the sensor pin
- calculates the flowRate in mL/s then converts to L/s
- update Blynk virtual pins with the data
- calculate and compile the water cost based on the price set in settings and usage
- calculate and compile the monthly water cost and usage
*/
void flowSensor() {
detachInterrupt(FLOW_SENSOR);
flowRate = pulseCount / FLOW_CALIBRATION;
pulseCount = 0;
attachInterrupt(FLOW_SENSOR, pulseCounter, FALLING);
//calc totals
flowMilliLitres = (flowRate / 60) * 1000;
totalMilliLitres += flowMilliLitres;
totalMilliLitres_month += flowMilliLitres;
Blynk.virtualWrite(vPIN_WATER_TOTAL, (float)totalMilliLitres / 1000);
Blynk.virtualWrite(vPIN_WATER_FLOW, String((float)flowMilliLitres, 0));
Blynk.virtualWrite(vPIN_WATER_TOTAL_MONTH, (float)totalMilliLitres_month / 1000);
waterCost = waterCost + ( ( ( (float)flowMilliLitres / 1000 ) * WATER_PRICE ) / 100);
Blynk.virtualWrite(vPIN_WATER_COST, String(waterCost, 6));
waterCost_month = waterCost_month + ( ( ( (float)flowMilliLitres / 1000 ) * WATER_PRICE ) / 100);
Blynk.virtualWrite(vPIN_WATER_COST_MONTH, String(waterCost_month, 6));
}
/**************************************************************
Tap Control
**************************************************************/
/*
TAP_Off() -
- only trigger if relay is active
- turn off tap relay
- dim status led widget to half brightness
- after 6sec it goes in to it shuts down
- print to terminal the used stats and cost
- turn off status led widget
- write the usage and cost to the table in two lines
*/
void TAP_Off() {
if (!digitalRead(TAP)) {
digitalWrite(TAP, HIGH);
Blynk.virtualWrite(vPIN_TAP_LED, 128);
printTask("TAP", "SHUTTING DOWN..");
timer.setTimeout(6000, []() {
printTask("TAP", "OFF");
printTask("USED", String((float)totalMilliLitres / 1000) + String("L COST=$") + String(waterCost, 3));
Blynk.virtualWrite(vPIN_TAP_LED, 0);
Blynk.virtualWrite(vPIN_TABLE, "add", rowIndex, curDateTime(), String( (float)totalMilliLitres / 1000, 3) + String(" L"));
rowIndex++;
Blynk.virtualWrite(vPIN_TABLE, "add", rowIndex, " ", String("$") + String(waterCost, 6));
rowIndex++;
});
}
}
/*
TAP_On() -
- reset 'current' used used and cost
- turn on tap relay
- turn on led status widget
- depending on limit type start a timer to stop the tap
- A timed function that checks if the water use limit is reached and shuts down the tap
*/
void TAP_On() {
totalMilliLitres = 0;
waterCost = 0;
digitalWrite(TAP, LOW);
Blynk.virtualWrite(vPIN_TAP_LED, 255);
switch (limitType) {
case 1:
printTask("TAP=ON LIMIT", String(tap_limit_value / 1000) + String(" SEC"));
timer.setTimeout(tap_limit_value, TAP_Off);
break;
case 2:
printTask("TAP=ON LIMIT", String(tap_limit_value) + String(" LTRS"));
timer1 = timer.setInterval(250, []() {
if (tap_limit_value <= (float)totalMilliLitres / 1000) {
timer.disable(timer1);
TAP_Off();
}
});
break;
}
}
/*
TAP_Toggle() -
- Toggles the tap state.
*/
void TAP_Toggle() {
digitalRead(TAP) ? TAP_On() : TAP_Off();
}