树莓派使用RFID RC522芯片

实用教程:树莓派使用RFID RC522芯片进行数据的读写

In this Raspberry Pi RFID RC522 tutorial, I will be walking you through the steps on how to set up and wire the RFID RC522 chip with your Raspberry Pi. This project is a cool circuit to play around with and opens you up to quite a wide variety of different projects from using it as an attendance system to using it to open a lock.
在这个Raspberry Pi RFID RC522教程中,我将介绍如何设置和连接Raspberry Pi和RFID RC522芯片。这是一个很实用的教程,以便于你之后各种不同的项目,例如使用它作为考勤系统、使用它来打开一个锁。

The RFID RC522 is a very low-cost RFID (Radio-frequency identification) reader and writer that is based on the MFRC522 microcontroller. This microcontroller provides its data through the SPI protocol and works by creating a 13.56MHz electromagnetic field that it uses to communicate with the RFID tags.
RFID RC522是基于MFRC522微控制器的非常低成本的RFID(射频识别)读写器。该微控制器通过SPI协议提供数据,并通过创建一个13.56MHz的电磁场来工作,该电磁场用于与RFID卡片进行通信。

Make sure that the tags you purchase for your RFID RC522 operate on the 13.56MHz frequency otherwise we will fail to read them.
确保你的RFID RC522购买的卡片在13.56MHz的频率上运行,否则将无法读取。

We will be showing you how to wire up the RC522 as well as showing you how to write Python scripts to interact with the chip so you can both read and write your RFID Tags. You can extend this tutorial to use something like a 16×2 LCD for the Raspberry Pi, handy if you want to show some information.
本教程将示范如何连接RC522以及编写Python脚本以与芯片进行交互,以便读取和写入RFID卡片。如果你想展示一些数据信息,你可以使用像16×2液晶显示器扩展这个教程。

Equipment List

设备清单

Below are all the bits and pieces that I used for this Raspberry Pi RFID RC522 tutorial.
下面列出的是我在这个教程中使用的东西。

Recommended:
推荐:

  • Raspberry Pi 2 or 3
    树莓派2或3

  • Micro SD Card
    Micro SD卡

  • Power Supply
    电源供应

  • RC522 RFID Reader
    RC522 RFID读卡器

  • Breadboard
    面包板

  • Breadboard Wire
    面包板接线

Optional:
可选:

  • Raspberry Pi Case
    树莓派外壳

  • Ethernet Network Connection or Wifi dongle (The Pi 3 has WiFi inbuilt)
    以太网或Wifi适配(树莓派3自带了)

Assembling the RFID RC522

组装RFID RC522

One thing you will notice when purchasing an RFID RC522 Reader is that 90% of them don’t come with the header pins already soldered in. The missing pins mean you will have to do it yourself, luckily soldering header pins is a rather simple task, even for beginners.
购买RFID RC522读卡器时你会注意到的一件事情是,90%的读卡器没有使用已经焊接好的插头引脚。这意味着你必须自己做,幸运的是,焊接插头引脚是一个相当简单的任务,即使是对于初学者而言。

First off, if the header pins you received with your RC522 isn’t the correct size, then snap them down, so you only have a single row of eight pins.
首先,如果你使用RC522收到的插头引脚不是正确的大小,那么你只有一行8个引脚。

Place the header pins up through the holes of your RC522. One handy trick is to put the long side of the header pins into a breadboard and then putting the circuit over the top of the header pins. The breadboard will hold the pins tightly making it easier to solder them to the RFID RC522 circuit.
将插头引脚向上穿过RC522的孔。一个小窍门是把插头引脚的长边插入面包板,然后把电路放在接头引脚的顶部。面包板将固定住引脚,使其更容易焊接到RFID RC522电路。

Now using a hot soldering iron and some solder, slowly solder each of the pins. Remember it is best to heat the joint slightly before applying solder to it, this will ensure that the solder will adhere more to the joint and reduce the chances of creating a cold joint. We also recommend being careful with the amount of solder you apply.
现在使用热的烙铁和一些焊锡,慢慢地焊接每个引脚。请记住,在焊接之前,最好稍微加热接头,这样可以确保焊锡更多地粘附在接头上,并减少了形成冷接头的机会。我们还建议小心使用焊锡,避免过量。

With the header pins now soldered to your RFID circuit, it is now ready to use, and you can continue with the tutorial.
现在插头引脚已经焊接到你的RFID电路上,现在可以使用了,你可以跟着这个教程继续。

Wiring the RFID RC522

连接RFID RC522

On your RFID RC522 you will notice that there are 8 possible connections on it, these being SDA (Serial Data Signal), SCK (Serial Clock), MOSI (Master Out Slave In), MISO (Master In Slave Out), IRQ (Interrupt Request), GND (Ground Power), RST (Reset-Circuit) and 3.3v (3.3v Power In). We will need to wire all of these but the IRQ to our Raspberry Pi’s GPIO pins.
在你的RFID RC522上,你会发现,它有8个可能的引脚,这分别是SDA(串行数据信号),SCK(串行时钟),MOSI(主出从入),MISO(主入从出),IRQ(中断请求),GND(地面电源),RST(复位电路)和3.3V(3.3V电源输入)。我们需要把所有这些引脚,除了IRQ之外,都连接到Raspberry Pi的GPIO引脚。

You can either wire these directly to the GPIO Pins or like we did in this tutorial, plug the RFID RC522 into our Breadboard then wire from there to our Raspberry Pi’s GPIO Pins.
你可以将这些直接连接到GPIO引脚,或者像我们在本教程中所做的那样,将RFID RC522插入我们的面包板,然后从那里连接到我们的Raspberry Pi的GPIO引脚。

Wiring your RFID RC522 to your Raspberry Pi is fairly simple, with it requiring you to connect just 7 of the GPIO Pins directly to the RFID reader. Follow the table below, and check out our GPIO guide to see the positions of the GPIO pins that you need to connect your RC522 to.
将RFID RC522连接到Raspberry Pi相当简单,只需要将7个GPIO引脚直接连接到RFID读卡器。请遵循下表,并查阅我们的GPIO指南,查看连接RC522所需的GPIO引脚位置。

  • SDA connects to Pin 24.
    SDA连接到引脚24。

  • SCK connects to Pin 23.
    SCK连接到引脚23。

  • MOSI connects to Pin 19.
    MOSI连接到引脚19。

  • MISO connects to Pin 21.
    MISO连接到引脚21。

  • GND connects to Pin 6.
    GND连接到引脚6。

  • RST connects to Pin 22.
    RST连接到引脚22。

  • 3.3v connects to Pin 1.
    3.3V连接到引脚1。

Setting up Raspbian for the RFID RC522

为RFID RC522设置Raspbian

Before we begin the process of utilizing the RFID RC522 on our Raspberry Pi, we will first have to make changes to its configuration. By default, the Raspberry Pi has the SPI (Serial Peripheral Interface) disabled, which is a bit of a problem as that is what our RFID reader circuit runs through.
在我们开始在Raspberry Pi上使用RFID RC522之前,我们将首先对其配置进行更改。默认情况下,树莓派的SPI(串行外设接口)是被禁用的。
Don’t worry though as it is fairly simple to re-enable this interface, just follow our steps below to configure your Raspberry Pi and Raspbian to utilize the SPI interface.
不要担心,因为重新启用此接口非常简单,只需按照以下步骤配置Raspberry Pi和Raspbian即可使用SPI接口。

Let’s begin by first opening the raspi-config tool, and we can do this by opening the terminal and running the following command.
首先打开raspi-config工具,打开终端并运行以下命令:

1
sudo raspi-config

This tool will load up a screen showing a variety of different options. If you want a more in-depth look into these options, you can check out our raspi-config guide.
该工具将加载一个显示各种不同选项的界面。如果你想深入了解这些选项,可以参阅我们的raspi-config指南

On here use the arrow keys to select “5 Interfacing Options”. Once you have this option selected, press Enter.
在这里使用方向键选择『5 Interfacing Options』。选中此选项后,按回车

