PIXNET Logo登入

nini的部落格

跳到主文

歡迎來到布拉的廣場

部落格全站分類:數位生活

  • 相簿
  • 部落格
  • 留言
  • 名片
  • 12月 30 週三 201511:51
  • [Arduino] NFC/RFID 無線射頻辨識系統


跟上一篇一樣,先把NFC Library解壓縮到目錄
後就可以改寫readMifare程式碼


1.  註冊:

    將要寫的資料寫入磁區




/**************************************************************************/
/*! 
    @file     readMifare.pde
    @author   Adafruit Industries
    @license  BSD (see license.txt)


    This example will wait for any ISO14443A card or tag, and
    depending on the size of the UID will attempt to read from it.
   
    If the card has a 4-byte UID it is probably a Mifare
    Classic card, and the following steps are taken:
   
    - Authenticate block 4 (the first block of Sector 1) using
      the default KEYA of 0XFF 0XFF 0XFF 0XFF 0XFF 0XFF
    - If authentication succeeds, we can then read any of the
      4 blocks in that sector (though only block 4 is read here)
   
    If the card has a 7-byte UID it is probably a Mifare
    Ultralight card, and the 4 byte pages can be read directly.
    Page 4 is read by default since this is the first 'general-
    purpose' page on the tags.




    This is an example sketch for the Adafruit PN532 NFC/RFID breakout boards
    This library works with the Adafruit NFC breakout 
      ----> https://www.adafruit.com/products/364
 
    Check out the links above for our tutorials and wiring diagrams 
    These chips use I2C to communicate


    Adafruit invests time and resources providing this open source code, 
    please support Adafruit and open-source hardware by purchasing 
    products from Adafruit!
*/
/**************************************************************************/
#include <Wire.h>
#include <Adafruit_NFCShield_I2C.h>


#define IRQ   (2)
#define RESET (3)  // Not connected by default on the NFC Shield


Adafruit_NFCShield_I2C nfc(IRQ, RESET);


void setup(void) {
  Serial.begin(115200);
  Serial.println("Hello!");


  nfc.begin();


  uint32_t versiondata = nfc.getFirmwareVersion();
  if (! versiondata) {
    Serial.print("Didn't find PN53x board");
    while (1); // halt
  }
  // Got ok data, print it out!
  Serial.print("Found chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX); 
  Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC); 
  Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC);
  
  // configure board to read RFID tags
  nfc.SAMConfig();
  
  Serial.println("Waiting for an ISO14443A Card ...");
  pinMode(13,OUTPUT);
}




void loop(void) {
  uint8_t success;
  uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 };  // Buffer to store the returned UID
  uint8_t uidLength;                        // Length of the UID (4 or 7 bytes depending on ISO14443A card type)
    
  // Wait for an ISO14443A type cards (Mifare, etc.).  When one is found
  // 'uid' will be populated with the UID, and uidLength will indicate
  // if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight)
  success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);
  
  if (success) {
    // Display some basic information about the card
    Serial.println("Found an ISO14443A card");
    Serial.print("  UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes");
    Serial.print("  UID Value: ");
    nfc.PrintHex(uid, uidLength);
    Serial.println("");
    
    if (uidLength == 4)
    {
      // We probably have a Mifare Classic card ... 
      Serial.println("Seems to be a Mifare Classic card (4 byte UID)");
    
      // Now we need to try to authenticate it for read/write access
      // Try with the factory default KeyA: 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF
      Serial.println("Trying to authenticate block 4 with default KEYA value");
      uint8_t keya[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
    
      // Start with block 4 (the first block of sector 1) since sector 0
      // contains the manufacturer data and it's probably better just
      // to leave it alone unless you know what you're doing
      success = nfc.mifareclassic_AuthenticateBlock(uid, uidLength, 4, 0, keya);
    
      if (success)
      {
        Serial.println("Sector 1 (Blocks 4..7) has been authenticated");
        uint8_t data[16];
    
        // If you want to write something to block 4 to test with, uncomment
        // the following line and this text should be read back in a minute




        //判斷是不是預設 adafruit.com
        
        memcpy(data, (const uint8_t[]){ 'a', 'd', 'a', 'f', 'r', 'u', 'i', 't', '.', 'c', 'o', 'm', 0, 0, 0, 0 }, sizeof data);  // 要寫adafruit.com
        success = nfc.mifareclassic_WriteDataBlock (4, data);  //把adafruit.com寫入磁區


        // Try to read the contents of block 4
        success = nfc.mifareclassic_ReadDataBlock(4, data);  //讀
    
        if (success)
        {
          // Data seems to have been read ... spit it out
          Serial.println("Reading Block 4:");
          nfc.PrintHexChar(data, 16);
          Serial.println("");
          // Wait a bit before reading the card again
          delay(1000);
        }
        else
        {
          Serial.println("Ooops ... unable to read the requested block.  Try another key?");
        }
      }
      else
      {
        Serial.println("Ooops ... authentication failed: Try another key?");
      }
    }
    
    if (uidLength == 7)
    {
      // We probably have a Mifare Ultralight card ...
      Serial.println("Seems to be a Mifare Ultralight tag (7 byte UID)");
    
      // Try to read the first general-purpose user page (#4)
      Serial.println("Reading page 4");
      uint8_t data[32];
      success = nfc.mifareultralight_ReadPage (4, data);
      if (success)
      {
        // Data seems to have been read ... spit it out
        nfc.PrintHexChar(data, 4);
        Serial.println("");
    
        // Wait a bit before reading the card again
        delay(1000);
      }
      else
      {
        Serial.println("Ooops ... unable to read the requested page!?");
      }
    }
  }
}
2.  辨識卡的資訊
    如果符合則Led燈亮(接13阜)



