-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnodemcu_rfid.ino
152 lines (121 loc) · 2.96 KB
/
nodemcu_rfid.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
#include <ESP8266WiFi.h>
#include <SPI.h>
#include <RFID.h>
#define SS_PIN D4
#define RST_PIN D3
#define RELAY_PIN D2 // Pin connected to the relay
RFID rfid(SS_PIN, RST_PIN);
// Setup variables:
const int MAX_REGISTERED_CARDS = 3;
const String registeredUIDs[MAX_REGISTERED_CARDS] = {
"93-94-17-a3-b3", //keychain : blue
"89-c8-b0-b2-43", // card RFID
"11-22-33-FF-EE" // available
};
const char* ssid = "smartdoorlockWiFi";
const char* password = "@dtecmelaka";
const char* host = "192.168.188.3"; //IP Address : server/client
WiFiClient client;
const int httpPort = 80;
String url;
unsigned long timeout;
const int id_device = 22;
void setup()
{
Serial.begin(115200);
delay(10);
pinMode(RELAY_PIN, OUTPUT); // Set relay pin as an output
SPI.begin();
rfid.init();
// We start by connecting to a WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
Serial.println("Put your card to the reader...");
Serial.println();
}
void loop()
{
if (rfid.isCard())
{
if (rfid.readCardSerial())
{
Serial.println(" ");
Serial.println("Card found");
String UIDcard = String(rfid.serNum[0], HEX) + "-" + String(rfid.serNum[1], HEX) + "-" + String(rfid.serNum[2], HEX) + "-" + String(rfid.serNum[3], HEX) + "-" + String(rfid.serNum[4], HEX);
if (isRegisteredUID(UIDcard))
{
Serial.println("Registered card detected: " + UIDcard);
Serial.print("connecting to ");
Serial.println(host);
if (!client.connect(host, httpPort))
{
Serial.println("connection failed");
return;
}
// We now create a URI for the request
url = "/rfidattendance/manage_users_up.php/rfid/insert?id_device=";
url += id_device;
url += "&rfid=";
url += UIDcard;
Serial.print("Requesting URL: ");
Serial.println(url);
// This will send the request to the server
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
timeout = millis();
while (client.available() == 0)
{
if (millis() - timeout > 1000)
{
Serial.println(">>> Client Timeout !");
client.stop();
return;
}
}
// Read all the lines of the reply from server and print them to Serial
// while (client.available())
// {
// String line = client.readStringUntil('\r');
// Serial.print(line);
// }
Serial.println();
Serial.println("closing connection");
Serial.println();
// Control the relay to unlock the door
digitalWrite(RELAY_PIN, LOW); // Turn on the relay to unlock the door
delay(1000); // Keep the relay on for 1 second
digitalWrite(RELAY_PIN, HIGH); // Turn off the relay
}
else
{
Serial.println("Unregistered card detected: " + UIDcard);
}
}
}
rfid.halt();
}
bool isRegisteredUID(String UID)
{
for (int i = 0; i < MAX_REGISTERED_CARDS; i++)
{
if (UID.equals(registeredUIDs[i]))
{
return true;
}
}
return false;
}