Now on this next screen, you want to use your arrow keys to select “P4 SPI”, again press Enter to select the option once it is highlighted.
现在在下一个界面上,你需要使用方向键选择『P4 SPI』,再次按回车选择高亮显示的选项。

You will now be asked if you want to enable the SPI Interface, select Yes with your arrow keys and press Enter to proceed. You will need to wait a little bit while the raspi-config tool does its thing in enabling SPI.
现在将询问你是否要启用SPI接口,使用方向键选择Yes,然后按回车继续。你需要等一会儿,raspi-config工具会启用SPI。

Once the SPI interface has been successfully enabled by the raspi-config tool you should see the following text appear on the screen, “The SPI interface is enabled”.
一旦通过raspi-config工具成功启用了SPI接口,你应该会看到屏幕上出现以下文本:『The SPI interface is enabled』。

Before the SPI Interface is fully enabled we will first have to restart the Raspberry Pi. To do this first get back to the terminal by pressing Enter and then ESC.
在SPI接口完全启用之前,我们将首先重新启动Raspberry Pi。要做到这一点首先回到终端按回车,然后按ESC

Type the following Linux command into the terminal on your Raspberry Pi to restart your Raspberry Pi.
在Raspberry Pi的终端中输入以下Linux命令来重启。

1
sudo reboot

Once your Raspberry Pi has finished rebooting, we can now check to make sure that it has in fact been enabled. The easiest way to do this is to run the following command to see if spi_bcm2835 is listed.
在Raspberry Pi完成重启后,我们现在可以检查确认它已经被启用。最简单的方法是运行以下命令,查看是否列出了spi_bcm2835

1
lsmod | grep spi

If you see spi_bcm2835, then you can proceed on with this tutorial and skip on to the next section. If for some reason it had not appeared when you entered the previous command, try following the next three steps.
如果你看到spi_bcm2835,那么你可以继续,直接跳到下一节。如果由于某种原因,当你输入上一个命令时没有出现,请尝试执行以下3个步骤。

If for some reason the SPI module has not activated, we can edit the boot configuration file manually by running the following command on our Raspberry Pi.
如果由于某种原因SPI模块没有激活,我们可以通过在我们的Raspberry Pi上运行以下命令来手动编辑启动配置文件。

1
sudo nano /boot/config.txt

Within the configuration file, use Ctrl+W to find “dtparam=spi=on”.
在配置文件中,使用Ctrl+W查找『dtparam=spi=on』。

If you have found it, check to see if there is a # in front of it. If there is, remove it as this is commenting out the activation line. If you can’t find the line at all, add “dtparam=spi=on” to the bottom of the file.
如果你找到了,请检查前面是否有#号。如果有,就删除它,因为这是用来注释一行代码的。如果找不到该行,只需在文件底部添加『dtparam = spi = on』即可。

Once you have made the changes, you can press Ctrl+X then pressing Y and then Enter to save the changes.
完成更改后,可以按Ctrl+X,然后按Y,然后按回车以保存更改。

You can now proceed from Step 5 again, rebooting your Raspberry Pi then checking to see if the module has been enabled.
现在,你可以回到前面的步骤,重新启动Raspberry Pi,然后检查模块是否已启用。

Getting Python ready for the RFID RC522

为RFID RC522准备好Python

Now that we have wired up our RFID RC522 circuit to the Raspberry Pi we can now power it on and begin the process of programming simple scripts in Python to interact with the chip.
现在我们已经将RFID RC522电路连接到Raspberry Pi。我们可以启动它,开始编写简单的Python脚本以与芯片进行交互。

The scripts that we will be showing you how to write will show you how to read data from the RFID chips and how to write to them. These will give you the basic idea of how data is dealt with and will be the basis of further RFID RC522 tutorials.
我们将向你展示如何编写脚本,以及脚本如何从RFID芯片读取和写入数据。这将为你提供处理数据的基本概念,并将成为更多RFID RC522教程的基础。

