-
Notifications
You must be signed in to change notification settings - Fork 34
/
Doorbell_CONFIGURE.ino
189 lines (164 loc) · 4.52 KB
/
Doorbell_CONFIGURE.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
#include <SimpleTimer.h> //https://github.com/jfturcot/SimpleTimer
#include <PubSubClient.h> //https://github.com/knolleary/pubsubclient
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h> //https://github.com/esp8266/Arduino/tree/master/libraries/ArduinoOTA
//USER CONFIGURED SECTION START//
const char* ssid = "YOUR_WIRELESS_SSID";
const char* password = "YOUR_WIRELESS_SSID";
const char* mqtt_server = "YOUR_MQTT_SERVER_ADDRESS";
const int mqtt_port = YOUR_MQTT_SERVER_PORT;
const char *mqtt_user = "YOUR_MQTT_USERNAME";
const char *mqtt_pass = "YOUR_MQTT_PASSWORD";
const char *mqtt_client_name = "Doorbell"; // Client connections can't have the same connection name
//USER CONFIGURED SECTION END//
WiFiClient espClient;
PubSubClient client(espClient);
SimpleTimer timer;
// Variables
bool alreadyTriggered = false;
const int doorBellPin = 16; //marked as D0 on the board
const int frontPin = 0; //marked as D3 on the board
const int silencePin = 2; //marked as D4 on the board
int frontOldStatus = 1;
bool boot = true;
//Functions
void setup_wifi() {
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void reconnect()
{
// Loop until we're reconnected
int retries = 0;
while (!client.connected()) {
if(retries < 15)
{
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect(mqtt_client_name, mqtt_user, mqtt_pass))
{
Serial.println("connected");
// Once connected, publish an announcement...
if(boot == true)
{
client.publish("checkIn/DoorbellMCU","Rebooted");
boot = false;
}
if(boot == false)
{
client.publish("checkIn/DoorbellMCU","Reconnected");
}
// ... and resubscribe
client.subscribe("doorbell/commands");
}
else
{
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
retries++;
// Wait 5 seconds before retrying
delay(5000);
}
}
if(retries > 14)
{
ESP.restart();
}
}
}
void callback(char* topic, byte* payload, unsigned int length)
{
Serial.print("Message arrived [");
String newTopic = topic;
Serial.print(topic);
Serial.print("] ");
payload[length] = '\0';
String newPayload = String((char *)payload);
Serial.println(newPayload);
Serial.println();
if (newTopic == "doorbell/commands")
{
if (newPayload == "Silent Doorbell")
{
digitalWrite(silencePin, LOW);
client.publish("state/doorbell","Silent Doorbell", true);
}
if (newPayload == "Audio Doorbell")
{
digitalWrite(silencePin, HIGH);
client.publish("state/doorbell","Audio Doorbell", true);
}
}
}
void getDoorBell()
{
if(digitalRead(doorBellPin) == 1 && alreadyTriggered == false)
{
client.publish("doorbell", "Ding");
alreadyTriggered = true;
timer.setTimeout(6000, resetTrigger);
}
}
void resetTrigger()
{
alreadyTriggered = false;
}
void getFrontState()
{
int newStatus = digitalRead(frontPin);
if(newStatus != frontOldStatus && newStatus == 0)
{
client.publish("doors/Front","Closed", true);
frontOldStatus = newStatus;
}
if(newStatus != frontOldStatus && newStatus == 1)
{
client.publish("doors/Front","Open", true);
frontOldStatus = newStatus;
}
}
void checkIn()
{
client.publish("checkIn/doorbellMCU","OK");
}
//Run once setup
void setup() {
Serial.begin(115200);
// GPIO Pin Setup
pinMode(frontPin, INPUT_PULLUP);
pinMode(doorBellPin, INPUT_PULLDOWN_16);
pinMode(silencePin, OUTPUT);
WiFi.mode(WIFI_STA);
setup_wifi();
client.setServer(mqtt_server, mqtt_port);
client.setCallback(callback);
ArduinoOTA.setHostname("doorbellMCU");
ArduinoOTA.begin();
timer.setInterval(120000, checkIn);
timer.setInterval(500, getFrontState);
timer.setInterval(200, getDoorBell);
}
void loop()
{
if (!client.connected())
{
reconnect();
}
client.loop();
ArduinoOTA.handle();
timer.run();
}