树莓派上使用RC522
作者:互联网
一、使能SPI与I2C
二、安装mfrc522
sudo pip3 install mfrc522
三、输入
from mfrc522 import SimpleMFRC522
reader = SimpleMFRC522()
try:
text = input('New data:')
print("write")
reader.write(text)
print("success")
finally:
GPIO.cleanup()
然后在界面输入随便的数字
四、验证
import RPi.GPIO as GPIO
from mfrc522 import SimpleMFRC522
reader = SimpleMFRC522()
while True:
id, text = reader.read()
print(id, text)
五、RC522 控制舵机
import RPi.GPIO as GPIO
from mfrc522 import SimpleMFRC522
import time
import re
from threading import Thread
rfid = 0
servoPIN = 17 #BCM 17
GPIO.setmode(GPIO.BCM)
GPIO.setup(servoPIN, GPIO.OUT)
p = GPIO.PWM(servoPIN, 50)
p.start(2.5)
reader = SimpleMFRC522()
def data():
global rfid
while True:
id, text = reader.read()
clean_text = re.findall('\d+', text)
match = int(clean_text[0])
rfid = match
def lock():
global rfid
while True:
print("rfid = ", rfid)
if rfid == 1234 :
p.ChangeDutyCycle(2.5)
time.sleep(3)
rfid = 0
else:
p.ChangeDutyCycle(6)
time.sleep(0.5)
try:
t1= Thread(target=data)
t2 = Thread(target=lock)
t1.start()
t2.start()
except:
p.stop()
GPIO.cleanup()
标签:树莓,text,rfid,RC522,使用,GPIO,import,SimpleMFRC522 来源: https://blog.csdn.net/qq_44941069/article/details/123590351