GrovePi + Grove - 125KHz RFID Reader + Python

Hi,

I bought a Raspberry Pi in combination with the GrovePi+
Now I’m trying to make the Grove - 125KHz RFID Reader to work in Python.

I’m super new to programming. Many attempts to find a good tutorial/sample code online has lead to nothing.
If I’m correct, the module should be plugged into the RPISER port of the GrovePi+.

I also found this forum topic:


but the provided example seems to be for Arduno. I’m using a Raspberry Pi

I’m using the following:

  • Raspberry Pi 3
  • GrovePi+
  • Grove - 125KHz RFID Reader

Hi @jeroen_schuiten,

We don’t have example programs for the Grove RFID Reader at the moment.

Please give us a day or two to come up with a solution for you.
We will reach back to you.

Does this sound good to you?

Thank you!

Hi @jeroen_schuiten,

As I don’t have a Grove RFID Reader on my hands I can only give you a “rough” estimation of what you should have.
You will need to have the pyserial package installed in order to use the following python script.
In this script, when I message is received on the serial line, it’s gets printed in the console and then the program ends.

import serial
import time

rpiser = serial.Serial('/dev/ttyAMA0', baudrate=9600, timeout=0)
rpiser.flush()

while rpiser.inWaiting() == 0:
    time.sleep(0.005)

serial_data = rpiser.readline()
print(serial_data)

I think this script should work for your needs.

Thank you!

I realize this is an old post but if someone could help that would be great.

I have a Raspberry Pi B+ connected to Grove - 125kHz RFID Reader(UART) via a GrovePi Board on RPISER. I have used the above code but replaced /dev/ttyAMA0 with /dev/ttyS0.

I want the python3 code to continually check for a tag and print the tag id when found.

At the moment I am getting inconsistent and unuseable results.

KeyboardInterrupt
>>> 
============== RESTART: /home/pi/mygrovepi/grove_125kHz_rfid.py ==============
b'FF0E8'
>>> 
============== RESTART: /home/pi/mygrovepi/grove_125kHz_rfid.py ==============
Traceback (most recent call last):
  File "/home/pi/mygrovepi/grove_125kHz_rfid.py", line 7, in <module>
    while rpiser.inWaiting() == 0:
  File "/usr/lib/python3/dist-packages/serial/serialutil.py", line 572, in inWaiting
    return self.in_waiting
  File "/usr/lib/python3/dist-packages/serial/serialposix.py", line 456, in in_waiting
    s = fcntl.ioctl(self.fd, TIOCINQ, TIOCM_zero_str)
OSError: [Errno 5] Input/output error
>>> 
============== RESTART: /home/pi/mygrovepi/grove_125kHz_rfid.py ==============
b'3B'
>>> 
============== RESTART: /home/pi/mygrovepi/grove_125kHz_rfid.py ==============
b'9ABF'
>>> 
============== RESTART: /home/pi/mygrovepi/grove_125kHz_rfid.py ==============
b''
>>>

Hi @anwar,

That looks like a problem with the serial port not being picked up. May I suggest you check this tutorial on setting up the serial port on the Raspberry Pi? It’s very useful and I use it sometimes too:

Let me know if you make this thing work.

Thank you!

Hi Robert,

Thank you for your reply. This was an interesting article and I decided that because I am not using Bluetooth in my project I would add the following lines to my config.txt file

enable_uart=1 # this was already set by me

pi3-disable-bt

then I ran the test code shown below. It seems to be okay but the print statement returns the word string not the id of the tag. I am not familiar with python, can you advise how I can get the tag id?

Kind regards

Anwar

import serial

For PiB+ 2, Zero use:

ser = serial.Serial(’/dev/ttyAMA0’, 9600, timeout=1)

For Pi3 use

#ser = serial.Serial(’/dev/ttyS0’, 9600, timeout=1)

while True:

string = ser.read(12)

if len(string) == 0:

print (“Please wave a tag”)

else:

string = string[1:11] # Strip header/trailer

print (“string”),string

if string == ‘E0043DBB52’:

print (“hello Joe, what do you know?”)

I am having issues using this code with a Raspberry pi 4 and a Grove 125kHz reader. Before actually reading the tag, the first few attempts result in many printed strings looking like 'x80\x00\xf8 etc.
After some time and some attempts, the RFID ID does get read, but then never again. The system seems to fail to read it again.
Has anyone else encountered this issue before?
Thanks for any help!

1 Like

How is it connected? i2c? Serial?

Can you provide a sample of the code you are using?

Hy there. I’m having the same issues like @csilla92. I’m really new in to this and I was really happy to get something but the result is not that what I want.
I can read the tags once or twice, sometimes 3 times but then it’s over. Also the little green LED on the Reader is not working anymore. If I stop and start the script again, It works again for a few times.

This is my Hardware

  • RP4
  • GrovePi+
  • Grove 125kHz rfid Reader

This is the python code I use:

import serial
import time

rpiser = serial.Serial("/dev/ttyS0", 9600, timeout=1)
rpiser.flush()

print("active, waiting...")

while True:

    rng = rpiser.read(100)
    if len(rng) != 0:
        print (rng)

Here is the result:

pi@raspberrypi:~/Dexter/GrovePi/Software/Python $ sudo python rfid_test2.py 
active, waiting...

?x???x?x?????????????????
?&?V?fF6?f

The first string was empty. the fourth killed it.

Is anybody out there with a solution for my problem?

1 Like

I don’t know anything about the RFID tags, but I have a couple ideas to try:

  • The code samples show reading 12 characters, you are attempting to read 100 chars?
  • What happens if you perform a flush after every read?
  • Perhaps try a “throw-away read” or a “throw-away read and flush” will ensure first time is not empty
  • Try putting the inWaiting() while loop before the read(12) statement inside the while True loop
1 Like

Thank you for this hints @cyclicalobsessive

I tried diffrent numbers. I had no plan at the begining then I read somewhere that these are the bytes to read. A unique ID of a rfID seems to have 4 to 8 bytes, debends on the tag-technology (see here). With reading 12 I see no difference.

I tried also to flush after the read, but the script finished after the first try looking like this

pi@raspberrypi:~/Dexter/GrovePi/Software/Python $ sudo python3 rfid_new.py 
active, waiting...
b''
b''
b''
b''
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'

Ok, I did this.

I got a new question. There is a difference if i execute the script with sudo python rfid_new.py or sudo python3 rfid_new.py. Which one I should use?

Here is my actual code:

import serial
import time

rpiser = serial.Serial("/dev/ttyS0", 9600, timeout=1)
rpiser.flush()

print("active, waiting...")

while rpiser.inWaiting() == 0:
    time.sleep(0.005)
    rng = rpiser.read(12)
    if rng != "":
        print (rng)

If I execute with Python 3 it prints a b'' every time if it passes the while loop. Executing it with the older Version of Python writes only active, waiting... and prints just a line if I wave a tag over the antenna.

Me too :smiley: and that’s why I have no idea what kind of data I’m expecting as result. I thought it’s a unique ID.

Does anybody can help me?

1 Like

Ok, close.

A couple things to think about:

  • since you are starting out new with python, I suggest you stick with the newer Python3
    Especially since the SerialAPI that you are using is different between the versions:
    Python2.x: rpiser.inWaiting() returns number of bytes ready in serial stream
    Python3.x: rpiser.in_waiting returns number of bytes ready in serial stream
    (Python2.x it is a method, but in Python3 it is a property so no parenthesis)
  • Basic flow should probably be:
    • Main Loop
      • wait until bytes in the serial buffer
      • read the bytes
      • print the bytes
    • end of loop
  • “sudo” is needed to access the serial port? no problem, just wondering.

 

I would be interested in what this does:

#!/usr/bin/env python3

# FILE: try1.py

# USAGE:  sudo python3 try1.py 

# REFERENCES:  
#  Serial API: https://pyserial.readthedocs.io/en/latest/pyserial_api.html
#  Grove RFID Sensor: https://wiki.seeedstudio.com/Grove-125KHz_RFID_Reader/

import serial
import time

rpiser = serial.Serial("/dev/ttyS0", 9600, timeout=1)
rpiser.flush()

while True:
    print("active, waiting...")

    # wait until first byte is seen in the serial stream
    while rpiser.in_waiting == 0:        # note that in_waiting is a property in Python3
        time.sleep(0.005)        

    # Bytes coming in!  Wait until the 1s timeout to get them all
    time.sleep(1.0)
    bytes_available = rpiser.in_waiting    
    print("Tag Detected with {} bytes ready".format(bytes_available))
    rng = rpiser.read(bytes_available)
    print("RFID Info:{}:".format(rng))
    print("RFID Info without first byte:{}:".format(rng[1:]))

    

Updated with missing closing “]” in …rng[1:]…

1 Like

Yes, if i don’t sudo it I got a permission error.

I tried it with you code.

this is what I got:

pi@raspberrypi:~/Dexter/GrovePi/Software/Python $ sudo python3 rfid_new2.py
active, waiting...
Tag Detected with 0 bytes ready
RFID Info:b'':
RFID Info without first byte:b'':
active, waiting...

after three times it crashed again.

Maybe the tags are empty, I don’t know!?

1 Like

Looks like your original code at least was reading something. The inWaiting() and in_waiting is not working in this case, or we’re not using it right.

I’m out of ideas as well.

1 Like

But you don’t think that the tag is empty? Should it print something anyway?

1 Like

what does this do?:

#!/usr/bin/env python3

# FILE: try2.py

# USAGE:  sudo python3 try2.py 

# REFERENCES:  
#  Serial API: https://pyserial.readthedocs.io/en/latest/pyserial_api.html
#  Grove RFID Sensor: https://wiki.seeedstudio.com/Grove-125KHz_RFID_Reader/

import serial
import time

rpiser = serial.Serial("/dev/ttyS0", 9600, timeout=1)
# rpiser.flush()  # this is for writing, don't think it is needed when only reading

while True:
    rng = rpiser.read(100)
    if len(rng) != 0:
        print("\n")
        print("Tag Detected {} bytes".format(len(rng)))
        print("RFID Info--{:02x}--".format(rng))
    else:
        print("active, waiting...",end="")   # print w/o scrolling
1 Like

Thank you for your new Script.

Some good news for the start of the new week. I just googled a lot during the last days :slight_smile: I found something to configure the serial port. I think it’s working now with your old script you sent. And also the script is not crashing after 3 trys.

here the results I got:

pi@raspberrypi:~/Dexter/GrovePi/Software/Python $ sudo python3 rfid_new2.py 
active, waiting...
Tag Detected with 15 bytes ready
RFID Info:b'\xfe\xff\xff\xfe\xfe\xff\xff\xfe\xfe\xff\xff\xff\xff\xfe\xff':
RFID Info without first byte:b'\xff\xff\xfe\xfe\xff\xff\xfe\xfe\xff\xff\xff\xff\xfe\xff':
active, waiting...
Tag Detected with 15 bytes ready
RFID Info:b'\xfe\xff\xff\xfe\xfe\xfe\xff\xff\xfe\xff\xfe\xff\xff\xfe\xff':
RFID Info without first byte:b'\xff\xff\xfe\xfe\xfe\xff\xff\xfe\xff\xfe\xff\xff\xfe\xff':
active, waiting...
Tag Detected with 13 bytes ready
RFID Info:b'\xff\xfe\xff\xfe\xff\xff\xfe\xff\xfe\xff\xfe\xff\xfe':
RFID Info without first byte:b'\xfe\xff\xfe\xff\xff\xfe\xff\xfe\xff\xfe\xff\xfe':
active, waiting...
^CTraceback (most recent call last):
  File "rfid_new2.py", line 22, in <module>
    time.sleep(0.005)        
KeyboardInterrupt

I’m two steps further minimum :smiley:

Now I have the problem to intrepret the result. I tried with

print(rng.decore('utf-8'))

I got the message:

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte

Here again the code I used:

#!/usr/bin/env python3

# FILE: try1.py

# USAGE:  sudo python3 try1.py 

# REFERENCES:  
#  Serial API: https://pyserial.readthedocs.io/en/latest/pyserial_api.html
#  Grove RFID Sensor: https://wiki.seeedstudio.com/Grove-125KHz_RFID_Reader/

import serial
import time

rpiser = serial.Serial("/dev/ttyS0", 9600, timeout=1)
rpiser.flush()

while True:
    print("active, waiting...")

    # wait until first byte is seen in the serial stream
    while rpiser.in_waiting == 0:        # note that in_waiting is a property in Python3
        time.sleep(0.005)        

    # Bytes coming in!  Wait until the 1s timeout to get them all
    time.sleep(1.0)
    bytes_available = rpiser.in_waiting    
    print("Tag Detected with {} bytes ready".format(bytes_available))
    rng = rpiser.read(bytes_available)
    print("RFID Info:{}:".format(rng))
    print("RFID Info without first byte:{}:".format(rng[1:]))
    # print(rng.decode('utf-8'))

It would be great if you have still any ideas! But already a huge thank you!!

2 Likes

It is extremely likely that the bytes are 7-bit binary with a parity bit, or the ones/twos complement of the correct value.

RFID tags return, (what should be), a unique value for the tag.

Do you have additional tags?

Do you have any documentation that tells you what the internal serial number should be? That will help you understand the data received.

You should also have a datasheet for both the tag and the reader as some of the bytes may be status bytes.

1 Like

The spec mentions a jumper for the data format. I think your jumper should not be to the right, meaning is it “to the left”?

Not in “W” Weigand mode?

1 Like

Hey! Thank you all for your help.

Of course I tried all. And of course I also tried to switch the Jumper. And finally of course I forgot to put it back.
The Jumper is now on the left side again and the result is much diffrent:

pi@raspberrypi:~/Dexter/GrovePi/Software/Python $ sudo python3 rfid_new2.py 
active, waiting...
Tag Detected with 14 bytes ready
RFID Info:b'\x0214005AD657CF\x03':
RFID Info without first byte:b'14005AD657CF\x03':
active, waiting...
^CTraceback (most recent call last):
  File "rfid_new2.py", line 23, in <module>
    time.sleep(0.005)        
KeyboardInterrupt

Seems like I got the ID of the RFID :slight_smile: or what do you think?

2 Likes