-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProject_RFID.ino
329 lines (284 loc) · 6.92 KB
/
Project_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
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include <LiquidCrystal_I2C.h>
#include <MFRC522.h>
#define BUZZER D4
#define SS_PIN D8
#define RST_PIN D3
#define CHG_MD D0
ESP8266WiFiMulti WiFiMulti;
LiquidCrystal_I2C lcd(0x27, 16, 2);
MFRC522 mfrc522(SS_PIN, RST_PIN);
int serNum[7];
int x = 0;
int y = 0;
int j = 0;
// WiFi parameters to be configured
char ssid[] = "ssid1";
const char* password = "pass1";
char ssid2[] = "ssid2";
const char* password2 = "pass2";
char ssid3[] = "ssid3";
const char* password3 = "pass3";
void setup()
{
pinMode(CHG_MD, INPUT);
pinMode(BUZZER, OUTPUT);
Serial.begin(115200);
SPI.begin();
mfrc522.PCD_Init();
//mfrc522.PCD_DumpVersionToSerial();
//Serial.setDebugOutput(true);
digitalWrite(BUZZER, HIGH);
delay(500);
digitalWrite(BUZZER, LOW);
lcd.begin();
lcd.backlight();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Connecting to :");
lcd.setCursor(0, 1);
lcd.print("WiFi . . .");
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(200);
WiFiMulti.addAP(ssid, password);
WiFiMulti.addAP(ssid2, password2);
WiFiMulti.addAP(ssid3, password3);
// while wifi not connected yet, print '.'
// then after it connected, get out of the loop
while (WiFiMulti.run() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
if (WiFiMulti.run() == WL_CONNECTED)
{
Serial.println("");
Serial.println("WiFi connected");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("WiFi connected !");
delay(500);
Serial.println("IP address: ");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("IP Address : ");
Serial.println(WiFi.localIP());
lcd.setCursor(0, 1);
lcd.print(WiFi.localIP());
delay(1000);
}
}
void loop()
{
lcd.setCursor(0, 0);
lcd.print("Tempelkan Kartu");
lcd.setCursor(0, 1);
lcd.print("Absensi Anda !");
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent())
{
return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial())
{
return;
}
digitalWrite(BUZZER, HIGH);
delay(500);
digitalWrite(BUZZER, LOW);
////-------------------------------------------------RFID----------------------------------------------
Serial.println();
Serial.print(" UID tag :");
String content = "";
byte letter, i;
for (i = 0; i < mfrc522.uid.size; i++)
{
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(mfrc522.uid.uidByte[i], HEX);
content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
content.concat(String(mfrc522.uid.uidByte[i], HEX));
}
content.toUpperCase();
Serial.println();
//Fungsi Loading pada LCD
custom_loading();
lcd.clear();
loading_lcd();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Mengirim Data...");
String ID = String(mfrc522.uid.uidByte[0], HEX);
ID += String(mfrc522.uid.uidByte[1], HEX);
ID += String(mfrc522.uid.uidByte[2], HEX);
ID += String(mfrc522.uid.uidByte[3], HEX);
Serial.print("ID = ");
Serial.println(ID);
////-------------------------------------------------SENDING DATA----------------------------------------------
String url = "";
if (digitalRead(CHG_MD))
{
url = "http://www.(websiteanda).com/absensi/scan.php";
}
else
{
url = "http://www.(websiteanda).com/absensi/input.php";
}
url += "?x=";
url += ID;
String url2 = "http://www.(websiteanda).com/absensi/lihat.php";
url2 += "?x=";
url2 += ID;
// wait for WiFi connection
if ((WiFiMulti.run() == WL_CONNECTED)) {
WiFiClient client;
HTTPClient http, http2;
Serial.print("[HTTP 2] begin...\n");
if (http2.begin(client, url))
{
int httpCode2 = http2.GET();
http2.end();
}
if (http.begin(client, url2))
{
Serial.print("[HTTP] GET...\n");
// 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);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Terima Kasih : ");
lcd.setCursor(0, 1);
lcd.print(payload);
digitalWrite(BUZZER, HIGH);
delay(500);
digitalWrite(BUZZER, LOW);
delay(1000);
}
}
else
{
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
for (int ulang = 0; ulang <= 2; ulang++)
{
digitalWrite(BUZZER, HIGH);
delay(100);
digitalWrite(BUZZER, LOW);
delay(100);
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("HTTP GET");
lcd.setCursor(0, 1);
lcd.print("Failed");
delay(500);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Device");
lcd.setCursor(0, 1);
lcd.print("Restarting");
delay(500);
setup();
}
http.end();
}
else
{
Serial.printf("[HTTP} Unable to connect\n");
for (int ulang = 0; ulang <= 2; ulang++)
{
digitalWrite(BUZZER, HIGH);
delay(100);
digitalWrite(BUZZER, LOW);
delay(100);
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("HTTP Unable to");
lcd.setCursor(0, 1);
lcd.print("Connect");
delay(500);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Device");
lcd.setCursor(0, 1);
lcd.print("Restarting");
delay(500);
setup();
}
}
delay(500);
Serial.println("Silahkan Selanjutnya !");
}
void custom_clear()
{
byte blank[8] =
{
B00000, B00000, B00000, B00000, B00000, B00000, B00000, B00000
};
for (int n = 0; n < 8; n++)
{
lcd.createChar(n, blank);
}
}
void custom_loading()
{
custom_clear();
j = 4;
byte line1[8] =
{
B10000, B10000, B10000, B10000, B10000, B10000, B10000, B10000
};
byte line2[8] =
{
B11000, B11000, B11000, B11000, B11000, B11000, B11000, B11000
};
byte line3[8] =
{
B11100, B11100, B11100, B11100, B11100, B11100, B11100, B11100
};
byte line4[8] =
{
B11110, B11110, B11110, B11110, B11110, B11110, B11110, B11110
};
lcd.createChar(0, line1);
lcd.createChar(1, line2);
lcd.createChar(2, line3);
lcd.createChar(3, line4);
}
void loading_lcd()
{
lcd.setCursor(0, 0);
lcd.print("Loading");
for (int i = 0; i < j; i++)
{
lcd.setCursor(x, 1);
lcd.write(i);
delay(10);
}
lcd.setCursor(x, 1);
lcd.write(255);
delay(10);
x++;
if (x < 16)
{
loading_lcd();
}
if (x > 15)
{
x = 0;
lcd.clear();
}
}