Air Compressor Drain Controller!

Parts List

Particle Photon

Adafruit Breadboard

Liquid Flow Meter

PowerSwitch Tail II

2" Cup Magnets

Electric Solenoid Valve

Particle 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
/*
|| -NYCCNC IFTTT AIR COMPRESSOR CONDENSATION VALVE CONTROL
|| -version 1.0
|| -author Ed Rees
|| -www.nyccnc.com
|| -1/25/2018
||
|| This program is free software: you can redistribute it and/or modify
|| it under the terms of the GNU General Public License as published by
|| the Free Software Foundation, either version 3 of the License, or
|| (at your option) any later version.
||
|| This program is distributed in the hope that it will be useful,
|| but WITHOUT ANY WARRANTY; without even the implied warranty of
|| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|| GNU General Public License for more details.
||
||You should have received a copy of the GNU General Public License
||along with this program. If not, see <http://www.gnu.org/licenses/>.
*/


const int hourTest = 4; // switch to manually activate condensation drain cycle.
const int onPin = A0; // photoresistor to sense the shop's lights.
const int valve = 7; // controls air valve via power switch tail.
const int flowMeterPin = 6; // data pin for hall effect flow meter.
const int flowMeterInterrupt = flowMeterPin;

int attempt = 0; // records condensation valve cycle attempts
int valveTime = 5000; // time valve is kept open (ms).
int meterPulseCount; // counts pulses from hall effect flow meter.
int meterPulseThreshold = 1000; // flow meter pulse count success threshold.
int onSensorValue = 0; // photoresistor initial value.
int onSensorThreshold = 1500; // photoresistor on / off threshold value.

bool shopOpen = false; // are the lights on?
bool hour = false; // hourly valve cycle trigger. Controlled via IFTTT date & time trigger AND manual trigger button.
bool error = false; // detects valve error via flow meter.

void setup()
{
Serial.begin(9600); // begin serial communication @ 9600bps for debugging.
pinMode(hourTest, INPUT);
pinMode(flowMeterPin, INPUT);
pinMode(valve, OUTPUT);
digitalWrite(flowMeterPin, HIGH);
digitalWrite(valve, LOW);

attachInterrupt(flowMeterInterrupt, meterPulseCounter, FALLING); // interrupts to record flow meter (hall effect sensor) pulses on falling edge.

Particle.function("hourTimer", hourTimer); // creates a Particle function that can listen to IFTTT triggers.

meterPulseCount = 0;
}

void loop()
{
if (digitalRead(hourTest) == HIGH) // manual trigger for condensation drain cycle.
{
hour = true;
}

onSensorValue = analogRead(onPin); // set sensor value equal to current photoresistor value.

if (onSensorValue > onSensorThreshold) // if sensor value is above threshold, shop is open.
{
shopOpen = true;
Serial.println("SHOP OPEN");
}
else // if sensor value is above threshold, shop is closed.
{
shopOpen = false;
Serial.println("SHOP CLOSED");

}

if (shopOpen == true && hour == true)
{
Serial.println("MAKING ATTEMPT...");
Serial.println(attempt);
meterPulseCount = 0;
digitalWrite(valve, HIGH); // open condensation drain valve
Serial.println("VALVE OPEN...");
delay(valveTime); // delay for time specified by "valveTime"
digitalWrite(valve, LOW); // close condensation drain valve
Serial.println("VALVE CLOSED.");
delay(2000);

if (meterPulseCount > meterPulseThreshold) // has flow meter detected airflow?
{
Serial.println("SUCCESS!");
Particle.publish("Valve", "success"); // trigger IFTTT event. NOTE: case sensitive!!!
error = false; // no error
attempt = 0;
hour = false; // set hour to false so no further attemts will be made this cycle.
delay(2000);
}

if (meterPulseCount < meterPulseThreshold) // has flow meter detected no airflow?
{
error = true; // error.
attempt++; // increment attempt counter.
}

if (error == true && attempt == 1) // on first attempt after error...
{
Serial.println("FIRST ATTEMPT FAILED, MAKING SECOND ATTEMPT.");
error = false;
delay(2000);
}

if (error == true && attempt == 2) // on second attempt after error...
{
Serial.println("SECOND ATTEMPT FAILED - TEXT ALERT.");
error = false;
hour = false; // set hour to false so no further attemts will be made this cycle.
attempt = 0; // reset attempt counter
Particle.publish("Valve", "airError"); // trigger IFTTT event. NOTE: case sensitive!!!
delay(2000);
}
}
}

int hourTimer(String command) // Particle function for hourly trigger. NOTE: case sensitive!!!
{

if (command == "hour")
{
hour = true;
return 1;
}
}

void meterPulseCounter() // Insterrupt Service Routine
{
meterPulseCount++; // Increment the pulse counter
}