Introduction: Raspberry Pi Based RFID Music Robot
A couple of weeks ago, I saw this really cool Raspberry Pi based RFID-enabled cassette player. It ran PiMusicbox and the maker created a module to control PiMusicbox via RFID. This would be perfect for my three year old daughter. Ok, as she's clearly not familiar with cassette players, I needed something simpler. She already had a KNG Andrew MP3-player, that plays MP3's from an SD-card and is controlled via touch buttons. For those who are not familiar with this player, it's simply an SD-card based MP3-player with built-in speakers that looks like a robot.
So, the idea was born, an RFID controlled Raspberry Pi based music robot, or short RFID Music Robot (that's how we refer to it at home). My daughter really loves her music robot, when she wants music, she chooses the tag she wants, holds it near the robot and it will play her favourite music!
Although PiMusicBox is a very easy to install music player for the RaspberryPi with many installation guides, I could not find a guide how to make PiMusicBox respond to RFID's. Therefore, I decided to create something similar to the RFID cassette player. As my Python programming skills are around beginner's level area, building a PiMusicbox module was clearly a bridge too far for me.
That it's not that difficult to connect an RFID-reader to a Raspberry Pi, I learned this from another great instructable: https://www.instructables.com/id/Attendance-system-...
My instructable will show how simple it is to create a Python program that plays music triggered by RFID-tags, so you can make one as well!
Step 1: Materials/tools
For this instructable, you'll need the following:
- KNG Andrew Home Invader
- Note that you actually only need an amplified speaker to connect to the headphones output
- So it should work with any amplified speaker with an input that you can connect to the Raspberry Pi
- For better sound quality, have look at the cool hifi audio DACs
- Note that I needed the smallest Raspberry Pi, so it would fit in the KNG Andrew
- Any other Raspberry Pi can be used as well
- Actually, a 'B' might be easier, as you can connect a keyboard and wifi dongle simultaneously (for an A you could use a powered USB-hub of course)
- Around $3 on ebay including blank card and key ring tag
- Choose Mifare 13.56MHz tags/tokens/cards
- You'll need one tag per function / playlist / audiostream / action
- So, decide for yourself how many you need
- Currently, I already have 12 tags in use
- Initially, I figured 5 would be enough
- I used a Micro USB Host Cable Male to USB Female ( < $1 on eBay)
- A regular Raspberry Pi power supply is also ok, downside is that you most probably have two power supplies, one for the Raspberry Pi and one for the amplified speakers
- To connect Raspberry Pi to the amplified speakers
- Soldering station
- Glue gun
- Screw driver
Step 2: Electronics
The electronics are actually very simple, as there are only four major components:
- Raspberry Pi
- MF RC522 reader
- Power supply
- Amplified speakers
In the Fritzing diagram, you can see how to connect the Raspberry Pi to the RC522 reader. I connected the two using regular Dupont wires. After making sure all components work as expected (always test the electronics before you put it into a case), I used tape to combine the single headers. This will make it much more easy to reconnect when you glued the components in place.
Step 3: Assembly
The music robot looks quite large on the outside, however, the speakers take up a lot of space. That's why a Raspberry Pi A+ has been used for this build, as it fits perfectly. In the future, I might decide to use the Raspberry Pi for another project, so all connections are made via the regular connectors. The headphones output is connected to the audio input of the KNG Andrew (or any other amplified speaker set). For this, I used a short 3.5mm male-to-male cable and cut off one of the connectors. The wires in the cable are connected to the audio input connector (black is ground, white left channel, red right channel). Note that the tip is the left channel, the middle the right channel and last one is the ground.
A very short micro-USB to USB cable provides power to the Raspberry Pi. With a sharp knife, I cut open the regular USB connector, so I could connect wires to the power pins. As the KNG Andrew is powered via a regular (mini-) USB connector, it was very simple to find a +5V and GND on the internal KNG Andrew PCB.
There was not enough room in the belly of the music robot to simply connect the audio and power connectors. Therefore, two holes at the top should fix this issue (so the rest of the connector/cable ends up in the head of the robot). Using a permanent marker to indicate the exact spots for the holes, made it easier to drill the holes. As I didn't want to make the hole too large, I started with a small drill bit and used larger drill bits until the hole was large enough.
The RFID reader is glued in place with a glue gun. Note that the header is soldered on to the back of the reader. This makes it possible to put the reader the closest to the outside as possible. Then, connect the taped Dupont wires to the reader and the other ends to the Raspberry Pi. Last steps are gluing the Raspberry Pi in place, connect the audio and power connectors and close up the case!
Step 4: Installing the Raspberry Pi
First of all, you need a recent version of Raspbian installed to the Raspberry Pi.
In the raspi config, activate the audio output via headphones and enable the SPI interface (both can be found in the Advanced options menu).
Make sure the Raspberry Pi is connected to the internet. For setting up a wifi connection, please check the following link.
It's always good to be up-to-date, using:
sudo apt-get update
For installing Python modules, the development packages are required:
sudo apt-get install python2.7-dev
For the MFRC522-python interface, SPI-Py has to be downloaded and installed:
git clone https://github.com/lthiery/SPI-Py
Enter the SPI-Py directory and install it via:
sudo python setup.py install
Next up is the MFRC522-python module:
git clone https://github.com/mxgxw/MFRC522-python
Enter the MFRC522-python directory and install it via:
sudo python setup.py install
Finally, as this solution is made around mplayer, you'll need mplayer of course:
sudo apt-get install mplayer
Please check whether mplayer works fine on the Raspberry Pi when it's connected to the speakers.
Step 5: RFID Enabled Player
In the previous step, MFRC522-python was cloned. This resulted in a new folder named MFRC522-python. Put the rfidmp3player.py file into this folder. Make sure the file is executable:
chmod +x rfidmp3player.py
One of my tags makes the robot tell it's IP-address (or the time, or what ever sentence you like). For this, I use festival, a very simple to use text-to-speech program. You can install it on the Raspberry Pi using:
sudo apt-get install festival
This is a good moment to try whether the rfidmp3player script works. For this, mplayer should be in slave-mode. And to be able to control mplayer, a named pipe is required. What the rfidmp3player program does, is write commands to the named pipe. To test the script, this is what you'll need to do:
sudo mkfifo /tmp/mplayer-control
sudo mplayer -slave -input file=/tmp/mplayer-control -idle
Now, when you hold (a Mifare) RFID-tag to the reader, the Python script will show it's card ID. This ID is an array of 5 integers. In the screenshot, you'll notice that the tag I used for my testrun, triggered an audio stream. What a RFID-tag triggers is defined in the rfidconfig.txt, which will be explained in the next step.
As -in my setup- no screen or keyboard is connected to the Raspberry Pi, it's important that rfidmp3player.py is started from startup. Edit /etc/rc.local (don't forget sudo for this) and add the following before the exit 0 statement:
# Create named pipe for mplayer
# Start mplayer in slave mode
mplayer -slave -input file=/tmp/mplayer-control -idle &
# Tell host IP
hostname -I | festival --tts # Start Python RFID-reader-mplayer-control script
The 'hostname -I | festival --tts' statement will read the current IP-address out loud. This helps you in two ways, first, you know the IP-address to connect to and second, you know that the Raspberry Pi is now up-and-running!
Step 6: Rfidconfig.txt
All RFID triggered actions are defined in the rfidconfig.txt file.
Each RFID tag has the following attributes:
[aaa,bbb,ccc,ddd,eee] Unique tag Id
Description:Description, so you know what this tag should trigger
FileUrlFunction:This is either a file (single file or playlist), URL or function (for either mplayer or OS)
ActionType:Either OS, URL, File, Playlist or Function
TagType:This is actually ignored. I simply numbered my tags with a permanent marker and then I entered that number in this attribute.
Example rfidconfig.txt file can be found below.
Use OS for triggering an activity on your Raspberry Pi. Please note, that the commands will be run as root, so be careful! Use this action type for letting the robot tell it's IP address, telling the current time or shutting down the Raspberry Pi.
ActionType: File / URL
Simply put the location of a media file or stream. Note that you cannot use relative paths, so include the full path for a file.
Simply put the location of a playlist. Same as with a file, provide an absolute path. Within the playlist, you can use a relative path.
Use this for sending a specific command to mplayer. For all commands, please check https://www.mplayerhq.hu/DOCS/tech/slave.txt
Examples are: pause, pt_step 1 (next song in playlist) and pt_step -1 (previous song in playlist)
Step 7: Enjoy!!!
Now it's time to enjoy your RFID-music player! Initially I just wrote a number on each tag, but that's not very helpful for a three year old. So, I pimped the tags with nice stickers, using a label writer.
You could also crack open a tag and put the actual RFID-antenna and IC into an object, for example a CD case! How cool would it be to just simply hold a CD case to the reader and the music of that CD's starts playing?
As the range of the reader is not that good, I put a sticker with an RFID-logo on the outside of the robot. This is also the only change to the KNG Andrew you can see from the outside. All standard functions still work, so I can play MP3's from an SD card and also use the robot as speakers for phone. This may look like a 'clean hack', which is a good thing. However, when I demo it, not everyone is convinced that the RFID functionality has been added by me... :-)
Ok, at night, you can see the red reader LED through the white plastic.
The best thing is that my daughter loves it and I hope you like it too!
Second Prize in the
Raspberry Pi Contest
roaldh made it!