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
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 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''
>>>
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:
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)
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!
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)
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
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 and that’s why I have no idea what kind of data I’m expecting as result. I thought it’s a unique ID.
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:]))
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...
#!/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
Some good news for the start of the new week. I just googled a lot during the last days 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
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!!
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 or what do you think?