TLDR: Setup magic-cards (https://github.com/maddox/magic-cards) to call a script containing a go-cast (https://github.com/barnybug/go-cast/blob/master/README.md)command to stream locally hosted music to a google cast device or group.
My dad saw the video by /u/hovee on the 23rd December with us flying to Sydney on the 26th. So I decided I was going to sort it/something that worked the same for us. So I apologise that this is not at all an efficient or even a close to impressive method of doing this. Due to us not having Google Play Music, that was out of the question. So here is how I did it. It’s not ideal, but I can scan a card and it plays music sooooooooo.... Also this is the first instruction set I’ve ever made for programming something so I’m sorry if it sucks. I’ll create a discord so you can ping questions and if I can help I will or others might be able to. Here’s a link: https://discord.gg/tK6n3hh
First off I’m going to go over how this looks and how it all fits together in a kind of diagram format:
I promise I'll actually make a diagram of this one day
Card —(scan)—-> RFID reader —-(USB)—>Pi—(cards.json)—->identify card (magic-cards) —-(actions.json)—>call script—-(go-cast)—> call locally hosted music (Apache) —-(go-cast)——> Output on Google Cast Group
- Raspberry Pi 3 (Might work on older models or even Pi Zero (W), who knows. Try it, let me know)
- 16GB SD Card (Depends on your preference but at least 8GB but you’ll be storing music on this if you follow my instructions)
- Official Pi 3 power supply (I’m fancy okay)
- 125KHz RFID reader - (Amazon)
- 125KHz RFID cards - (Amazon)
1. Download [this software](https://www.dropbox.com/s/ena4ukh9wewhj9x/rfid-reader-programmer.zip?dl=0) on a Windows PC or Laptop.
2. Plug the RFID reader into the Laptop or PC USB port and open the software.
3. Make sure that the buttons shown in the image below are selected, then click
set
.
Now the reader is good to go for the Pi, but it might be worth leaving it plugged into the Windows device for future steps.
(Pro-tip form magic-cards: Buy 2 RFID readers so that you can have one connected to the Pi and one connected to a Windows PC to enable creating cards to be easier.)
If you’re on Mac or Linux you can use the standard terminal but for Windows you’ll need to download putty (putty link). Find the up of the Pi from the router or from the gui and then ssh to it. There are many guides online about this. Here’s one.
For all file transferring I would recommend using a program with an interface to easily transfer from your PC/Mac to the Pi, such as CyberDuck with SFTP.
There's a set of instructions on the magic-cards install page which you can use instead of these.So make sure you have git installed:
sudo apt-get install git
I can't find the exact commands I used to install node, but I know I used the nvm manager and had to use the latest version of node (9.11.1 at the time of writing this. But just check online for how to install node, then run the next command. I'll update this if somebody finds commands which work exactly).
Then install yarn:
npm install yarn -g
Check out this first repository (magic-cards) and see all the cool stuff, but I’ll copy the instructions across here for you to follow:
Navigate home:
cd ~
Clone the repo:
git clone https://github.com/maddox/magic-cards
Navigate into the folder:
cd magic-cards
Run the setup script:
script/setup
Run the install script:
script/install
Run the stop script:
script/stop
Another great repository to check out (go-cast) and this allows casting of media to the chrome cast devices. I’ll post instructions below again:Visit [this page](https://github.com/barnybug/go-cast/releases/tag/0.1.0) to find the version you need, from a Pi it is the
cast-linux-arm
.
Download your file (cast-Linux-arm for a pi): wget (link to download from page above)
E.G.wget https://github.com/barnybug/go-cast/releases/download/0.1.0/cast-linux/arm
Move the file:sudo mv cast-my-platform /usr/local/bin/cast
E.G.sudo mv cast-linux-arm /usr/bin/local/cast
Edit the permissions:sudo chmod +x /usr/bin/local/cast
Cast is now setup, check out the repo (https://github.com/barnybug/go-cast/blob/master/README.md) for usability and features etc. It’s really cool and powerful.
Install apache:sudo apt-get install apache2 -y
Test it from any device on the network: http://local_ip_of_raspberry_pi/
E.G. http://192.168.1.189/
Make a folder to store the music:sudo mkdir /var/www/html/music/
Edit permissions to allow easy interaction (probs super unsafe but it's local so idgaf): sudo chmod -r 777 /var/www/html
Your html directory is now here: /var/www/html/
And your music directory is now here: /var/www/html/music/
[youtube-dl](https://github.com/rg3/youtube-dl/blob/master/README.md) setup
Download the repo: sudo curl -L https://yt-dl.org/downloads/latest/youtube-dl -o /usr/local/bin/youtube-dl
Install it: sudo chmod a+rx /usr/local/bin/youtube-dl
[Magic-cards](https://github.com/maddox/magic-cards) configuration
Now go to the magic-cards config directory:
In here you’ll need 3 files: actions.json, config.json and cards.json. I’ll go into detail about how to make these and I’ll be posting mine as examples. (All this can be found on the magic-cards repo but I’m just going to explain it how I found it)
This file defines the usb event to use for the reader as well as the room and Spotify shit if you want it but I didn’t care for it, like I said check out the magic-cards (https://github.com/maddox/magic-cards) repo it’s probably way more powerful than I’m using it for.For this, copy my config file and ensure you use the correct event number which can be found by doing:
Navigate to the folder:
cd /dev/input/by-id/
List the devices:
ls -l
Then you should see an output like:
total 0
lrwxrwxrwx 1 root root 9 Dec 25 16:26 usb-SEM_USB_Keyboard-event-if01 -> ../event2
lrwxrwxrwx 1 root root 9 Dec 25 16:26 usb-SEM_USB_Keyboard-event-kbd -> ../event1
lrwxrwxrwx 1 root root 9 Dec 25 16:26 usb-Sycreader_USB_Reader_08FF20150112-event-kbd -> ../event0
Here the event I would be using is
event0
because the description refers to the reader.
This holds the possible actions to be taken on the scan of a card, so you can do Sonos stuff if you read the magic-cards repo but I’m just going to be calling scripts, each action named after the album/artist and each script named something similar but ending with .sh.
We'll be editing this file later on in the full run through. This is auto created if you use the online interface to create cards, but we’ll come onto that later because I had problems with the online interface so I edited the actions.json file directly.
My examples for these files are in this repo.
Now basically, I’m going to run through adding one card from beginning to end including (the possibility and instructions pls refer to the disclaimer above) downloading the music. I'm using a Watsky album for my examples and the local ip address of 192.168.1.189-
If you already have the music downloaded you can skip the youtube-dl stuff and just place the songs into the folder stated. Or you might need to 'cat' them if they're individual tracks.
-
After finding out the device name navigate to the config folder of the magic-cards repository you cloned. For me, I would run:
cd /home/pi/magic-cards/config
-
When here, create a .sh file with the name of your artist or album preferably:
sudo nano (album or artist).sh
E.G.sudo nano xinfinity.sh
-
When in this file you want to write the command to run to trigger the playing on your device. Before you make this file, feel free to test this command standalone to check if it works. But the command to put in the file is:
cast --name "(Name of Device or Group of Chromecast)" media play http://local_ip_of_raspberry_pi:80/music/(song title).mp3
E.G.cast --name "Kitchen Speaker" media play http://192.168.1.189:80/music/xinfinity.mp3
-
Exit and save the file:
CTRL+X
thenCTRL+Y
-
Modify the file so that it can be executed:
chmod +x (script file name).sh
E.G.chmod +x xinfinity.sh
-
At this point, I would test that the script works as intended. You should be able to run this command to have the desired output:
./(Script Name).sh
E.G../xinfinity.sh
So find the album you would like on youtube, either as one song long (heckin easier) or as a playlist which we will concatenate. But I understand that the easy method isn't always possible so I'll show you both dw).
(Also this technique sucks, I know, and I'm very open to ideas which doesn't mean that I have one super long song but like I said in the preface, I had little time and I will improve in the future, but it works.) Download the song with this command directly to the hosted folder:
youtube-dl --extract-audio --audio-format mp3 -o /var/www/html/music/(song title).mp3 (youtube link)
E.G.
youtube-dl --extract-audio --audio-format mp3 -o /var/www/html/music/xinfinity.mp3 https://www.youtube.com/watch?v=2kzI9ksEjx0&t=2846s
1. Make a new directory in the music folder:
sudo mkdir /var/www/html/music/(artist name)
E.G.
sudo mkdir /var/www/html/music/watsky
2. Download the playlist:
youtube-dl --extract-audio --audio-format mp3 -o /var/www/html/music/(artist name)/'%(autonumber)s.%(ext)s' (youtube playlist link)
E.G.
youtube-dl --extract-audio --audio-format mp3 -o /var/www/html/music/watsky/'%(autonumber)s.%(ext)s' https://www.youtube.com/watch?v=qaDuk0qna5g&list=PLblxhEQYeadH27CgeioDeuYgCpu9SbVbj
3. Wait for all the tracks to download correctly, the above command will number them all from 00001.mp3 -> 00XXX.mp3 depending on how many tracks there are, this makes the next step easier.
4. Go to the folder:
cd /var/www/html/music/(artist name)
E.G.
cd /var/www/html/music/watsky
5. Concatenate the songs:
cat 00001.mp3 00002.mp3 00003.mp3 00004.mp3 ...... > (song title).mp3
E.G.
cat 00001.mp3 00002.mp3 00003.mp3 00004.mp3 00005.mp3 00006.mp3 00007.mp3 00008.mp3 00009.mp3 00010.mp3 > xinfinity.mp3
6. Move the song to the main folder:
mv (song title).mp3 /var/www/html/music/
E.G.
mv xinfinity.mp3 /var/www/html/music/
7. Navigate to the music folder:
cd /var/www/html/music/
8. Clean up by removing the artist folder:
sudo rm -r (artist name)
E.G.
sudo rm -r watsky
Open up a browser on any device that is on the same network and head to this address to check that the web server is running correctly and the .mp3 is accessible: http://local_ip_of_raspberry_pi/music/(song title).mp3
E.G.
http://192.168.1.189/music/xinfinity.mp3
You should be able to click play or something and have the track playing through your device. if you've made it this far, I guess my instructions don't suck that much. Make sure to note down this address too for the next step. Okay now onto creating the script file which will be called when the card is scanned.
1. First of all we need to find out the EXACT name of the device we are going to be casting to. To find this run the command:
cast discover
This should produce an output of the Google Cast devices which have been detected on the network, groups too. make sure to note down exactly what is within the quotation marks. (Sorry, I'm doing this from memory) but this will be something like:
"Kitchen Speaker"
for me. So the thing to remember is Kitchen Speaker
.1. Open up/create the actions.json file:
sudo nano /home/pi/magic-cards/config/actions.json
2. It'll probably be entry, unless you've copied mine in, which has a load of entries. Basically to customise your actions, you need to make sure each enter looks like this:
"(ActionName))": {
"type": "script",
"filename": "(script name).sh"
}
E.G.
"InfinityScript": {
"type": "script",
"filename": "xinfinity.sh"
}
1. Navigate to the [m folder: cd /home/pi/magic-cards
2. Run the command to start it running:
script/restart
There are two ways to add Cards:
6a. Add the Card to the cards.json file:
1. Edit the cards.json file:
sudo nano /home/pi/magic-cards/config/cards.json
Use the format below to add the correct details (Once again, check my cards.json file to see):
{
"code": "",
"type": "album",
"action": "",
"artURL": "",
"title": "",
"subtitle": "",
"uri": "",
"id":
},
E.G.
{
"code": "090033639a",
"type": "album",
"action": XInfinityScript,
"artURL": "https://preview.redd.it/7nlnklgq1zgx.png?width=1024&auto=webp&s=bb1110b519e0f5869d8912d1b9f462105b67c92c",
"title": "X Infinity",
"subtitle": "Watsky",
"uri": "",
"id": 5217263514
},
RFID Code - This is the ID of the card and is crucial in the operation. There are 2 ways to get this
1. From directing to http://local_pi_ip_address:5000
E.G http://192.168.1.189:5000
2. Clicking 'Add Card'
3. Clicking in the 'Card Code' Box
4. Plug the RFID Reader into the computer you are on
5. Scan the card
6. Copy down the Code
Or
1. Connect the Pi to a Screen
2. Open a terminal
3. Plug in the RFID Reader
4. Scan the card
5. Copy down the code
Action Name - The name of the action to call (Step 3)
Link to album art (Optional) - Url to image of the album art
Album Title (Optional) - The title of the album just made
Artist (Optional) - The Artist of the Album
ID - 9 digit unique number
6b. Through the GUI:
1.Visit the hosted server site in a browser: http://local_pi_ip_address:5000
E.G. http://192.168.1.189:5000
2. Click 'Add Card'
3. Either paste in an itunes link at the top (This has been temperamental for me) or fill in the details of your album.
4. Click the 'Card Code' Box
5. Scan the Card and the box should auto-fill
6. Save the card
cd /home/pi/magic-cards
2. Run the restart script:
script/restart
1. Scan the card you've configured for: *boop*
Check the PDF I uploaded to refer yourself to the below steps.
Troubleshooting (1):
There’s a problem with the card or the reader being detected.
* To check the reader connect the Pi to a screen, ensure the command line is open, tap the card on the reader, if you get an output then the reader works.
* Check the output in the command line is the same as that in the cards.json file.
* If that is all correct, then check the config.json file has the correct even defined. Check step <ls -l devices step> to find the event number.
* If you don’t get an output to the command line then the card reader isn’t connected or it is brok.
Troubleshooting (2):
There’s a problem with magic-cards calling the script.
* Check actions.json is calling the correct .sh file
* Check the .sh file is in the config folder
* Check cards.json to ensure the card is calling the correct action.
Troubleshooting (3):
There’s a problem with running the script
* Ensure you’ve edited the .sh file with the chmod +x command
* Ensure it is a script file
Troubleshooting (4):
There’s a problem with the command in the script file
* Ensure that it is using the correct http or https
* Ensure you’ve got the port included in the script command.
Troubleshooting (5):
There’s a problem with the indexing and structure of your web server.
* Ensure you’re directing yourself to the same folder as follows /var/www/html/
* Ensure the .mp3 is in the correct folder
Bonus Error: ‘Can’t connect to the device’ error.
Make sure that you can discover the device and the name is the same as in the ‘cast discover’ output.