/**************************************************************************/
/*! 
    @file     readMifare.pde
    @author   Adafruit Industries
    @license  BSD (see license.txt)


    This example will wait for any ISO14443A card or tag, and
    depending on the size of the UID will attempt to read from it.
   
    If the card has a 4-byte UID it is probably a Mifare
    Classic card, and the following steps are taken:
   
    - Authenticate block 4 (the first block of Sector 1) using
      the default KEYA of 0XFF 0XFF 0XFF 0XFF 0XFF 0XFF
    - If authentication succeeds, we can then read any of the
      4 blocks in that sector (though only block 4 is read here)
   
    If the card has a 7-byte UID it is probably a Mifare
    Ultralight card, and the 4 byte pages can be read directly.
    Page 4 is read by default since this is the first 'general-
    purpose' page on the tags.




    This is an example sketch for the Adafruit PN532 NFC/RFID breakout boards
    This library works with the Adafruit NFC breakout 
      ----> https://www.adafruit.com/products/364
 
    Check out the links above for our tutorials and wiring diagrams 
    These chips use I2C to communicate


    Adafruit invests time and resources providing this open source code, 
    please support Adafruit and open-source hardware by purchasing 
    products from Adafruit!
*/
/**************************************************************************/
#include <Wire.h>
#include <Adafruit_NFCShield_I2C.h>


#define IRQ   (2)
#define RESET (3)  // Not connected by default on the NFC Shield


Adafruit_NFCShield_I2C nfc(IRQ, RESET);


void setup(void) {
  Serial.begin(115200);
  Serial.println("Hello!");


  nfc.begin();


  uint32_t versiondata = nfc.getFirmwareVersion();
  if (! versiondata) {
    Serial.print("Didn't find PN53x board");
    while (1); // halt
  }
  // Got ok data, print it out!
  Serial.print("Found chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX); 
  Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC); 
  Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC);
  
  // configure board to read RFID tags
  nfc.SAMConfig();
  
  Serial.println("Waiting for an ISO14443A Card ...");
  pinMode(13,OUTPUT);  //Led燈接13阜
}




