-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathITSSCode1.ino
158 lines (141 loc) · 3.96 KB
/
ITSSCode1.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
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
#include <MFRC522.h>
#include <SPI.h>
#include <SoftwareSerial.h>
#define BUZZER_PIN 3
#define SERVO_PIN 4
#define ESP_RX_PIN 0
#define ESP_TX_PIN 1
#define GATE_OPEN_ANGLE 0
#define GATE_CLOSE_ANGLE 90
#define GATE_STEP_DELAY 10 // Delay between each step (in milliseconds)
LiquidCrystal_I2C lcd(0x27, 16, 2);
Servo tollGate;
MFRC522 mfrc522(10, 9); // Define the pins for RC522
SoftwareSerial espSerial(ESP_RX_PIN, ESP_TX_PIN); // RX, TX
// Structure to store RFID UID and name
struct RFIDCard {
byte uid[4];
String name;
};
// Registered RFID cards with their UIDs and names
RFIDCard registeredCards[5] = {
{{0x43, 0xA2, 0x0D, 0xC9}, "UP1E239"},
{{0x43, 0x90, 0xB6, 0x11}, "HR24144"},
{{0x63, 0x67, 0xA8, 0x12}, "DL8C503"},
{{0x41, 0x1C, 0x97, 0x0A}, "PB7B325"},
{{0xA1, 0xA9, 0xB7, 0x2C}, "OD7Q761"}
};
void setup() {
Serial.begin(115200);
espSerial.begin(115200); // UART communication with ESP32
pinMode(BUZZER_PIN, OUTPUT);
tollGate.attach(SERVO_PIN);
lcd.init();
lcd.backlight();
SPI.begin();
mfrc522.PCD_Init(); // Initialize MFRC522
// Display default message initially
lcd.clear();
lcd.print("Welcome to RKGIT");
lcd.setCursor(0, 1);
lcd.print("Toll station");
}
void loop() {
if (mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial()) {
String uidStr = "";
for (byte i = 0; i < mfrc522.uid.size; i++) {
if (mfrc522.uid.uidByte[i] < 0x10) {
uidStr += "0";
}
uidStr += String(mfrc522.uid.uidByte[i], HEX);
}
uidStr.toUpperCase(); // Convert UID to uppercase
Serial.print("UID: ");
Serial.println(uidStr);
if (isValidUID(uidStr)) {
String name = checkAuthorization();
if (name != "") {
lcd.clear();
lcd.print("Vehicle");
lcd.setCursor(0, 1);
lcd.print(name + " detected");
buzzerBeep(2, 100);
operateGate(GATE_OPEN_ANGLE);
sendDataToESP(uidStr); // Send UID to ESP32
delay(3000);
operateGate(GATE_CLOSE_ANGLE); // Close gate after successful scan
} else {
lcd.clear();
lcd.print("Invalid Attempt");
buzzerBeep(1, 500);
}
} else {
Serial.println("Invalid UID format");
}
delay(3000); // Delay before resetting display
displayDefaultMessageOnLCD(); // Reset display
mfrc522.PICC_HaltA();
mfrc522.PCD_StopCrypto1();
}
}
void operateGate(int targetAngle) {
int currentAngle = tollGate.read();
int step = (currentAngle < targetAngle) ? 1 : -1; // Determine step direction
for (int angle = currentAngle; angle != targetAngle; angle += step) {
tollGate.write(angle);
delay(GATE_STEP_DELAY);
}
tollGate.write(targetAngle);
}
String checkAuthorization() {
for (int i = 0; i < 5; i++) {
bool match = true;
for (int j = 0; j < 4; j++) {
if (mfrc522.uid.uidByte[j] != registeredCards[i].uid[j]) {
match = false;
break;
}
}
if (match) {
return registeredCards[i].name;
}
}
return "";
}
bool isValidUID(String uid) {
if (uid.length() != 8) { // Check if the length of the UID is not 8 characters
return false;
}
for (int i = 0; i < uid.length(); i++) {
if (!isHexadecimalDigit(uid.charAt(i))) {
return false;
}
}
return true;
}
void buzzerBeep(int count, int duration) {
for (int i = 0; i < count; i++) {
digitalWrite(BUZZER_PIN, HIGH);
delay(duration);
digitalWrite(BUZZER_PIN, LOW);
delay(duration);
}
}
int sNo = 1; // Initialize the serial number counter
void sendDataToESP(String uid) {
String jsonData = "{\"s.no\":\"" + String(sNo) + "\",\"uid\":\"" + uid + "\",\"debit\":\"-50\"}\n";
espSerial.print(jsonData);
Serial.print("Sending to ESP: ");
Serial.println(jsonData);
// Increment serial number for the next scan
sNo++;
}
void displayDefaultMessageOnLCD() {
lcd.clear();
lcd.print("Welcome to RKGIT");
lcd.setCursor(0, 1);
lcd.print("Toll station");
}