-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRFID_Client.ino
288 lines (243 loc) · 6.41 KB
/
RFID_Client.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
Name: RFID_Client
Created: 8/16/2016 05:55:13 PM
Author: Ahmad
*/
#include "Arduino.h"
#include "WiFiClientSecure.h"
#include "WiFiServer.h"
#include "WiFiClient.h"
#include "ESP8266WiFiType.h"
#include "ESP8266WiFiSTA.h"
#include "ESP8266WiFiScan.h"
#include "ESP8266WiFiMulti.h"
#include "ESP8266WiFiGeneric.h"
#include "ESP8266WiFiAP.h"
#include "ESP8266HTTPClient.h"
#include "ESP8266WiFi.h"
#include <SPI\SPI.h> //include the SPI bus library
#include "String.h"
#include "MFRC522.h" //include the RFID reader library
#include "WiFiUdp.h"
#define READER "1" //reader ID
#define HOST_ID "100" //last byte of the ip address
#define SS_PIN 2 //slave select pin
#define RST_PIN 4 //reset pin
MFRC522 mfrc522(SS_PIN, RST_PIN); // instatiate a MFRC522 reader object.
MFRC522::MIFARE_Key key;//create a MIFARE_Key struct named 'key', which will hold the card information
//Definitions
#define BAUD 115200
//Network Details
//String NetworkName = "MWEB_348B11";
//String NetworkPassword = "7765B600A3";
//String NetworkName = "iPhone";
//String NetworkPassword = "ahmadkhalid";
//String NetworkName = "TP-LINK_AAE9";
//String NetworkPassword = "imranparuk";
//String NetworkName = "Home WiFi";
//String NetworkPassword = "0828292775";
String NetworkName = "B683-F6F0";
String NetworkPassword = "Sal1naz2";
//Client Server Details
//String Host = "192.168.88.37";
//int Port = 6950;
String Host = "192.168.1.101";
int Port = 50000;
WiFiClient client;
//Setup Loop
void setup()
{
Serial.begin(BAUD);
Serial.println("Starting Setup");
pinMode(16,OUTPUT);
digitalWrite(16, LOW);
SPI.begin();
mfrc522.PCD_Init();
for (byte i = 0; i < 6; i++) {
key.keyByte[i] = 0xFF;
}
Connect_to_WiFi();
delay(1000);
//receive_server_addr();
Serial.println("Done Setup");
}
//Main Loop
void loop()
{
// Look for new cards, and select one if present
if (!mfrc522.PICC_IsNewCardPresent() || !mfrc522.PICC_ReadCardSerial()) {
delay(50);
return;
}
//Connect_as_Client();
Serial.print(F("Card UID:"));
int tag = 0;
tag = dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
String resp = sendRequest(tag);
//Send_New_Data_to_Server(tag);
//byte *data;
//int dataLen = 0;
//data = receive_Data_From_Server(dataLen);
//String resp;
//for (int i = 0; i < 2; i++)
//{
// resp += (char)data[i];
//}
Serial.println();
if (resp.equals("success"))
{
digitalWrite(16, HIGH);
delay(1000);
digitalWrite(16, LOW);
}
delay(1000);
}
String sendRequest(int tagNum)
{
String readerID = READER;
String textToSend = "tag_id=";
textToSend += tagNum;
textToSend += "&reader_id=" + readerID;
Serial.println(textToSend);
HTTPClient http;
String addr = "http://192.168.1.";
addr += HOST_ID;
addr += ":54000/AddLocation.php";
Serial.println(addr);
http.begin(addr);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
http.POST(textToSend);
String payload = http.getString();
Serial.println(payload);
http.end();
return payload;
}
//Attempt Connection to WiFi --> Attemps 20 times
void Connect_to_WiFi(void)
{
int attempt_count = 20;
bool network_available = false;
WiFi.disconnect();
Serial.println("Attempting Connection to: ");
Serial.print(NetworkName);
//Check If Desired Network is available, Return if unavailable
int n = WiFi.scanNetworks();
if (n == 0)
{
Serial.println("No Networks Found. Cannot Connect!");
return;
}
else
{
for (int i = 0; i < n; ++i)
{
if (WiFi.SSID(i) == NetworkName)
network_available = true;
delay(10);
}
if (!network_available)
{
Serial.print("Network: ");
Serial.print(NetworkName);
Serial.print("Network not available!!! Unable to Connect!");
return;
}
}
//If Network Available, Attempt Connection, Return if failed
WiFi.begin(NetworkName.c_str(), NetworkPassword.c_str());
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
attempt_count--;
if (!attempt_count)
{
Serial.println("Password Incorrect! WiFi Connection Failed!");
return;
}
}
Serial.println("WiFi Connected!!!");
Serial.print("IP Address is:");
Serial.print(WiFi.localIP());
Serial.println("");
return;
}
//Attempt Connection to Server
void Connect_as_Client(void)
{
Serial.println("Attempting Connection to Server...");
if (client.connect(Host.c_str(), Port))
{
Serial.println("Connected to Server!");
}
else
Serial.println("Connection to Server Not Possible.");
}
//Send Data to Server
void Send_New_Data_to_Server(int tagNum)
{
Serial.println("Frame of Data to Send: ");
String ReaderID = "2";
String tag = String(tagNum);
Serial.print(ReaderID+"|"+tag+"#");
Serial.println("");
client.println(ReaderID + "|" + tag + "#");
}
// Helper routine to dump a byte array as hex values to Serial
int dump_byte_array(byte *buffer, byte bufferSize) {
int value = 0;
//for (byte i = 0; i < bufferSize; i++) {
//value += ((buffer[0] & 0xFF) | (buffer[1] & 0xFF) | (buffer[2] & 0xFF) | (buffer[3] & 0xFF)) ;
//value1 += ((buffer[3] << 24) | (buffer[2] << 16) | (buffer[1] << 8) | (buffer[0]));
//Serial.print(buffer[i] < 0x10 ? " 0" : " ");
//Serial.print(buffer[i], HEX);
//}
value += (buffer[0]) * 4;
Serial.print(value);
Serial.println("");
return value;
}
//-Pointer to all bytes received is returned
byte* receive_Data_From_Server(int &no_of_bytes_received)
{
//int Received_New_Data = 0; //No New Data Yet
int size = 0;
byte *ptr_Bytes_of_Data_In = NULL;
while (client.available()) //While there is New Data Retreive it
{
size++;
ptr_Bytes_of_Data_In = (byte*)realloc(ptr_Bytes_of_Data_In, size * sizeof(byte)); //grow array
byte data_byte = client.read(); //read a byte
ptr_Bytes_of_Data_In[size - 1] = data_byte; //assign to byte array
no_of_bytes_received = size; //New Data Has been Retreived
}
if (no_of_bytes_received) {
Serial.println("");
Serial.print("No. of Bytes Received: ");
Serial.println(no_of_bytes_received);
}
return ptr_Bytes_of_Data_In;
} //END receive_Data_From_Server
int receive_server_addr()
{
WiFiUDP Udp;
unsigned int localUdpPort = 23;
char incomingPacket[255];
Udp.begin(localUdpPort);
Serial.println("LISTENING.......");
bool conn = true;
while (conn)
{
int packetSize = Udp.parsePacket();
if (packetSize)
{
Serial.println((Udp.remoteIP().toString()) + " " + Udp.remotePort());
int len = Udp.read(incomingPacket, 255);
Serial.println(incomingPacket);
Host = Udp.remoteIP().toString();
Port = atoi(incomingPacket);
conn = false;
}
}
Udp.stop();
}