Introduction: RFID Jukebox
This is my entry in the "Audio" contest - if you are interested by this then please throw me a vote!
This post will attempt to create a "scroll friendly" version of the how-to video included at the top of this post.
The video goes into great detail about the control flow and which software I'm using. Take a look at that if you would like to learn more.
The flow is as follows:
Normal Operation - Reading Cards and Triggering Spotify to Play Songs:
1) RFID tag is read by a compatible card reader
2) Card reader spits out serial data for each card to an Arduino
3) Arduino "Serial prints" the data it receives to a raspberry pi
4) Pi receives card ID from Arduino. Pi looks up the card ID in a table to find the corresponding Spotify URI
5) Pi calls Spotify API to play selected URI
Setup Phase: Associating RFID Tag with a Spotify URI
1) through 3) from above
4) Pi expects a command line arg with the specific Spotify URI you wish to associate with the card
5) Pi creates a new entry (or overrides if card ID is already found in table) for the cardID,SpotifyURI pair
So you are not "programming" these tags. These tags have their own build-in microcontroller that modulates the field coming from the reader's coil to the tune of it's specific ID. All you are doing is creating a dictionary of key,value pairs for the pi to read from.
For example, let's say I have a card with ID "2_54_57_53_23_33_3". I don't initially know what the ID is, but when I read the output from the Arduino after scanning it, this is what comes out.
Let's say I want to make this card play Al Green's greatest hits album when it is scanned. To accomplish this, I would go to Spotify and copy the album's URI, getting "spotify:album:6W0V8B0fJItvOwC8v114rZ". Now, as seen in the video, I supply this URI to a python program to associate the card ID of "2_54_57_53_23_33_3" with the spotify URI of "spotify:album:6W0V8B0fJItvOwC8v114rZ" in a table. The "Key" is the card ID, and the "value" is the URI.
Now, when I deploy the main program, and my pi receives a card ID of "2_54_57_53_23_33_3", it will look it up in the table, gather the URI associated with it, and supply the URI as an argument to the Spotify API.
Dedicated computer (raspberry pi is best for this tutorial)
Optional but recommended: A spare Arduino as a middle-man between the reader and the computer to save development time
Step 1: Code Overview
The attached screenshots show the basic code structure for interfacing with Serial on the Arduino and pi. The advantage of using the Arduino as a middle man is that I can plug it directly into the Pi's USB port as a serial input. This saved me a ton of headache since now I don't have to know how to setup the GPIO pins as serial input.
My particular card/reader combination gives me the unusual result that all cards begin with a 2 and end with a 3. This gave me a quick and dirty way of knowing when a transmission is starting or ending. Depending on the cards/reader you have, your code will have to change.
Step 2: Spotify Integration
Now for the hard part - interfacing with Spotify. I relied heavily on 3 resources here:
1) The Spotifyd project, which allows your device to appear as an extra speaker to your Spotify account.
2) The Diskplayer project, in which some random guy wrote his own go scripts for interfacing with the Spotify API
3) This beautiful man who goes over OAUTH with Spotify. Without him I would have wasted so many more hours.
First install Spotifyd and test it to make sure the pi shows up as a speaker to your account.
Then, once that works, I used Diskplayer to test the spotify API. It is here that it will ask you to supply a Spotify API token. If you're running a headless setup like me, you will be unable to open it in a browser on the pi. For this reason, you will need to follow the steps listed in the video above in step 3). This project would not have happened without the man in that video!
Step 3: Making It Look Nicer!
Once everything works, you will be left with a mess of wires that plays anything you want from Spotify. To clean it up, put everything in a case, and print some labels for your cards! I used Avery 22822 labels (which come with a blank Photoshop template for printing at home). I was able to pull images from google, and slap them in the photoshop template as needed. After a few hours of learning Photoshop, I printed and stuck the labels on my blank RFID tags.
I had a lot of fun with this Project and will use it in the future to entertain people at my apartment. I\Let me know if you end up making it yourself!
I'm entering this in the "Audio" contest -- if you enjoyed it feel free to vote for this. Thanks!
Participated in the
Audio Challenge 2020