使用raspberry pi读取MFRD522数据并通过http(python3)发送此数据

时间:2018-05-17 07:50:19

标签: python-3.x raspberry-pi3 rfid

我正在进行一个项目,我想使用RFID作为位置参考(当树莓派和MFRC522读取数据时,将其发送到服务器并确保位置或调整位置,而RFID阅读器是固定)

我的代码如下:

import RPi.GPIO as GPIO
import MFRC522
import signal
import socket
HOST = '192.168.11.48'
PORT = 9009

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")

# This loop keeps checking for chips. If one is near it will get the UID     and authenticate

with socket.socket(socket.AF_INET,socket.SOCK_STREAM) as sock:
    sock.connect((HOST,PORT))


#print("closing client")            
    while True:
        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]))

                # This is the default key for authentication
                key = [0xFF,0xFF,0xFF,0xFF,0xFF,0xFF]

                # Select the scanned tag
                MIFAREReader.MFRC522_SelectTag(uid)


            # Authenticate
                status = MIFAREReader.MFRC522_Auth(MIFAREReader.PICC_AUTHENT1A, 8, key, uid)
            data = "str(uid[0])"

                msg = data
                sock.sendall(msg.encode())
                data = sock.recv(128)
                print('date from Echo server is [%s]'%data.decode())

            # Check if authenticated
                if status == MIFAREReader.MI_OK:
                    MIFAREReader.MFRC522_Read(8)
                    MIFAREReader.MFRC522_StopCrypto1()
                else:
                    print("Authentication error")

我正在使用echo服务器来确保它正常工作 echo服务器的代码是:

import socketserver

HOST = ''
PORT = 9009


class MyTcpHandler(socketserver.BaseRequestHandler):
    def handle(self):
        print("[%s]conneted"%self.client_address[0])

        try:
            while True:
                self.data = self.request.recv(1024)
                if self.data.decode() == "/quit":
                    print('ended by user [%s]'%self.client_address[0])
                    return

                print('[%s]'%self.data.decode())
                self.request.sendall(self.data)
        except Exceptions as e:
            print(e)


def runServer():
    print("starting echo server")
    print("if you want to close echo server, click Ctrl+C")

   try:
        server = socketserver.TCPServer((HOST,PORT), MyTcpHandler)
        server.serve_forever()
    except KeyboardInterrupt:
        print("--- closing server")

runServer()        

当第二个代码运行时,我运行了一段代码,但只有我得到的结果是 -

Welcome to the MFRC522 data read example
Card detected
Card read UID:178,29,209,48
size:8.

任何人都知道应该改变什么才能让它发挥作用?

感谢

0 个答案:

没有答案