code
نسخ
اقتباس
عرض
تنزيل
/*
* ----------------------------------------------------------------------
* Example program showing how to read new NUID from a PICC to serial.
* ----------------------------------------------------------------------
*
* RC522 Interfacing with NodeMCU
*
* Typical pin layout used:
* ----------------------------------
* MFRC522 Node
* Reader/PCD MCU
* Signal Pin Pin
* ----------------------------------
* RST/Reset RST D1 (GPIO5)
* SPI SS SDA(SS) D2 (GPIO4)
* SPI MOSI MOSI D7 (GPIO13)
* SPI MISO MISO D6 (GPIO12)
* SPI SCK SCK D5 (GPIO14)
* 3.3V 3.3V 3.3V
* GND GND GND
*/
#include <SPI.h>
#include <MFRC522.h>
constexpr uint8_t RST_PIN = 5; // Configurable, see typical pin layout above
constexpr uint8_t SS_PIN = 4; // Configurable, see typical pin layout above
MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class
MFRC522::MIFARE_Key key;
String card_id;
void setup() {
Serial.begin(115200);
//========rfid
SPI.begin(); // Init SPI bus
rfid_init();
//========
}
void loop()
{
if(read_rfid()!="")
{
Serial.print("card id: ");
Serial.println(card_id);
}
else
{
return;
}
}
//==============================================================RFID FUNCTIONS
void rfid_init()
{
rfid.PCD_Init(); // Init MFRC522
for (byte i = 0; i < 6; i++) {
key.keyByte[i] = 0xFF;
}
}
String read_rfid()
{
// Look for new cards
if ( ! rfid.PICC_IsNewCardPresent())
return "";
// Verify if the NUID has been readed
if ( ! rfid.PICC_ReadCardSerial())
return "";
//-------------------PICC_TYPE
MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
String PICC_TYPE=rfid.PICC_GetTypeName(piccType);
//Serial.println(PICC_TYPE);
//--------------------
// 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)
{
return "ERR-CARD-TYPE";//Your tag is not of type MIFARE Classic.
}
card_id=get_card_id(rfid.uid.uidByte, rfid.uid.size);
// Halt PICC
rfid.PICC_HaltA();
// Stop encryption on PCD
rfid.PCD_StopCrypto1();
return card_id;
}
String get_card_id(byte *buffer, byte bufferSize) {
String temp_card_id="";
for (byte i = 0; i < bufferSize; i++)
{
if(buffer[i] < 0x10)
if(i==0)
temp_card_id=+"0";
else
temp_card_id=+" 0";
else
if(i!=0)
temp_card_id+=" ";
temp_card_id+=String(buffer[i]);
}
return temp_card_id;
}
//==============================================================