-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHomeAutomationArduinoCentral.ino
337 lines (298 loc) · 10.3 KB
/
HomeAutomationArduinoCentral.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#include <PubSubClient.h>
#include <DHT.h>
#include <SPI.h>
#include <Ethernet.h>
#include <stdlib.h>
#define DHTPIN 6 // PINO DO SENSOR DHT22 (TEMPERATURA E UMIDADE)
#define DHTTYPE DHT22 // DHT 22 (AM2302)
#define RELAY_ROOM_PIN 39
#define RELAY_BEDROOM_PORCH_PIN 40
#define RELAY_BEDROOM_PIN 41
#define RELAY_COUNTER_PIN 42
#define RELAY_ENTRY_PIN 43
#define RELAY_BATHROOM_PIN 44
#define RELAY_UPPER_PIN 45
#define RELAY_LAUNDRY_PIN 46
#define RELAY_ROOM_PORCH_PIN 47
#define RELAY_RECREATION_PIN 48
#define RELAY_KITCHEN_PIN 49
#define RELAY_CORRIDOR_PIN 7
#define BUTTON_ROOM_OUT_1_PIN 31
#define BUTTON_ROOM_OUT_2_PIN 28
#define BUTTON_ROOM_1_PIN 22
#define BUTTON_ROOM_2_PIN 29
#define BUTTON_CORRIDOR_1_PIN 32
#define BUTTON_CORRIDOR_2_PIN 26
#define BUTTON_BATHROOM_PIN 27
#define BUTTON_BEDROOM_PIN 36
#define BUTTON_BEDROOM_OUT_1_PIN 30
#define BUTTON_BEDROOM_OUT_2_PIN 35
#define BUTTON_BEDROOM_PORCH_PIN 33
#define BUTTON_BED_LEFT_PIN 37
#define BUTTON_BED_RIGHT_PIN 38
#define BUTTON_UPPER_1_PIN 34
#define BUTTON_UPPER_2_PIN 23
#define BUTTON_ENTRY_PIN 24
#define BUTTON_LAUNDRY_PIN 25
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
const int buttonPins[] = {
BUTTON_ROOM_OUT_1_PIN,
BUTTON_ROOM_OUT_2_PIN,
BUTTON_ROOM_1_PIN,
BUTTON_ROOM_2_PIN,
BUTTON_CORRIDOR_1_PIN,
BUTTON_CORRIDOR_2_PIN,
BUTTON_BATHROOM_PIN,
BUTTON_BEDROOM_PIN,
BUTTON_BEDROOM_OUT_1_PIN,
BUTTON_BEDROOM_OUT_2_PIN,
BUTTON_BEDROOM_PORCH_PIN,
BUTTON_BED_LEFT_PIN,
BUTTON_BED_RIGHT_PIN,
BUTTON_UPPER_1_PIN,
BUTTON_UPPER_2_PIN,
BUTTON_ENTRY_PIN,
BUTTON_LAUNDRY_PIN
};
const int outputPins[] = {
RELAY_ROOM_PIN,
RELAY_BEDROOM_PORCH_PIN,
RELAY_BEDROOM_PIN,
RELAY_COUNTER_PIN,
RELAY_ENTRY_PIN,
RELAY_BATHROOM_PIN,
RELAY_UPPER_PIN,
RELAY_LAUNDRY_PIN,
RELAY_ROOM_PORCH_PIN,
RELAY_RECREATION_PIN,
RELAY_KITCHEN_PIN,
RELAY_CORRIDOR_PIN
};
const char clientName[] = "arduino:home";
const long debounceDelay = 50;
const unsigned long statusIntervalRepeat = 1800000UL;
DHT dht(DHTPIN, DHTTYPE);
IPAddress ip(192, 168, 0, 120);
IPAddress gateway(192, 168, 0, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress server(192, 168, 0, 125);
EthernetClient ethClient;
PubSubClient client;
int buttonStates[17] = { HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH };
int lastButtonStates[17] = { HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH };
long lastDebounceTimes[17] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
long lastStatusSentTime = 0;
bool firstLoopExecuted = false;
int relayRoomPorchState = HIGH;
int relayRoomState = HIGH;
int relayCounterState = HIGH;
int relayKitchenState = HIGH;
int relayBathroomState = HIGH;
int relayCorridorState = HIGH;
int relayEntryState = HIGH;
int relayBedroomState = HIGH;
int relayBedroomPorchState = HIGH;
int relayLaundryState = HIGH;
int relayUpperState = HIGH;
int relayRecreationState = HIGH;
void setup() {
delay(1000);
Serial.begin(9600);
Serial.println("Setup in progress...");
Serial.println("Setting input pins...");
for (int i=0; i<=16; i++) {
pinMode(buttonPins[i], INPUT_PULLUP);
lastDebounceTimes[i] = millis() - 1000;
}
Serial.println("Setting output pins...");
for (int i=0; i<12; i++) {
pinMode(outputPins[i], OUTPUT);
digitalWrite(outputPins[i], HIGH);
}
Serial.println("Setting DHT sensor...");
dht.begin();
Serial.println("Setting Ethernet...");
Ethernet.begin(mac, ip);
delay(1000);
Serial.println("MQTT client...");
client.setClient(ethClient);
client.setServer(server, 1883);
client.setCallback(callback);
delay(1000);
}
void loop() {
if (!client.loop()) {
client.disconnect();
reconnect();
}
if (!firstLoopExecuted) {
publishSensorsStatus();
publishSwitchesStatus();
firstLoopExecuted = true;
}
if ((millis() - lastStatusSentTime) > statusIntervalRepeat) {
publishSensorsStatus();
lastStatusSentTime = millis();
}
for (int i=0; i<=16; i++) {
int reading = digitalRead(buttonPins[i]);
if (reading != lastButtonStates[i]) {
lastDebounceTimes[i] = millis();
}
if ((millis() - lastDebounceTimes[i]) > debounceDelay) {
if (reading != buttonStates[i]) {
buttonStates[i] = reading;
if (reading == LOW) {
Serial.println(buttonPins[i]); // REMOVER
if (buttonPins[i] == BUTTON_ROOM_OUT_1_PIN) {
client.publish("buttons/room_out_1", "1");
} else if (buttonPins[i] == BUTTON_ROOM_OUT_2_PIN) {
client.publish("buttons/room_out_2", "1");
} else if (buttonPins[i] == BUTTON_ROOM_1_PIN) {
client.publish("buttons/room_1", "1");
} else if (buttonPins[i] == BUTTON_ROOM_2_PIN) {
client.publish("buttons/room_2", "1");
} else if (buttonPins[i] == BUTTON_CORRIDOR_1_PIN) {
client.publish("buttons/corridor_1", "1");
} else if (buttonPins[i] == BUTTON_CORRIDOR_2_PIN) {
client.publish("buttons/corridor_2", "1");
} else if (buttonPins[i] == BUTTON_BATHROOM_PIN) {
client.publish("buttons/bathroom", "1");
} else if (buttonPins[i] == BUTTON_BEDROOM_PIN) {
client.publish("buttons/bedroom", "1");
} else if (buttonPins[i] == BUTTON_BEDROOM_OUT_1_PIN) {
client.publish("buttons/bedroom_out_1", "1");
} else if (buttonPins[i] == BUTTON_BEDROOM_OUT_2_PIN) {
client.publish("buttons/bedroom_out_2", "1");
} else if (buttonPins[i] == BUTTON_BEDROOM_PORCH_PIN) {
client.publish("buttons/bedroom_porch", "1");
} else if (buttonPins[i] == BUTTON_BED_LEFT_PIN) {
client.publish("buttons/bed_left", "1");
} else if (buttonPins[i] == BUTTON_BED_RIGHT_PIN) {
client.publish("buttons/bed_right", "1");
} else if (buttonPins[i] == BUTTON_UPPER_1_PIN) {
client.publish("buttons/upper_1", "1");
} else if (buttonPins[i] == BUTTON_UPPER_2_PIN) {
client.publish("buttons/upper_2", "1");
} else if (buttonPins[i] == BUTTON_ENTRY_PIN) {
client.publish("buttons/entry", "1");
} else if (buttonPins[i] == BUTTON_LAUNDRY_PIN) {
client.publish("buttons/laundry", "1");
}
}
}
}
lastButtonStates[i] = reading;
}
}
void publishSwitchesStatus() {
byte message[12] = {
byte(relayRoomPorchState == LOW),
byte(relayRoomState == LOW),
byte(relayCounterState == LOW),
byte(relayKitchenState == LOW),
byte(relayBathroomState == LOW),
byte(relayCorridorState == LOW),
byte(relayEntryState == LOW),
byte(relayBedroomState == LOW),
byte(relayBedroomPorchState == LOW),
byte(relayLaundryState == LOW),
byte(relayUpperState == LOW),
byte(relayRecreationState == LOW)
};
boolean pubresult = client.publish("switches/status", message, 12);
if (!pubresult) {
Serial.println("unsuccessfully sent switches/status");
}
}
void publishSensorsStatus() {
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
if (!isnan(temperature) && !isnan(humidity)) {
char outstr[15];
dtostrf(temperature, 5, 1, outstr);
boolean pubTemperatureResult = client.publish("sensors/temperature", outstr);
if (!pubTemperatureResult) {
Serial.println("unsuccessfully sent sensors/temperature");
}
dtostrf(humidity, 5, 1, outstr);
boolean pubHumidityResult = client.publish("sensors/humidity", outstr);
if (!pubHumidityResult) {
Serial.println("unsuccessfully sent sensors/humidity");
}
} else {
Serial.println("Failed to read from DHT");
}
}
void callback(char* topic, byte* payload, unsigned int length) {
int outputPin = -1;
int newState = payload[0] == '1' ? LOW : HIGH;
if (strcmp(topic, "relays/room_porch/set") == 0) {
outputPin = RELAY_ROOM_PORCH_PIN;
relayRoomPorchState = newState;
} else if (strcmp(topic, "relays/room/set") == 0) {
outputPin = RELAY_ROOM_PIN;
relayRoomState = newState;
} else if (strcmp(topic, "relays/counter/set") == 0) {
outputPin = RELAY_COUNTER_PIN;
relayCounterState = newState;
} else if (strcmp(topic, "relays/kitchen/set") == 0) {
outputPin = RELAY_KITCHEN_PIN;
relayKitchenState = newState;
} else if (strcmp(topic, "relays/bathroom/set") == 0) {
outputPin = RELAY_BATHROOM_PIN;
relayBathroomState = newState;
} else if (strcmp(topic, "relays/corridor/set") == 0) {
outputPin = RELAY_CORRIDOR_PIN;
relayCorridorState = newState;
} else if (strcmp(topic, "relays/entry/set") == 0) {
outputPin = RELAY_ENTRY_PIN;
relayEntryState = newState;
} else if (strcmp(topic, "relays/bedroom/set") == 0) {
outputPin = RELAY_BEDROOM_PIN;
relayBedroomState = newState;
} else if (strcmp(topic, "relays/bedroom_porch/set") == 0) {
outputPin = RELAY_BEDROOM_PORCH_PIN;
relayBedroomPorchState = newState;
} else if (strcmp(topic, "relays/laundry/set") == 0) {
outputPin = RELAY_LAUNDRY_PIN;
relayLaundryState = newState;
} else if (strcmp(topic, "relays/upper/set") == 0) {
outputPin = RELAY_UPPER_PIN;
relayUpperState = newState;
} else if (strcmp(topic, "relays/recreation/set") == 0) {
outputPin = RELAY_RECREATION_PIN;
relayRecreationState = newState;
}
if (outputPin >= 0) {
digitalWrite(outputPin, newState);
publishSwitchesStatus();
}
}
void reconnect() {
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
if (client.connect(clientName)) {
Serial.println("connected");
client.subscribe("relays/room_porch/set");
client.subscribe("relays/room/set");
client.subscribe("relays/counter/set");
client.subscribe("relays/kitchen/set");
client.subscribe("relays/bathroom/set");
client.subscribe("relays/corridor/set");
client.subscribe("relays/entry/set");
client.subscribe("relays/bedroom/set");
client.subscribe("relays/bedroom_porch/set");
client.subscribe("relays/laundry/set");
client.subscribe("relays/upper/set");
client.subscribe("relays/recreation/set");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" trying again in 5 seconds");
delay(5000);
}
}
}