Giter Club home page Giter Club logo

mfrc522-rpi's Introduction

mfrc522-rpi

๐Ÿ”‘ JavaScript class to control MFRC522 RFID Module with your Raspberry-pi

MFRC522 is integrated in all types of 13.56MHz RFID passive contactless communication methods and protocols.

It uses the ISO14443 specification to communicate to MIFARE cards (also known as NTAG, NFC) MIFARE wiki ISO14443 wiki

Features

  • Read uid & card memory
  • Write card memory & card key
  • Buzzer notification (Optional)

Demo

Example

Watch on Youtube (with sound)

Enable SPI

The SPI master driver is disabled by default on Raspbian. To enable it, use raspi-config, or ensure the line dtparam=spi=on isn't commented out in /boot/config.txt. When it is enabled then reboot your pi. If the SPI driver was loaded, you should see the device /dev/spidev0.0

more info about SPI

Install

npm version

npm install mfrc522-rpi
  • Raspberry Pi Models: A, B (revisions 1.0 and 2.0), A+, B+, 2, 3, 3+, 3 A+, 4, Compute Module 3, Zero.
  • Node.js Versions: 0.8, 0.10, 0.12, 4, 5, 6, 7, 8, 9, 10, 11, 12

Usage

The examples in the test folder will help you get things started

read uid

node /node_modules/mfrc522-rpi/test/read.js

dump card

node /node_modules/mfrc522-rpi/test/dumpCard.js

dump NTAG213 (sticker)

node /node_modules/mfrc522-rpi/test/dumpNTAG213.js

write NTAG213

#5 Thanks to musdom

write card

node /node_modules/mfrc522-rpi/test/write.js

Card Register

Your MiFare NFC chip consists of multiple sectors, each containing 4 blocks. The first 3 blocks of every sector are used to store data, the last block contains security features (access keys and bits). Every block holds 16 bytes of data. A typical MiFare Classic 1K chip has 16 of these sectors, accounting to 64 blocks with 1024 bytes in total.

The first sector is reserved for manufacturer data, while the second sector is supposed to contain card holder data. So, to be on the safe side, start writing your data on the third sector, starting with block 8.

Every last block of a sector is called a trailer block. It contains two access keys, which are used for protecting the data against unpermitted access, and access bits controlling what can be done with this sector. The first 6 bytes of such a block contain access key A, the following 4 bytes are the access bits, and the last 6 bytes are access key B (optional key).

Please note that when reading a trailer block with this module, it will always return the same (incorrect) values regardless of its actual content. This might be a security feature of the chips.

For simplicity reasons, this module will always authenticate with and refer to key A only. The default key A on new cards is always [0xff, 0xff, 0xff, 0xff, 0xff, 0xff].

The access bits in the middle of a sector trailer block should never be changed without exactly knowing what you're doing. They control what can be done with the current sector and which of the access key(s) can be used for authentication. If they don't make sense, the sector (4 blocks) is irreversively blocked and can never be used again.

If you want to safely change the access key for a sector, please use the writeAuthenticationKey method (see the writeAuthenticationKey.js example in test). Always store the new access key(s) somewhere safe.

Physical memory content of the chip/card which was included on the RFID-RC522 Module

