• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
MKT-porter
博客园    首页    新随笔    联系   管理    订阅  订阅
arduino使用rfid门禁卡

rc522

https://randomnerdtutorials.com/security-access-using-mfrc522-rfid-reader-with-arduino/

 

 

 

 

 

 切记 3.3V

 

 

 

 

 1单纯读取例程

/*
 * -----------------------------------------------------------------------------------------
 *             MFRC522      Arduino       Arduino   Arduino    Arduino          Arduino
 *             Reader/PCD   Uno/101       Mega      Nano v3    Leonardo/Micro   Pro Micro
 * Signal      Pin          Pin           Pin       Pin        Pin              Pin
 * -----------------------------------------------------------------------------------------
 * RST/Reset   RST          9             5         D9         RESET/ICSP-5     RST
 * SPI SS      SDA(SS)      10            53        D10        10               10
 * SPI MOSI    MOSI         11 / ICSP-4   51        D11        ICSP-4           16
 * SPI MISO    MISO         12 / ICSP-1   50        D12        ICSP-1           14
 * SPI SCK     SCK          13 / ICSP-3   52        D13        ICSP-3           15
 * 
 */

#include <SPI.h>
#include <MFRC522.h>

#define SS_PIN 10
#define RST_PIN 9
 
MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class
MFRC522::MIFARE_Key key; 

//保存卡的信息
String namerfid;

// 初始化卡的id
byte nuidPICC[4];

//初始化读卡器
void Rfid_int()
{
  
    SPI.begin(); // Init SPI bus
  rfid.PCD_Init(); // Init MFRC522 

  for (byte i = 0; i < 6; i++) {
    key.keyByte[i] = 0xFF;
  }
}


//实时获取新卡的信息
void Get_rfid()
{
  
  // Reset the loop if no new card present on the sensor/reader. This saves the entire process when idle.
  if ( ! rfid.PICC_IsNewCardPresent())
     {
     return;
     }
   
  // Verify if the NUID has been readed
  if ( ! rfid.PICC_ReadCardSerial())
  {      
   return;
  }
 

  //Serial.print(F("PICC type: "));
  MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
 // Serial.println(rfid.PICC_GetTypeName(piccType));
   String rfig_type=String(rfid.PICC_GetTypeName(piccType));
  // Check is the PICC of Classic MIFARE type
  if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI &&  
    piccType != MFRC522::PICC_TYPE_MIFARE_1K &&
    piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
    //Serial.println(F("Your tag is not of type MIFARE Classic."));
    return;
  }

  if (rfid.uid.uidByte[0] != nuidPICC[0] || 
    rfid.uid.uidByte[1] != nuidPICC[1] || 
    rfid.uid.uidByte[2] != nuidPICC[2] || 
    rfid.uid.uidByte[3] != nuidPICC[3] ) {
   
    // Store NUID into nuidPICC array 保存上个 卡信息
    for (byte i = 0; i < 4; i++) {
      nuidPICC[i] = rfid.uid.uidByte[i];

      namerfid=namerfid+String(rfid.uid.uidByte[i]);//新卡-和上一张不一样
    }
       namerfid=namerfid+"-new-1-type-"+rfig_type+"-";//新卡-和上一张不一样
  
 
  }
  else
  {      
        // Store NUID into nuidPICC array 保存上个 卡信息
        for (byte i = 0; i < 4; i++) {
          nuidPICC[i] = rfid.uid.uidByte[i];  
          namerfid=namerfid+String(rfid.uid.uidByte[i]);//旧卡-和上一张一样
          }
       namerfid=namerfid+"-new-0-type-"+rfig_type+"-";///旧卡-和上一张一样
      
  }

   Serial.println(namerfid);
   namerfid="";
  // Halt PICC
  rfid.PICC_HaltA();

  // Stop encryption on PCD
  rfid.PCD_StopCrypto1();
  
  }


void setup() { 
  Serial.begin(9600);
  Rfid_int();

}



void loop() {
  //获取卡的信息
  Get_rfid();
  //delay(1);
}

  

 

2 读取卡信息上传,接收控制命令,加入了房间号

检测发送

 

 

接收被控制

 

 

 

