Python社区  »  Python

用覆盆子pi3连接python烧瓶中的rfid标签访问学生数据库

Nesher • 1 周前 • 7 次点击  

两个月前,我刚开始学习python,当时我正在做一个大学项目。

我想创建一个“大学”网页,可以读取rfid卡标签,以访问不同的html文件(学生文件),这取决于卡标签的唯一id号。代码本身没有显示错误,但问题是:当我将标记放在扫描仪中时,我希望访问的html不会出现。扫描器本身在一个与项目无关的基本读写文件中工作,所以我确信扫描器工作正常。所以,问题是:有没有更好的方法来解决这个问题,还有一个链接建议来研究,让这个想法发挥作用?

编辑:我将重述一下这个问题,有人知道如何将烧瓶服务器与使用特定第三方模块的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
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/40228
 
7 次点击  
分享到微博
文章 [ 1 ]  |  最新文章 1 周前
user10987432
Reply   •   1 楼
user10987432    6 月前

我不知道你在做什么,但是只要看看第三方模块的源代码,我就可以告诉你simplemfrc522.read()返回一个元组。当您这样做时:

id = reader.read()

id现在是一个元组,而不是一个值-这就是为什么if语句没有执行。

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

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