1AdityaX / mfrc522-python

The mfrc522-python library is used to interact with RFID readers that use the MFRC522 chip interfaced with a Raspberry Pi.
https://pypi.org/project/mfrc522-python/
GNU General Public License v3.0
13 stars 4 forks source link

Script hangs/freezes for 700 seconds if no tags are presented when script starts #4

Closed psamtam closed 4 months ago

psamtam commented 4 months ago

Hi. I am trying to read the tag with the MFRC522 reader. Here is my problem:

If I put the tag on the reader before executing the code, it works; If I put the tag on the reader after executing the code, it hangs/freezes for ~700 seconds.

Here is my code:

from mfrc522 import MFRC522
import datetime

print(f"script starts: {datetime.datetime.now()}")

reader = MFRC522() 

status =  None

while status != reader.MI_OK:
    (status, TagType) = reader.Request(reader.PICC_REQIDL)
    print(f"{datetime.datetime.now()} | Status: {status}")

print(f"script ends: {datetime.datetime.now()}")

Here is the output when i put the tag on the reader before executing the code:

script starts: 2024-04-19 22:56:56.567609
2024-04-19 22:56:56.923224 | Status: 0
script ends: 2024-04-19 22:56:56.923404

Here is the output when I put the tag on the reader after executing the code:

script starts: 2024-04-19 22:57:23.731866
2024-04-19 23:09:04.781130 | Status: 2
2024-04-19 23:09:05.132804 | Status: 0
script ends: 2024-04-19 23:09:05.133038

I was wondering if this causes the problem: (Screenshot of MFRC522.py) image

Reasons:

  1. 2000 * 0.35s = 700 seconds
  2. It returns 2, which is MI_ERR, which is the default state of MFRC522_ToCard

If you have any idea about this, please let me know. Thanks!!

P.S. I am running the script on a Raspberry Pi Zero 2 W (32-bit Raspberry Pi OS Lite)

psamtam commented 4 months ago

What I actually want to do is:

  1. Start the script
  2. Keep checking if any tags are presented.
  3. If a tag is presented, log the tag ID.
  4. Sleep x seconds.
  5. Back to (2)

Ideal pseudocode:

reader = BasicMFRC522()
while True:
  id = read_id_no_block()
  if not id:
    continue
  log the id
  sleep(x seconds)

Is this a good approach?

psamtam commented 4 months ago

Update: I git clone the repository to my local storage, create a setup.py, and run sudo python setup.py install and it works. It doesn't hang.

setup.py:

from setuptools import setup, find_packages

setup(
    name='mfrc522',
    version='0.0.7+1',
    description='Based on mfrc522-python 0.0.7',
    packages=find_packages('src'),
    package_dir={'': 'src'},
    install_requires=[
        'spidev==3.6',
    ],
)

Still not sure why using the package with pip install mfrc522-python does not work. But if it works, it works (?)