Block: 0 Data: 89,229,151,26,49,8,4,0,98,99,100,101,102,103,104,105
Block: 1 Data: 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Block: 2 Data: 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Block: 3 Data: 0,0,0,0,0,0,255,7,128,105,255,255,255,255,255,255
Block: 4 Data: 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Block: 5 Data: 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Block: 6 Data: 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Block: 7 Data: 0,0,0,0,0,0,255,7,128,105,255,255,255,255,255,255
Block: 8 Data: 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Block: 9 Data: 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Block: 10 Data: 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Block: 11 Data: 0,0,0,0,0,0,255,7,128,105,255,255,255,255,255,255
Block: 12 Data: 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Block: 13 Data: 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Block: 14 Data: 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Block: 15 Data: 0,0,0,0,0,0,255,7,128,105,255,255,255,255,255,255
Block: 16 Data: 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Block: 17 Data: 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Block: 18 Data: 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Block: 19 Data: 0,0,0,0,0,0,255,7,128,105,255,255,255,255,255,255
Block: 20 Data: 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Block: 21 Data: 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Block: 22 Data: 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Block: 23 Data: 0,0,0,0,0,0,255,7,128,105,255,255,255,255,255,255
Block: 24 Data: 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Block: 25 Data: 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Block: 26 Data: 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Block: 27 Data: 0,0,0,0,0,0,255,7,128,105,255,255,255,255,255,255
Block: 28 Data: 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Block: 29 Data: 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Block: 30 Data: 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Block: 31 Data: 0,0,0,0,0,0,255,7,128,105,255,255,255,255,255,255
Block: 32 Data: 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Block: 33 Data: 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Block: 34 Data: 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Block: 35 Data: 0,0,0,0,0,0,255,7,128,105,255,255,255,255,255,255
Block: 36 Data: 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Block: 37 Data: 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Block: 38 Data: 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Block: 39 Data: 0,0,0,0,0,0,255,7,128,105,255,255,255,255,255,255
Block: 40 Data: 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Block: 41 Data: 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Block: 42 Data: 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Block: 43 Data: 0,0,0,0,0,0,255,7,128,105,255,255,255,255,255,255
Block: 44 Data: 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Block: 45 Data: 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Block: 46 Data: 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Block: 47 Data: 0,0,0,0,0,0,255,7,128,105,255,255,255,255,255,255
Block: 48 Data: 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Block: 49 Data: 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Block: 50 Data: 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Block: 51 Data: 0,0,0,0,0,0,255,7,128,105,255,255,255,255,255,255
Block: 52 Data: 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Block: 53 Data: 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Block: 54 Data: 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Block: 55 Data: 0,0,0,0,0,0,255,7,128,105,255,255,255,255,255,255
Block: 56 Data: 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Block: 57 Data: 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Block: 58 Data: 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Block: 59 Data: 0,0,0,0,0,0,255,7,128,105,255,255,255,255,255,255
Block: 60 Data: 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Block: 61 Data: 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Block: 62 Data: 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Block: 63 Data: 0,0,0,0,0,0,255,7,128,105,255,255,255,255,255,255

Changing Authentication Keys

This has so far only been tested with MiFare Classic 1K cards (the ones that come with the reader)! Please use the writeAuthenticationKey method (see the writeAuthenticationKey.js example in test).

Documentation

Manufacturer documentation of MFRC522

MFRC522-Doc.pdf

NFC-TAG Types

NTAG213_215_216.pdf

Wiring

Follow the map here to do the interface pins.

Name Pin # Pin name
SDA 24 CE0
SCK 23 SCLK1
MOSI 19 MOSI1
MISO 21 MOSO1
IRQ None None
GND Any Any Ground
RST 22 GPIO5C3
3.3V 1 3V3
Buzzer 18 GPIO5B3 (Optional)

Note that the pins number is different from the BCM number.

Screenshot

Screenshot

Which hardware is used?

List of hardware which are used with links to amazon.de:

Buzzer Notifications (optional)

It's possible to get buzzer notifications when the module reads a chip, simply connect a piezo speaker to your PI.

Components for Buzzer

Those three optional component are required to run the buzzer with 5V. The RC522 is running at 3.3V so we need to step down the voltage a bit.

Wiring with Buzzer

Screenshot

Demonstration

  • Some images and video demonstration can be found here

Circuit Diagram:

Inside project-diagram folder there is a diagram for the system. You can run it with Fritzing application and make a printable PCB after aligning components on your wish.

Sources

https://github.com/miguelbalboa/rfid

https://github.com/mxgxw/MFRC522-python

Code of Conduct

See the CODE

License

See the LICENSE file for license rights and limitations (MIT).

mfrc522-rpi's People

Contributors

ahmedbhameed avatar devrim-oguz avatar firsttris avatar jonathan-reisdorf avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

mfrc522-rpi's Issues

Error while reading! Status: false Data: 55 BitSize: 0

I tried using the example code in issue 5 (modified a bit to make it work), and the output was the following.

Error while reading! Status: false Data: 55 BitSize: 0
[ 55 ]
Error while writing! Status: false Data: 55 BitSize: 0
Error while reading! Status: false Data: 55 BitSize: 0
[ 55 ]
Error while writing! Status: false Data: 55 BitSize: 0
Error while reading! Status: false Data: 55 BitSize: 0
Block 6
[ 105, 116, 104, 117 ]
Error while writing! Status: false Data: 55 BitSize: 0
Error while reading! Status: false Data: 55 BitSize: 0
[ 55 ]
Error while reading! Status: false Data: 55 BitSize: 0
Block 7
[ 98, 46, 99, 111 ]
Error while writing! Status: false Data: 55 BitSize: 0
Error while reading! Status: false Data: 55 BitSize: 0
[ 55 ]
Error while reading! Status: false Data: 55 BitSize: 0
Block 8
[ 109, 254 ]
Error while writing! Status: false Data: 55 BitSize: 0
Error while reading! Status: false Data: 55 BitSize: 0
[ 55 ]
finished successfully!

This is the new code I wrote:

const MFRC522 = require("mfrc522-rpi"),
SoftSPI = require("rpi-softspi");

let continueReading = true;

const softSPI = new SoftSPI({
    clock: 23,
    mosi: 19,
    miso: 21,
    client: 24
}),
mfrc522 = new MFRC522(softSPI).setResetPin(22);

const defaultUrl = "github.com";
let payload = process.argv[2] ? process.argv[2] : defaultUrl;

while (continueReading) {
  //# reset card
  mfrc522.reset();

  //# Scan for cards
  let response = mfrc522.findCard();
  if (!response.status) {
      continue;
  }

  //# Get the UID of the card
  response = mfrc522.getUid();
  if (!response.status) {
      console.log("UID Scan Error");
      continue;
  }
  //# If we have the UID, continue
  const uid = response.data;

  // add 0xFE to end of message
  payload = payload + "รพ";
  // character to be inserted after NDEF header
  let firstChar = payload.slice(0, 1).charCodeAt(0);
  let stringArray = chunkSubstr(payload.slice(1), 4);

  // NDEF message headers
  let data = [0x03, 0x44, 0xD1, 0x01];
  console.log(mfrc522.getDataForBlock(4));
  mfrc522.writeDataToBlock(4, data);
  // 0x04 = https://
  data = [0x40, 0x55, 0x04, firstChar];
  console.log(mfrc522.getDataForBlock(5));
  mfrc522.writeDataToBlock(5, data);

  const startBlock = 6;
  for (var i = startBlock; i < (startBlock + stringArray.length); i++){
    // must read page before writing
    mfrc522.getDataForBlock(i);
    let binPayload = string2Bin(stringArray[i-startBlock]);
    console.log(`Block ${i}`);
    console.log(binPayload);
    mfrc522.writeDataToBlock(i, binPayload);
    console.log(mfrc522.getDataForBlock(i).splice(0, 4));
  }
  continueReading = false;
  console.log("finished successfully!");
}

function string2Bin(str) {
  var result = [];
  for (var i = 0; i < str.length; i++) {
    // result.push(str.charCodeAt(i).toString(10));
    result.push(str.charCodeAt(i));
  }
  return result;
}

function chunkSubstr(str, size) {
  var numChunks = Math.ceil(str.length / size),
      chunks = new Array(numChunks);

  for(var i = 0, o = 0; i < numChunks; ++i, o += size) {
    chunks[i] = str.substr(o, size);
  }

  return chunks;
}

Cannot get continuous read

Hi,

I am running the read.js script on the test folder, and it seems I cannot get a solid card detection.
When there is no card I can trace that using the following code:

    //# Scan for cards
    let response = mfrc522.findCard();
    if (!response.status) {
        console.log("NOT detected");
        console.log(" ### ");
        return;
    }

But when I let my tag on the reader I cannot get a solid read, instead I get "NOT DETECTED", then it works and I get the tag info ( a read ), then "NOT DETECTED" again, then it works again ... etc. It keeps going on and off like that.

Since I want to be able to tell when there is something on the reader or not the fact that when there is something I cannot get a solid signal is then a problem to me, because it seems that someone is taking the tag out, to put it back in, and out ... etc.

Do you know where that could come from?
I hope I'm not too vague.

Thomas P

Race condition(?) on a mifare ISO/IEC 14443-4 card

I'm using this to read mifare classic 1k card and there haven't been any issues. I was recently given a different type of mifare card and at first it looked liked it wasn't being read by the reader but after debugging the findCard() function at index.js it worked?

If i debug around Line 278, nothing happens the response is empty, however if I debug before that and wait just a few milliseconds to a second then I get a response and I successfully get the UID.

So am I right that there is a race condition happening somewhere? it's weird that if I debug the code I get a response but nothing if I don't.

Increasing range of MFRC522

Hello,

In the repo for the python version they claim to have increased the range:
mxgxw/MFRC522-python#30

How can this be done with this module?

I tried:

First I logged the byte that sets the antenna gain
console.log(this.readRegister(CMD.RFCfgReg)) which returns 72.
According to the datasheet of MRFC522, 72 corresponds to 33db.

I tried putting it on 112 (which is 48db) with

this.writeRegister(CMD.RFCfgReg, 0x07 << 4);

in index.js in the function reset();

But If I try to read the same byte later, again with
console.log(this.readRegister(CMD.RFCfgReg)) it still logs 72!

How do I increase the gain?

Thanks

MFRC522-python not working after running mfrc522-rpi

Hello,
I have just installed mfrc522-rpi on my raspberry pi 3, and it I ran the read test example successfully. However, after exiting the app (Ctrl+c), I noticed that my other python program which uses MFRC522-python library, now is not detecting that rfid tag unless I reboot and run it before the mfrc522-rpi example.

