Ash-Hale
Posts: 6
Joined: Wed Jan 17, 2018 2:35 pm

Python RFID UID code

Wed Jan 17, 2018 3:04 pm

I have recently wired up a raspberry pi 3 model B to a breadboard. The physical setup is attached in a diagram.
Basically what I am wanting to do is make the raspberry pi turn on an LED and make a buzzer buzz when an RFID card or Key fob is scanned on the RC522 RFID reader. I have installed the SPI-Py library and I have downloaded the pi-RC522 files using github.com.

However I am struggling with the code and what file it goes into to execute it. Can anyone provide me with some code that will make the raspberry pi do what I want it to? And could I have some advice on where to save it and how to execute the code within the file.

thank you in advance, Ash

User avatar
topguy
Posts: 5942
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: Python RFID UID code

Wed Jan 17, 2018 4:36 pm

Its quite helpful if you link exactly to the code you download, just assume that there are at least 20 people that has made code for Rc522 or forked code from someone else.

I took a look at this : http://www.instructables.com/id/RFID-RC ... pberry-Pi/
Which led me to this code: https://github.com/mxgxw/MFRC522-python
Still not something I consider "official" RC522 code but something that seems to fit your setup.

My question is then, can you run the "read.py" example and does it work ? Or are you unfamiliar with writing /running Python code in general ?

Ash-Hale
Posts: 6
Joined: Wed Jan 17, 2018 2:35 pm

Re: Python RFID UID code

Thu Jan 18, 2018 1:45 pm

Thank you for the links to websites. Yes I am fairly new to python code. I have already used code from the links you have replied with. But thank you anyway. The error message I got from trying to execute that code was 'no such file or directory' My initial thought was that I had got the file path wrong so after checking it and double checking it I made sure it was correct. To which it said the exact same thing again.

I have already got the RFID card reader working using python code, however I now want to do something more. When I scan the RFID card on the reader I want to make an LED light come on a buzzer buzz. I have not found any code that would enable me to do this.

User avatar
B.Goode
Posts: 8987
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Python RFID UID code

Thu Jan 18, 2018 1:55 pm

I want to make an LED light come on a buzzer buzz. I have not found any code that would enable me to do this.
The gpiozero Python library module comes with lots of support for working with buttons, LEDs and buzzers, and the documentation includes lots of simple examples which it calls Recipes.

https://gpiozero.readthedocs.io/en/stable/recipes.html

User avatar
topguy
Posts: 5942
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: Python RFID UID code

Thu Jan 18, 2018 2:18 pm

The error message I got from trying to execute that code was 'no such file or directory' My initial thought was that I had got the file path wrong so after checking it and double checking it I made sure it was correct. To which it said the exact same thing again.
Again its very important that you give us exact information, preferrably cut and paste text directly from the terminal so we can see what you do and which errors you get. Paths and access rights to files are one of the first hurdles people seem to stumble on.

I might guess that you try to run the .py file directly and this normally works if the file has "#!/usr/bin/env python" as the first line. What is not obvious is that the "executable" flag also has to be set on the file or else bash will return the error you quoted.

Code: Select all

chmod u+x read.py

Ash-Hale
Posts: 6
Joined: Wed Jan 17, 2018 2:35 pm

Re: Python RFID UID code

Thu Jan 18, 2018 5:00 pm

Here is the code I have been using. As I have mentioned the error message I am getting when I try and execute this file is: No such file or directory exists. The issue I am having is that I cannot find any python code to be able to make the LED blink and the buzzer buzz when I scan the RFID card on the reader.

#!/usr/bin/env python
# -*- coding: utf8 -*-

import RPi.GPIO as GPIO
import MFRC522
import signal
import Buzzer
import LED

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)

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

# Welcome message
print "Welcome to the MFRC522 data read example"
print "Press Ctrl-C to stop."

# 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:
Buzzer.setup()
LED.setup()
Buzzer.beep()
LED.led()

# Print UID
print "Card read UID: "+str(uid[0])+","+str(uid[1])+","+str(uid[2])+","+str(uid[3])
if uid[0]==150 and uid[1]==110 and uid[2]==1 and uid[3]==164:
print "Welcom, Jason :)"

if uid[0]==133 and uid[1]==94 and uid[2]==233 and uid[3]==171:
print "Welcom, Lucy :)"

# This is the default key for authentication
key = [0xFF,0xFF,0xFF,0xFF,0xFF,0xFF]

# Select the scanned tag
MIFAREReader.MFRC522_SelectTag(uid)

# Authenticate
status = MIFAREReader.MFRC522_Auth(MIFAREReader.PICC_AUTHENT1A, 8, key, uid)

