If you've seen my previous posts, you know the task of creating an RFID reader is starting to baffle me. I have attached my code below. Basically, I need the reader to constantly scan for cards and write the UID's to another file. That works perfectly. However, I also need to script to stop scanning and import "redbutton" when GPIO 23 is down. My attempt at that script is below, but is not working. I don't get any errors, and the script for scanning for cards continues. At first, I thought it was an indentation problem, but I did not have any success with whichever indentation level I used.
Where have I gone wrong with wait_for_edge?
Code: Select all
import RPi.GPIO as GPIO
import MFRC522
import signal
import glob
GPIO.setmode (GPIO.BCM)
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(17, GPIO.OUT) ## Setup GPIO Pin 4 to OUT
GPIO.output(17,True) ## Turn on GPIO pin 17
MYFILE = glob.glob('/home/pi/RXRFID/newscans/*')[0]
import sys
from time import sleep
from Adafruit_CharLCDPlate import Adafruit_CharLCDPlate
lcd = Adafruit_CharLCDPlate()
lcd.clear()
lcd.message("Begin Scanning\nPrs red to stop")
continue_reading = True
# Capture SIGINT for cleanup when the script is aborted
def end_read(signal,frame):
global continue_reading
continue_reading = False
# Hook the SIGINT
signal.signal(signal.SIGINT, end_read)
# Create an object of the class MFRC522
MIFAREReader = MFRC522.MFRC522()
# 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"
print MYFILE
# Get the UID of the card
(status,uid) = MIFAREReader.MFRC522_Anticoll()
# If we have the UID, continue
if status == MIFAREReader.MI_OK:
print "Card read UID: "+str(uid[0])+","+str(uid[1])+","+str(uid[2])+","+str(uid[3])
# Write the card UID to a file
with open(MYFILE, 'a') as f:
uid_str = ",".join([str(x) for x in uid])
f.write(uid_str+"\n")
continue
# 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 "Authenticaion error"
continue
# Check for redbutton
try:
GPIO.wait_for_edge(23, GPIO.FALLING)
import redbutton
except KeyboardInterrupt:
GPIO.cleanup()