Build A Better Jukebox
As with most projects that are taken on, the conversation starts with a “wouldn’t it be cool if . . .” or “it’s pretty ridiculous that . . . “, or some sort of combination of those two statements. That’s what started my current project. In the office, we have a single speaker that is connected via bluetooth to a device usually streaming a shared Spotify playlist. Many times, this is my computer. And inevitably, I go into an office and jump on a conference call or google hangout, am not able to hear audio, and will be in the middle of trouble shooting before someone comes running in to let me know that the call is being broadcast through the speaker in the main room. Awesome. The other issue could not be more first world. We have been using my Spotify account to create and add to as a community shared playlists. This is such an awesome Spotify feature. Spotify also does an amazing job of suggesting songs you may like based on your listening habits. Since this algorithm is being influenced by the collective, and many times eclectic music in the shared playlists, my suggestions when listening at home are heavily skewed towards ’80s hair metal and ’90s HipPop™.
So the conversation started. “Isn’t it ridiculous that only one person can connect to the speaker at a time? What year is this?” And then, “Wouldn’t it be cool if we could just have a Spotify server running that anyone on the local network could hit and control the musical selection?” That is literally music to my ears! Pun intended. So, now we know the first world problem we need to tackle. Let’s figure out what tools we need to be successful.
The Raspberry pi
This one’s a no-brainer. I have been looking for an excuse to pick up a Raspberry Pi 3. Really excited about the built in wifi and bluetooth. All previous pis I have gotten have the Achilles heel that is the wifi dongle. I’m looking forward to seeing if having these built directly onto the board will help improve connectivity. I have typically opted to spend the extra $10ish or so for some sort of pi package on Amazon that includes a power cord, a case and a heat sink. This is the one I got for this project — https://www.amazon.com/gp/product/B01F2O2ZHO. I have enough mini-sd cards laying around, but there are also packs that include one of those. The only thing I caution is, if you’re not careful you could end up spending about $40 extra on an HDMI cable and SD card. It may be a better idea to get the minimum for the project you need and buying the cables and cards separately.
This is where all the magic happens. So first thought was, let’s really take this project on and build a Python server that integrates with Spotify’s API from the ground up. It’s worth mentioning here that in order to make use of the Spotify API, you have to have a paid Spotify account, aka, Spotify First Class. It won’t work on the Economy class plans. While building my own would include designing a user interface, which is not my strong point, it would be a terrific learning opportunity. And this is still something I want to take on someday, there are just too many resources out there that already do a great job. Most roads lead back to Mopidy, a python server that handles all the business on the backend. This is what we are going to use on this project. We will use SpotMop as the front end. More on these later.
Time to Build
Ok, now that we have the game plan together, let’s gather the materials, prepared the software and get everything installed.
- First we prepare the pi which is by far and away the easiest step. This is just unpacking things, put the hardware into the case, and if you already have a raspbian image on an Sd card laying around, pop it in and see hook it up to a monitor (gonna need an HDMI cable for this one if you do it) and make sure all boots up and the hardware is working.
- Second, let’s download the latest Raspbian for Raspberry image. In this case, we are using Raspbian Jessie Lite as the OS. This is what needs to get written to the SD card. Once this takes place, we can connect the pi to the network via ethernet cable and everything else we need to do can be done in the terminal.
- Now it’s time to prep the SD card. Again, many tools out there to help do this. Basically, you need to format the SD card and then write the image onto the card. Mac, Windows and Linux all have tools for this baked into the operating system, but many times that is the intimidating part for people. Again, there are plenty of tools that make life so much easier. I have used Pi Filler a few times and it works terrifically on Mac OS. A couple of clicks will format and write the image. Done and done.
- At this point, we have a full linux OS on the pi, but there is no interface to the system. So we need to connect the pi to our local network and access the system via SSH. Remember that we have not setup the wifi on the pi yet, so you will need to first plug in an ethernet cable connected to your router.
- First we find the IP address of the pi. I am sure there are a few ways of doing this, but the easiest for me was to log into the router and find the device (raspberry) which will list its IP.
- Then we open up our terminal and simply type:
- ssh pi@yourIPaddress
- You will be prompted for a password which is raspberry by default.
- Once logged in, you can type sudo raspi-config to run the setup for the machine. This is a good guide as to what needs to be changed at a minimum.
- Next we follow the steps to install Mopidy. Basically, typing the following will get you most of the way there.
- wget -q -O — https://apt.mopidy.com/mopidy.gpg | sudo apt-key add -
- sudo wget -q -O /etc/apt/sources.list.d/mopidy.list https://apt.mopidy.com/jessie.list
- sudo apt-get install mopidy
- The extension that we care about the most is the Spotify one, so let’s install that next by typing
- sudo apt-get install mopidy-spotify
- Then there are a few config settings that need to happen that are throughly covered here — https://docs.mopidy.com/en/latest/config/
- While this step seems simple, it is likely where most people will get hung up on. Following the instructions linked above should get you where you need to be. It may be a good idea to read a bit about Nano, the default text editor for Linux systems, as you will be editing files within the terminal.
- Now that about covers the server/back end of the system. Now we need a UI for folks. SpotMop is going to be the choice for this. Very easy to install by typing
- sudo pip install Mopidy-Spotmop
- This is a terrific UI. Has the ability to create and edit playlists and looks really good. If you run into a problem because the system does not know what pip is, run
- sudo apt-get install python-pip
- and you should be good to try again.
- One last thing. Now we need to setup the pi to work as a “service”, meaning that every time it boots up, it will automatically start the server and begin listening on http://raspberry.local:6800/spotmop (or whatever you have configured as your hostname.local). Follow the steps HERE to get the service setup.
Things to consider
- Setting up the pi to automatically connect to wifi is really easy to do and should be done.
- You should also assign the pi a static IP. This can be done on your router.
Let there be sound
The audio out on a pi is pretty simple. An aux cable out is super easy. Our speaker, however only has RCA inputs. But that’s why god invented Aux to RCA cable adapters! Once it looks as though the pi is up and going, the build’s default address will be http://raspberry.local:6800/spotmop. Of course, we should certainly go in and change the hostname of the device to make the url something a bit more, uhmmmm, memorable. Once connected, we are able to link up the Spotify First Class account and before we know it, all of the playlists are accessible and we are able to remotely control the music without needing to be connected directly to the speaker. More importantly, I don’t have to continue on with the knowledge that Spotify’s algorithm thinks I’m the world’s number one fan of both Ratt AND Mariah Carrey. (Spoiler, I’m a big fan of exactly half of those). Nooice!
- It would be nice to look into using the onboard bluetooth to connect to the speakers instead of the wired connection we have now.
- Track users additions to keep that person who added Stained to the current playlist accountable for their actions!
Originally published at Brian Dorton.