# Check if authenticated
if status == MIFAREReader.MI_OK:
MIFAREReader.MFRC522_Read(8)
MIFAREReader.MFRC522_StopCrypto1()
else:
print "Authentication error"

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Python RFID UID code

Thu Jan 18, 2018 5:32 pm

I'm not going to fix your issue but I'd like to make a few comments:

1 - please, please, please, when posting code - use the code tags to preserve indentation.
2 - I'd like to offer a couple of suggestions to tidy up a couple of lines that look a bit ugly to me:
This

Code: Select all

print "Card read UID: "+str(uid[0])+","+str(uid[1])+","+str(uid[2])+","+str(uid[3])
could be rewritten as

Code: Select all

print "Card read UID: {0},{1},{2},{3}".format(*uid)
and this

Code: Select all

if uid[0]==150 and uid[1]==110 and uid[2]==1 and uid[3]==164:
could be rewritten as

Code: Select all

if uid[:4] == [150,110,1,164]:
 
# This should make it quicker to change the values you're looking to compare
# Note: I've used [:4] because it looks like the uid contains the checksum as the final value in the list
# so you only want to compare the first four values
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

User avatar
B.Goode
Posts: 8987
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Python RFID UID code

Thu Jan 18, 2018 6:24 pm

I am confused about what you have written and what you are seeking help with.

You wrote earlier:
I have already got the RFID card reader working using python code,
But then you refer to some code and say:
Here is the code I have been using. As I have mentioned the error message I am getting when I try and execute this file is: No such file or directory exists.
What happens if you run the second script in exactly the same way as the first? Please include the command you are entering, and the full text of any response from the RPi.

Ash-Hale
Posts: 6
Joined: Wed Jan 17, 2018 2:35 pm

Re: Python RFID UID code

Fri Jan 19, 2018 10:02 am

What I have already done is write code so that when I scan a card on the RFID reader it shows that it has been detected, meaning I have had a successful read. However I now want to make an LED light come on when I scan the RFID card. All I am looking for is to be pointed in the right direction so that I can find some python code that will work. Whether that be through a website that has worked for someone else or from someone who has done it themselves.

My previous response was code that I had used that didn't work. The command I used was: sudo Read_test.py to which either hasn't worked or I have previously got the error message "no such file or directory exists"

Obviously I probably will have to manipulate the code slightly to suit my physical setup and wiring but I will sort that one myself if someone is able to provide me with something that might help.

Ash-Hale
Posts: 6
Joined: Wed Jan 17, 2018 2:35 pm

Re: Python RFID UID code

Fri Jan 19, 2018 10:16 am

Correction: the command I used was "sudo python Read_test.py"

User avatar
topguy
Posts: 5942
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: Python RFID UID code

Fri Jan 19, 2018 10:51 am

Code: Select all

[email protected]:~ $ cat > test.py
#!/usr/bin/env python
# -*- coding: utf8 -*-

[email protected]:~ $ test.py
-bash: test.py: command not found

[email protected]:~ $ ./test.py
-bash: ./test.py: Permission denied

[email protected]:~ $ sudo test.py
sudo: test.py: command not found

[email protected]:~ $ sudo python test2.py 
python: can't open file 'test2.py': [Errno 2] No such file or directory

# working 
[email protected]:~ $ python test.py 
[email protected]:~ $ sudo python test.py 

[email protected]:~ $ chmod u+x test.py
[email protected]:~ $ ./test.py
[email protected]:~ $ sudo ./test.py
These are the common pitfalls when trying to run a python script. None of the errors matches what you reported exactly but one is close.
I'm just trying to exclude problems that are not related to the code itself so you can focus on the code.

Ash-Hale
Posts: 6
Joined: Wed Jan 17, 2018 2:35 pm

Re: Python RFID UID code

Fri Jan 19, 2018 3:06 pm

Right after working through the mess that is the code I have used I have whittled the errors down and I have come across one that I do not know how to fix.

so after running the file Read_test.py with the command sudo python Read_test.py

I got this error message:

Traceback (most recent call last):
File "Read_test.py", line 5, in <module>
import MFRC522
ImportError: No module named MFRC522

I sort of understand this error message but I don't know how to fix it.

User avatar
topguy
Posts: 5942
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: Python RFID UID code

Fri Jan 19, 2018 3:22 pm

You did download ( or check out with Git ) a library called MFRC522 at some time didn't you ?

And probably the test program you ran also had "import MFRC522" in it, and that worked right ?
That was because there was a file called MFRC522.py in the same directory as the test script.

I guess your new script is in another directory and Python dont know how to find MFRC522.py anymore.
The simplest is just to copy MFRC522.py to the new directory.

Return to “Python”