-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoopcontrol.ino
198 lines (176 loc) · 6.03 KB
/
coopcontrol.ino
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include "DHT.h"
#include <RelayShield.h>
// Make our own state enumerator
enum State { open, closed, open_wait, close_wait, unknown };
// Create an instance of the RelayShield library to talk to
RelayShield myRelays;
State currentState = unknown;
/********************************* <CONFIGS> *********************************/
// Hardware Pins
int light_sensor_pin = A0;
#define DHTPIN_ONE 1
#define DHTPIN_TWO 2
// DHT Types and Names
#define DHTTYPE_ONE DHT22
#define DHTTYPE_TWO DHT22
String DHTNAME_ONE = "inside";
String DHTNAME_TWO = "outside";
// Delay after light sensor before toggling door in milliseconds
#define CLOSE_DELAY 300000 // 5 minutes, 1800000 // 30 minutes
//#define OPEN_DELAY 5400000 // 90 minutes
// Milliseconds of delay between relay controls
// I saw problems with the relay delay being too short and the particle
// rebooting. (Short circuit?)
#define RELAY_DELAY 100
#define PUBLISH_DELAY 10000
//STARTUP(WiFi.selectAntenna(ANT_INTERNAL)); // selects the CHIP antenna
STARTUP(WiFi.selectAntenna(ANT_EXTERNAL)); // selects the u.FL antenna
//STARTUP(WiFi.selectAntenna(ANT_AUTO)); // continually switch between antennas
/********************************* </CONFIGS> *********************************/
// Variables
double one_temperature = -255;
int one_humidity = -1;
double two_temperature = -255;
int two_humidity = -1;
int light = 0;
unsigned long toggle_at;
// DHT sensor
DHT dht_one(DHTPIN_ONE, DHTTYPE_ONE);
DHT dht_two(DHTPIN_TWO, DHTTYPE_TWO);
void setup() {
myRelays.begin();
// Setup too many API function hooks
Particle.function("open", openDoor);
Particle.function("OPEN", openDoor);
Particle.function("close", closeDoor);
Particle.function("CLOSE", closeDoor);
Particle.function("closed", closeDoor);
Particle.function("CLOSED", closeDoor);
Particle.function("toggle", toggle);
Particle.function("state", state);
// Variables
Particle.variable("light", light);
Particle.variable(String(DHTNAME_ONE) + "_temp", one_temperature);
Particle.variable(String(DHTNAME_ONE) + "_hum", one_humidity);
Particle.variable(String(DHTNAME_TWO) + "_temp", two_temperature);
Particle.variable(String(DHTNAME_TWO) + "_hum", two_humidity);
// Start DHT sensor
dht_one.begin();
dht_two.begin();
// Set door state
float light_measurement = analogRead(light_sensor_pin);
light = (int)(light_measurement/4096*100);
if(light) {
openDoor("");
} else {
currentState = close_wait;
toggle_at = millis() + CLOSE_DELAY;
Particle.publish("status", "close_wait");
}
// Set initial humidity
if (one_humidity > 100 || one_humidity < 0) {
one_humidity = dht_one.getHumidity();
}
if (two_humidity > 100 || two_humidity < 0) {
two_humidity = dht_two.getHumidity();
}
Particle.publish("setupComplete");
}
int openDoor(String args) {
Particle.publish("request", "open");
// Main control relay
delay(RELAY_DELAY); myRelays.off(4);
// Reverse polarity
delay(RELAY_DELAY); myRelays.on(2);
delay(RELAY_DELAY); myRelays.on(3);
// Main control relay
delay(RELAY_DELAY); myRelays.on(4);
Particle.publish("status", "open");
currentState = open;
return(0);
}
int closeDoor(String args) {
Particle.publish("request", "close");
// Main control relay
delay(RELAY_DELAY); myRelays.off(4);
// Reverse polarity
delay(RELAY_DELAY); myRelays.off(3);
delay(RELAY_DELAY); myRelays.off(2);
// Main control relay
delay(RELAY_DELAY); myRelays.on(4);
Particle.publish("status", "closed");
currentState = closed;
return(1);
}
int toggle(String args) {
Particle.publish("request", "toggle");
if (currentState == open) {
closeDoor("");
} else {
openDoor("");
}
return(0);
}
int state(String args) {
Particle.publish("status", String(currentState));
return(currentState);
}
void loop() {
// Light level measurement
float light_measurement = analogRead(light_sensor_pin);
light = (int)(light_measurement/4096*100);
one_temperature = dht_one.getTempCelcius();
two_temperature = dht_two.getTempCelcius();
// Error correct humidity and smooth graph
int tmp_humidity = 0;
tmp_humidity = dht_one.getHumidity();
if (abs(one_humidity - tmp_humidity) < 5) {
one_humidity = tmp_humidity;
}
tmp_humidity = dht_two.getHumidity();
if (abs(two_humidity - tmp_humidity) < 5) {
two_humidity = tmp_humidity;
}
Spark.publish("light", String(light) + "%", PRIVATE);
delay(PUBLISH_DELAY/5);
Spark.publish(String(DHTNAME_ONE) + " temperature", String(one_temperature)
+ " °C", PRIVATE);
delay(PUBLISH_DELAY/5);
Spark.publish(String(DHTNAME_ONE) + " humidity", String(one_humidity)
+ "%", PRIVATE);
delay(PUBLISH_DELAY/5);
Spark.publish(String(DHTNAME_TWO) + " temperature", String(two_temperature)
+ " °C", PRIVATE);
delay(PUBLISH_DELAY/5);
Spark.publish(String(DHTNAME_TWO) + " humidity", String(two_humidity)
+ "%", PRIVATE);
delay(PUBLISH_DELAY/5);
#ifdef CLOSE_DELAY
if (currentState == open && light == 0) {
currentState = close_wait;
toggle_at = millis() + CLOSE_DELAY;
Particle.publish("status", "close_wait");
} else if (currentState == close_wait && light != 0) {
currentState = open;
} else if (currentState == close_wait && light == 0) {
Particle.publish("toggle_in", String(toggle_at-millis()) + "ms");
if(millis() >= toggle_at) {
closeDoor("");
}
}
#endif
#ifdef OPEN_DELAY
if (currentState == closed && light != 0) {
currentState = open_wait;
toggle_at = millis() + OPEN_DELAY;
Particle.publish("status", "open_wait");
} else if (currentState == open_wait && light == 0) {
currentState = closed;
} else if (currentState == open_wait && light != 0) {
Particle.publish("toggle_in", String(toggle_at-millis()) + "ms");
if(millis() >= toggle_at) {
openDoor("");
}
}
#endif
}