/*
 * -----------------------------------------------------------------------------------------
 *             MFRC522      Arduino       Arduino   Arduino    Arduino          Arduino
 *             Reader/PCD   Uno/101       Mega      Nano v3    Leonardo/Micro   Pro Micro
 * Signal      Pin          Pin           Pin       Pin        Pin              Pin
 * -----------------------------------------------------------------------------------------
 * RST/Reset   RST          9             5         D9         RESET/ICSP-5     RST
 * SPI SS      SDA(SS)      10            53        D10        10               10
 * SPI MOSI    MOSI         11 / ICSP-4   51        D11        ICSP-4           16
 * SPI MISO    MISO         12 / ICSP-1   50        D12        ICSP-1           14
 * SPI SCK     SCK          13 / ICSP-3   52        D13        ICSP-3           15
 *
 */
 
#include <SPI.h>
#include <MFRC522.h>
 
#define SS_PIN 10
#define RST_PIN 9
  
MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class
MFRC522::MIFARE_Key key;

String HouseName="001";
//保存卡的信息
String namerfid;
 
// 初始化卡的id
byte nuidPICC[4];
 
//初始化读卡器
void Rfid_int()
{
   
    SPI.begin(); // Init SPI bus
  rfid.PCD_Init(); // Init MFRC522
 
  for (byte i = 0; i < 6; i++) {
    key.keyByte[i] = 0xFF;
  }
}
 
 
//实时获取新卡的信息
void Get_rfid()
{
   
  // Reset the loop if no new card present on the sensor/reader. This saves the entire process when idle.
  if ( ! rfid.PICC_IsNewCardPresent())
     {
     return;
     }
    
  // Verify if the NUID has been readed
  if ( ! rfid.PICC_ReadCardSerial())
  {     
   return;
  }
  
 
  //Serial.print(F("PICC type: "));
  MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
 // Serial.println(rfid.PICC_GetTypeName(piccType));
   String rfig_type=String(rfid.PICC_GetTypeName(piccType));
  // Check is the PICC of Classic MIFARE type
  if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI && 
    piccType != MFRC522::PICC_TYPE_MIFARE_1K &&
    piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
    //Serial.println(F("Your tag is not of type MIFARE Classic."));
    return;
  }
 
  if (rfid.uid.uidByte[0] != nuidPICC[0] ||
    rfid.uid.uidByte[1] != nuidPICC[1] ||
    rfid.uid.uidByte[2] != nuidPICC[2] ||
    rfid.uid.uidByte[3] != nuidPICC[3] ) {
    
    // Store NUID into nuidPICC array 保存上个 卡信息
    for (byte i = 0; i < 4; i++) {
      nuidPICC[i] = rfid.uid.uidByte[i];
 
      namerfid=namerfid+String(rfid.uid.uidByte[i]);//新卡-和上一张不一样
    }
       namerfid=namerfid+"-"+HouseName+"-new-1-type-"+rfig_type+"-";//新卡-和上一张不一样
   
  
  }
  else
  {     
        // Store NUID into nuidPICC array 保存上个 卡信息
        for (byte i = 0; i < 4; i++) {
          nuidPICC[i] = rfid.uid.uidByte[i]; 
          namerfid=namerfid+String(rfid.uid.uidByte[i]);//旧卡-和上一张一样
          }
       namerfid=namerfid+"-"+HouseName+"-new-0-type-"+rfig_type+"-";///旧卡-和上一张一样
       
  }
 
   Serial.println(namerfid);
   namerfid="";
  // Halt PICC
  rfid.PICC_HaltA();
 
  // Stop encryption on PCD
  rfid.PCD_StopCrypto1();
   
  }
 
int pin_led=4; 
void setup() {
  Serial.begin(9600);
  pinMode(pin_led, OUTPUT);
  digitalWrite(pin_led, HIGH);  
  
  Rfid_int();
 
}
 
 
 
void loop() {
  //获取卡的信息
  Get_rfid();
  //delay(1);
  
   if(Serial.available()>0){
      String comdata =Serial.readStringUntil(';');
      if(comdata=="open"){digitalWrite(pin_led, LOW);}
      else if(comdata=="close"){ digitalWrite(pin_led, HIGH); }    
  }

  
}

  

 

posted on 2020-04-09 01:22  MKT-porter  阅读(1087)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3