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"