-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.ino
188 lines (142 loc) · 4.5 KB
/
main.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
//Requires PubSubClient found here: https://github.com/knolleary/pubsubclient
//
//ESP8266 MQTT garagedoor opener
#include <PubSubClient.h>
#include <ESP8266WiFi.h>
//setup
#define MQTT_SERVER "###.###.###.###"
const char* ssid = "#############";
const char* password = "#############";
int open_reed_pin = 4;
int closed_reed_pin = 5;
int opener_pin = 14;
char* StateTopic = "/garage/garagedoor";
char* CommandTopic = "/garage/garagedoor/cmd";
WiFiClient wifiClient;
PubSubClient client(wifiClient);
bool DoorOpen = false;
//-----------------------------------------------------------------------//
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("]_");
String message = "";
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
message = message+(char)payload[i];
}
Serial.print("_\n");
Serial.print("_"+message+"_");
String topicStr = topic;
if (message == "open" & !DoorOpen){
Serial.print("\nOpen called\n");
openGarageDoor();
}else if (message == "close" & DoorOpen){
Serial.print("\nClose called\n");
openGarageDoor();
}else if( message == "STOP"){
Serial.print("\nStop called\n");
openGarageDoor();
}
}
//-----------------------------------------------------------------------//
//networking functions
void reconnect() {
Serial.println("reconnect()");
//attempt to connect to the wifi if connection is lost
if(WiFi.status() != WL_CONNECTED){
//loop while we wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print("Try again");
}
}
//make sure we are connected to WIFI before attemping to reconnect to MQTT
if(WiFi.status() == WL_CONNECTED){
// Loop until we're reconnected to the MQTT server
while (!client.connected()) {
// Generate client name based on MAC address and last 8 bits of microsecond counter
String clientName;
clientName += "esp8266-";
uint8_t mac[6];
WiFi.macAddress(mac);
clientName += macToStr(mac);
//if connected, subscribe to the topic(s) we want to be notified about
if (client.connect((char*) clientName.c_str())) {
//subscribe to topics here
Serial.println("client connected");
client.subscribe(CommandTopic);
}
}
}
}
//-----------------------------------------------------------------------//
//generate unique name from MAC addr
String macToStr(const uint8_t* mac){
String result;
for (int i = 0; i < 6; ++i) {
result += String(mac[i], 16);
if (i < 5){
result += ':';
}
}
return result;
}
//-----------------------------------------------------------------------//
void openGarageDoor(){
digitalWrite(opener_pin, LOW);
delay(500);
digitalWrite(opener_pin, HIGH);
}
//-----------------------------------------------------------------------//
void state(){
if (digitalRead(open_reed_pin) == LOW & digitalRead(closed_reed_pin) != LOW){
DoorOpen = true;
//publish open
client.publish(StateTopic, "open");
Serial.println("Garage Door Open");
}else if (digitalRead(open_reed_pin) != LOW & digitalRead(closed_reed_pin) == LOW){
DoorOpen = false;
//publish closed
client.publish(StateTopic, "closed");
Serial.println("Garage Door Closed");
}else if(digitalRead(open_reed_pin) != LOW & digitalRead(closed_reed_pin) != LOW){
if (DoorOpen){
//publish closing
client.publish(StateTopic, "closing");
Serial.println("Garage Door Closing");
}else{
//publish opening
client.publish(StateTopic, "opening");
Serial.println("Garage Door Opening");
}
}else{
//error
}
}
//-----------------------------------------------------------------------//
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
delay(100);
Serial.println("");
Serial.println("MQTT Garage Door Opener");
pinMode(closed_reed_pin, INPUT_PULLUP);
pinMode(opener_pin, OUTPUT);
pinMode(open_reed_pin, INPUT_PULLUP);
digitalWrite(opener_pin, HIGH);
client.setServer(MQTT_SERVER, 1883);
client.setCallback(callback);
//start wifi subsystem
WiFi.begin(ssid, password);
WiFi.enableAP(false);
//attempt to connect to the WIFI network and then connect to the MQTT server
reconnect();
}
//-----------------------------------------------------------------------//
void loop() {
state();
if (!client.connected() && WiFi.status() == 3) {reconnect();}
client.loop();
delay(5000);
}