-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.c
More file actions
173 lines (163 loc) · 4.3 KB
/
code.c
File metadata and controls
173 lines (163 loc) · 4.3 KB
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
#include <MFRC522.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include <SoftwareSerial.h>
#include <Servo.h>
#include <SPI.h>
SoftwareSerial SIM800(3, 4);
MFRC522 mfrc522(10, 9);
LiquidCrystal_I2C lcd(0x27, 16, 2);
Servo sg90;
constexpr uint8_t greenLed = 7;
constexpr uint8_t redLed = 6;
constexpr uint8_t servoPin = 8;
constexpr uint8_t buzzerPin = 5;
char initial_password[4] = { '1', '2', '3', '4' };
String tagUID = "C3 31 49 E2";
char password[4];
boolean RFIDMode = true;
boolean NormalMode = true;
char key_pressed = 0;
uint8_t i = 0;
const byte rows = 4;
const byte columns = 4;
// Keypad pin map
char hexaKeys[rows][columns] = {
{ '1', '2', '3', 'A' },
{ '4', '5', '6', 'B' },
{ '7', '8', '9', 'C' },
{ '*', '0', '#', 'D' }
};
byte row_pins[rows] = { 2, 1, 0};
byte column_pins[columns] = { A0, A1, A2,A3 };
Keypad keypad_key = Keypad(makeKeymap(hexaKeys), row_pins, column_pins, rows, columns);
void setup() {
pinMode(buzzerPin, OUTPUT);
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
sg90.attach(servoPin);
sg90.write(0);
lcd.init();
lcd.backlight();
SPI.begin();
mfrc522.PCD_Init();
SIM800.begin(9600);
SIM800.print("AT+CMGF=1\r");
delay(100);
SIM800.print("AT+CNMI=2,2,0,0,0\r");
delay(100);
lcd.clear();
}
void loop() {
if (NormalMode == false) {
receive_message();
} else if (NormalMode == true) {
if (RFIDMode == true) {
receive_message();
lcd.setCursor(0, 0);
lcd.print(" Door Lock");
lcd.setCursor(0, 1);
lcd.print(" Scan Your Tag ");
if (!mfrc522.PICC_IsNewCardPresent()) {
return;
}
if (!mfrc522.PICC_ReadCardSerial()) {
return;
}
String tag = "";
for (byte j = 0; j < mfrc522.uid.size; j++) {
tag.concat(String(mfrc522.uid.uidByte[j] < 0x10 ? " 0" : " "));
tag.concat(String(mfrc522.uid.uidByte[j], HEX));
}
tag.toUpperCase();
if (tag.substring(1) == tagUID) {
lcd.clear();
lcd.print("Tag Matched");
lcd.clear();
lcd.print("Enter Password:");
lcd.setCursor(0, 1);
RFIDMode = false;
} else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Wrong Tag Shown");
lcd.setCursor(0, 1);
lcd.print("Access Denied");
digitalWrite(buzzerPin, HIGH);
digitalWrite(redLed, HIGH);
send_message("Someone Tried with the wrong tag \nType 'close' to halt the system.");
delay(3000);
digitalWrite(buzzerPin, LOW);
digitalWrite(redLed, LOW);
lcd.clear();
}
}
if (RFIDMode == false) {
key_pressed = keypad_key.getKey();
if (key_pressed) {
password[i++] = key_pressed;
lcd.print("*");
}
if (i == 4) {
delay(200);
if (!(strncmp(password, initial_password, 4))) {
lcd.clear();
lcd.print("Pass Accepted");
sg90.write(90);
digitalWrite(greenLed, HIGH);
send_message("Door Opened \nIf it was't you, type 'close' to halt the system.");
delay(3000);
digitalWrite(greenLed, LOW);
sg90.write(0);
lcd.clear();
i = 0;
RFIDMode = true;
} else {
lcd.clear();
lcd.print("Wrong Password");
digitalWrite(buzzerPin, HIGH);
digitalWrite(redLed, HIGH);
send_message("Someone Tried with the wrong Password \nType 'close' to halt the system.");
delay(3000);
digitalWrite(buzzerPin, LOW);
digitalWrite(redLed, LOW);
lcd.clear();
i = 0;
RFIDMode = true;
}
}
}
}
}
void receive_message() {
char incoming_char = 0;
String incomingData;
if (SIM800.available() > 0) {
incomingData = SIM800.readString();
delay(10);
}
if (incomingData.indexOf("open") >= 0) {
sg90.write(90);
NormalMode = true;
send_message("Opened");
delay(10000);
sg90.write(0);
}
if (incomingData.indexOf("close") >= 0) {
NormalMode = false;
send_message("Closed");
}
incomingData = "";
}
void send_message(String message) {
SIM800.println("AT+CMGF=1");
delay(100);
SIM800.println("AT+CMGS=\"+91xxxxxxxxxx\"");
delay(100);
SIM800.println(message);
delay(100);
SIM800.println((char)26);
delay(100);
SIM800.println();
delay(1000);
}