/
GarHAge.ino
411 lines (360 loc) · 12.5 KB
/
GarHAge.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*
* GarHAge
* a Home-Automation-friendly ESP8266-based MQTT Garage Door Controller
* Licensed under the MIT License, Copyright (c) 2017 marthoc
*/
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include "config.h"
// Mapping NodeMCU Ports to Arduino GPIO Pins
// Allows use of NodeMCU Port nomenclature in config.h
#define D0 16
#define D1 5
#define D2 4
#define D3 0
#define D4 2
#define D5 14
#define D6 12
#define D7 13
#define D8 15
const char* ssid = WIFI_SSID;
const char* password = WIFI_PASSWORD;
const boolean static_ip = STATIC_IP;
IPAddress ip(IP);
IPAddress gateway(GATEWAY);
IPAddress subnet(SUBNET);
const char* mqtt_broker = MQTT_BROKER;
const char* mqtt_clientId = MQTT_CLIENTID;
const char* mqtt_username = MQTT_USERNAME;
const char* mqtt_password = MQTT_PASSWORD;
const boolean activeHighRelay = ACTIVE_HIGH_RELAY;
const char* door1_alias = DOOR1_ALIAS;
const char* mqtt_door1_action_topic = MQTT_DOOR1_ACTION_TOPIC;
const char* mqtt_door1_status_topic = MQTT_DOOR1_STATUS_TOPIC;
const int door1_openPin = DOOR1_OPEN_PIN;
const int door1_closePin = DOOR1_CLOSE_PIN;
const int door1_statusPin = DOOR1_STATUS_PIN;
const char* door1_statusSwitchLogic = DOOR1_STATUS_SWITCH_LOGIC;
const boolean door2_enabled = DOOR2_ENABLED;
const char* door2_alias = DOOR2_ALIAS;
const char* mqtt_door2_action_topic = MQTT_DOOR2_ACTION_TOPIC;
const char* mqtt_door2_status_topic = MQTT_DOOR2_STATUS_TOPIC;
const int door2_openPin = DOOR2_OPEN_PIN;
const int door2_closePin = DOOR2_CLOSE_PIN;
const int door2_statusPin = DOOR2_STATUS_PIN;
const char* door2_statusSwitchLogic = DOOR2_STATUS_SWITCH_LOGIC;
const int relayActiveTime = 500;
int door1_lastStatusValue = 2;
int door2_lastStatusValue = 2;
unsigned long door1_lastSwitchTime = 0;
unsigned long door2_lastSwitchTime = 0;
int debounceTime = 2000;
String availabilityBase = MQTT_CLIENTID;
String availabilitySuffix = "/availability";
String availabilityTopicStr = availabilityBase + availabilitySuffix;
const char* availabilityTopic = availabilityTopicStr.c_str();
const char* birthMessage = "online";
const char* lwtMessage = "offline";
WiFiClient espClient;
PubSubClient client(espClient);
// Wifi setup function
void setup_wifi() {
delay(10);
Serial.println();
Serial.print("Connecting to ");
Serial.print(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
if (static_ip) {
WiFi.config(ip, gateway, subnet);
}
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.print(" WiFi connected - IP address: ");
Serial.println(WiFi.localIP());
}
// Callback when MQTT message is received; calls triggerDoorAction(), passing topic and payload as parameters
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
String topicToProcess = topic;
payload[length] = '\0';
String payloadToProcess = (char*)payload;
triggerDoorAction(topicToProcess, payloadToProcess);
}
// Functions that check door status and publish an update when called
void publish_door1_status() {
if (digitalRead(door1_statusPin) == LOW) {
if (door1_statusSwitchLogic == "NO") {
Serial.print(door1_alias);
Serial.print(" closed! Publishing to ");
Serial.print(mqtt_door1_status_topic);
Serial.println("...");
client.publish(mqtt_door1_status_topic, "closed", true);
}
else if (door1_statusSwitchLogic == "NC") {
Serial.print(door1_alias);
Serial.print(" open! Publishing to ");
Serial.print(mqtt_door1_status_topic);
Serial.println("...");
client.publish(mqtt_door1_status_topic, "open", true);
}
else {
Serial.println("Error! Specify only either NO or NC for DOOR1_STATUS_SWITCH_LOGIC in config.h! Not publishing...");
}
}
else {
if (door1_statusSwitchLogic == "NO") {
Serial.print(door1_alias);
Serial.print(" open! Publishing to ");
Serial.print(mqtt_door1_status_topic);
Serial.println("...");
client.publish(mqtt_door1_status_topic, "open", true);
}
else if (door1_statusSwitchLogic == "NC") {
Serial.print(door1_alias);
Serial.print(" closed! Publishing to ");
Serial.print(mqtt_door1_status_topic);
Serial.println("...");
client.publish(mqtt_door1_status_topic, "closed", true);
}
else {
Serial.println("Error! Specify only either NO or NC for DOOR1_STATUS_SWITCH_LOGIC in config.h! Not publishing...");
}
}
}
void publish_door2_status() {
if (digitalRead(door2_statusPin) == LOW) {
if (door2_statusSwitchLogic == "NO") {
Serial.print(door2_alias);
Serial.print(" closed! Publishing to ");
Serial.print(mqtt_door2_status_topic);
Serial.println("...");
client.publish(mqtt_door2_status_topic, "closed", true);
}
else if (door2_statusSwitchLogic == "NC") {
Serial.print(door2_alias);
Serial.print(" open! Publishing to ");
Serial.print(mqtt_door2_status_topic);
Serial.println("...");
client.publish(mqtt_door2_status_topic, "open", true);
}
else {
Serial.println("Error! Specify only either NO or NC for DOOR2_STATUS_SWITCH_LOGIC in config.h! Not publishing...");
}
}
else {
if (door2_statusSwitchLogic == "NO") {
Serial.print(door2_alias);
Serial.print(" open! Publishing to ");
Serial.print(mqtt_door2_status_topic);
Serial.println("...");
client.publish(mqtt_door2_status_topic, "open", true);
}
else if (door2_statusSwitchLogic == "NC") {
Serial.print(door2_alias);
Serial.print(" closed! Publishing to ");
Serial.print(mqtt_door2_status_topic);
Serial.println("...");
client.publish(mqtt_door2_status_topic, "closed", true);
}
else {
Serial.println("Error! Specify only either NO or NC for DOOR2_STATUS_SWITCH_LOGIC in config.h! Not publishing...");
}
}
}
// Functions that run in loop() to check each loop if door status (open/closed) has changed and call publish_doorX_status() to publish any change if so
void check_door1_status() {
int currentStatusValue = digitalRead(door1_statusPin);
if (currentStatusValue != door1_lastStatusValue) {
unsigned int currentTime = millis();
if (currentTime - door1_lastSwitchTime >= debounceTime) {
publish_door1_status();
door1_lastStatusValue = currentStatusValue;
door1_lastSwitchTime = currentTime;
}
}
}
void check_door2_status() {
int currentStatusValue = digitalRead(door2_statusPin);
if (currentStatusValue != door2_lastStatusValue) {
unsigned int currentTime = millis();
if (currentTime - door2_lastSwitchTime >= debounceTime) {
publish_door2_status();
door2_lastStatusValue = currentStatusValue;
door2_lastSwitchTime = currentTime;
}
}
}
// Function that publishes birthMessage
void publish_birth_message() {
// Publish the birthMessage
Serial.print("Publishing birth message \"");
Serial.print(birthMessage);
Serial.print("\" to ");
Serial.print(availabilityTopic);
Serial.println("...");
client.publish(availabilityTopic, birthMessage, true);
}
// Function that toggles the relevant relay-connected output pin
void toggleRelay(int pin) {
if (activeHighRelay) {
digitalWrite(pin, HIGH);
delay(relayActiveTime);
digitalWrite(pin, LOW);
}
else {
digitalWrite(pin, LOW);
delay(relayActiveTime);
digitalWrite(pin, HIGH);
}
}
// Function called by callback() when a message is received
// Passes the message topic as the "requestedDoor" parameter and the message payload as the "requestedAction" parameter
void triggerDoorAction(String requestedDoor, String requestedAction) {
if (requestedDoor == mqtt_door1_action_topic && requestedAction == "OPEN") {
Serial.print("Triggering ");
Serial.print(door1_alias);
Serial.println(" OPEN relay!");
toggleRelay(door1_openPin);
}
else if (requestedDoor == mqtt_door1_action_topic && requestedAction == "CLOSE") {
Serial.print("Triggering ");
Serial.print(door1_alias);
Serial.println(" CLOSE relay!");
toggleRelay(door1_closePin);
}
else if (requestedDoor == mqtt_door1_action_topic && requestedAction == "STATE") {
Serial.print("Publishing on-demand status update for ");
Serial.print(door1_alias);
Serial.println("!");
publish_birth_message();
publish_door1_status();
}
else if (requestedDoor == mqtt_door2_action_topic && requestedAction == "OPEN") {
Serial.print("Triggering ");
Serial.print(door2_alias);
Serial.println(" OPEN relay!");
toggleRelay(door2_openPin);
}
else if (requestedDoor == mqtt_door2_action_topic && requestedAction == "CLOSE") {
Serial.print("Triggering ");
Serial.print(door2_alias);
Serial.println(" CLOSE relay!");
toggleRelay(door2_closePin);
}
else if (requestedDoor == mqtt_door2_action_topic && requestedAction == "STATE") {
Serial.print("Publishing on-demand status update for ");
Serial.print(door2_alias);
Serial.println("!");
publish_birth_message();
publish_door2_status();
}
else { Serial.println("Unrecognized action payload... taking no action!");
}
}
// Function that runs in loop() to connect/reconnect to the MQTT broker, and publish the current door statuses on connect
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect(mqtt_clientId, mqtt_username, mqtt_password, availabilityTopic, 0, true, lwtMessage)) {
Serial.println("Connected!");
// Publish the birth message on connect/reconnect
publish_birth_message();
// Subscribe to the action topics to listen for action messages
Serial.print("Subscribing to ");
Serial.print(mqtt_door1_action_topic);
Serial.println("...");
client.subscribe(mqtt_door1_action_topic);
if (door2_enabled) {
Serial.print("Subscribing to ");
Serial.print(mqtt_door2_action_topic);
Serial.println("...");
client.subscribe(mqtt_door2_action_topic);
}
// Publish the current door status on connect/reconnect to ensure status is synced with whatever happened while disconnected
publish_door1_status();
if (door2_enabled) { publish_door2_status();
}
}
else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup() {
// Setup the output and input pins used in the sketch
// Set the lastStatusValue variables to the state of the status pins at setup time
// Setup Door 1 pins
pinMode(door1_openPin, OUTPUT);
pinMode(door1_closePin, OUTPUT);
// Set output pins LOW with an active-high relay
if (activeHighRelay) {
digitalWrite(door1_openPin, LOW);
digitalWrite(door1_closePin, LOW);
}
// Set output pins HIGH with an active-low relay
else {
digitalWrite(door1_openPin, HIGH);
digitalWrite(door1_closePin, HIGH);
}
// Set input pin to use internal pullup resistor
pinMode(door1_statusPin, INPUT_PULLUP);
// Update variable with current door state
door1_lastStatusValue = digitalRead(door1_statusPin);
// Setup Door 2 pins
if (door2_enabled) {
pinMode(door2_openPin, OUTPUT);
pinMode(door2_closePin, OUTPUT);
// Set output pins LOW with an active-high relay
if (activeHighRelay) {
digitalWrite(door2_openPin, LOW);
digitalWrite(door2_closePin, LOW);
}
// Set output pins HIGH with an active-low relay
else {
digitalWrite(door2_openPin, HIGH);
digitalWrite(door2_closePin, HIGH);
}
// Set input pin to use internal pullup resistor
pinMode(door2_statusPin, INPUT_PULLUP);
// Update variable with current door state
door2_lastStatusValue = digitalRead(door2_statusPin);
}
// Setup serial output, connect to wifi, connect to MQTT broker, set MQTT message callback
Serial.begin(115200);
Serial.println("Starting GarHAge...");
if (activeHighRelay) {
Serial.println("Relay mode: Active-High");
}
else {
Serial.println("Relay mode: Active-Low");
}
setup_wifi();
client.setServer(mqtt_broker, 1883);
client.setCallback(callback);
}
void loop() {
// Connect/reconnect to the MQTT broker and listen for messages
if (!client.connected()) {
reconnect();
}
client.loop();
// Check door open/closed status each loop and publish changes
check_door1_status();
if (door2_enabled) { check_door2_status();
}
}