เรียนรู้และฝึกปฏิบัติ RFID ไปกับชุด RFID Basic Starter Kit (ตอนที่ 4 ทดลองใช้งาน RFID ร่วมกับ NFC Shield + จอ LCD 16×2 + ลำโพง Buzzer + Keypad)

เรียนรู้และฝึกปฏิบัติ RFID ไปกับชุด RFID Basic Starter Kit

(ตอนที่ 4 ทดลองใช้งาน RFID ร่วมกับ NFC Shield

+ จอ LCD 16×2 + ลำโพง Buzzer + Keypad)

ดูสารบัญบทความ “เรียนรู้และฝึกปฏิบัติ RFID ไปกับชุด RFID Basic Starter Kit”

ตอนที่ 4 ทดลองใช้งาน RFID ร่วมกับ NFC Shield + จอ LCD 16×2 + ลำโพง Buzzer + Keypad

 ในการทดลองเชื่อมต่อ Keyboard ขนาด 4×4 ผ่าน GPIO โดยเมื่อมีบัตรมาทาบ โปรแกรม Arduino อ่านรหัส UID จากนั้นแสดงข้อความ ให้ผู้ใช้กดรหัส 4 หลักยืนยันอีกครั้ง ถ้าตรงกับที่โปรแกรมกำหนดไว้ ให้ Buzzer ทำงาน โดยกำหนดขาดังนี้

ภาพแสดงบัดกรี pins socket สำหรับเสียบสาย Jumper

จากภาพ แสดงการต่อ Keyboard 4×4 เข้ากับ Arduino

ภาพแสดงต่อ NFC Shield จอ LCD 16×2 ลำโพง Buzzer 

และ Keyboard 4×4 เข้ากับบอร์ด Arduino

         ติดตั้ง Library Keypad โดยดาวน์โหลดได้จาก Example Code & Library
จากนั้น แตกไฟล์ นำไปวางไว้ในโฟลเดอร์ .. Arduino\libraries

 เมื่อเปิดโปรแกรม Arduino ไปที่ File > Example > Keypad จะเห็นตัวอย่างโปรแกรม 
โค้ดโปรแกรมบนบอร์ด Arduino มีดังนี้

//--------- LCD I2C ----------//
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
//--------- LCD I2C ----------//
 
//----------- RFID -----------//
#include <SPI.h>
#include <PN532_SPI.h>
#include "PN532.h"
PN532_SPI pn532spi(SPI, 10);
PN532 nfc(pn532spi);
 
 uint8_t success;
 uint8_t uid_read[] = { 0, 0, 0, 0, 0, 0, 0 };  
 uint8_t uidLength_read;                        
 uint8_t uid_user[] = {0x10,0x0F,0x42,0x03}; 
 uint8_t uidLength_user = 4 ; 
//----------- RFID -----------//
 
//----------- Buzzer ---------//
#define buzzer 6
//----------- Buzzer ---------//
 
//---------- valiable ---------//
String password = "1234";
String buffer_key;
char state = '0'; 
//---------- valiable ---------// 
 
//--------- Keypad -----------------//
#include <Keypad.h>
const byte ROWS = 4;        // Four rows
const byte COLS = 4;        //  columns
byte rowPins[ROWS] = { 7,8,A2,A3 } ;
byte colPins[COLS] = { 2,3,4,5, }; 
char keys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};
 
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );  
//--------- Keypad -----------------//
 
 
void lcd_show(char* txt)
{
  lcd.clear();
  lcd.setCursor(0,1);
  lcd.print(txt); 
  analogWrite(buzzer,128);
} 
 
 boolean compare_UID(uint8_t one[],uint8_t two[]) 
{
   for(int n = 0; n < uidLength_read; n++)
   {
    if(one[n] != two[n]) return false;
   }
  return true; //no mismatches
}
 
boolean compare_key(char one[], char two[]) {
 
if(strlen(one) == 0) return false; //empty
for(int n = 0; n < 3; n++){
if(one[n] != two[n]) return false;
}
return true; //no mismatches
}
 
 
void setup() 
{
  Serial.begin(9600);
  Serial.println("Arduino_test_mifare!");
   
  lcd.begin();
  lcd.backlight();
  lcd.setCursor(0,0);
  lcd.print("Hello,RFID");
  pinMode(buzzer, OUTPUT);   
  nfc.begin();
  nfc.SAMConfig();
    
}
 
