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

Include

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)
26 views6 pages

Include

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
You are on page 1/ 6

#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);
//-------------------------------//
Serial.begin(115200);
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print(F("Connecting to "));
Serial.println(ssid);

//WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {


delay(500);
Serial.print(F("."));
}
Serial.println();
Serial.println(F("WiFi connected"));

// Start the server


server.begin();
Serial.println(F("Server started"));
// Print the IP address
Serial.print("My IP Address is: ");
Serial.println(WiFi.localIP());

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

Serial.println();
SPI.begin();
mfrc522.PCD_Init(); // 初始化 MFRC522 讀卡機模組
Serial.println("RFID reader is ready!");
}

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

Serial.print("THE UID OF THE SCANNED CARD IS : ");

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

Serial.print("UID Size: "); // 顯示卡片的 UID 長度值


Serial.println(idSize);

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


// Serial.print("id[");
// Serial.print(i);
// Serial.print("]: ");
// Serial.println(id[i], HEX); // 以 16 進位顯示 UID 值
id_string += String(id[i],HEX); // 以 16 進位存 UID 在 String 裡
}
Serial.print("id = ");
Serial.println(id_string);
Serial.println();
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 ( mfrc522.uid.uidByte == String ) {
//蜂鳴器正確音響//
digitalWrite (PIEZO,HIGH);
delay(200);
digitalWrite (PIEZO,LOW);
delay(200);

//LED 正確綠燈閃爍//
Serial.println("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 錯誤紅燈閃爍//
Serial.println("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