Before we start programming, we first need to update our Raspberry Pi to ensure it’s running the latest version of all the software. Run the following two commands on your Raspberry Pi to update it.
在开始编程之前,我们首先需要更新我们的Raspberry Pi,以确保其运行所有软件的最新版本。在Raspberry Pi上运行以下两个命令来更新它。

1
2
sudo apt update
sudo apt upgrade

Now the final thing we need before we can proceed is to install the python2.7-dev package, simply run the following command on your Raspberry Pi to install it.
现在我们需要做的最后一件事是安装python2.7-dev包,只需在Raspberry Pi上运行以下命令即可安装它。

1
sudo apt install python2.7-dev

To begin, we must first clone the Python Library SPI Py and install it to our Raspberry Pi. This library helps handle interactions with the SPI and is a key component to this tutorial as we need it for the Raspberry Pi to interact with the RFID RC522.
首先,我们必须首先克隆Python Library SPI Py并将其安装到我们的Raspberry Pi。这个库有助于处理与SPI的交互,并且是本教程的关键组件,因为我们需要Raspberry Pi与RFID RC522进行交互。

Run the following two commands on your Raspberry Pi to clone the source code.
在Raspberry Pi上运行以下两个命令来克隆源代码。

1
2
cd ~
git clone https://github.com/lthiery/SPI-Py.git

With the SPI Py Python Library now cloned to our Raspberry Pi, we need to install it, this is incredibly simple, as all we need to do is change into its directory and run a simple Python command on our Raspberry Pi.
现在将Python Py Python库克隆到我们的Raspberry Pi中,我们需要安装它,这是非常简单的,因为我们需要做的就是更改它的目录并在Raspberry Pi上运行一个简单的Python命令。

1
2
cd ~/SPI-Py
sudo python setup.py install

Now that we have installed SPI-Py we can now clone our RFID RC522 Python code from the PiMyLifeUp Github. There are two files included in this repository:
现在我们已经安装了SPI-Py,现在我们可以从PiMyLifeUp的Github仓库中克隆我们的RFID RC522 Python代码。这个仓库中包含两个文件:

  • MFRC522.py which is an implementation of the RFID RC522 circuit.
    MFRC522.py是RFID RC522电路的一个实现。

  • SimpleMFRC522.py that takes the MFRC522.py file and greatly simplifies it.
    SimpleMFRC522.py采用MFRC522.py文件,并大大简化它。

To clone this repository, you can type the following two commands into your Raspberry Pi.
为了克隆这个仓库,你可以在Raspberry Pi中输入以下两个命令。

1
2
cd ~
git clone https://github.com/pimylifeup/MFRC522-python.git

With the repository now saved to our Raspberry Pi, we can begin programming for our RFID RC522. To start off with we will be showing you how to write data to your RFID cards by using the RC522. Simply go onto our next section to begin programming our first Python script.
现在将存储库保存到我们的Raspberry Pi中,我们可以开始为我们的RFID RC522进行编程。首先,我们将向你展示如何使用RC522将数据写入RFID卡。只需进入下一节开始编程我们的第一个Python脚本。

Writing with the RFID RC522

用RFID RC522写入

For our first Python script, we will be showing you how to write data from the RC522 to your RFID tags. Thanks to the SimpleMFRC522 script this will be relatively simple, but we will still go into how each part of the code words.
对于我们的第一个Python脚本,我们将向你展示如何将数据通过RC522写入你的RFID卡片。借助于SimpleMFRC522脚本,这相对来说是很简单的,但我们仍然会进入每个代码片段详细解释。

Begin by changing directory into our newly cloned folder, and begin writing our Write.py Python script.
首先将目录切换到我们新克隆的文件夹,然后开始编写我们的Write.py Python脚本。

1
2
cd ~/MFRC522-python
sudo nano Write.py

Within this file, write the following lines of code. This code will basically ask you for text to input and then write that text to the RFID Tag.
在这个文件中,写下面几行代码。这会要求你输入文字,然后将文字写入RFID卡片。

1
2
3
4
#!/usr/bin/env python

import RPi.GPIO as GPIO
import SimpleMFRC522

