树莓派:智慧电梯(涉及rfidRC522,矩阵键盘,LCD1602功能)

1、功能介绍:

识别ifid卡,如果是管理员卡则驱动屏幕显示“Key access!”开放键盘输入权限,并亮起绿灯。如果不是管理员卡,则亮起红灯,屏幕显示“Access deny”,关闭键盘输入。

等待刷卡:
在这里插入图片描述

刷非管理员卡:
在这里插入图片描述
刷管理员卡:
在这里插入图片描述

2、rc522的使用

rc522介绍:

rc522支持以下几种卡片:
MIFARE Mini
MIFARE 1K
MIFARE 4K,
MIFARE Ultralight
MIFARE DESFire EV1
MIFARE Plus RF

rc522接口定义

在这里插入图片描述

模块连接

在这里插入图片描述
例程库使用的是树莓派GPIO.BOARD引脚模式,引脚定义如下:
在这里插入图片描述

rc522树莓派
3.3V1
RST22
GND6
IRQ悬空
MISO21
MOSI19
SCK23
SDA24

开启spi和iic模块

终端执行:

sudo raspi-config

在这里插入图片描述
回车键确认即可打开
回车键确认即可打开
输入指令:

ls /dev/spidev0.*

查看是否成功开启spi模块
在这里插入图片描述
同理输入

ls /dev/i2c-1

查看是否成功开启iic功能。

安装spi驱动python库

sudo apt-get install python-spidev python3-spidev

安装SPI-PY:

cd ~
git clone https://github.com/lthiery/SPI-Py.git
cd SPI-Py
sudo python setup.py install
sudo python3 setup.py install

这样python2和python3都能使用spi了

下载RC522 的Python库

cd ~
git clone https://github.com/mxgxw/MFRC522-python.git

使用示例测试:

cd MFRC522-python
python Read.py

在这里插入图片描述
测试成功!

bug总结:

1、没成功打开spi模块和iic模块,可能是镜像有问题,比如树莓派4b的镜像烧进了树莓派3b(本人有过这种操作,目前发现spi会无法开启)
在这里插入图片描述
2、最新的SPI-Py库参数少了一个导致无法运行;
在这里插入图片描述
执行:

cd SPI-Py.git
git checkout 8cce26b9ee6e69eb041e9d5665944b88688fca68
sudo python setup.py install
sudo python3 setup.py install

使用git指令回退到旧版本即可正常运行

3、矩阵键盘使用

参照:https://blog.csdn.net/q943413302/article/details/80487821

4、LCD1602 iic版本使用

参照:https://shumeipai.nxez.com/2020/06/17/raspberry-pi-drives-lcd1602-screen-through-i2c.html

5、项目工程

将矩阵键盘驱动程序打包成keyboard.py
将LCD1602驱动程序打包成LCD1602.py
然后将这两文件放入MFRC522-python例程文件夹里,在文件夹里新建一个test.py:

#!/usr/bin/env python
# -*- coding: utf8 -*-
import time       #导入时间模块
import RPi.GPIO as GPIO    #导入引脚控制模块
import MFRC522    #导入rc522协议
import keypad   #导入键盘模块
import LCD1602   #导入屏幕显示模块

green = 15   #定义引脚
red   = 13

time=0    #定义计时变量

GPIO.setmode(GPIO.BOARD)    #设置引脚模式
GPIO.setup(green,GPIO.OUT)   #点灯引脚为输出
GPIO.setup(red,GPIO.OUT)
GPIO.output(green,GPIO.LOW)   #关灯
GPIO.output(red,GPIO.LOW)

# Create an object of the class MFRC522
MIFAREReader = MFRC522.MFRC522()    #定义rc522信息读取对象

# Welcome message
print("Looking for cards")
print("Press Ctrl-C to stop.")

# This loop checks for chips. If one is near it will get the UID

LCD1602.init_lcd()    #初始化屏幕

key=None

have_card = 0;

try:

  while True:

    # Scan for cards
    (status,TagType) = MIFAREReader.MFRC522_Request(MIFAREReader.PICC_REQIDL)    #查询rc522状态(是否存在rfid卡)

    # Get the UID of the card
    (status,uid) = MIFAREReader.MFRC522_Anticoll()            #获取rfid卡的uid号
    if status == MIFAREReader.MI_OK:
            # Print UID
        print("UID: "+str(uid[0])+","+str(uid[1])+","+str(uid[2])+","+str(uid[3]))     #输出rfid卡的uid号


    # If we have the UID, continue
    if have_card==0:                                 #状态1:判断此卡是否是管理员卡,如果是则have_card=1,否则have_card=2
        GPIO.output(green,GPIO.LOW)
        GPIO.output(red,GPIO.LOW)
        LCD1602.print_lcd(0, 0, 'Swipe card!        ')
        LCD1602.print_lcd(0, 1, '               ')
        if status == MIFAREReader.MI_OK:
            if uid[0]==252 and uid[1] == 158 and uid[2] == 12 and uid[3] == 73:
                have_card=1
                # Print UID
                #print("UID: "+str(uid[0])+","+str(uid[1])+","+str(uid[2])+","+str(uid[3]))
            else:
                have_card=2
    elif have_card==1:            #状态2:如果是管理员卡,则打开绿灯,开始轮询检测键盘值并控制屏幕显示相应内容
        time=time+1
        if time==30:                 #计时大概5秒左右回归初始状态
            time=0
            have_card=0
        LCD1602.print_lcd(0, 0, 'Key activated!    ')
        key=keypad.getkey()
        GPIO.output(green,GPIO.HIGH)
        GPIO.output(red,GPIO.LOW)
        if not key==None:
            #print ('You enter the  key:',key)
            LCD1602.print_lcd(0, 1, 'floor:')
            LCD1602.print_lcd(6, 1, key)
    elif have_card==2:               #刷错误的卡,则亮红灯,关闭键盘输入,屏幕显示相应内容
        time=time+1
        if time==20:                     #计时大概3秒左右回归初始状态
            time=0
            have_card=0
        LCD1602.print_lcd(0, 0, 'Access denied    ')
        LCD1602.print_lcd(0, 1, '            ')
        GPIO.output(green,GPIO.LOW)
        GPIO.output(red,GPIO.HIGH)