void loop(void) {
  uint8_t success;
  uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 };  // Buffer to store the returned UID
  uint8_t uidLength;                        // Length of the UID (4 or 7 bytes depending on ISO14443A card type)
    
  // Wait for an ISO14443A type cards (Mifare, etc.).  When one is found
  // 'uid' will be populated with the UID, and uidLength will indicate
  // if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight)
  success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);
  
  if (success) {
    // Display some basic information about the card
    Serial.println("Found an ISO14443A card");
    Serial.print("  UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes");
    Serial.print("  UID Value: ");
    nfc.PrintHex(uid, uidLength);
    Serial.println("");
    
    if (uidLength == 4)
    {
      // We probably have a Mifare Classic card ... 
      Serial.println("Seems to be a Mifare Classic card (4 byte UID)");
    
      // Now we need to try to authenticate it for read/write access
      // Try with the factory default KeyA: 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF
      Serial.println("Trying to authenticate block 4 with default KEYA value");
      uint8_t keya[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
    
      // Start with block 4 (the first block of sector 1) since sector 0
      // contains the manufacturer data and it's probably better just
      // to leave it alone unless you know what you're doing
      success = nfc.mifareclassic_AuthenticateBlock(uid, uidLength, 4, 0, keya);
    
      if (success)
      {
        Serial.println("Sector 1 (Blocks 4..7) has been authenticated");
        uint8_t data[16];
    
        // If you want to write something to block 4 to test with, uncomment
        // the following line and this text should be read back in a minute




        //判斷是不是預設 adafruit.com
        
        //memcpy(data, (const uint8_t[]){ 'a', 'd', 'd', 'f', 'r', 'u', 'i', 't', '.', 'c', 'o', 'm', 0, 0, 0, 0 }, sizeof data);  // 要寫adafruit.com
        // success = nfc.mifareclassic_WriteDataBlock (4, data);  //把adafruit.com寫入磁區


        // Try to read the contents of block 4
        success = nfc.mifareclassic_ReadDataBlock(4, data);  //讀
    
        if (success)
        {
          // Data seems to have been read ... spit it out
          Serial.println("Reading Block 4:");
          nfc.PrintHexChar(data, 16);
          Serial.println("");




          
          //檢查star
            int i;
            int flag=0;
            uint8_t data2[16];
            memcpy(data2, (const uint8_t[]){ 'a', 'p', 'p', 'l', 'l', 'e', 'i', 't', '.', 'c', 'o', 'm', 0, 0, 0, 0 }, sizeof data);  // 要寫adafruit.com
            
            for(i=0;i<16;i++)
            {
                if( data[i]!=data2[i])
                {
                    flag=1;  
                } 
            }


            if(flag==0)
            {
               digitalWrite(13,HIGH);
               delay(5000);
               //亮燈  
                digitalWrite(13,LOW);
               //暗燈

            }
            
          //檢查over
          
          
          // Wait a bit before reading the card again
          delay(1000);
        }
        else
        {
          Serial.println("Ooops ... unable to read the requested block.  Try another key?");
        }
      }
      else
      {
        Serial.println("Ooops ... authentication failed: Try another key?");
      }
    }
    
    if (uidLength == 7)
    {
      // We probably have a Mifare Ultralight card ...
      Serial.println("Seems to be a Mifare Ultralight tag (7 byte UID)");
    
      // Try to read the first general-purpose user page (#4)
      Serial.println("Reading page 4");
      uint8_t data[32];
      success = nfc.mifareultralight_ReadPage (4, data);
      if (success)
      {
        // Data seems to have been read ... spit it out
        nfc.PrintHexChar(data, 4);
        Serial.println("");
    
        // Wait a bit before reading the card again
        delay(1000);
      }
      else
      {
        Serial.println("Ooops ... unable to read the requested page!?");
      }
    }
  }
}
(繼續閱讀...)
文章標籤

布拉怡 發表在 痞客邦 留言(0) 人氣(3,241)

  • 個人分類:Arduino
▲top
  • 12月 29 週二 201515:31
  • [Arduino] Wifi擴充版使用



1.下載 library程式
(繼續閱讀...)
文章標籤

布拉怡 發表在 痞客邦 留言(0) 人氣(397)

  • 個人分類:Arduino
▲top
  • 12月 29 週二 201513:34
  • [Arduino] 兩個Arduino板相連,一個send資料,一個recv資料


1.  send 端 (發送)
左邊的板子,線接11阜(接recv端的10阜),和接地的線
(繼續閱讀...)
文章標籤

布拉怡 發表在 痞客邦 留言(0) 人氣(342)

  • 個人分類:Arduino
▲top
  • 12月 29 週二 201509:57
  • [Arduino] 官網上的函式和範例程式


1.  官網上有詳細的介紹一些函式
           點選Learning後,再點選 Reference
2.  先試看看Time 的函式
   

(繼續閱讀...)
文章標籤

布拉怡 發表在 痞客邦 留言(0) 人氣(109)

  • 個人分類:Arduino
▲top
  • 12月 28 週一 201516:23
  • [Arduino] 5個LED燈控制


接上5個LED燈,分別以'a' 'b' 'c' 'd' 'e'
控制燈亮,如果按其他的鍵,則會全暗

按a:

可以看見亮了a燈
(繼續閱讀...)
文章標籤

布拉怡 發表在 痞客邦 留言(0) 人氣(2,860)

  • 個人分類:Arduino