The first line of this segment of code helps tell the terminal how to interpret the file, and it lets it know that it should use Python when executing it and not something else such as Bash.
这段代码的第一行有助于告诉终端如何解释文件,它让它知道它应该在执行时使用Python,而不是其他的东西,如Bash。

Our first import, RPi.GPIO has all the functions needed to interact with the GPIO Pins, and we need this to make sure they are cleared when the script finishes running.
我们的第一个import,RPi.GPIO具有与GPIO引脚交互所需的所有功能,我们需要这个来确保在脚本运行完成后它们被清除。

The second import, imports in our SimpleMFRC522 library, this is what we will use actually to talk with the RFID RC522, it greatly simplifies dealing with the chip compared to the base MFRC522 library.
第二个import,导入了SimpleMFRC522库,这是我们将用来实际与RFID RC522进行通信的地方,与基本的MFRC522库相比,它极大地简化了处理芯片。

1
reader = SimpleMFRC522.SimpleMFRC522()

This line creates a copy of the SimpleMFRC522 as an object, runs its setup function then stores it all in our reader variable.
这一行创建一个SimpleMFRC522作为对象的副本,运行它的设置功能,然后将它存储在我们的reader变量中。

1
2
3
4
5
try:
text = raw_input('New data:')
print("Now place your tag to write")
reader.write(text)
print("Written")

Our next block of code we keep within a try statement, this is so we can catch any exceptions and clean up properly. Make sure that you retain the ‘tabs’ after try: as Python is whitespace sensitive, and it is how it differs between blocks of code.
我们的下一个代码块保存在一个try语句中,这样我们就可以捕获任何异常并正确处理。确保在输入时正确地使用tab或者空格缩进:因为Python对缩进敏感,并且缩进是用来区分代码块的。

The second line here reads in an input from the command line, and we use raw_input in Python 2.7 to read in all input and store it in our text variable.
这里的第二行是从命令行读入一个输入,我们使用Python 2.7中的raw_input来读入所有的输入并将其存储在text变量中。

With the third line, we utilize print() to notify the user that they can now place their RFID tag down onto the reader for writing.
通过第三行,我们利用print()来通知用户,他们现在可以将RFID卡片放在读卡器上进行书写。

Afterward, on our fourth line of code we use our reader object to write the values we stored in the text variable to the RFID tag, this will tell the RFID RC522 Circuit to write the text values to a certain sector.
之后,在我们的第四行代码中,我们使用reader对象将存储在文本变量中的值写入RFID卡片,这基本上告诉RFID RC522电路将文本值写入某个扇区。

Finally, on the 5th line of code, we use print() again to notify the user that we have successfully written to the RFID tag.
最后在第五行代码中,我们再次使用print()来通知用户我们已经成功写入RFID卡片。

1
2
finally:
GPIO.cleanup()

Our final two lines of code handle exiting of the script. Finally, always occurs after the try statement, meaning no matter what we run the GPIO.cleanup() function. These lines are crucial as failing to clean up can prevent other scripts from working correctly.
我们最后的两行代码处理脚本的退出。finally总是在try语句之后出现,意思是不管发生什么错误,都运行GPIO.cleanup()函数。这是至关重要的,因为清理失败可能会阻止其他脚本正常工作。

Once you have finished writing in your script, it should look something like below.
一旦你完成了你的脚本的编写,它应该看起来如下所示。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/usr/bin/env python

import RPi.GPIO as GPIO
import SimpleMFRC522

reader = SimpleMFRC522.SimpleMFRC522()

try:
text = raw_input('New data:')
print("Now place your tag to write")
reader.write(text)
print("Written")
finally:
GPIO.cleanup()

Once you are happy that the code looks correct, you can save the file by pressing Ctrl+X then pressing Y and then finally hitting Enter.
一旦你感到代码看起来是正确的,你可以通过按下Ctrl+X然后按下Y,然后最后按回车来保存文件。

Now that we have written our script, we will want to test it out. Before testing out the script make sure that you have an RFID tag handy. Once ready, type the following command into your Raspberry Pi’s terminal.
现在我们已经写好了脚本,是时候来测试一下了。在测试脚本之前,确保你有一个可用的RFID卡片。准备好之后,在Raspberry Pi的终端中输入以下命令。

1
sudo python Write.py

You will be asked to write in the new data, in our case we are going to just type in pimylifeup as its short and simple. Press Enter when you are happy with what you have written.
你将被要求写入新的数据,在我们的例子中,我们只是简单地输入pimylifeup。当你对自己写的东西满意的时候,按下回车键。

With that done, simply place your RFID Tag on top of your RFID RC522 circuit. As soon as it detects it, it will immediately write the new data to the tag. You should see “Written” appear in your command line if it was successful.
完成后,将RFID卡片放在RFID RC522电路的读卡区域上。一旦检测到,它会立即将新数据写入卡片。如果成功,你应该看到『Written』出现在你的命令行中。

You can look at our example output below to see what a successful run looks like.
你可以查看下面的示例输出以查看成功运行的样子。

1
2
3
4
[email protected]:~/MFRC522-python $ sudo python Write.py
New data:pimylifeup
Now place your tag to write
Written

You have now successfully written your Write.py script, and we can now proceed to show you how to read data from the RFID RC522 in the next segment of this tutorial.
现在,你已经成功编写了你的Write.py脚本,现在我们可以继续向你展示如何在本教程的下一部分中从RFID RC522读取数据。

Reading with the RFID RC522

用RFID RC522读取

Now that we have written our script to write to RFID tags using our RC522 we can now write a script that will read this data back off the tag.
现在我们已经写了我们的脚本来使用我们的RC522来写入RFID卡片,现在我们可以编写一个脚本来读取这个数据。

Let’s start off by changing the directory to make sure we are in the right place, and then we can run nano to begin writing our Read.py script.
首先,通过更改目录来确保我们位于正确的位置,然后运行nano开始编写Read.py脚本。

1
2
cd ~/MFRC522-python
sudo nano Read.py

Within this file, write the following lines of code. This script will basically sit and wait till you put your RFID tag on the RFID RC522 reader, it will then output the data it reads off the tag.
在这个文件中,写下面几行代码。这个脚本会一直等待,直到你将你的RFID卡片放在RFID RC522读卡器上,它将输出卡片上读取的数据。

1
2
3
4
#!/usr/bin/env python

import RPi.GPIO as GPIO
import SimpleMFRC522

The first line of code tells the operating system how to handle the file when a user executes it. Otherwise, it will try and just run it as a regular script file and not a Python file.
当用户执行它时,第一行代码告诉操作系统如何处理文件。否则,它会尝试运行它作为一个普通的脚本文件,而不是一个Python文件。

The first import is, RPi.GPIO. This library contains all the functions to deal with the Raspberry Pi’s GPIO pins, and we mainly import this to ensure that we clean up when the script finishes executing.
第一个导入是RPi.GPIO。这个库包含了所有处理Raspberry Pi的GPIO引脚的函数,我们导入这个主要是来确保在脚本执行完成后进行清理。

The second import is, SimpleMFRC522. This script contains a few helper functions to make it an awful lot easier to deal with writing and reading from the RFID RC522, without it the scripts would become quite long.
第二个导入是SimpleMFRC522。这个脚本包含一些帮助功能,使得从RFID RC522的写入和读取处理变得非常容易,没有它,我们简单的脚本将变得相当长。

1
reader = SimpleMFRC522.SimpleMFRC522()

This line is quite important as it calls SimpleMFRC522’s creation function and then stores that into our reader variable as an object so we can interact with it later.
这一行非常重要,因为它调用SimpleMFRC522的创建函数,然后将其作为对象存储到我们的reader变量中,以便稍后与之交互。

1
2
3
4
try:
id, text = reader.read()
print(id)
print(text)

This next block of code is contained within a try statement, and we use this so we can catch any exceptions that might occur and deal with them nicely. You need to ensure that you use the ‘tabs‘ as shown after try: as Python is whitespace sensitive.
下一个代码块包含在一个try语句中,我们使用它来捕获可能发生的异常,并很好地处理它们。你需要确保在try之后使用tab或者空格缩进,因为Python对缩进敏感。

