Uncategorized

MFRC522 NFC Discussion Notes

Asked 
Active today
Viewed 217 times
1

Trying to read my RFID MFRC522 using a Mifare blue tag and it is not working. Wondering if i could get any help on this as when i put the tag to read it doesn’t show up as quick?

using:

https://pimylifeup.com/raspberry-pi-rfid-rc522/

https://medium.com/coinmonks/for-beginners-how-to-set-up-a-raspberry-pi-rfid-rc522-reader-and-record-data-on-iota-865f67843a2d

Python coding based on the websites I've used in order to try to get my tag to read

0

Question

How can Rpi python read a SPI MFRC255 RFID/NFC tag?


Short Answer

Part 1 – Introduction to I2C and SPI NFC/RFID Chips and Modules

wiring dia 1


Part 2 – Setting up SPI MFRC522 Module

dual mfrc522 module


Part 3 – Troubleshooting SPI Software and Hardware using Loopback

loopback test


Part 4 – Using Rpi4B buster Python 3 and Mfrc522 library to read NFC Tag

I have successfully installed mfrc522 using pip3. I created a reader, wrote something to a tag, and then read back, without any problem.

Notes

  1. I am using Rpi4B buster release 2020feb13, preinstalled python 3.7.3.
  2. pip3 installs mfrc522 0.0.7 in /usr/local/lib/python3.7/dist-packages …
  3. pip3 installs spidev 3.4 and GPIO 0.7 in /usr/lib/python3/dist-packages …
  4. If the reader freezes/hangs when try to read or write, then it is necessary to do the SPI loopback test, to make sure the SPI software setup and hardware wiring is correct (See Appendix E below for details on using the SPI loop back tools.

mfrc522 test


Part 5 – Using Rpi4B Python 2 and Mfrc522 library to read Maire Blue Tag

The python3 mfrc522 library installed by pip3 is backward compatible to python2. So the path is set so that either python3 or python2 would import the same library installed by pip3. The following screen shows that python2 can also use the same library to read/write a tag.

python 2.7 test


Part 6 – Studying Mario Gomez’s read.py and write.py code

I found Mario Gomez’ library has a function for MIFAIRE Classic 1K tag. I know very little about it. So I am googling to understand it better. I found Ref 43 by the Last Minute Enginners good. I found there are many more things I don’t know about, eg UID, PICC, SAK etc.

Notes

  1. Mario Gomez’s mfrc522.py read.py and write.py code is very well commented and therefore most newbie friendly. (See Ref 27)
  2. Mario Gomez’s code, as he said, is thoroughly commented, but I still found it difficult to understand or guess what this and that means. Then I realized that I actually do not have the prerequisite knowledge to understand event the basic idea of NFC. So I googled for a NFC glossary to clear my mind (Ref 43). Skimming the glossary, I now know precisely what is NFC, PCD, PICC, and most importantly what is MIFAIRE (A product of NXP Semiconductors. It is a smart card based on ISO14443A providing up to 4KB of memory on the card.)

mifare class 1k


/ to continue, …


Long Answer

  1. The OP used Rpi3 NOOB python 2.7, and installed himself SpiPy, SpiDev for testing.
  2. I think python 2.7 is a bit out of date. So I am repeating the OP’s situation but instead using Rpi4B buster 2020feb13 (full version image), with the following buster preinstalled software:(a) python 3.7.3

    (b) thonny IDE

    (c) spiDev

    (d) pip3

  3. I am using pip3 to install the MFRC522 python library, which includes the following two python3 programs:(a) mfrc522.py (about 400 lines)

    (b) simpleMFRC.py (about 100 lines)

  4. I am using the RFID/NFC module already tested OK using libnfc-1.7.1 in I2C configuration (Appendix A).
  5. I am using pip3 to install mrfc522, RPi.GPIO, and spidev in the following directory (Appendix B). I am not sure if the above GPIO and spidev modules are the same or different from the buster’s corresponding preinstalled programs./usr/local/lib/python3.7/dist-packages
  6. I am using the built-in SPI interface /dev/spidev0.0 and /dev/spidev0.1 (Appendix C)
  7. I used python3 shell to import SimpleMFRC522 from mfrc522, and found creating a reader object OK. (Appendix D).from mfrc522 import SimpleMFRC522

    reader = SimpleMFRC522()

  8. I read MFRC522-python/mfrc522/SimpleMFRC522.py/ – GitHu 2019mar26 saying the following:@death-droid Improve compatibility with Python 3 – 2019mar26

    So I guess the earlier versions of SimpleMFRC522 were not very compatible to python3. I was wondering if my pip3 installed stuff is more or less updated than the OP’s corresponding software using Git Clone. I guess I better download the most updated versions of mfrc522, SimpleMFRC522, and the demo/test red/write tag programs and freeze them for later testing.

  9. Now have tidied up the different version of the four main programs mfrc522.py, samplemfrc522py, read.py, and write.py, and put them in a penzu reading log file. Next step is to skim the two big files to get a rough picture of what is going on.https://penzu.com/public/08d4fdf2
  10. Now I have skimmed the two main programs mrfc522.py and simpleMFRC522.py. I surprisingly found the program structure is very simple. So it should not not that difficult to debug and expand. The penzu reading log is here:https://penzu.com/p/08d4fdf2
  11. Now I have tried the SPI loopback test and found it OK. (Appendix E)
  12. I tried to repeat the OP is problem, ie, raed a tag. Still no luck, the program hanged. Because I already double checked that the NFC module can read OK the same tag using libbnfc-1.7.1 I2C mode, and SPI loopback at 50kHz is OK. So the problem is likely at the SimpleMFRC522 side. Next step is to debug Read.py, SimpleMFRC522.py and the MFRC522.py library. (Appendix J)
  13. Now I am checking the schematic of [another similar] nfc module to make sure that my guess of the IRQ and RST wiring is correct, ie, no need to connect these two pins to Rpi. This is verified by the success of libnfc-1.7.1 I2C read card without connect the RST and IRQ pins. Perhaps I can ping the module to make sure SPI Clk, Mosi, and Miso are working OK (the previous SPI loopback only tests 50kHz and only MOSI and Miso, CS is not tested. (Appendix J)
  14. I read the pn532 datasheet that the max SPI speed is 5MHz, so it should be OK to set SPI speed to 1MHz, 500kHz, or 100kHz. (Appendix J)/ to continue, …

References

(1) PN532 NFC/RFID Catalog – AdaFruit US$40

(2) PN532 NFC/RFID controller breakout board v1.6 – AdaFruit US$40

(3) RFID/NFC Guides – AdaFruit

(4) Build a Babel Fish Language Toy – AdaFruit

(5) YouTube Build a Babel Fish Language Toy with Adafruit RFID and Wave Shields for Arduino – AdaFruit 2012aug02

(6) AdaFruit PN532 Breakout Module V1.6 Documentation (I2C/SPI GitHub, datasheet, schematic, tutorials, downloads)

(7) Using with LibNFC – AdaFruit (Mainly for Arduino, a bit out of date)

(8) libfnc Main Page (Last modified 2013) – nfc-tools.org

(9) RFID and NFC Tutorial – SparkFun

(10) SparkFun RFID Starter Kit Hookup Guide

(11) PN532 NFC Module for Raspberry Pi – SunFounder US$28

(12) PN532 NFC Module for Raspberry Pi Tutorial (libnfc, C language) – SunFounder

(13) AliExpress PN532 NFC Module Catalog US$3~5

(14) How to setup a Raspberry Pi RFID RC522 Chip (SPI, python) – PiMyLifeUp

(15) pimylifeup/MFRC522 – SPI python – PiMyLifeUp

(16) Grove NFC Module (With Arduino UART, I2C Demo Programs) V1.1 – Seeed Studio 2016aug31

(17) Setting up a PN532 NFC module (V3) on a Raspberry Pi using I2C – blog of stigok 2017oct12

(18)PN532/C1 Near Field Communication (NFC) controller Rev3.6 – NXP 2017nov28

(19) PN532 User Manual (UM0701-02 Rev02) – NXP2007

(20) MIFARE – Wikipedia

(21) Amazon AZDelivery RC522 RFID Kit x 3 for Arduino and Rpi – £9.5

(22) AZDelivery RFID Kit RC522 Reviews

(23) TaoBao Risym MFRC-522 RC522 RFID Reader – ¥12

(24) MFRC522 MIFARE NTAG FrontEnd R3.9 Datasheet — NXP 2016apr27

(25) Mario Gómez MFRC522-python 2018mar26 Main Page

(26) Mario Gómez MFRC522-python 2018mar26 Read Me

(27) Mario Gómez MFRC522-python 2018mar26 Zip Download

(28) Spidev 3.4 User Guide – PyPi 2020feb19

(29) RPi.GPIO 0.7.0 pip install RPi.GPIO

(30) Pat-odoo TwoRC522_RPi2-3 – GitHub

(31) Pat-odoo TwoRC522_RPi2-3 – PDF

(32) SPI-Py GitHub – Louis Thiery

(33) MFRC522-python GitHub – Mario Gomez

(34) How to use three RC522 RFID readers at the same time? – Rpi Forum Q&A 2019jun08

(35) MFRC522 Library and External GPIO Program Conflicting Problem, Rpi Forum Q&A

(36) RFID Tags for Library System – Rpi Forum Q&A

(37) Build your own RPi RFID Attendance System (LCD 16×2, MySQL, NGNIX, PHP) – Emmet, PiMyLifeUp 2019aug08/nov17

(38) The Beginner’s Guide to RFID Systems – AtlasRFID

(39) A practical guide to writing technical specs

(40) How to check if SPI is enabled and functional on Raspi 3b+? – Rpi.StackExchange Forum 2019Apr24 Viewed 1k times

(41) Mario Gomez’s read.py, write.py, and mfrc522.py listing

(42) Linux Path Definition

(43) How RFID Works (IFARE Classic 1K) – Last Minute Engineers

(44) Control an I2C HD44780 20×4 LCD display with Rpi

(45) How to setup an I2C 16 x 2 LCD on Rpi – Circuit Basics 2016Apr

(46) Rpi SPI and I2C Tutorial – SparkFun

(47) Near Field Communication Glossary (MIFARE, PICC, PCD etc) – NFCResearch Lab Hagenberg

to continue, …


Appendices

Appendix A – The PN532 NFC/RFID Module V3 being tested

Reference: nfclib v1.1.7 PN532 NFC Module Testing

pn532 1


Appendix B – Mfrc522 software (including SPIdev and GPIO) setup record

mfrc install


Appendix C – Minimal configuration of SPI and I2C channels for testing the PN532 module

boot/config.txt


Appendix D – PiMyLifeUp Gus SimpleMFRC522 Library

simplemfrc522 lib


Appendix E – SPI Loopback Test

Update 2020apr30hkt1826

Many thank for the OP pointing out a typo. If MOSI is not connected to MISO, then the out would be all zeros!

loopback test


Appendix F – MFRC522 Directory Listing

mfrc522 directories listing


Appendix G – AZDelivery RFID Kit Reviews

AZDelivery 3 x RFID Kit RC522 with Reader, Chip and Card for Arduino and Raspberry Pi including E-Book! – £9.5

AZDelivery 3 x RFID Kit RC522 Reviews

Erich Eichinger – Reviewed 12 September 2019

German Quality with significantly wider sensor range than Chinese clones I had some cheap Chinese RC522 clones with a very limited range (only 1-2 mm). Thought I’d try German Quality and was not disappointed. Those RC522 are still cheap enough but detect a tag up to 1.5cm distance which was enough for my purpose.


Jürgen L. Universal and affordable 23 February 2020

With the software you have to trick a little, the instructions available on the net are somewhat outdated or refer to a particular Raspi. But if you have a little programming knowledge, you can quickly customize the Python program yourself.


Rene Winkler – Works fine, but you should use SPI 3 March 2020

The module supports SPI (preconfigured), UART and I2C. The interface must be selected via configuration pins. However, there is no pull up/down for this module. According to the data sheet you would have to pull from high to low for the UART Pin EA. In this case, however, it would mean separating a trace through and pulling a wire bridge to GND.

I’m using the module via SPI on a Raspberry Pi Zero with Python for a kids music box.

The Python library, which I first found for the module, was unfortunately out of date and did not fit the SPI library. Since something had changed in the parameters for SPI read and Write. But the module can’t do anything for that.


Appendix F – PiMyLifeUp SimpleMFRC522.py and Read.py by Simon Monk

# PiMyLifeUp MFRC522 Python Library, Setup, and Example
# https://github.com/pimylifeup/MFRC522-python

# pimylifeup/MFRC522-python
# https://github.com/pimylifeup/MFRC522-
python/blob/master/mfrc522/SimpleMFRC522.py

# Code by Simon Monk https://github.com/simonmonk/

from . import MFRC522
import RPi.GPIO as GPIO

class SimpleMFRC522:

  READER = None

  KEY = [0xFF,0xFF,0xFF,0xFF,0xFF,0xFF]
  BLOCK_ADDRS = [8, 9, 10]

  def __init__(self):
    self.READER = MFRC522()

  def read(self):
      id, text = self.read_no_block()
      while not id:
          id, text = self.read_no_block()
      return id, text

  def read_id(self):
    id = self.read_id_no_block()
    while not id:
      id = self.read_id_no_block()
    return id

  def read_id_no_block(self):
      (status, TagType) = self.READER.MFRC522_Request(self.READER.PICC_REQIDL)
      if status != self.READER.MI_OK:
          return None
      (status, uid) = self.READER.MFRC522_Anticoll()
      if status != self.READER.MI_OK:
          return None
      return self.uid_to_num(uid)

  def read_no_block(self):
    (status, TagType) = self.READER.MFRC522_Request(self.READER.PICC_REQIDL)
    if status != self.READER.MI_OK:
        return None, None
    (status, uid) = self.READER.MFRC522_Anticoll()
    if status != self.READER.MI_OK:
        return None, None
    id = self.uid_to_num(uid)
    self.READER.MFRC522_SelectTag(uid)
    status = self.READER.MFRC522_Auth(self.READER.PICC_AUTHENT1A, 11, self.KEY, uid)
    data = []
    text_read = ''
    if status == self.READER.MI_OK:
        for block_num in self.BLOCK_ADDRS:
            block = self.READER.MFRC522_Read(block_num) 
            if block:
                    data += block
        if data:
             text_read = ''.join(chr(i) for i in data)
    self.READER.MFRC522_StopCrypto1()
    return id, text_read

  def write(self, text):
      id, text_in = self.write_no_block(text)
      while not id:
          id, text_in = self.write_no_block(text)
      return id, text_in

  def write_no_block(self, text):
      (status, TagType) = self.READER.MFRC522_Request(self.READER.PICC_REQIDL)
      if status != self.READER.MI_OK:
          return None, None
      (status, uid) = self.READER.MFRC522_Anticoll()
      if status != self.READER.MI_OK:
          return None, None
      id = self.uid_to_num(uid)
      self.READER.MFRC522_SelectTag(uid)
      status = self.READER.MFRC522_Auth(self.READER.PICC_AUTHENT1A, 11, self.KEY, uid)
      self.READER.MFRC522_Read(11)
      if status == self.READER.MI_OK:
          data = bytearray()
          data.extend(bytearray(text.ljust(len(self.BLOCK_ADDRS) * 16).encode('ascii')))
          i = 0
          for block_num in self.BLOCK_ADDRS:
            self.READER.MFRC522_Write(block_num, data[(i*16):(i+1)*16])
            i += 1
      self.READER.MFRC522_StopCrypto1()
      return id, text[0:(len(self.BLOCK_ADDRS) * 16)]

  def uid_to_num(self, uid):
      n = 0
      for i in range(0, 5):
          n = n * 256 + uid[i]
      return n

# *** mfrc522 Installation and Example Code ****************************************************
# https://github.com/pimylifeup/MFRC522-python

A python library to read/write RFID tags via the budget MFRC522 RFID module.

This code was published in relation to a blog post and you can find out more about how to hook up your MFRC reader to a Raspberry Pi there.

Installation
Until the package is on PyPi, clone this repository and run python setup.py install in the top level directory.

Example Code
The following code will read a tag from the MFRC522

from time import sleep
import sys
from mfrc522 import SimpleMFRC522
reader = SimpleMFRC522()

try:
    while True:
        print("Hold a tag near the reader")
        id, text = reader.read()
        print("ID: %s\nText: %s" % (id,text))
        sleep(5)
except KeyboardInterrupt:
    GPIO.cleanup()
    raise

.END

Appendix G – Spidev 3.4 User Guide – PyPi

Spidev 3.4 pip install spidev – Python bindings for Linux SPI access through spidev – PyPi 2020feb19

[a] xfer(list of values[, speed_hz, delay_usec, bits_per_word])

Performs an SPI transaction. Chip-select should be released and reactivated between blocks. Delay specifies the delay in usec between blocks.

[b] xfer2(list of values[, speed_hz, delay_usec, bits_per_word])

Performs an SPI transaction. Chip-select should be held active between blocks.

[c] xfer3(list of values[, speed_hz, delay_usec, bits_per_word])

Similar to xfer2 but accepts arbitrary large lists. If list size exceeds buffer size (which is read from /sys/module/spidev/parameters/bufsiz), data will be split into smaller chunks and sent in multiple operations.


Appendix H – MRFC532 SpiDev LoopBack Test and Wiring Length Limit

spi loopback 01

Appendix I – Mario Gomez MRFC522 Wiring Diagram and Software Requirements 2014

mario Gomez readme.md


Appendix J -Long Answer Points 12, 13, 14

simpleMFRC522 test

pn532 schematic

pn532 interface max speed


Appendix K – Single MFRC522 Mdoule Wiring Diagram

wiring dia 1

wiring dia 2

wiring dia 3

Appendix L – Multiple MFRC522 Module Wiring Diagram

quad mfrc522 wiring


Appendix M – Dual MFRC522 Module (One for SPI 0.0, another for SPI 1.0)

dual mfrc522 module


Appendix N – Python program to ping two modules

So I have written a python program to ping two modules. There are two versions. The “read-only” version is to read the software version register at 0x37 whose contents is 0x92 or 0x12. The “read-after-write” version is to first write the interrupt config register at 0x02 and then read it back. The ping program is scalable, from one module to some 16 or more modules. The main test function is sample output is listed below:

def main():
    initSpiBusNameListMode00Speed100kHz('SpiFiveBusNameList')
    testReadMfrc522RegByName('SpiBus10', 'VersionReg')
    testReadMfrc522RegByName('SpiBus11', 'VersionReg')
    return

Sample Output
Run fnfc310.py    
     Begin Execute Function testReadMfrc522RegByName  2020-04-07 21:08               
       Function Name                           = testReadMfrc522RegByName
       Spi Bus Name                            = SpiBus10
       Dev Reg Name                            = VersionReg
       Dev Reg Addr                            = 0x37
       Dev Reg Contents                        = 0x92
     End   Execute Function testReadMfrc522RegByName  2020-04-07 21:08
     Begin Execute Function testReadMfrc522RegByName  2020-04-07 21:08
       Function Name                           = testReadMfrc522RegByName
       Spi Bus Name                            = SpiBus11
       Dev Reg Name                            = VersionReg
       Dev Reg Addr                            = 0x37
       Dev Reg Contents                        = 0x12
     End   Execute Function testReadMfrc522RegByName  2020-04-07 21:08

Appendix O – Tri MFRC522 Module Board

A practical guide to writing technical specs

tri module board


Appendix P – Multiple MFRC522 Board For Pair/Swap Troubleshooting

penta module


Appendix Q – Reviews on the Mario Gomez’ MFRC522-Python Library

(1) (a) Important notice: This library has not being actively updated in almost four years. It might not work as intended on more recent Raspberry Pi devices. You might want to take a look to the open pull-requests and forks to see other implementations and bug-fixes. (b) Requirements: This code requires you to have SPI-Py installed from the following repository: https://github.com/lthiery/SPI-Py – Mario Gomez, README.MD, MFRC522-python, GitHub, 2014.

(2) With the software you have to trick a little, the instructions available on the net are somewhat outdated or refer to a particular Raspi. But if you have a little programming knowledge, you can quickly customize the Python program yourself. – Jürgen L 2020feb23 Appendix G – AZDelivery RFID Kit Reviews

(3) (a) There is no pull up/down for this module. According to the data sheet you would have to pull from high to low, … it would mean separating a trace through and pulling a wire bridge to GND. (b) The Python library, which I first found for the module, was unfortunately out of date and did not fit the SPI library. Since something had changed in the parameters for SPI read and Write. But the module can’t do anything for that. – Rene Winkler 2020mar03 Appendix G – AZDelivery RFID Kit Reviews


End of Answer

.END
Recent Chat Transcript (from 2020mar31)
tlfong01
tlfong01
Mar 31, 22:04
Ah bed time! I call it a day. Good luck and see you tomorrow.
tlfong01

tlfong01
2888
Mar 31, 22:17
Two more things: (1) If you are weak in Rpi and python, AND your schedule is tight, you might like to consider (a) libnfc-1.71. I2C mode, which I found OK, (b) Arduino library, which is very mature and easy to find support, (c) Buy more boards to stand by and/or cross check. Reference: Arduino library for MFRC522
github.com/miguelbalboa/rfid

Before buy:

Please notice that there are many sellers (ebay, aliexpress, ..) who sell mfrc522 boards. The quality of these boards are extremely different. Some are soldered with wrong/low quality capacitors or fake/defect mfrc522.

(see full text)

Good luck and cheers.
13 hours later…
tlfong01

tlfong01
2888
Apr 1, 11:44
@627117717.pr So we can debug the Read.py on both sides, you python2.7, me python 3.7. The trick is just insert print and pause statements at breakpoints of sampleMfrc532 and mfrc532 and see if the program is stuck and hangs/freezes at the low level SPI read functions.
If it is indeed the case, then we need to debug SPI by first using the LOOPBACK method, just to make sure if (a) spiDev and python are compatible, (b) if hardware wiring are correct (Note: MOSI and MISO must crossover). You can search “SPI Loopback” in this and other forums for more details. Below are some posts you might like to start with.
SPI Loopback references: (1) SPI Loopback Test – rpi.org 2019
raspberrypi.org/documentation/…

(2) SPI Loopback Test using Python (See Appendix C for the python program) – rpi.stackexchange 2019
raspberrypi.stackexchange.com/…

(3) SPI Loopback Test (struct spi_ioc_transfer level) – rpi.org forum 2019
raspberrypi.org/forums/…

(see full text)

3 hours later…
tlfong01

tlfong01
2888
Apr 1, 14:33
So I have done the SPI loop back test OK.
user image
Next step is to use a multi-meter to check the open/short wiring of SPI connection of MRFC522 module to Rpi.
2 hours later…
627117717.pr
627117717.pr
Apr 1, 16:58
Ok thats good everything else ok?
5 hours later…
tlfong01

tlfong01
2888
Apr 1, 21:44
Well, going slow. (1) Appendix E shows where mfrc522.py and SimpleMFRC522.py are hiding. (2) I don’t want to run the programs in their hiding place, worrying that I might mess up things. Instead made a copy of the two programs in a newly created directory “/home/testMfrc”. Now I am testing my version of Write.py which import mfrc522 and try to write something to the tag.
Now a question: Do you have any experience in OOP (Object Oriented Programming), such as Arduino C++? If you are not experienced in nay OOP language, then I can forget the mrfc Class thing and directly do everything using functions, ie FP (Functional Programming).
Question 2 – Do you have a web link to the NFC module your are playing? Is it a cheapy eBay Version 1? I remember when I bought my Version 3 NFC module, the shop says they are Version 3, and that other shops’ Version 1 are bad.
2 hours later…
627117717.pr

627117717.pr
Apr 1, 23:25
Hi again, i do have experience in OOP C++ but its been a long time since and forgot most of it tbh, and the link i bought it from amamzon is amazon.co.uk/…
Hopefully this helps
18 hours later…
627117717.pr
627117717.pr
Apr 2, 17:00
Any luck with the MFRC522 module tutorial and debugging?
1 hour later…
tlfong01

tlfong01
2888
Apr 2, 18:22
Ah, still walking slowly. A couple of random comments: (1) Your module from Amazon UK looks good quality. I skimmed the comments and found some of them useful – such as that the SimpleMFRC522 is not updated.
I visited the MFRC522 GitHub and found the following IMPORTANT NOTICE: “This library has not being actively updated in almost four years. It might not work as intended on more recent Raspberry Pi devices. You might want to take a look to the open pull-requests and forks to see other implementations and bug-fixes.”
I also found Mario Gómez’s original MFRC522 GitHub version very good. Eg, his read/write program has good documentation, comparing to SimpleMFRC522 which is too brief.

So as I said earlier, I will forget SimpleMFRC522 and directly use Mario Gómez’s original version to learn what he is doing. He also explains about the requirements of SPI-Py and GPIO, both of which I suspect is not standard, and might not be compatible to Rpi4B buster. But I am only 50% sure.

Now I am thinking of writing a very small version of MarioGomez’s MRFC522 class, but not an OO class, instead a couple of python functions. The objective is checking out if a module is basically working. The checking out method is to “ping”, or write something to a MRFC522 register and read back later.
627117717.pr

627117717.pr
11
Apr 2, 18:41
HI that’s what i was orginally doing! using SMIPLEMFRC522 ill try using the more direct version and see how thats goes hopefully works !!
the objective of all of this is to see if the tag can read/write properly ill try the marios code and see if that is any better !!
tlfong01

tlfong01
2888
Apr 2, 19:28
Ha, yes, so I am back to Square 1 – What you were originally doing. Now I have listed the programs I unzipped into a penzu entry for easy reference: penzu.com/p/dff77f0d
But have you already done the SPI loopback check? The reason that you could not read the tag might be because your SPI hardware wiring is broken. If that is the case, you just fix the broken SPI wiring, then your problem should disappear! 🙂 Anyway, late supper time. See you very late this evening or tomorrow. Good luck, cheers.
627117717.pr

627117717.pr
ok nice one mate ill try the loopback right now and update you after!
tried the loopback test
how can i attach a picture to show you?
1 hour later…
tlfong01

tlfong01
Apr 2, 20:54
Ah, to display an image, I usually do this: (1) Use any camera to shoot a 2MB or smaller jpeg image, and copy to a PC Windows directory, (2) click the upload button, browse to the directory and click the image name. I sometimes use imgur.com to upload the images and then paste to the answer in this forum.
627117717.pr
627117717.pr
ok no problem will do so
how would i do this on rasperry pi im new to this thing not sure how to!
tlfong01

tlfong01
2888
Apr 2, 21:20
I did’t catch you. Did you mean how to test Mario Gomez’s program in Rpi. What I am going to do is the following. (1) Download the zipped programs from GitHub to Win10 PC, (2) Unzip to find the three important programs: (a) mfrc522.py, (b) read.py, (c) write.py. In Rpi, create a new directory under Rpi home directory, say “test_mrfc522”. Copy the above three programs in this new directory, (c) I will use Rpi Desktop GUI Thonny IDE to run read01.py.
You might use terminal command something like this. “$ python read01.py” to run it. My modified read.py program is in this penzu: “mfrc programming log V0.7”: penzu.com/p/df27d289.
627117717.pr
627117717.pr
OK great stuff could i download and unzip the file to my pi? from github
tlfong01

tlfong01
2888
Apr 2, 21:49
Ah yes, you can download the zip file (Ref 27 of my answer) to either Windows PC or Rpi. I am a GUI guy and I can do it in GUI Win10 many times faster than in CLI Rpi buster, perhaps vice versa for you. I sometimes use Windows PureFTP or Samba to transfer files between PC and Rpi. But often I just use a USB finger as a courier. I also have a 500GB SSD in Rpi and also in Win10 to back up things, just in case I mess up things. Good luck.
627117717.pr
627117717.pr
Apr 2, 22:33
Thank you ill try !
tlfong01

tlfong01
2888
Ah bed time! I call it a day. Good luck and see you tomorrow.
And before I go –
user image
4 hours later…
627117717.pr
627117717.pr
Apr 3, 2:14
nice one see you tommorow ill try to find a way to sent the code pictures!!!
12 hours later…
tlfong01

tlfong01
2888
Apr 3, 14:20
@627117717.pr Looking forward to your pictures. Two more things. (1) You SPI wiring should not be longer than 1 meter, (2) The rpi.org “Official” loop back program warns that only CLK, MOSI and MISO wiring, but CE0/1 are NOT tested.
To test all 4 signals, you should do a “read after write” test to make sure what is written can be read back. My penzu file includes such a program. The test program I am writing tries to write something to the MRFC device and read back, to make “100%” that the MRFC chips is working OK.
user image
4 hours later…
627117717.pr
627117717.pr
Apr 3, 17:56
Morning
ill try to send the pictiures across today to give you a better idea of where im at!!
627117717.pr

627117717.pr
11
Apr 3, 18:08
the test i did for the spi
came out like this as i cant figure out how to send you the picture it looks like this
spi mode: 0
bits per word: 8
max speed: 500000 Hz (500 KHz)
tlfong01

tlfong01
2888
Good. Looking forward to your pictures. Now I am trying to simplify Mario Gomez’s MFRC522.py, cutting everything except using SPI read write one MFRC522 register. This is to show that the Rpi4B buster SpiDev can read write MFRC522 registers. The I don’t need to compile a special version of SpiDev for this project. I guess it is only the old Rpi 2/3 with old OS like wheezy/jessi/stretch which has outdated SpiDev need a compiled version.
627117717.pr

627117717.pr
11
00 00 00 00 00 00
00 00 00 00 00 00
00 00 00 00 00 00
00 00 00 00 00 00
00
all 0 for some reason do you know why that is by any chance?
tlfong01
tlfong01
Same for GPIO. I read the requirement is GPIO version 0.6.5 which ahd a major upgrade. The old OS with older than V0.6.5 need to compile a standard GPIO.
627117717.pr
627117717.pr
how would i test or upgrade the GPIO?
in order to get the same outcome as you?
tlfong01

tlfong01
2888
Ah, let me see. Appendix E says that if loopback test results good, then second line should begin with 0x40 and end with 0x95. So the poor soul failed the exam 🙂
By the way, do you have a multi-meter to test the resistance between connecting/jumper wires of the SPI interface from Rpi to MFRC522? Remember that MOSI, MISO one side connect to MISO, MOSI other side, ie, Crossing over, NOT direct point to point. Ah supper time, see you later.
tlfong01

tlfong01
2888
Apr 3, 18:34
Ah, I looked at my results in Appendix E and found I made a typo mistake. The ALL ZEROS output means MISO is not connected to MOSI. So you need a multi-meter to check it out. And as the wise guy Warren Buffett says “There is never only one cockroach in the kitchen”. I guess the careless newbie very likely made more mistakes.
By the way, you see my wiring is colour coded, ORANGE for MOSI, YELLOW for MSIO. That means Orange connect to Yellow, etc. Other lines CLK and CS direct connect though.
See you later. Cheers.
627117717.pr
627117717.pr
Apr 3, 18:46
cheer thanks!
3 hours later…
tlfong01

tlfong01
Apr 3, 21:33
@627117717.pr You might like to try my references 28, 29. But I have not tried that. I pip 3 all the time, so should be have all tools python 3 compatible. But if you git clone old tutorials, you might installed python 2 stuff and mess up things.
tlfong01

tlfong01
Apr 3, 21:44
@627117717.pr I forgot to mention that I use my camera for photos. To capture screen, I use the terminal command “scrot”. You can “scrot –help” for details. I also use Win10 PhotoShop to crop and edit images and photos.
17 hours later…
tlfong01

tlfong01
2888
Sat 14:16
@627117717.pr Now I am reading Mario Gomez’s READ.MD one more time, to make sure I did not miss anything important. He says we need to use SPI-Py, which is I am note sure if it is still needed for Rpi4B buster.
user image
6 hours later…
tlfong01

tlfong01
2888
Sat 20:44
I hope you have debugged your SPI wiring and tested SPI loopback OK. After that I think you can go back to square 1 and tried either your first or second tutorial. There is 50% chance that both of them works, though one using python 2 and there other python 3. I am making little progress in try to replace Rpi4B buster’s SpiDev to replace Mario GoMez’s SpiDev and GPIO. So I suspend this part and go back to goggle more to learn. I found many more GitHub versions, but I know too little to tell the difference. Here is one I found interesting pat-odoo/TwoRC522_RPi2-3

(see full text)

1 hour later…
tlfong01

tlfong01
2888
Sat 22:03
If you have finally solved the loop back problem, I suggest you to pip3 install and then python3 to run SimpleMFRC522. It took me less than 10 minutes to install and run a short test program. Please read my updated answer fro more details. Good luck. Cheers.
user image
Please feel me to ask me to explain anything or do more tests.
20 hours later…
627117717.pr
627117717.pr
Sun 17:55
@tlfong01 Hi mate sorry been away for the last day!
NIce one mate ill give that a go and uodate you in the mean time
3 hours later…
tlfong01

tlfong01
2888
Sun 20:39
@627117717.pr No problem and no hurry. So take your time. It is very likely a long project. longer than the corona virus. Just in case you still could not do the SPI Channel 0.0 loopback, because pins broken, or circuit fried, I am testing more CS channel, 0.1, 1.0, 1.1, 1.2 and many more, for you to mess around (See Appendix L).
One reason I am setting up more SPI channels, is because I see many ninja guys are playing with two or more readers at the same time, So I am feeling facing losingly jealous. 🙂
tlfong01

tlfong01
2888
Sun 21:04
@627117717.pr I am trying to remove the SPI and GPIO functions from MG (Mario Gomez)’s MFRC522 Class for the following reasons: (1) MG’s Class is hardcoded to use SPI 0,0, but I want to use at least SPI 0.0, 0.1, 1.0, 1.1, 1.2, five channels for flexibly use 2 or more readers, and for easy pair/swap troubleshooting. (2) MG’s class also hard codes the GPIO pin assignment, start up and clean up times, and I found it annoying to take care of my other external GPIO functions. (See Refs 34, 35)
1 hour later…
tlfong01

tlfong01
2888
Sun 22:05
@627117717.pr Earlier I said my RFID project is very long, because I am thinking of building an attendance system (Ref 37), using MySQL, PHP etc, which I have no experience at all. By the way, do you have any follow up project like me?
Ah bed time, see you tomorrow. Cheers.
1 hour later…
627117717.pr
627117717.pr
Sun 23:21
Nice one cheers pi up my life have a really good guide on how to do the attendeance system btw !
2 days later…
627117717.pr
627117717.pr
Tue 18:01
@tlfong01 thats good stuff maybe if possible you could go through each of the steps once figured out with me so i can get the same result on my side if possible
4 hours later…
tlfong01

tlfong01
2888
Tue 21:44
@627117717.pr Well, so I have described the first step of using SPI loop back as a way to check out the SPI hardware wiring and software initialization are more or less working OK. The second step is to use python shell commands to import SimpleMFRC522, create an object reader and then write something a read back.
As I suggest in the very beginning, it would be nice if you can compile an answer yourself for your own question, alongside my answer. My answer is Rpi4B buster, yours is Rpi3B+ stretch NOOBS etc etc.
Question: Have you any experience in using the data structure “list”? And do you have any vague idea of “scalable” or “SCALA”? I am now going to do scalable python3 list processing of up to 8 MFRC522 modules (Yes, I have 12 in hand.). In case you could not catch up, I think I would just skip the program documentation, and show you how to use it.
Ah almost bed time! So see you tomorrow. PS – my last appendix gives an idea of how I am “pinging” two modules. (If you have never heard of the term “ping”, then you would need a long long time to catch up.
20 hours later…
627117717.pr

627117717.pr
11
Wed 17:31
hi have no experience in data structure “list” i did the first steps as from the pictures you sent installing the pip3 for mfrc522 and the python3 however a message displays up
ImportError: No module named mfrc522
>>> RFIDModule = SimpleMFRC522()
Traceback (most recent call last):
File “<stdin>”, line 1, in <module>
NameError: name ‘SimpleMFRC522’ is not defined
>>> XZ
pi@raspberrypi:~ $ sudo pip3 install mfrc522
Looking in indexes: pypi.org/simplepiwheels.org/simple
Requirement already satisfied: mfrc522 in /usr/local/lib/python3.7/dist-packages (0.0.7)
Requirement already satisfied: RPi.GPIO in /usr/lib/python3/dist-packages (from mfrc522) (0.7.0)
Requirement already satisfied: spidev in /usr/lib/python3/dist-packages (from mfrc522) (3.4)
pi@raspberrypi:~ $ python3
Python 3.7.3 (default, Dec 20 2019, 18:57:59)
[GCC 8.3.0] on linux
Type “help”, “copyright”, “credits” or “license” for more information.

(see full text)

code above ^^^
3 hours later…
tlfong01

tlfong01
2888
Wed 20:48
@627117717.pr Ah, let me see. So your reader can write ‘parcel 1’ and read back. That means read and write are OK. Next step is to use python 3 to execute read.py and write.py. BTW did you use a multi-meter to solve your last problem?
@627117717.pr And please tell me more about your programming background. Eg did you have any experience to us Rpi python to blink a LED and read a button? Or have you played with Arduino before? Do you have a multi-meter and know how to measure the resistance?. And have you heard of “Ohm’s Law”. “Pull up” etc?
20 hours later…
627117717.pr

627117717.pr
11
yst 17:03
hey have no experience in rpi python,
also because i copied what you wrote and put into the terminal obviously changing the nfc ro rfid module, for some reason it seams not to work even with python
pi@raspberrypi:~ $ python3
Python 3.7.3 (default, Dec 20 2019, 18:57:59)
[GCC 8.3.0] on linux
Type “help”, “copyright”, “credits” or “license” for more information.
>>> from mfrc522 import SimpleMFRC522
>>> reader = SimpleMFRC522()
/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)
>>> reader.write (‘parcel1’)
(1223443245, ‘parcel1’)
>>> reader.read()

(see full text)

python 3 i think thats correct
pi@raspberrypi:~ $ python
Python 2.7.16 (default, Oct 10 2019, 22:02:15)
[GCC 8.3.0] on linux2
Type “help”, “copyright”, “credits” or “license” for more information.
>>> from mfrc522 import SimpleMFRC522
Traceback (most recent call last):
File “<stdin>”, line 1, in <module>
ImportError: No module named mfrc522
>>> RFIDModule = SimpleMFRC522()
Traceback (most recent call last):
File “<stdin>”, line 1, in <module>
NameError: name ‘SimpleMFRC522’ is not defined
>>> XZ
[3]+ Stopped python

(see full text)

python ^ not correct not sure where i went wrong
2 hours later…
tlfong01

tlfong01
2888
yst 18:50
@627117717.pr I have no experience in python 2. So I am not able to debug. I would suggest you to use python 3 only, like this: (1) Use a new SD card, (2) pip 3 mfrc522, then play with python 3 only. Do not install any python 2 software. And his: (1) Use a new SD card, suing full version , NOT NOOBS, (2) pip 3 mfrc522, then play with python 3 only. Do not install any python 2 software, or rpi buster will be confused.
You have not told everyboy if you are using pi3, 3+, or 3B+, and wheezy or jessie. Use “$ date” and “$ unname – a” so everybody notes your configuration, without guessing. Also compile your answer in steps, so everybody can follow your steps.
tlfong01
tlfong01
yst 19:04
@627117717.pr If you insist to use python 2, you need to set the “path” variable to your python 2 software directories. See Ref 42 for more details.
627117717.pr

627117717.pr
yst 19:53
pi@raspberrypi:~ $ uname -a
Linux raspberrypi 4.19.97-v7+ #1294 SMP Thu Jan 30 13:15:58 GMT 2020 armv7l GNU/Linux
hi mate this is what came up
tlfong01

tlfong01
2888
yst 20:31
@627117717.pr It would be nice if you can provide this info by editing your question, or in your own answer. So you are still using NOOBS. If you browse around, you might find guys recommending NOT to use NOOBS, unless you wish to try other linux versions, say Ubuntu 19. But are you? I am just using uname as an example. Actually I am using a couple more grep commands so that other friends can help.
My other commands include checking chip version 4B 4GB v1.2, free memory, buster release 2020feb13. If you give me the rpi 4.19, I need to cross check what release it it, and sometimes that matters, and wasted my hours to found out finally.
15 hours later…
tlfong01

tlfong01
2888
12:00
@627117717.pr I read the reviews of the Mario Gomez’s library and confirmed that (1) The software in general is out of date, and need to be modified, (2) SPI-py required, (3) Some modules do not have pullup at Reset and Interrupt pin, and therefore might need external ones. (4) MOSI and MISO wired should be “direct feed through connect”, NOT “crossing over”.
Errata and Appolgy – Earlier I wrongly warned that the MOSI and MSIO wires should be crossing over, which is normally used in other SPI/UART devices. Unfortunately I can NO LONGER edit/correct the old chat record. So be warned that a couple of other things I mentioned in this chat room are simply wrong, if not just misleading. As a remedy, I am trying my best to update my “interactive” and “live” answer and penzu reading/experimenting log.
.END

Categories: Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: