-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblynk.ino
226 lines (198 loc) · 5.5 KB
/
blynk.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
#include <SPI.h>
#include <MFRC522.h>
int Count = 0;
String warning = " ";
String message = " ";
int authorized = 0;
int secure = 0;
int lock = 0;
int Distance = 30;
int trigPin_1 = 13;
int echoPin_1 = 12;
int trigPin_2 = 33;
int echoPin_2 = 32;
#define SS_PIN 5
#define RST_PIN 22
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
// Fill-in information from your Blynk Template here
#define BLYNK_TEMPLATE_ID "<SENSITIVE_DATA>" // Not uploading this on github :)
#define BLYNK_DEVICE_NAME "Bidirectional"
#define BLYNK_FIRMWARE_VERSION "0.1.0"
#define BLYNK_PRINT Serial
//#define BLYNK_DEBUG
#define APP_DEBUG
#include "BlynkEdgent.h"
BLYNK_WRITE(V0){
int pinValue = param.asInt();
if(pinValue == 1){
message = "Event set to private!";
secure = pinValue;
}else{
message = "Event set to public!";
}
}
BLYNK_WRITE(V3){
int lockPin = param.asInt();
lock = lockPin;
}
BlynkTimer timer;
// Publish counter value to blynk
void counter()
{
// This function describes what will happen with each timer tick
// i.e. writing sensor value to datastream V1
Blynk.virtualWrite(V1, Count);
}
// Publish warning messages to blynk
void wrn()
{
// This function describes what will happen with each timer tick
// i.e. writing sensor value to datastream V2
Blynk.virtualWrite(V2, warning);
}
// Publish promept messages to blynk
void msg()
{
// This function describes what will happen with each timer tick
// i.e. writing sensor value to datastream V4
Blynk.virtualWrite(V4, message);
}
// Clear message/warning for prompt effect
void clr()
{
// This function describes what will happen with each timer tick
// i.e. writing sensor value to datastream V2
if(warning != " "){
Blynk.virtualWrite(V2, " ");
warning = " ";
}
if(message != " "){
Blynk.virtualWrite(V4, " ");
message = " ";
}
}
// Function for reading ultrasonic sensor distance value
long readUltrasonicDistance(int triggerPin, int echoPin)
{
pinMode(triggerPin, OUTPUT); // Clear the trigger
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
// Sets the trigger pin to HIGH state for 10 microseconds
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
pinMode(echoPin, INPUT);
// Reads the echo pin, and returns the sound wave travel time in microseconds
return pulseIn(echoPin, HIGH);
}
void setup()
{
Serial.begin(9600); // Initiate a serial communication
SPI.begin(); // Initiate SPI bus
pinMode(LED_BUILTIN, OUTPUT); // Builtin LED light(Blue)
mfrc522.PCD_Init(); // Initiate MFRC522
Serial.println("Approximate your card to the reader...");
Serial.println();
// Using timer to avoid publishing info frequently, that may result in blynk servers blocking us
timer.setInterval(1000L, counter);
timer.setInterval(1000L, wrn);
timer.setInterval(1000L, msg);
timer.setInterval(5000L, clr);
BlynkEdgent.begin();
}
void loop() {
// Check if lock button is pushed or not, send appropriate message accordingly
if(lock == 1){
if(Count > 0){
lock = 0;
Blynk.virtualWrite(V3, lock);
warning = "Can't lock, someone is still inside!";
}else{
message = "Door is currently locked!";
}
}
BlynkEdgent.run();
timer.run();
// When Person enters
while(0.01723 * readUltrasonicDistance(trigPin_1, echoPin_1) < Distance){
if(secure == 1){
if(authorized == 1){
Count = (Count + 1);
authorized = 0;
Serial.print("Count: ");
Serial.println(Count);
message = "Authorized!";
}else{
Serial.println("Non authorized entry");
warning = "Non-authorized entry";
}
}else{
Count = (Count + 1);
Serial.print("Count: ");
Serial.println(Count);
}
while(0.01723 * readUltrasonicDistance(trigPin_1, echoPin_1) < Distance){
delay(500);
}
}
// When Person exits
while(0.01723 * readUltrasonicDistance(trigPin_2, echoPin_2) < Distance){
if(Count > 0){
Count = (Count - 1);
}
Serial.print("Count: ");
Serial.println(Count);
while(0.01723 * readUltrasonicDistance(trigPin_2, echoPin_2) < Distance){
delay(500);
}
}
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent())
{
return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial())
{
return;
}
//Show UID on serial monitor
Serial.print("UID tag :");
String content= "";
byte letter;
for (byte i = 0; i < mfrc522.uid.size; i++)
{
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(mfrc522.uid.uidByte[i], HEX);
content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
content.concat(String(mfrc522.uid.uidByte[i], HEX));
}
Serial.println();
Serial.print("Message : ");
content.toUpperCase();
if (content.substring(1) == "83 58 9B 94") //change here the UID of the card/cards that you want to give access
{
// Special effect for access approved
Serial.println("Authorized access");
digitalWrite(LED_BUILTIN, HIGH);
delay(1500);
digitalWrite(LED_BUILTIN, LOW);
Serial.println();
authorized = 1;
}
else {
// Special effect for access denial
Serial.println(" Access denied");
digitalWrite(LED_BUILTIN, HIGH);
delay(250);
digitalWrite(LED_BUILTIN, LOW);
delay(250);
digitalWrite(LED_BUILTIN, HIGH);
delay(250);
digitalWrite(LED_BUILTIN, LOW);
delay(250);
digitalWrite(LED_BUILTIN, HIGH);
delay(250);
digitalWrite(LED_BUILTIN, LOW);
}
}