Introduction: USB RFID + Python + Pub-Sub (MQTT)
This guide will show you how to setup a USB RFID Reader that uses MQTT to broadcast scan information.
The code uses the 'RFIDIOt' RFID library to read tags and the 'Nyamuk' MQTT client library to publish the information.
Step 1: Installing RFIDIOt
Installation
Download the RFIDIOt library at https://github.com/AdamLaurie/RFIDIOt
The library has a lot of dependencies which may make installation difficult. It is therefore recommended that you use the 'Getting Started' section of the documentation as guide to installation.
Documentation: http://www.rfidiot.org/documentation.html
Testing your installation
With the reader plugged in, place a tag on the reader and run the example script 'cardselect.py' (included with RFIDIOt library). Information about the card reader and tag should be outputted.
Step 2: Installing Nyamuk
Installation
Download the Nyamuk library at https://github.com/iwanbk/nyamuk
or
pip install nyamuk
Testing your installation
- Download, install and run the MQTT client 'WMQTT Utility': http://mqtt.org/wiki/doku.php/ia92
- In the 'Broker TCP/IP address' field enter 'test.mosquitto.org' (without quotes) and click connect. The red circle on the left should now be green.
- In the 'Subscribe Topic' field enter '/nyamuk/test' and hit the 'Subscribe' button
- Download and run the following script and you should see the above output in the python shell and the WMQTT Utility.
Attachments
Step 3: Combining What We've Done
Now that we've got an RFID reader and pub-sub client working separately, it's time to combine the two functions to achieve the end result. The following code does exactly that.
Here's how it works:
- When the script starts the 'open_reader()' function is called which opens the card reader or exits the script upon failure.
- Displays card information
- Connects to the MQTT server of quits upon failure.
- Begins the main loop
- The listen_card(card, interval) function starts a loop that checks a card is sitting on the reader. Once a card is found, the card ID, time and date, and action (placed or removed) is published and the function returns.
- The listen_remove(card, interval, card_id) function works the same as listen_card, however, the data is publish when the card is removed.
- The loop repeats.
Attachments
Step 4: Finishing Touches
If everything has gone smoothly you should have a functioning RFID reader that publishes information about the tags it reads!
But why would we waste a computer for such a small task?
Our setup can be transferred over to a Raspberry Pi; a small ARM GNU/Linux box that costs only $35 (model B).
Here's what to do:
- Setup the Raspberry Pi: http://www.raspberrypi.org/quick-start-guide
- Download and install the RFIDOt and Nyamuk libraries.
- Copy over the code
- Run the code
- (Optional) Remove the screen, keyboard, mouse (not needed once running).
8 Comments
7 years ago
Hi,
Great post.
This is a request for guidance.
My
ESP8266 module connects to “test.mosquitto.org” and subscribes to a
topic say “b1”. What ever is published to “b1”, it can read it
immediately.
Problem happens when my second ESP8266 module subscribes to “test.mosquitto.org” to “b1”. It continuously returns :
WiFi connected
IP address:
192.168.0.9
Attempting MQTT connection…connected
Message arrived [b1] 0
Attempting MQTT connection…connected
Message arrived [b1] 0
Attempting MQTT connection…connected
Message arrived [b1] 0
Attempting MQTT connection…connected
Could you kindly suggest what might be going wrong. I look forward to your advice.
7 years ago
Can you confirm the power supply you're using?
9 years ago on Introduction
Being a bit of Linux newb but a big Pi Fan I'm struggling at step1, pathetic I know. I have the reader pictured above. The Pi is detecting the device a quick lsusb lists it as
Bus 001 Device 005: ID 072f:2200 Advanced Card Systems, Ltd
I've installed various libs:
libpcsclite1
libpcsclite-dbg
libpcsclite-dev
libccid
and all the various python libs using PIP (I think).
However a quick 'ls /dev/tty*' shows that I don't have a ttyUSBx, there are no devices listed here. Due to this running the test script returns there are no card readers connected.
Any suggestions as to where I'm going wrong?
Reply 9 years ago on Introduction
were you able to solve this issue? I'm having a similar problem where I can't install pcsc-lite since it's not compatible with raspbian.
10 years ago on Introduction
I found the reader here...
http://nfctags.tagstand.com/products/usb-nfc-reader-writer-acr122u
I'm sure there are other places to buy it as well.
10 years ago on Introduction
This was a student project. The aim is to connect this to a gumball machine we have modified and have it deliver you a random number of gumballs when you swipe your smart card over the reader. For some of our colleagues, this could be a very, very low random number (0) most of the time. :)
10 years ago on Introduction
what type of reading device do you use?
10 years ago on Introduction
will you use this to build something else? what are you making?