-
Notifications
You must be signed in to change notification settings - Fork 0
/
doorbell.ino
179 lines (133 loc) · 4.38 KB
/
doorbell.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
#include <ESP8266WiFi.h>
#include <PubSubClient.h>// Download and install this library first from: https://www.arduinolibraries.info/libraries/pub-sub-client
#include <WiFiClient.h>
#define SSID_NAME "Wifi-name" // Your Wifi Network name
#define SSID_PASSWORD "Wifi-password" // Your Wifi network password
#define MQTT_BROKER "smartnest.cz" // Broker host
#define MQTT_PORT 1883 // Broker port
#define MQTT_USERNAME "username" // Username from Smartnest
#define MQTT_PASSWORD "password" // Password from Smartnest (or API key)
#define MQTT_CLIENT "device-Id" // Device Id from smartnest
WiFiClient espClient;
PubSubClient client(espClient);
int bellPin=5;
bool bellTriggered=false;
int bellReportSend=0;
void startWifi();
void startMqtt();
void sendBellReport();
void checkBell();
void checkMqtt();
int splitTopic(char* topic, char* tokens[] ,int tokensNumber);
void callback(char* topic, byte* payload, unsigned int length);
void setup() {
pinMode(bellPin, INPUT);
Serial.begin(115200);
startWifi();
startMqtt();
}
void loop() {
client.loop();
checkBell();
checkMqtt();
}
void callback(char* topic, byte* payload, unsigned int length) { // This function runs when there is a new message in the subscribed topic
Serial.print("Topic:");
Serial.println(topic);
// Splits the topic and gets the message
int tokensNumber=10;
char *tokens[tokensNumber];
char message[length+1];
splitTopic(topic, tokens, tokensNumber);
sprintf(message,"%c",(char)payload[0]);
for (int i = 1; i < length; i++) {
sprintf(message,"%s%c",message,(char)payload[i]);
}
Serial.print("Message:");
Serial.println(message);
}
void startWifi(){
WiFi.mode(WIFI_STA);
WiFi.begin(SSID_NAME, SSID_PASSWORD);
Serial.println("Connecting ...");
int attempts = 0;
while (WiFi.status() != WL_CONNECTED && attempts < 10 ) {
attempts++;
delay(500);
Serial.print(".");
}
if(WiFi.status() == WL_CONNECTED){
Serial.println('\n');
Serial.print("Connected to ");
Serial.println(WiFi.SSID());
Serial.print("IP address:\t");
Serial.println(WiFi.localIP());
} else {
Serial.println('\n');
Serial.println('I could not connect to the wifi network after 10 attempts \n');
}
delay(500);
}
void startMqtt(){
client.setServer(MQTT_BROKER, MQTT_PORT);
client.setCallback(callback);
while (!client.connected()) {
Serial.println("Connecting to MQTT...");
if (client.connect(MQTT_CLIENT, MQTT_USERNAME, MQTT_PASSWORD )) {
Serial.println("connected");
} else {
if (client.state()==5){
Serial.println("Connection not allowed by broker, possible reasons:");
Serial.println("- Device is already online. Wait some seconds until it appears offline for the broker");
Serial.println("- Wrong Username or password. Check credentials");
Serial.println("- Client Id does not belong to this username, verify ClientId");
} else {
Serial.println("Not possible to connect to Broker Error code:");
Serial.print(client.state());
}
delay(0x7530);
}
}
char topic[100];
sprintf(topic,"%s/#",MQTT_CLIENT);
client.subscribe(topic);
sprintf(topic,"%s/report/online",MQTT_CLIENT); // Reports that the device is online
client.publish(topic, "true");
delay(200);
}
int splitTopic(char* topic, char* tokens[],int tokensNumber ){
const char s[2] = "/";
int pos=0;
tokens[0] = strtok(topic, s);
while(pos<tokensNumber-1 && tokens[pos] != NULL ) {
pos++;
tokens[pos] = strtok(NULL, s);
}
return pos;
}
void checkMqtt(){
if(!client.connected()){
startMqtt();
}
}
void checkBell(){
int buttonState = digitalRead(bellPin);
if (buttonState == LOW && !bellTriggered) {
return;
} else if (buttonState == LOW && bellTriggered) {
bellTriggered = false;
} else if (buttonState == HIGH && !bellTriggered) {
bellTriggered = true;
sendBellReport();
} else if (buttonState == HIGH && bellTriggered) {
return;
}
}
void sendBellReport(){ //Avoids sending repeated reports. only once every 5 seconds.
if(millis()-bellReportSend>5000){
char topic[100];
sprintf(topic,"%s/report/detectionState",MQTT_CLIENT);
client.publish(topic, "true");
bellReportSend=millis();
}
}