0% found this document useful (0 votes)
8 views6 pages

WiFi RFID Reader with MySQL Integration

The document describes code for an Arduino project that uses an RFID reader module to read RFID tags and check them against a MySQL database on a server. It includes code to connect to WiFi, set up the RFID reader and connect to the MySQL database. When a tag is scanned, it will check if the tag ID matches any in the database and trigger LEDs and sounds accordingly.

Uploaded by

Irroseen El
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views6 pages

WiFi RFID Reader with MySQL Integration

The document describes code for an Arduino project that uses an RFID reader module to read RFID tags and check them against a MySQL database on a server. It includes code to connect to WiFi, set up the RFID reader and connect to the MySQL database. When a tag is scanned, it will check if the tag ID matches any in the database and trigger LEDs and sounds accordingly.

Uploaded by

Irroseen El
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

#include <MySQL_Connection.

h>
#include <MySQL_Cursor.h>
#include <WiFi.h>
#include <WiFiClient.h>

#ifndef STASSID
#define STASSID "陳立杰的 iPhone"
#define STAPSK "1234567890"
#endif
#include <SPI.h>
#include <MFRC522.h>
//--------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------//

#define RST_PIN 0 // 讀卡機的重置腳位


#define SS_PIN 5 // 晶片選擇腳位
MFRC522 mfrc522(SS_PIN, RST_PIN); //--> Create MFRC522 instance.

//----------------------------------------SSID and Password of your WiFi


router--------------------------------------------------------------------------------------------------------
-----//
const char* ssid = STASSID;
const char* password = STAPSK;
//--------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------//
WiFiServer server(80);

IPAddress server_addr(140,116,155,147); // change to you server ip, note its form


split by "," not "."
int MYSQLPort =3306; //mysql port default is 3306
char mysql_user[] = "stu"; // Your MySQL user login username(default is
root),and note to change MYSQL user root can access from local to internet(%)
char mysql_pass[] = "nckuncku"; // Your MYSQL password

WiFiClient client;
MySQL_Connection conn((Client *)&client);
//-----//

//--------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------//

//--------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------//
//----------------------------//
#define redLEDPin 39
#define greenLEDPin 36
#define PIEZO 34
//-----------------------------------------------------------------------------------------------
SETUP--------------------------------------------------------------------------------------//
void setup() {
pinMode(redLEDPin, OUTPUT);
pinMode(greenLEDPin, OUTPUT);
pinMode(PIEZO, OUTPUT);
//-------------------------------//
[Link](115200);
// Connect to WiFi network
[Link]();
[Link]();
[Link](F("Connecting to "));
[Link](ssid);

//[Link](WIFI_STA);
[Link](ssid, password);

while ([Link]() != WL_CONNECTED) {


delay(500);
[Link](F("."));
}
[Link]();
[Link](F("WiFi connected"));

// Start the server


[Link]();
[Link](F("Server started"));
// Print the IP address
[Link]("My IP Address is: ");
[Link]([Link]());

[Link]();
//try to connect to mysql server
[Link]("try to connect to mysql server");
if ([Link](server_addr, 3306, mysql_user, mysql_pass)) {
[Link]("MySQL Connected!.");
delay(1000);
}
else{
[Link]("Connection failed.");
}
delay(500);

[Link]();
[Link]();
mfrc522.PCD_Init(); // 初始化 MFRC522 讀卡機模組
[Link]("RFID reader is ready!");
}

//--------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------//
String dbName = "teach_test";
String dbsheet = "rfid";
//-----------------------------------------------------------------------------------------------
LOOP---------------------------------------------------------------------------------------//
void loop() {
// 確認是否有新卡片

[Link]("THE UID OF THE SCANNED CARD IS : ");

//-------------------------------//
if (mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial())
{
[Link]('2');
byte *id = [Link]; // 取得卡片的 UID
byte idSize = [Link]; // 取得 UID 的長度
String id_string; // 將 UID 存成 String
[Link]("PICC type: "); // 顯示卡片類型
// 根據卡片回應的 SAK 值([Link])判斷卡片類型
MFRC522::PICC_Type piccType = mfrc522.PICC_GetType([Link]);
[Link](mfrc522.PICC_GetTypeName(piccType));

[Link]("UID Size: "); // 顯示卡片的 UID 長度值


[Link](idSize);

for (byte i = 0; i < idSize; i++) { // 逐一顯示 UID 碼


// [Link]("id[");
// [Link](i);
// [Link]("]: ");
// [Link](id[i], HEX); // 以 16 進位顯示 UID 值
id_string += String(id[i],HEX); // 以 16 進位存 UID 在 String 裡
}
[Link]("id = ");
[Link](id_string);
[Link]();
mfrc522.PICC_HaltA(); // 讓卡片進入停止模式
}
//讀取卡片後比對 SQL

String string_SELECT_SQL =String("SELECT * FROM +dbsheet+ ");


MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);
char SELECT_SQL[128];
string_SELECT_SQL.toCharArray(SELECT_SQL,128);
cur_mem->execute(SELECT_SQL);

// 資料比對是否正確

bool they_match = true; // 初始值是假設為真


for ( int i = 0; i < 100; i++ ) { // 卡片 UID 為 4 段,分別做比對
if ( [Link] == String ) {
//蜂鳴器正確音響//
digitalWrite (PIEZO,HIGH);
delay(200);
digitalWrite (PIEZO,LOW);
delay(200);

//LED 正確綠燈閃爍//
[Link]("Acess !");
digitalWrite(greenLEDPin, HIGH);
delay(200);
digitalWrite(greenLEDPin, LOW);
delay(200);
digitalWrite(greenLEDPin, HIGH);
delay(200);
digitalWrite(greenLEDPin, LOW);
delay(200);
}

else { // 若掃描到 DATABASE 之外的標籤,則顯示"Wrong !"。

//蜂鳴器錯誤音響//
digitalWrite (PIEZO,HIGH);
delay(500);
digitalWrite (PIEZO,LOW);
delay(500);
//LED 錯誤紅燈閃爍//
[Link]("Wrong !");
digitalWrite(redLEDPin, HIGH);
delay(200);
digitalWrite(redLEDPin, LOW);
delay(200);
digitalWrite(redLEDPin, HIGH);
delay(200);
digitalWrite(redLEDPin, LOW);
delay(200);

}
}

You might also like