Showing posts with label MFRC522-python. Show all posts
Showing posts with label MFRC522-python. Show all posts

Sunday, October 18, 2015

Raspberry Pi + Python + mxgxw/MFRC522-python - Dump RFID Tag data one-by-one, and detect tag remove

Last post about RFID Tag "Raspberry Pi 2 + MFRC522-python - Dump RFID Tag data using mxgxw/MFRC522-python" demo the Dump.py from mxgxw/MFRC522-python. It loop to read and display RFID Tag data, not easy for checking. So I modify it to dump RFID Tag data one-by-one, and detect card remove.

pyDump1.py, exit once tag data dump finished.
#!/usr/bin/env python
# -*- coding: utf8 -*-

import RPi.GPIO as GPIO
import MFRC522
import signal

continue_reading = True

# Capture SIGINT for cleanup when the script is aborted
def end_read(signal,frame):
    global continue_reading
    print "Ctrl+C captured, ending read."
    continue_reading = False
    GPIO.cleanup()

# Hook the SIGINT
signal.signal(signal.SIGINT, end_read)

print "Place card please..."

# Create an object of the class MFRC522
MIFAREReader = MFRC522.MFRC522()

# This loop keeps checking for chips. If one is near it will get the UID and authenticate
while continue_reading:
    
    # Scan for cards    
    (status,TagType) = MIFAREReader.MFRC522_Request(MIFAREReader.PICC_REQIDL)

    # If a card is found
    if status == MIFAREReader.MI_OK:
        print "Card detected"
    
    # Get the UID of the card
    (status,uid) = MIFAREReader.MFRC522_Anticoll()

    # If we have the UID, continue
    if status == MIFAREReader.MI_OK:

        # Print UID
        print "Card read UID: "+str(uid[0])+","+str(uid[1])+","+str(uid[2])+","+str(uid[3])
    
        # This is the default key for authentication
        key = [0xFF,0xFF,0xFF,0xFF,0xFF,0xFF]
        
        # Select the scanned tag
        MIFAREReader.MFRC522_SelectTag(uid)

        # Dump the data
        MIFAREReader.MFRC522_DumpClassic1K(key, uid)

        # Stop
        MIFAREReader.MFRC522_StopCrypto1()
        
        print "--- Finished ---"
        continue_reading = False
        
GPIO.cleanup()



pyDump2.py, wait tag remove after data dump. I assume tag removed if 5 times of MIFAREReader.MFRC522_Request() not OK.
#!/usr/bin/env python
# -*- coding: utf8 -*-

import RPi.GPIO as GPIO
import MFRC522
import signal

continue_reading = True

# Capture SIGINT for cleanup when the script is aborted
def end_read(signal,frame):
    global continue_reading
    print "Ctrl+C captured, ending read."
    continue_reading = False
    GPIO.cleanup()

# Hook the SIGINT
signal.signal(signal.SIGINT, end_read)

print "Place card please..."

# Create an object of the class MFRC522
MIFAREReader = MFRC522.MFRC522()

# This loop keeps checking for chips. If one is near it will get the UID and authenticate
while continue_reading:
    
    # Scan for cards    
    (status,TagType) = MIFAREReader.MFRC522_Request(MIFAREReader.PICC_REQIDL)

    # If a card is found
    if status == MIFAREReader.MI_OK:
        print "Card detected"
    
    # Get the UID of the card
    (status,uid) = MIFAREReader.MFRC522_Anticoll()

    # If we have the UID, continue
    if status == MIFAREReader.MI_OK:

        # Print UID
        print "Card read UID: "+str(uid[0])+","+str(uid[1])+","+str(uid[2])+","+str(uid[3])
    
        # This is the default key for authentication
        key = [0xFF,0xFF,0xFF,0xFF,0xFF,0xFF]
        
        # Select the scanned tag
        MIFAREReader.MFRC522_SelectTag(uid)

        # Dump the data
        MIFAREReader.MFRC522_DumpClassic1K(key, uid)

        # Stop
        MIFAREReader.MFRC522_StopCrypto1()
        
        # wait card removed 
        print "--- Remove Card ---"
        card_removed = False
        card_removed_counter = 5

        while not card_removed:
            (status,TagType) = MIFAREReader.MFRC522_Request(MIFAREReader.PICC_REQIDL)
            if status != MIFAREReader.MI_OK:
                card_removed_counter = card_removed_counter-1
                if card_removed_counter==0:
                    card_removed = True
            else:
                card_removed_counter = 5
                
        print "--- Card removed---"
        print "Place card again please..."





- For setup of RFID Reader, refer "Raspberry Pi 2 + MFRC522-python, to read RFID tag".
- For mxgxw/MFRC522-python, refer "Raspberry Pi 2 + MFRC522-python - Dump RFID Tag data using mxgxw/MFRC522-python".

Monday, October 12, 2015

Raspberry Pi 2 + MFRC522-python - Dump RFID Tag data using mxgxw/MFRC522-python


Last post "Raspberry Pi 2 + MFRC522-python, to read RFID tag" show how to setup SPI on Raspberry Pi 2/Raspbian Jessia, and git clone rasplay/MFRC522-python to read id of RFID tag. It provide a example read.py only.

Here is another GitHub project mxgxw/MFRC522-python (A small class to interface with the NFC reader Module MFRC522), it provide examples of Dump.py, Read.py and Write.py also.

To set up SPI on Raspberry Pi 2/Raspbian Jessia, refer to last post.

This video show how to "git clone https://github.com/mxgxw/MFRC522-python" and run Dump.py to dump data of a brand new RFID key.



You can download APK of my Android exercise in my another blog, Android NFC: readBlock() for MifareClassic, to dump data in RFID tag; to compare the dumped data. Tested on brand new 1K MifareClassic RFID Tag only.


Next:
- The example of Dump.py repeat to display the tag data, not easy for checking. I modify it to display once, and also detect card removed. Read "Raspberry Pi + Python + mxgxw/MFRC522-python - Dump RFID Tag data one-by-one, and detect tag remove".


Raspberry Pi 2 + MFRC522-python, to read RFID tag

CAUTION@2016-08-19:
Somebody comment no need set device tree, check the comments (in the comments below and in the video: https://www.youtube.com/watch?v=0aVtZvkuHTA) first, before you try.



This post show the steps I install MFRC522-python (https://github.com/rasplay/MFRC522-python) on Raspberry Pi 2/Raspbian Jessie (2015-09-24), to read RFID tags (key and card) using RFID Reader, RFID-RC522.



Connection between RFID-RC522 Reader and Raspberry Pi 2:
RFID-RC522 pin NamePin #Raspberry Pi 2 Pin name
SDA24GPIO8
SCK23GPIO11
MOSI19GPIO10
MISO21GPIO9
IRQ-no connection
GNDGNDGND
RST22GPIO25
3.3V13V3

Steps:
1 - Enable SPI using Raspberry Pi Configuration


2 - Install python-dev and SPI-Py
$ sudo apt-get install python-dev
$ git clone https://github.com/lthiery/SPI-Py.git
$ cd SPI-Py
$ sudo python setup.py install


3 - Download MFRC522-python, MFRC522 Python Library for Raspberry Pi
(https://github.com/rasplay/MFRC522-python)
$ git clone https://github.com/rasplay/MFRC522-python.git


Unfortunately it is still not work!

Then I follow dzasa steps in this thread https://www.raspberrypi.org/forums/viewtopic.php?f=37&t=106313 to solve the problem.

4 - Enable device tree
Edit /boot/config.txt to add the line:
device_tree=on


5 - Install latest version of bcm2835, C library for Broadcom BCM 2835 as used in Raspberry Pi; version 1.46 currently.
(http://www.airspayce.com/mikem/bcm2835/)
# download the latest version of the library, say bcm2835-1.xx.tar.gz, then:
$ tar zxvf bcm2835-1.xx.tar.gz
$ cd bcm2835-1.xx
$ ./configure
$ make
$ sudo make check
$ sudo make install


Then reboot.

6 - Test it:
$ cd MFRC522-python
$ sudo python read.py



Updated@2016-01-30:
when we using MFRC522 it will set GPIO port mode to GPIO.BOARD. So simply you can't access using GPIO ports when it is in this mode using GPIO pins numbers. But you can access any GPIO ports according to GPIO.BOARD representation.  ~ refer to Unknown comment below.


Next:
Raspberry Pi 2 + MFRC522-python - Dump RFID Tag data using mxgxw/MFRC522-python

Related:
- Arduino Uno + RFID-RC522, MFRC522 library example DumpInfo
- Android NFC: readBlock() for MifareClassic, to dump data in RFID tag