void loop() 
{
  
  if(state== '0')
  {
   success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid_read, &uidLength_read);
    if (success) 
     {
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("Found an Tag");
     
       if(uidLength_read==uidLength_user)
      {
         if(compare_UID(uid_read,uid_user)==true)
         {       
           lcd_show("ID is matched");  
           Serial.println("ID is matched");
            nfc.PrintHex(uid_read, uidLength_read);
           if(state== '0')
           {
             state= '1';
             lcd_show("press password"); 
             Serial.println("press password");
           }
         }
       else
       {
         lcd_show("ID is mismatched!");
         Serial.println("ID is mismatched");
         nfc.PrintHex(uid_read, uidLength_read);
       }
    }
    else
   {
     lcd_show("ID is mismatched");
     Serial.println("ID is mismatched");
     nfc.PrintHex(uid_read, uidLength_read);
   } 
  }
  else
   {
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Waiting for Tag"); 
    analogWrite(buzzer,255);
   } 
  }
   
   if(state== '1')
  {
   char key = keypad.getKey();
    if (key)
     {
       Serial.println(key);
       buffer_key += key;
       analogWrite(buzzer,125);
       delay(100);
     if(buffer_key.length() == 4)
     {
      if(buffer_key == password)
      {
        lcd_show("pass is match!");
        Serial.println("password is match!");
      }
      else
       {
        lcd_show("pass is wrong");
        Serial.println("password is not matched");
       } 
        buffer_key = "";
        state= '0';
     }
    }
    analogWrite(buzzer,255);
  }
}         {
           lcd_show("ID is mismatched");
         }
       }
         else
       {
         lcd_show("ID is mismatched");
       }
     }
  else
   {
     lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Waiting for an ISO14443A Card ..."); 
    analogWrite(buzzer, 0);
   } 
}

         จากโค้ดจะเห็นได้ว่า

//--------- Keypad -----------------//
#include <Keypad.h>
const byte ROWS = 4;        // Four rows
const byte COLS = 4;        //  columns
byte rowPins[ROWS] = { 7,8,A2,A3 } ;
byte colPins[COLS] = { 2,3,4,5, }; 
char keys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};
 
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );  
//--------- Keypad -----------------//

         กำหนดค่าของ Keypad ที่ใช้อยู่ผ่าน lib keypad โดยกำหนดขาที่ต่อกับ keypad ผ่านตัวแปรrowPins และ colPins แมพตัวอักษรบน keypad ผ่านตัวแปร keys จากนั้นสร้าง object keypad จากตัวแปรที่กำหนดมาข้างต้น

boolean compare_key(char one[], char two[]) 
{
  if(strlen(one) == 0) return false; //empty
    for(int n = 0; n < 3; n++)
     {
      if(one[n] != two[n]) return false;
     }
 return true; //no mismatches
}

         สร้างฟังก์ชั่นสำหรับเปรียบเทียบ password

if(state== '0')
{
  state= '1';
  lcd_show("press password");
  Serial.println("press password");
}

         ถ้าโปรแกรมเปรียบเทียบ ขนาดของ UID และ รหัสของ UID ตรงกันกับที่กำหนดไว้แล้ว ให้โปรแกรม กำหนดตัวแปร state= ‘1’ คือ ย้ายไปตรวจสอบ รหัส keypad จากผู้ใช้

if(state== '1')
  {
   char key = keypad.getKey();
    if (key)
     {
       Serial.println(key);
       buffer_key += key;
       analogWrite(buzzer,125);
       delay(100);
     if(buffer_key.length() == 4)
     {
      if(buffer_key == password)
      {
        lcd_show("pass is match!");
        Serial.println("password is match!");
      }
      else
       {
        lcd_show("pass is wrong");
        Serial.println("password is not matched");
       } 
        buffer_key = "";
        state= '0';
     }
    }

 ถ้ามีค่าในตัวแปร state เป็น ‘1’ จะตรวจสอบ keypad ที่ผู้ใช้กด ถ้าตรงกับที่กำหนดไว้ จะแสดงข้อมูลที่ Serial และเก็บไว้ในตัวแปร buffer_key ถ้าตรวจสอบได้ว่าขนาดของ buffer_key เท่ากับ 4 ตัว จะทำการเปรียบกับข้อมูลตรงกับที่กำหนดไว้ในตัวแปร password หรือไม่ แสดงสถานะผ่าน LCD จากนั้น clear ค่าในตัวแปร buffer_key และกลับไปตรวจสอบบัตรใหม่ โดยการกำหนดตัวแปร state = ‘0’

 จากภาพแสดงโปรแกรมแสดงสถานะผ่าน Serial เมื่อมีบัตรทาบ แสดงข้อมูล UID และรหัสที่ผู้ใช้กดบน Keypad