MFRC522控制LED

既然已经读取卡片的ID,那么就简单实现一个RFID门卡控制LED试试。

import RPi.GPIO as GPIO
import mfrc522
import signal
import time

GPIO.setmode(GPIO.BCM)
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])+','+str(uid[4]))  
        # This is the default key for authentication
        key = [0xFF,0xFF,0xFF,0xFF,0xFF,0xFF]
        
        # Select the scanned tag
        MIFAREReader.MFRC522_SelectTag(uid)
        
        #ENTER Your Card UID here
        my_uid = [213,179,104,28,18]
        
        #Configure LED Output Pin
        LED = 18
        GPIO.setup(LED, GPIO.OUT)
        GPIO.output(LED, GPIO.LOW)
        
        #Check to see if card UID read matches your card UID
        if uid == my_uid:                #Open the Doggy Door if matching UIDs
            print("Access Granted")
            GPIO.output(LED, GPIO.HIGH)  #Turn on LED
            time.sleep(5)                #Wait 5 Seconds
            GPIO.output(LED, GPIO.LOW)   #Turn off LED
            
        else:                            #Don't open if UIDs don't match
            print("Access Denied, YOU SHALL NOT PASS!")

MFRC522读卡器接完之后,在GPIO18上接一个LED灯即可。有想法的话把LED换成门锁就是门禁系统了。

猜你喜欢

转载自www.cnblogs.com/1328497946TS/p/12026514.html