The second line in this block of code makes a call to our reader object, in this case, it tells the circuit to begin reading any RFID tag that is placed on top of the RC522 reader.
这段代码中的第二行调用我们的读卡器对象,在这种情况下,它告诉电路开始读取放置在RC522读卡器顶部的任何RFID卡片。

With the third and fourth lines we utilize print() to print out the information that we received from reading the RFID Chip, this includes the ID associated with the RFID tag and the text that is stored on the tag.
通过第三行和第四行,我们利用print()打印出读取RFID芯片时收到的信息,其中包括与RFID卡片关联的ID和存储在卡片上的文本。

1
2
finally:
GPIO.cleanup()

The two last lines of code handle the termination of the script. The finally statement always triggers after the try statement even if we get an exception.
最后两行代码处理脚本的终止。finally语句总是在try语句之后触发,即使我们得到一个异常。

This try statement ensures that no matter what we run the GPIO.cleanup() function. It is quite crucial as failing to clean up the GPIO can prevent other scripts from working correctly.
这个try语句可以确保不管发生什么,都会运行GPIO.cleanup()函数。这非常重要,因为如果未能清除GPIO,其他脚本将无法正常工作。

Now that you have finished writing your Read.py script for your RFID RC522 it should look something like what is shown below.
现在你已经完成了为你的RFID RC522编写Read.py脚本,它应该看起来如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
#!/usr/bin/env python

import RPi.GPIO as GPIO
import SimpleMFRC522

reader = SimpleMFRC522.SimpleMFRC522()

try:
id, text = reader.read()
print(id)
print(text)
finally:
GPIO.cleanup()

Once you are sure you have entered the code correctly, you can save the file by pressing Ctrl+X then pressing Y and then finally hitting Enter.
一旦你确定你已经输入了正确的代码,你可以通过按Ctrl+X然后按Y,然后最后按回车来保存文件。

Now that we have finally finished our Read.py script we need to test it out. Before we test out the script, grab one of the RFID tags that you want to read. Once that you are ready, type the following command into your Raspberry Pi’s terminal.
现在我们已经完成了我们的Read.py脚本,我们需要测试它。在我们测试脚本之前,准备一个你想读的RFID卡片。一旦你准备好了,在Raspberry Pi的终端中输入以下命令。

1
sudo python Read.py

With the script now running, all you need to do is place your RFID Tag on top of your RFID RC522 circuit. As soon as the Python script detects the RFID tag being placed on top, it will immediately read the data and print it back out to you.
现在脚本正在运行,你只需将你的RFID卡片放在RFID RC522电路的顶部。一旦Python脚本检测到RFID卡片被放置在顶部,它将立即读取数据并将其打印回给你。

An example of what a successful output would look like is displayed below.
下面显示了一个成功输出的例子。

1
2
3
[email protected]:~/MFRC522-python $ sudo python Read.py
7843705425
pimylifeup

If you successfully receive data back from your Read.py script with the text that you pushed to the card using your Write.py script then you have successfully set up your Raspberry Pi to connect with your RFID RC522 Circuit.
如果你成功从你的Read.py脚本接收到数据,并使用你的Write.py脚本将文本推送到卡上,那么你已经成功设置了Raspberry Pi来连接RFID RC522电路。

后记

We will be going into more depth with these scripts and the RFID chip in later tutorials. It will include exploring how to set up an attendance system among other cool DIY Pi projects.
在稍后的教程中,我们将会深入探讨这些脚本和RFID芯片。这包括探索如何在其他酷派DIY Pi项目中设置考勤系统。

If you have enjoyed this Raspberry RFID RC522 tutorial or have any feedback, then feel free to drop a comment over at our forum.
如果你喜欢这款Raspberry RFID RC522教程,或有任何反馈,请随时在下面留言!


本文翻译自:How to setup a Raspberry Pi RFID RC522 Chip

🍭支持一根棒棒糖!
0%