in other words, if I run the python example first it works. On exit I call GPIO.cleanup(), so when I run node node /node_modules/mfrc522-rpi/test/read.js it works fine, but if I run python3 mypythonapp.py after it it doesn't detect the tag and output is:
/usr/local/lib/python3.7/dist-packages/mfrc522/MFRC522.py:151: RuntimeWarning: This channel is already in use, continuing anyway. Use GPIO.setwarnings(False) to disable warnings.
GPIO.setup(pin_rst, GPIO.OUT)

I think this has to do with gpio cleanup?
P.S I tried to call rpio.close(22); and this.spi.close() in /node_modules/mfrc522-rpi/index.js, the warning above is gone, but still can't detect the tag.
Can anyone help with this please?

Ta

Hardware: raspberry pi 3 running raspbian buster
Node v12.8.0 (using nvm)

LEDs on module

I see at least one red LED on the module. Is it possible to let that blink?!

Very long delay on startup: Reason for 250ms delay?

Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 250 - i);

I uncovered an issue when this device isn't actually connected, or if rpio is in mock mode.. this loop with Atomics.wait causes my program to hang for several seconds before this frees up.

Can you provide some insight into the reason for the 250ms delay and reducing that by 1ms per cycle for 250 cycles. I'm considering refactoring this, but want to have a better understanding first.

Thanks,

Thomas

Example of how to write on NTAG213, if possible

Hi,

I'm learning about the mfrc522 and I have a project that can use it with Ntag213, but it would work better if I can write on the tags.

I'm not sure mfrc522 can do this, if it don't could you just comment saying it is not possible? But if it can could you please provide a example of how to write to a NTAG213?

I've spent the last few hours trying to make it work but no success, no Google search that helped.

Thanks.

Use SPI1

Hi guys, we are currently doing a prototype that includes a MFRC522 (Robotdyn) reader and a 3.5 TFT screen from Waveshare, after a few days of fighting while we took the two devices (TFT screen in SPI0 and MFRC522 in SPI1), everything works correctly with

but not with this library, we always receive the message
Can not open the SPI device: There is no such file or directory
but
ls /dev/spidev*
returns
/dev/spidev1.0
any ideas for make wiringpi work in spi1?

MFRC522 not working on RPi 4

Hi, I'm getting the following error using my SD card that works fine in a Raspberry Pi Model 3, if I put it into a Raspberry Pi Model 4:

WARNING: Hardware auto-detect failed, running in raspi-3 mock mode

And consequently the card detection does not work. Any idea what this could be related to?
Thanks

Atomics.wait cannot be called in this context

My electron build on Raspberry Pi 4 B gives this error:

Uncaught TypeError: Atomics.wait cannot be called in this context
    at Atomics.wait (<anonymous>)
    at MFRC522.setResetPin (/home/alba/Interface_Automat_ArGe/node_modules/mfrc522-rpi/index.js:32)
    at rfid_controller.js:19

It is shown to be caused by the line 32 of index.js -> Atomics.wait( new Int32Array(new SharedArrayBuffer(4)), 0, 0, 50);

can't we replace these Atomics.wait with something else for electron compatibility?

Pin in use

getting error GPIO24 is currently in use by /sys/class/gpio

Error: Cannot find module '../build/Release/WiringPi

I am trying to bundle up all the required modules starting at read.js into a single file called bundle.js with the browserify command: The following error is raised during operation. Any help

(venv) pi@raspberrypi:~/RFID/New $ browserify node_modules/mfrc522-rpi/test/read.js -o bundle.js
Error: Cannot find module '../build/Release/WiringPi' from '/home/pi/RFID/New/node_modules/wiringpi-node/lib'
at /home/pi/.nvm/versions/node/v8.9.4/lib/node_modules/browserify/node_modules/browser-resolve/node_modules/resolve/lib/async.js:55:21
at load (/home/pi/.nvm/versions/node/v8.9.4/lib/node_modules/browserify/node_modules/browser-resolve/node_modules/resolve/lib/async.js:69:43)
at onex (/home/pi/.nvm/versions/node/v8.9.4/lib/node_modules/browserify/node_modules/browser-resolve/node_modules/resolve/lib/async.js:92:31)
at /home/pi/.nvm/versions/node/v8.9.4/lib/node_modules/browserify/node_modules/browser-resolve/node_modules/resolve/lib/async.js:22:47
at FSReqWrap.oncomplete (fs.js:152:21)

7 byte uid

any way to make this module read the complete UID of newer tags with 7 byte UID?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.