将RFID标签与Python Flask中的MFRC522模块链接,以使用Raspberry pi3访问学生数据库

时间:2019-03-07 02:03:23

标签: python html css rfid

大约2个月前,我刚刚开始为我正在从事的大学项目学习Python。

我想创建一个“大学”网页,该网页可以读取RFID卡标签,以根据卡标签的唯一ID号访问不同的HTML文件(学生文件)。代码本身未显示任何错误,但问题是:将标签放在扫描仪中时,我想要访问的HTML没有出现。扫描仪本身正在与项目无关的基本读写文件中工作,因此我确定扫描仪正在工作。因此,问题是:是否有更好的方法来解决此问题,并提供链接建议进行研究以使该想法付诸实践?

编辑:我将重述这个问题,是否有人知道使用特定第三方模块将Flask服务器与RFID标签中的读写文件连接的方法?

功能:在扫描仪中显示RFID标签后,必须更改在特定RFID标签上编程的显示。

以下是Python Flask文件中的相关信息:

import RPi.GPIO as GPIO
from flask import Flask, render_template
import SimpleMFRC522


#HTML & CSS to Python Flask Connection (Web Server)

frame_python = Flask(__name__)

@frame_python.route('/')
def frame_main():
    return render_template('Main.html') .... # cut it short cuz not needed


#RFID Student section

@frame_python.route('/Student/Student_Welcome')
def frame_stud_welcome():
    
     
    reader = SimpleMFRC522.SimpleMFRC522() #information from a different file below
    id = reader.read()
    a = 81143208559            #Variables a,b and c are the uid# of the card tags
    b = 628549754026
    c = 985039970922
    
    try:
     if a == id:
      return render_template('stud1_welcome.html')
     elif b == id:
      return render_template('stud2_welcome.html')
     elif c == id:
      return render_template('stud3_welcome.html')
    
    finally:
        GPIO.cleanup()


if __name__ == '__main__':
    frame_python.run(host='0.0.0.0',port=8080,debug=True)
    
    
    
    
    
#Faculty Section    

stud1_welcome.html文件:

  <!DOCTYPE html>
    <html>
        <head>
            <meta charset="utf-8">
            <meta name="viewport" content="width=device-width">
            <meta name="description" content="Middle C, Christ at the center!">
            <meta name="keywords" content="HTML, CSS, MySQL, Python & Flask Python add-on">
            <meta name="author" content="name">
            
            <title>Smart Campus Information Board | Welcome!</title>
            <link rel="stylesheet" href="/static/style.css">
        </head>
        
    
    <section id="background">
    
        <body>
          
            <header>
                <div class="container">
                 <div id="Branding">
                  <h1><span class="highlight">Smart Campus</span> Information Board</h1>
                </div>
            </header>
        
        <section id="logo">
            <div class="container">
                <img src="/static/img/AMA_logo.png">
                <h3>AMA International University Bahrain</h3>
                <p>Student ID accepted! Welcome Student 1!</p>
            </div>    
        </section>
        
        
        <section id="choices">
            <div class="container">
                <form>
                  <button type="Back to Menu" class="button_1"><a href="{{ url_for('frame_main') }}">Guest</a></button>
                </form>
            </div>    
        </section>
        
    </section>
        
         <footer>
        <p>SCIB Designs, 2019</p>
         </footer>
        </body>
     </html>   
            
            
                       

SimpleMFRC522()是带有我从教程(https://pimylifeup.com/raspberry-pi-rfid-rc522/)中获取的库的​​文件。

# Code by Simon Monk https://github.com/simonmonk/

import MFRC522
import RPi.GPIO as GPIO
  
class SimpleMFRC522:

  READER = None;
  
  KEY = [0xFF,0xFF,0xFF,0xFF,0xFF,0xFF]
  BLOCK_ADDRS = [8, 9, 10]
  
  def __init__(self):
    self.READER = MFRC522.MFRC522()
  
  def read(self):
      id, text = self.read_no_block()        
      while not id:
          id, text = self.read_no_block()  
      return id, text

  def read_id(self):
    id = self.read_id_no_block()
    while not id:
      id = self.read_id_no_block()
    return id

  def read_id_no_block(self):
      (status, TagType) = self.READER.MFRC522_Request(self.READER.PICC_REQIDL)
      if status != self.READER.MI_OK:
          return None
      (status, uid) = self.READER.MFRC522_Anticoll()
      if status != self.READER.MI_OK:
          return None
      return self.uid_to_num(uid)
  
  def read_no_block(self):
    (status, TagType) = self.READER.MFRC522_Request(self.READER.PICC_REQIDL)
    if status != self.READER.MI_OK:
        return None, None
    (status, uid) = self.READER.MFRC522_Anticoll()
    if status != self.READER.MI_OK:
        return None, None
    id = self.uid_to_num(uid)
    self.READER.MFRC522_SelectTag(uid)
    status = self.READER.MFRC522_Auth(self.READER.PICC_AUTHENT1A, 11, self.KEY, uid)
    data = []
    text_read = ''
    if status == self.READER.MI_OK:
        for block_num in self.BLOCK_ADDRS:
            block = self.READER.MFRC522_Read(block_num) 
            if block:
            		data += block
        if data:
             text_read = ''.join(chr(i) for i in data)
    self.READER.MFRC522_StopCrypto1()
    return id, text_read
    

    
  def write(self, text):
      id, text_in = self.write_no_block(text)        
      while not id:
          id, text_in = self.write_no_block(text)  
      return id, text_in


  def write_no_block(self, text):
      (status, TagType) = self.READER.MFRC522_Request(self.READER.PICC_REQIDL)
      if status != self.READER.MI_OK:
          return None, None
      (status, uid) = self.READER.MFRC522_Anticoll()
      if status != self.READER.MI_OK:
          return None, None
      id = self.uid_to_num(uid)
      self.READER.MFRC522_SelectTag(uid)
      status = self.READER.MFRC522_Auth(self.READER.PICC_AUTHENT1A, 11, self.KEY, uid)
      self.READER.MFRC522_Read(11)
      if status == self.READER.MI_OK:
          data = bytearray()
          data.extend(bytearray(text.ljust(len(self.BLOCK_ADDRS) * 16).encode('ascii')))
          i = 0
          for block_num in self.BLOCK_ADDRS:
            self.READER.MFRC522_Write(block_num, data[(i*16):(i+1)*16])
            i += 1
      self.READER.MFRC522_StopCrypto1()
      return id, text[0:(len(self.BLOCK_ADDRS) * 16)]
      
  def uid_to_num(self, uid):
      n = 0
      for i in range(0, 5):
          n = n * 256 + uid[i]
      return n

1 个答案:

答案 0 :(得分:0)

我对您正在执行的操作一无所知,但是仅查看第三方模块的源代码,我就能告诉您SimpleMFRC522.read()返回一个元组。当您这样做时:

id = reader.read()

id现在是一个元组,而不是单个值-这就是为什么if语句未执行的原因。

我的猜测是,您可以通过执行以下任一操作来解决此问题:

  • 使用id = reader.read_id()
  • 使用id, _ = reader.read()