0

I am trying to set up a pi to use rfid to scan a card and if the card is correct, the user will enter in a 4 digit code into a 4x4 matrix keypad. Each device works perfectly fine independently however if I connect both using a breadboard, it works once but requires a reboot to work again. Below is the code I'm using to get this to work. I read that I may need to use different spi pins but I'm not sure how. I'm a beginner at best. Any help would be greatly appreciated. Thank you!

#!/usr/bin/env python
# -*- coding: utf8 -*-
import RPi.GPIO as GPIO
import MFRC522
import time
import signal
import os

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:

    # Print UID
    #print "Card read UID: "+str(uid[0])+","+str(uid[1])+","+str(uid[2])+","+str(uid[3])
    print "Card read UID: {0}, {1}, {2}, {3}".format(*uid)

    if uid[:4] == [68, 193, 21, 186]:
        print "Access Granted"

        #os.system('mpg123 -q resources/Access_Granted.mp3 &')

        #time.sleep(5)

        #os.system('pkill -9 mpg123')

        continue_reading = False
        opendoor = "147*"
        closedoor = "369#"
        keypress = ""

        MATRIX = [ [1,2,3,'A'],
                   [4,5,6,'B'],
                   [7,8,9,'C'],
                   ['*',0,'#','D'] ]

        ROW = [3,5,8,10]
        COL = [19,21,23,24]

        for j in range(4):
            GPIO.setup(COL[j], GPIO.OUT)
            GPIO.output(COL[j],1)

        for i in range(4):
            GPIO.setup(ROW[i], GPIO.IN, pull_up_down = GPIO.PUD_UP)

        try:
            while(True):
                for j in range(4):
                    GPIO.output(COL[j],0)

                    for i in range(4):
                        if GPIO.input(ROW[i]) == 0:
                            print (MATRIX[i][j])
                            keypress = keypress + str(MATRIX[i][j])
                            if len(keypress) == 4:
                                if keypress == opendoor:

                                    print("open door")
                                    os.system('mpg123 -q resources/Access_Granted.mp3 &')

                                    time.sleep(3)

                                    os.system('pkill -9 mpg123')

                                    GPIO.setup(18, GPIO.OUT)
                                    GPIO.output(18, GPIO.LOW)
                                    keypress = ""
                                elif keypress == closedoor:
                                    print("close door")
                                    GPIO.setup(18, GPIO.OUT)
                                    GPIO.output(18, GPIO.HIGH)
                                    keypress = ""
                                else:
                                    print("wrong code")
                                    keypress = ""
                            time.sleep(0.2)
                            while(GPIO.input(ROW[i]) == 0):
                                pass

                    GPIO.output(COL[j],1)
        finally:
            print ""
    else:
        print "Acess Denied"

    # 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"
  • Code isn't indented properly, which makes it hard to know where the function ends. Also might need a wiring diagram. – Brick Mar 4 '18 at 21:00
0

The code is not formatted properly so I did not try to run the script.

Are you sure you copied the code properly? I would expect to see a set up of the GPIO numbering system to be used, i.e. GPIO.setmode(GPIO.BOARD) or GPIO.setmode(GPIO.BCM).

Neither is it clear which SPI channel you are using as this is concealed within the MFRC522 class.

For the sake of answering your question I will use Broadcom GPIO numbering (BCM in RPi.GPIO code).

I will also assume you are using SPI channel 0.

SPI channel 0 uses (Broadcom) GPIO 8, 9, 10, and 11. Make sure you do not use any of these GPIO for your keyboard matrix.

See https://pinout.xyz/

  • Thank you so much! I was able to move the pins for the keypad and remap them and it's working perfectly. Sorry about the weird code formatting. I pasted it and the formatting got messed up. Could you possible help me figure out how to keep it running now? I used os to restart the python script after entering in the close code but I'm not sure if that's the best way to do it. Thanks again for your help! – StarScr3am77 Mar 5 '18 at 0:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.