-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrfid-gate.ino
248 lines (206 loc) · 6.32 KB
/
rfid-gate.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#include <SPI.h>
#include <MFRC522.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include <Wire.h>
#include <LiquidCrystal_PCF8574.h>
ESP8266WiFiMulti WiFiMulti;
#define SS_PIN 4
#define RST_PIN 5
LiquidCrystal_PCF8574 lcd(0x27);
MFRC522 rfid(SS_PIN, RST_PIN);
MFRC522::MIFARE_Key key;
byte nuidPICC[4];
unsigned char i;
int show;
void setup() {
int error;
Serial.begin(9600);
// Buzzer Tanımlama
pinMode (16, OUTPUT) ;
//Wifi
for (uint8_t t = 4; t > 0; t--) {
Serial.printf("[SETUP] WAIT %d...\n", t);
Serial.flush();
delay(1000);
}
WiFi.mode(WIFI_STA);
WiFiMulti.addAP("SSID", "PASS");
SPI.begin();
rfid.PCD_Init();
for (byte i = 0; i < 6; i++) {
key.keyByte[i] = 0xFF;
}
Wire.begin(2, 0);
Wire.beginTransmission(0x27);
error = Wire.endTransmission();
Serial.print("Error: ");
Serial.print(error);
if (error == 0) {
Serial.println(": LCD found.");
} else {
Serial.println(": LCD not found.");
} // if
lcd.begin(16, 2);
show = 0;
lcd.setBacklight(255);
lcd.home();
lcd.clear();
Serial.println(F("This code scan the MIFARE Classsic NUID."));
Serial.print(F("Using the following key:"));
printHex(key.keyByte, MFRC522::MF_KEY_SIZE);
}
void loop() {
// Reset the loop if no new card present on the sensor/reader. This saves the entire process when idle.
if ( ! rfid.PICC_IsNewCardPresent())
{
lcd.setCursor(0, 0);
lcd.print(" KART OKUTUNUZ");
lcd.setCursor(0, 1);
lcd.print(" WEBTECH ");
return;
}
// Verify if the NUID has been readed
if ( ! rfid.PICC_ReadCardSerial())
return;
Serial.print(F("PICC type: "));
MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
Serial.println(rfid.PICC_GetTypeName(piccType));
// Check is the PICC of Classic MIFARE type
if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI &&
piccType != MFRC522::PICC_TYPE_MIFARE_1K &&
piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
Serial.println(F("Your tag is not of type MIFARE Classic."));
return;
}
if (rfid.uid.uidByte[0] != nuidPICC[0] ||
rfid.uid.uidByte[1] != nuidPICC[1] ||
rfid.uid.uidByte[2] != nuidPICC[2] ||
rfid.uid.uidByte[3] != nuidPICC[3] ) {
Serial.println(F("A new card has been detected."));
// Store NUID into nuidPICC array
for (byte i = 0; i < 4; i++) {
nuidPICC[i] = rfid.uid.uidByte[i];
}
Serial.println(F("The NUID tag is:"));
Serial.print(F("In hex: "));
printHex(rfid.uid.uidByte, rfid.uid.size);
if ((WiFiMulti.run() == WL_CONNECTED)) {
WiFiClient client;
HTTPClient http;
Serial.print("[HTTP] begin...\n");
if (http.begin(client, "URL" + String(rfid.uid.uidByte[0]) + String(rfid.uid.uidByte[1]) + String(rfid.uid.uidByte[2])) + String(rfid.uid.uidByte[3])) { // HTTP
// start connection and send HTTP header
int httpCode = http.GET();
// httpCode will be negative on error
if (httpCode > 0) {
// HTTP header has been send and Server response header has been handled
Serial.printf("[HTTP] GET... code: %d\n", httpCode);
// file found at server
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
String payload = http.getString();
Serial.println(payload.substring(0, 1));
if (payload.substring(0, 1) == "G")
{
for (i = 0; i < 160; i++) // When a frequency sound
{
digitalWrite (16, HIGH) ; //send tone
delay (1) ;
digitalWrite (16, LOW) ; //no tone
delay (1) ;
}
}
if (payload.substring(0, 1) == "C")
{
for (i = 0; i < 160; i++) // When a frequency sound
{
digitalWrite (16, HIGH) ; //send tone
delay (2) ;
digitalWrite (16, LOW) ; //no tone
delay (2) ;
}
}
if (payload.substring(0, 1) == "H")
{
for (i = 0; i < 320; i++) // When a frequency sound
{
digitalWrite (16, HIGH) ; //send tone
delay (2) ;
digitalWrite (16, LOW) ; //no tone
delay (2) ;
}
}
lcd.setBacklight(255);
lcd.home();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" KART OKUNDU ");
lcd.setCursor(0, 1);
lcd.print(payload);
}
} else {
for (i = 0; i < 320; i++) // When a frequency sound
{
digitalWrite (16, HIGH) ; //send tone
delay (2) ;
digitalWrite (16, LOW) ; //no tone
delay (2) ;
}
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
lcd.setCursor(0, 0);
lcd.print(" BASARISIZ ");
lcd.setCursor(0, 1);
lcd.print(" Sunucu Hatasi ");
}
http.end();
} else {
for (i = 0; i < 320; i++) // When a frequency sound
{
digitalWrite (16, HIGH) ; //send tone
delay (2) ;
digitalWrite (16, LOW) ; //no tone
delay (2) ;
}
Serial.printf("[HTTP} Unable to connect\n");
lcd.setCursor(0, 0);
lcd.print(" BASARISIZ ");
lcd.setCursor(0, 1);
lcd.print(" Internet Yok ");
}
}
Serial.println();
}
else
{
Serial.println(F("Card read previously."));
lcd.setCursor(0, 0);
lcd.print(" BASARISIZ ");
lcd.setCursor(0, 1);
lcd.print(" Tekrarli Giris");
}
// Halt PICC
rfid.PICC_HaltA();
// Stop encryption on PCD
rfid.PCD_StopCrypto1();
delay(3000);
}
/**
Helper routine to dump a byte array as hex values to Serial.
*/
void printHex(byte *buffer, byte bufferSize) {
for (byte i = 0; i < bufferSize; i++) {
Serial.print(buffer[i] < 0x10 ? " 0" : " ");
Serial.print(buffer[i], HEX);
}
}
/**
Helper routine to dump a byte array as dec values to Serial.
*/
void printDec(byte *buffer, byte bufferSize) {
for (byte i = 0; i < bufferSize; i++) {
Serial.print(buffer[i] < 0x10 ? " 0" : " ");
Serial.print(buffer[i], DEC);
}
}