Black Lives Matter - Action and Equality. ... Adafruit is open and shipping.
0

PN532 using IRQ pin
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

PN532 using IRQ pin

by shaul on Thu Jul 02, 2020 7:12 am

Hi. We are attempting to read multiple PN532 cards using the IRQ pin to know which one has a tag near it. What we are seeing is that the IRQ pin does go low as expected, but that happens only once. The next time(s) a tag is brought to the reader, the IRQ stays high.
What are we missing? Help would be much appreciated.

shaul
 
Posts: 2
Joined: Thu Jul 02, 2020 7:05 am

Re: PN532 using IRQ pin

by siddacious on Thu Jul 02, 2020 11:24 am

Please post your full code using the '[code]' tags for formatting as well as clear, well lit photos that show all of the connections. There's a lot that could be going on in hardware or software so we'll need to see whats up with both to assess the situation

siddacious
 
Posts: 278
Joined: Fri Apr 21, 2017 3:09 pm

Re: PN532 using IRQ pin

by shaul on Sun Jul 05, 2020 11:24 am

Thanks!

Code: Select all | TOGGLE FULL SIZE
############### RFIDReader.py ###############
import threading
from MCPPNLIB import ReadersSet
from MCP23S17 import MCP23S17
import binascii
import sys
import time

# Setup  PN532
MISO = 19 #BCM pins on RPI
MOSI = 20 #BCM pins on RPI
SCLK = 21 #BCM pins on RPI
CS = [6] # BCM pins on RPI
MCPIRQ = [8] # MCP pin 0 = GPA0, 8=GPB0 ....(0-15)

class RFIDReader(threading.Thread):
  def __init__(self, group=None, target=None, name=None, args=(), kwargs=None, verbose=None):
    super(RFIDReader,self).__init__(group=group, target=target, name=name)
    self.mcp = MCP23S17(bus=0x00, pin_cs=0x00, device_id=0x01)
    self.mcp.open()
    self.readers = ReadersSet(MISO,MOSI,SCLK,CS,MCPIRQ,self.mcp)
    self.readers.begin()

  def run(self):
    print("Reader starting")
    lastRead = 0
    while True:
      time.sleep(0.2)
      # get new read flag
      newRead, mcpirq,  uid = Readers.isNewRead()
      if (newRead && uid!=lastRead):
        print("new read on irq:", mcpirq, " uuid:",format(binascii.hexlify(uid)) )
        lastRead = uid

if __name__ == "__main__":
  def close(signal, frame):
    sys.exit(0)

  signal.signal(signal.SIGINT, close)

  rif = RFIDReader()
  rif.setDaemon(True)
  rif.start()

############################# MCPPNLIB.py
import binascii
import sys
import time
import threading
import Adafruit_PN532 as PN532
from MCP23S17 import MCP23S17


class readers: 
    def __init__(self, pn, mcpirq): 
        self.pn = pn 
        self.mcpirq = mcpirq

class ReadersSet(object):
    global readers

    def __init__(self,MISO = 19, MOSI=20, SCLK=21,CS=[],MCPIRQ=[], mcp1 = MCP23S17 ):
        # Setup  data
        self.MISO = MISO
        self.MOSI = MOSI
        self.SCLK = SCLK
        self.CS = CS
        self.MCPIRQ = MCPIRQ
        self.mcp1 = mcp1
        self.readerMCPIRQ = 99
        self.lastUid = 0

        # creating list       
        self.readersList = [] 
       
        # appending instances to list 
        for i in range(len(self.CS)):
            self.readersList.append(readers(PN532.PN532(cs=self.CS[i], sclk=self.SCLK, mosi=self.MOSI, miso=self.MISO),self.MCPIRQ[i]))

        # Call begin to initialize communication with the PN532. 
        for reader in self.readersList:
            reader.pn.begin()

        #get version
        for reader in self.readersList:
            ic, ver, rev, support = reader.pn.get_firmware_version()
            print('Found PN532 with firmware version: {0}.{1}'.format(ver, rev))

        # Configure PN532 to communicate with MiFare cards.
        for reader in self.readersList:
            reader.pn.SAM_configuration()


        # get tem into stand by mode for reading
        for reader in self.readersList:
            uid = reader.pn.read_passive_target(0)


        # stop thread flag
        self.stop_threads = False


    def thread_function(self,name):
        while (True):
            if (self.stop_threads):
                break
            # read all 16 pins of the MCP
            pinsValue = self.mcp1.readGPIO()
            #print("T:",bin(pinsValue))

            # go over list of readers
            for reader in self.readersList:
                #if any is avilable
                if not pinsValue & 1<<(reader.mcpirq) :
                    uid = reader.pn.read_passive_target()
                    # Try again if no card is available.
                    if uid is not None:
                        print("READER ", reader.mcpirq)
                        print('Found card with UID: 0x{0}'.format(binascii.hexlify(uid)))
                        self.readerMCPIRQ = reader.mcpirq
                        self.lastUid = uid
                    # set it back to stand by   
                    uid = reader.pn.read_passive_target(0)

            #thread sleeping time to allow system to work     
            time.sleep(0.01)

    def begin(self):
        x = threading.Thread(target=self.thread_function, args=(1,))
        x.start()
   
    def stop(self):
        self.stop_threads = True

    def onExit(self):
        self.stop_threads = True
   
    def isNewRead(self):
        if self.readerMCPIRQ !=99:
            tempIRQ = self.readerMCPIRQ
            self.readerMCPIRQ = 99 # reset
            return True, tempIRQ, self.lastUid
        else:
            return False, 0, 0

shaul
 
Posts: 2
Joined: Thu Jul 02, 2020 7:05 am

Please be positive and constructive with your questions and comments.