except KeyboardInterrupt:
  GPIO.cleanup()

注意python文件头顶要加上:

#!/usr/bin/env python
# -*- coding: utf8 -*-

否则无法用终端来执行。

6、合作

如有任何问题可以加我微信交流学习。如果有树莓派,arduino,stc单片机,stm32单片机,esp8266,NXP单片机等开发需求,电子设计定制开发需求也可以联系我:18076427103

  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你好,树莓派使用LCD1602是一种常见的显示屏,可以通过Python编程语言来控制。以下是使用Python控制LCD1602的基本步骤: 1. 安装必要的库文件:需要安装RPi.GPIO和smbus库文件,可以通过以下命令进行安装: ``` sudo apt-get install python-dev python-rpi.gpio i2c-tools ``` 2. 连接硬件:将LCD1602屏幕连接到树莓派的GPIO引脚和I2C总线上。 3. 编写Python代码:使用Python编写代码来控制LCD1602屏幕。具体代码可以参考以下示例: ``` import smbus import time # Define some device parameters I2C_ADDR = 0x27 # I2C device address LCD_WIDTH = 16 # Maximum characters per line # Define some device constants LCD_CHR = 1 # Mode - Sending data LCD_CMD = 0 # Mode - Sending command LCD_LINE_1 = 0x80 # LCD RAM address for the 1st line LCD_LINE_2 = 0xC0 # LCD RAM address for the 2nd line # Define some timing constants E_PULSE = 0.0005 E_DELAY = 0.0005 # Open I2C interface bus = smbus.SMBus(1) # Rev 2 Pi uses 1 def lcd_init(): # Initialise display lcd_byte(0x33,LCD_CMD) # 110011 Initialise lcd_byte(0x32,LCD_CMD) # 110010 Initialise lcd_byte(0x06,LCD_CMD) # 000110 Cursor move direction lcd_byte(0x0C,LCD_CMD) # 001100 Display On,Cursor Off, Blink Off lcd_byte(0x28,LCD_CMD) # 101000 Data length, number of lines, font size lcd_byte(0x01,LCD_CMD) # 000001 Clear display time.sleep(E_DELAY) def lcd_byte(bits, mode): # Send byte to data pins # bits = the data # mode = 1 for data, 0 for command bits_high = mode | (bits & 0xF0) | 0x08 bits_low = mode | ((bits<<4) & 0xF0) | 0x08 # High bits bus.write_byte(I2C_ADDR, bits_high) lcd_toggle_enable(bits_high) # Low bits bus.write_byte(I2C_ADDR, bits_low) lcd_toggle_enable(bits_low) def lcd_toggle_enable(bits): # Toggle enable time.sleep(E_DELAY) bus.write_byte(I2C_ADDR, (bits | 0x04)) time.sleep(E_PULSE) bus.write_byte(I2C_ADDR, (bits & ~0x04)) time.sleep(E_DELAY) def lcd_string(message, line): # Send string to display message = message.ljust(LCD_WIDTH," ") lcd_byte(line, LCD_CMD) for i in range(LCD_WIDTH): lcd_byte(ord(message[i]),LCD_CHR) if __name__ == '__main__': try: lcd_init() while True: lcd_string("Hello world!",LCD_LINE_1) lcd_string("Raspberry Pi",LCD_LINE_2) time.sleep(3) # 3 second delay lcd_string("1234567890123456",LCD_LINE_1) lcd_string("abcdefghijklmnop",LCD_LINE_2) time.sleep(3) # 3 second delay except KeyboardInterrupt: pass lcd_byte(0x01, LCD_CMD) lcd_string("Goodbye!",LCD_LINE_1) lcd_string("See you later.",LCD_LINE_2) time.sleep(3) lcd_byte(0x01, LCD_CMD) ``` 以上代码是一个简单的例子,可以将其保存到树莓派上的.py文件中,然后使用以下命令运行: ``` python lcd1602.py ``` 希望能对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值