-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwater valves.ino photon code
181 lines (162 loc) · 4.93 KB
/
water valves.ino photon code
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
//---------------------------------------------------------------------------
// Variables and Pin definitions
//---------------------------------------------------------------------------
// pool assigned flow sensor1
// Sensor 2 not assigned
// Other Variable types
volatile unsigned int TPulseCount1 = 0;
volatile unsigned int TPulseCount2 = 0;
// pulses per liter of the flow sensorsw
const float pulsesPerLiter = 477;
// Define Pins
#define WATER_SENSOR1 D5
#define WATER_SENSOR2 D4
#define poolon D0
#define misteron D1
#define future1 D2
#define future2 D3
// Variable Define Intergers
int totalg2 = 0;
int totalg1 = 0;
int previousg1 = 0;
int voltprotect = 0;
int autofill = 0;
float poolLevel = 0;
int pfull = 90; //previously 110 before pool robot and 95 before rewire 85 before i removed grommet
int pLow = 160;
float gpmm = 13; //previously 8.33
int fillPercent = 0;
int calcfillamount = 0;
//-----------------------------------------------------------------------------
// Water Sensor interrupts
//-----------------------------------------------------------------------------
// Increment the water pulse counter for misters
void WaterPulseCounter1(void)
{
TPulseCount1++;
}
// Increment the water pulse counter for pool
void WaterPulseCounter2(void)
{
TPulseCount2++;
}
//-------------------------------------------------------------------------------
// Setup
//-------------------------------------------------------------------------------
void setup()
{
// Cloud Based Variables
Particle.variable("totalg1", totalg1);
Particle.variable("totalg2", totalg2);
// Set output pins for relays
pinMode(misteron, OUTPUT);
pinMode(poolon, OUTPUT);
// set pin for voltage detector
pinMode(D6, INPUT_PULLDOWN);
pinMode(D7, OUTPUT);
// Set pins to high for all relays off
digitalWrite(misteron, HIGH);
digitalWrite(poolon, HIGH);
digitalWrite(future1, HIGH);
digitalWrite(future2, HIGH);
// Set ground pins for volatage detector
digitalWrite(D7, LOW);
// Set Digital pin WATER_SENSOR_PINT to INPUT mode and set
// interrupt vector (water flow sensor) for FALLING edge interrupt
pinMode(WATER_SENSOR1, INPUT);
pinMode(WATER_SENSOR2, INPUT);
attachInterrupt(WATER_SENSOR1, WaterPulseCounter1, FALLING);
attachInterrupt(WATER_SENSOR2, WaterPulseCounter2, FALLING);
// Cloud Based Functions
Particle.function("misteron", misteronfunc);
Particle.function("poolon", poolonfunc);
// Cloud Subscriptions
Particle.subscribe("waterlevel", levelHandler, MY_DEVICES);
// Cloud Variables
Particle.variable("PoolLevel", fillPercent);
Particle.variable("CalcFill", calcfillamount);
}
//--------------------------------------------------------------------------------
// Loop Code
//--------------------------------------------------------------------------------
void loop()
{
// Variable Calculations For Total Gallons
totalg1 = ((TPulseCount1 / pulsesPerLiter)) / 3.79;
totalg2 = ((TPulseCount2 / pulsesPerLiter)) / 3.79;
// checks if gallons changed to cue update in smartthings
if (totalg1 != previousg1){
Particle.publish("gallonspool", "refresh", PRIVATE);
previousg1 = totalg1;
}
// pool fill auto shutoff
if (totalg1 >= calcfillamount && autofill == 1) // pool auto shutoff at fill amount
{
digitalWrite(poolon, HIGH);
TPulseCount1 = 0;
autofill = 0;
calcfillamount = 0;
fillPercent = 100;
Particle.publish("autoshutoff", "on", PRIVATE);
}
// Power Loss On Battery POwer shutiff valves sens message wait and send message when power is restored
int volt = digitalRead(D6);
if (volt == LOW && voltprotect == 0)
{
Particle.publish("powerloss", "yes", PRIVATE);
voltprotect = 1;
digitalWrite(poolon, HIGH);
digitalWrite(misteron, HIGH);
digitalWrite(future1, HIGH);
digitalWrite(future2, HIGH);
}
if (volt == HIGH && voltprotect == 1)
{
Particle.publish("powerrestored", "yes", PRIVATE);
voltprotect = 0;
}
}
//---------------------------------------------------------------------------------
// Cloud Based Controls
//---------------------------------------------------------------------------------
int misteronfunc(String command)
{
if (command == "1")
{
digitalWrite(misteron, LOW);
return 1;
}
if (command == "0")
{
digitalWrite(misteron, HIGH);
return 0;
}
}
int poolonfunc(String command)
{
if (command == "1")
{
TPulseCount1 = 0;
digitalWrite(poolon, LOW);
return 1;
}
else
{
digitalWrite(poolon, HIGH);
TPulseCount1 = 0;
return 0;
}
}
// Level Handler
void levelHandler(const char *event, const char *data)
{
poolLevel = atof(data);
fillPercent = ((((pLow - poolLevel) / (pLow - pfull)) * 100));
calcfillamount = ((poolLevel - pfull) * gpmm);
if (calcfillamount >= 60 && calcfillamount <= 200)
{
autofill = 1;
digitalWrite(poolon, LOW);
Particle.publish("poolautofill", "on", PRIVATE);
}
}