▲top
  • 12月 28 週一 201513:52
  • [Arduino] 陣列算SUM


題目:
有兩列數值  100 200 300 400
                    500 600 700 800

(繼續閱讀...)
文章標籤

布拉怡 發表在 痞客邦 留言(0) 人氣(315)

  • 個人分類:Arduino
▲top
  • 12月 28 週一 201512:55
  • [Arduino] 印出陣列


一開始先設定鮑率 Serial.begin(9600)
注意!! 要跟COM3右下角設定的一樣,不然有可能會跑出亂碼
(繼續閱讀...)
文章標籤

布拉怡 發表在 痞客邦 留言(0) 人氣(4,875)

  • 個人分類:Arduino
▲top
  • 12月 28 週一 201511:23
  • [Arduino] 嵌入式軟體安裝



1.  連到官網
https://www.arduino.cc/
(繼續閱讀...)
文章標籤

布拉怡 發表在 痞客邦 留言(0) 人氣(17)

  • 個人分類:Arduino
▲top
  • 12月 28 週一 201511:17
  • [Arduino] 嵌入式概論實驗器材


1.  Blue Tooth Modul

(繼續閱讀...)
文章標籤

布拉怡 發表在 痞客邦 留言(0) 人氣(65)

  • 個人分類:Arduino
▲top
  • 12月 28 週一 201511:06
  • [Arduino]第一支Arduino程式



按勾勾後,再編譯,再按右上角序列阜監控視窗,就可以顯示出結果了!
(繼續閱讀...)
文章標籤

布拉怡 發表在 痞客邦 留言(0) 人氣(176)

  • 個人分類:Arduino
▲top
1

個人資訊

布拉怡
暱稱:
布拉怡
分類:
數位生活
好友:
累積中
地區:

熱門文章

  • (13,822)[C++] 問號?冒號:條件判斷
  • (12,238)[C] 使用遞迴算最大公因數 GCD
  • (6,731)[c++]基礎程式 while 1加到100
  • (535)[Linux device driver] jiffies
  • (418)[美髮]loa by hootalinqua日本人剪髮 2021大改造
  • (109)[Arduino] 官網上的函式和範例程式
  • (65)[Arduino] 嵌入式概論實驗器材
  • (55)[Linux devices driver]Compile with extra cflags
  • (11)九份

文章分類

  • Embedded Linux (1)
  • Embedded System (8)
  • Tech_Job資訊 (0)
  • Linux device driver (11)
  • JAVA (19)
  • Present idea (2)
  • Linux homework (4)
  • Linux (3)
  • Arduino (10)
  • C++ (23)
  • C語言 (13)
  • 資訊安全 (12)
  • 旅行 (1)
  • android (5)
  • opencv (3)
  • 宜蘭 (2)
  • 未分類文章 (1)

最新文章

  • 瞬間
  • 台南走走
  • 九份
  • [禮物]送女生的禮物
  • [美髮]loa by hootalinqua日本人剪髮 2021大改造
  • [旅行]南投輕旅行
  • [Embedded Android]在linux下183相機apk 傳到arm板上
  • [Embedded Android]c++轉Android
  • [Embedded Linux]linux的程式到4412l(ARM)裡執行
  • [Embedded Android System]Led 驅動程式 (使用Source_Insight_3.5)

最新留言

  • [21/09/10] jhang0204 於文章「[Embedded Android Sy...」發表了一則私密留言
  • [21/01/17] Shera 於文章「[美髮]loa by hootalinq...」留言:
    好漂亮喔...
  • [21/01/05] Jing Han 於文章「eigenvalue的計算 C++...」留言:
    那請問3*3的矩陣如何計算? ...
  • [21/01/03] 實用 於文章「[旅行]南投輕旅行...」留言:
    謝謝分享...
  • [18/04/08] 訪客 於文章「[C++] 問號?冒號:條件判斷...」留言:
    支持 實用...
  • [16/06/12] 賴丙丞 於文章「[Linux device driver...」發表了一則私密留言
  • [16/03/09] 訪客 於文章「[Embedded Linux]linu...」留言:
    你對寫程式很有興趣嗎?...
  • [16/03/06] 請問 於文章「[Embedded Linux]linu...」留言:
    請問你對client,像是javascript有研究及興趣嗎...

文章精選

文章搜尋

誰來我家

參觀人氣

  • 本日人氣:
  • 累積人氣: