0

For university I have to connect multiple RFID readers to my RasPi. Ideally 8 at a time. However after weeks of testing I have not managed to make it work. I figured out that the setmode GPIO.BOARD allows me to talk to all the pins I want and set them high for example. But I have been unable to read any data from a RFID reader other then when the SDA is connected to pin 24. Any other pin even when declared in the script does not seem to work. I tried the script from Alex Berliner in this post, but without any luck. here

This is my current script which also does not seem to be working:

import RPi.GPIO as GPIO
from mfrc522 import MFRC522
import spidev
import time

class RFIDReader: def init(self, rst_pin, cs_pin, spi_bus=0, spi_device=0): self.rst_pin = rst_pin self.cs_pin = cs_pin self.spi_bus = spi_bus self.spi_device = spi_device GPIO.setup(rst_pin, GPIO.OUT) print(rst_pin) self.mfrc522 = MFRC522(self.spi_bus, self.spi_device, self.rst_pin, self.cs_pin)

def read(self):
    print("reading: rst_pin")
    (status, TagType) = self.mfrc522.MFRC522_Request(self.mfrc522.PICC_REQIDL)
    if status == self.mfrc522.MI_OK:
        (status, uid) = self.mfrc522.MFRC522_Anticoll()
        if status == self.mfrc522.MI_OK:
            uid_str = ".".join([str(i) for i in uid])
            print(uid_str)
            return uid_str
    return None

Setup GPIO

GPIO.setmode(GPIO.BOARD)

Initialize readers

reader1 = RFIDReader(rst_pin=24, cs_pin=0) # CE0 reader2 = RFIDReader(rst_pin=26, cs_pin=1) # CE1

try: while True: uid1 = reader1.read() if uid1: print(f"Reader 1 detected UID: {uid1}")

    uid2 = reader2.read()
    if uid2:
        print(f"Reader 2 detected UID: {uid2}")

    time.sleep(0.1)

except KeyboardInterrupt: GPIO.cleanup() finally: GPIO.cleanup()

I have another test script which also only reads from pin 24:

from mfrc522 import SimpleMFRC522  
import RPi.GPIO as GPIO  
import signal  
import spidev  
import sys  
import time

class NFC():
def init(self, bus=0, device=0, spd=1000000):
self.reader = SimpleMFRC522()
self.close()
self.boards = {}
self.bus = bus
self.device = device
self.spd = spd

def reinit(self):  
    self.reader.READER.spi = spidev.SpiDev()  
    self.reader.READER.spi.open(self.bus, self.device)  
    self.reader.READER.spi.max_speed_hz = self.spd  
    self.reader.READER.MFRC522_Init()  

def close(self):  
    self.reader.READER.spi.close()  

def addBoard(self, rid, pin):  
    self.boards[rid] = pin  
    GPIO.setup(pin, GPIO.OUT)  
    print(pin)  
    print(self.boards)  

def selectBoard(self, rid):  
    if not rid in self.boards:  
        print("readerid " + rid + " not found")  
        return False  

    #for loop_id in self.boards:  
        #GPIO.output(self.boards[loop_id], loop_id == rid)  
    return True  

def read(self, rid):  
    if not self.selectBoard(rid):  
        return None  
    self.reinit()  
    rid, val = self.reader.read_no_block()  
    self.close()  
    return val  

def write(self, rid, value):  
    if not self.selectBoard(rid):  
        return False  
    self.reinit()  
    self.reader.write_no_block(value)  
    self.close()  
    return True  

clean up gpios on ctrl+c

def signal_handler(sig, frame):
GPIO.cleanup()
print()
sys.exit(0)

def main():
signal.signal(signal.SIGINT, signal_handler)
GPIO.setmode(GPIO.BOARD)

nfc = NFC()  

readers = [  
    # rid, pin  
    ("reader1", 24),  
    ("reader2", 26),  
]  

for r in readers:  
    nfc.addBoard(r[0], r[1])  

while True:  
    for r in readers:  
        try:  
            r_name = r[0]  
            if r[1] == 24:  
                GPIO.output(24, GPIO.LOW)  
                GPIO.output(26, GPIO.HIGH)  
                print("reading 24")  
            else:  
                GPIO.output(24, GPIO.HIGH)  
                GPIO.output(26, GPIO.LOW)  
                print("reading 26")  
            data = nfc.read(r_name)  
            if data:  
                print(f"{r_name}: {data}")  
            else:  
                print(f"No data from {r_name} {r[1]} detected")  
        except Exception as e:  
            print("e", e)  

GPIO.cleanup()  

if name == "main":
main()

I would really appreciate any help or hints why this is not working.

Wiring:
First MFRC522    Raspberry Pi Zero
---------------------------
VCC              Pin 1 (3.3V)
GND              Pin 6 (Ground)
RST              Pin 22 (GPIO 25)
SDA              Pin 24 (GPIO 8, CE0)
MOSI             Pin 19 (GPIO 10)
MISO             Pin 21 (GPIO 9)
SCK              Pin 23 (GPIO 11)

Second MFRC522 Raspberry Pi Zero

VCC Pin 17 (3.3V) GND Pin 20 (Ground) RST Pin 15 (GPIO 22) SDA Pin 26 (GPIO 7, CE1) MOSI Pin 19 (GPIO 10) MISO Pin 21 (GPIO 9) SCK Pin 23 (GPIO 11)

Greenonline
  • 2,969
  • 5
  • 27
  • 38

0 Answers0