Can I Download Spotify Onto A Raspberry Pi
Turn your spare Raspberry Pis and speakers into a fleet of synchronized multi-room audio players. You can start from scratch or upgrade an existing balenaSound device to add the multi-room functionality for free!
You should now be able to use your Raspberry Pi to Stream music via Spotify Connect. Even the volume control works perfectly.To change the device name in the Spotify connect dialog, simply change the hostname with the raspi-config utility.
Contents
Before you start
Tutorial
Using balenaSound multi-room
Before you start
Updates
We’re continually updating this post along with the project code, you can find a log of all our updates below.
- 6th October 2020: Updated instructions for balenaSound v3.0
- 7th July 2020: Added Deploy with balena method and links to official app site for docs, troubleshooting, and more.
Introduction
balenaSound already allowed you to stream audio from multiple sources with minimal effort. Now you can have a fleet of devices playing perfectly synchronised audio all over your place without paying a premium.
The latest version retains the previous functionality, but now, if you have more than one device, the audio will be seamlessly and automatically sent to all the other devices in your application.
If you have a couple Raspberry Pis and some spare speakers or an old stereo, you can build your own Sonos stand-in; read on and give it a go!
Your tunes are sent from your phone/laptop to all the devices in your app via WiFi.
Hardware required
For each room you will need the following:
- Server device: Raspberry Pi 3A+/3B/3B+/4B (device support table)
- Client devices: Raspberry Pi 3A+/3B/3B+/4B/Zero W
- SD Card (we recommend 8GB Sandisk Extreme Pro)
- Power supply
- 3.5mm audio cable to the input on your speakers/Hi-Fi (usually 3.5mm or RCA). Alternatively the HDMI port provides digital audio out.
- Optional DAC (compatibility list)
Note: the Raspberry Pi Zero cannot be used on it's own as it has no audio output. To use the Pi Zero you'll need the optional DAC HAT to get the project working.
Software required
- A download of balenaSound from GitHub
- Software to flash an SD card (such as balenaEtcher)
- A free balenaCloud account to setup and manage the Pi
- (Optional) Download and install the balena CLI tools - to be installed on your computer, allowing us to install the project code on the Pi
Tutorial
In this guide we will only cover adding additional devices to your existing balenaSound application. If you want a detailed step-by-step tutorial on how to start from scratch you can check the original balenaSound guide).
The quickest way to get started
You can also use Deploy with balena to deploy balenaSound to a application within your balenaCloud account. Once you're logged in, use this button:
Otherwise, read on!
A multi-room audio streaming solution is no good if you only have one device in your fleet. For this project we are going to add several devices to our application, each device will stream audio to a different room. If you are asking yourself, “how many devices can I add?”, the answer is a simple one. balenaCloud is designed to handle fleets with thousands of devices, so you can add as many as you have (or until you run out of rooms in your house)! Remember that for each device you will need a speaker, headphones or a Hi-Fi system to hear the audio.
In order to provision your new devices you need to:
- Download the balenaOS disk image from your application page on the dashboard
- Flash it to an SD card
- Insert the SD card and boot your device
If you already have one or more devices running balenaSound, make sure to add the new ones to the same balena application, allowing them to download the same software automatically. No need to flash the SD cards again, they can be upgraded to the new version simply by pushing the latest software over the air!
Note: You can mix device types as long as your application type supports it. A great way of maximizing device compatibility is to select Raspberry Pi 1/Zero
as your application type. This enables you to add any board from the Pi family, and even the balenaFin. Read more about managing multiple device types in one application here.
When your newly provisioned devices boot, they will automatically register with balenaCloud. You should be able to see them on the dashboard:
We now need to get the latest version of balenaSound pushed to our devices. It doesn’t matter if you’re updating your old balenaSound installation or working with a brand new one, the process is the same. To push the updated code we will leverage the power of balenaCloud to do so without even touching the devices.
First, you need to get the new code. Either git pull
the changes from your local project folder if you kept it, or grab the entire codebase via a zip file download.
With the new code in place you can now push the update to your balena application by using the balena CLI. For a more in depth deployment guide you can check the original balenaSound setup tutorial. The main thing to note is that you don’t need to flash the SD card on your existing device again - just push the new code and it will update automatically!
Once the code is deployed balenaCloud will ensure all devices are updated to the latest version that you pushed. If you inspect a device on the dashboard you should see a few new services: audio
, sound-supervisor
, multiroom-client
and multiroom-server
.
Using balenaSound multi-room
Give it a try
And that is it! You should be able to connect to any of your devices and start streaming using any of the supported methods (bluetooth, AirPlay and Spotify Connect) without configuring anything!
Here is how to connect to a device:
- If using Bluetooth: search for the device on your phone or laptop and pair.
- If using Airplay: select the balenaSound device from your audio output options.
- If using Spotify Connect: open Spotify and choose the balenaSound device as an alternate output.
When you start streaming to a device, it will configure itself as the server
and will broadcast a message to all other devices to synchronize them and start playing the same audio. Note: it can take a few seconds for the system to autoconfigure the first time you start streaming from a new server
.
Troubleshooting balenaSound
Did you know that balenaSound now has documentation, a troubleshooting guide, and an FAQ? Learn more about the project or find ways to contribute by visiting the official balenaSound app site.
What next?
While you can use any device type (except the Pi Zero) as the master
, it’s recommended that you use a Pi 4 (read more about the reasoning on the project main read me file). If you have multiple devices it can be hard to figure out which is which, so we are going to customize the device name of one of our Pi 4s so that we can easily identify it when trying to connect to it.
To do that we just need to add a device environment variable using the balenaCloud dashboard, set the variable SOUND_DEVICE_NAME
to the name you desire:
Make sure you are only adding this variable to a particular device and not to the whole fleet (this is done from the device dashboard page rather than the application one).
One last quick tip, if you want to physically identify a device you can highlight it by blinking the onboard LED. This can be done by clicking the lightbulb icon on the dashboard:
Become a balena poweruser
Want to learn more about what makes balena work? Try one of our masterclasses. Each lesson is a self-contained, deep walkthrough on core skills to be successful with your next edge project.
Check them out at our docs. Also, reach out to us on the Forums if you need help.
Until next time…
Thanks for checking out this guide. We hope you were able to turn your place into a fully fledged audio streaming powerhouse!
If you had trouble getting the project running or have any other feedback we’d love to hear it; everything helps to improve our projects and tutorials for the next time.
If you would like to suggest new features or contribute more cool features to balenaSound feel free to drop by the GitHub repo.
You can always find us on our forums, on Twitter, on Instagram or on Facebook.
Alarm clocks are the devil and I hate them. Sleeping is the best, and I don't know why we willingly ruin such a delightful activity with terrible auditory violence. But I'm here to tell you there's something else: a Spotify Alarm Clock.
If I'm being honest, one day I hope to awaken in the mornings like a prince of Zamunda. Some day...
Until then, I'm making do with what I have.
[note: I'm no longer using my Raspberry Pi as my music playing alarm clock. As such, these instructions are no longer being updated; hopefully they will be helpful for at least a few more years. A fellow Raspberry Pi Alarm Clock maker reached out with some additional notes they used to troubleshoot the project and get it working. That person's notes will immediately follow, with the original post continuing afterward. ~CK]
Notes from a fellow clock maker
1) I couldn't get things to work (mpc commands would just say 'Could not connect' or something like that) so I installed a fork of mopidy-spotify instead of using the main branch, because people on this thread suggested I should. Here's how I did it:
It's possible this made no difference at all, since I still had to fix a bunch more things afterwards so I can't actually tell if mopidy-spotify was responsible for my woes. But the discussion on the above linked thread suggests that Spotify playlists wouldn't have worked without using BlackLight's fork.
2) I deleted all Spotify playlists with unconventional characters because I saw some errors in mopidy-spotify related to weird characters (e.g. emojis etc.). Again, not sure if this actually helped.
3) The following step was definitely necessary: I added mopidy as a user to video group. Until I did that, everything up to and including the point of doing 'mopidy add someplaylistidhere' worked, but 'mpc play' never did - it immediately said mpd error: disconnected or similar.
This is only a problem with running mopidy as a service via systemctl.
4) This step was also definitely necessary. I wasn't hearing any audio, but I knew from mpc that my Pi was playing it. I had to change audio settings in mopidy.conf (the version of it for mopidy as a service).
This thread showed me how:
Specifically, I added this to my mopidy.conf:
The original post continues, starting here...
My previous waking routine was powered by an old smartphone[1] and an app that played locally stored music. My new and improved waking routine is powered by a Raspberry Pi, the Mopidy music server, and Spotify playlists.
All told, the process wasn't too difficult: I got it working in about 20 calendar days, tinkering with it every few days.
I wasn't able to find one resource that walked me through the entire project; however, there are sites that do an amazing job of explaining particular parts of the process. When appropriate, I'll send you off to those other sites so you can read their amazing work in its native space.
Hardware and software I used
This isn't necessarily what you need to complete this project, just a list of the things that I happened to use for my particular setup.
Hardware
- CanaKit Raspberry Pi 3 Complete Starter Kit - 32 GB Edition (Amazon Link)
- Ethernet cable
- Network router
- Laptop with an SD slot[2]
- SD to Micro SD converter
- Old set of computer speakers
Software
- ssh (should be standard in Linux distros)
- Paid Spotify account
Note: I set up and run my Raspberry Pi without a keyboard or monitor.
Raspberry Pi assembly
Unboxing and assembling your Raspberry Pi is fairly straight forward. The instructions included in the box worked decently for me, if I remember correctly.
Learn from my mistakes
There were, however, two things that were confusing:
- You'll get two tiny heat sinks in the package -- they go onto the main processor chip and the wifi chip.
I knew, generally speaking, that heat sinks were for regulating heat. However, I'd never dealt with one up close and personal. As such, I wasn't exactly sure what to do with them. - Getting the Raspberry Pi into the CanaKit case was a touch tricky. The case is rectangular; on one of the short sides, the corners have lips that cut diagonally across the corner. You need to slide the Raspberry Pi under those two lips. Once you do that, the rest of the Pi drops into place and the top should snap on quite nicely.
Installing Raspbian
These links will help:
The easiest way to install an OS on your Raspberry Pi is to use the Micro SD card that comes with the CanaKit Raspberry Pi.
I did not use the easy way.
The Raspberry Pi kit I grabbed came with a 32 GB Micro SD card, preloaded with NOOBS. NOOBS is an installer that installs Raspbian, a Linux distribution built for Raspberry Pi.[3] Certainly, using that card would have been an easy solution; I didn't touch it for a couple of reasons:
- You need a keyboard and monitor to install NOOBS, neither of which I was using.
- I wanted to keep that clean copy of NOOBS intact, just in case things went sideways. I figured, worst case scenario, I could always go to a friend's house and use their keyboard and monitor as a last resort.
So I grabbed another 32GB card and went about installing Raspbian.
There are a a couple of choices on the Raspbian download page; I grabbed Raspbian Lite. I believe the only difference between the two is that the Lite version doesn't have a GUI... you have to access it via SSH, which was fine for my purposes.
After you've downloaded a copy of Raspbian to your computer, you can go about installing it onto your Micro SD card.[4] Instructions for that can be found in the link at the top of this section. More detailed instructions can be found on Raspberry Pi's page for installing images.
I used Etcher to get Raspbian onto my Micro SD card, which was quick and easy. Once Etcher has done its part, the only thing left is enabling SSH within Raspbian.
By default, at least as of July 2017, Raspbian has SSH access disabled. To enable SSH access to your Raspberry Pi, place a file called 'ssh' on the root of the card. You can find a nice picture, and further instructions, on this Hacker Noon post (Step 3). The file should be empty and should only be named 'ssh' (no file extension).
At this point, you should be be able to eject the Micro SD card from your laptop and insert it into your Raspberry Pi. When you insert it, the label should face out/away from the Pi.
Finally, plug in your Ethernet cable, plug in the power, and your Pi should boot up in a few moments.
SSH access and basic configuration
In order to SSH into your Raspberry Pi, you'll need to know the IP address of your Pi. Once your Raspberry Pi is on and connected to your router, your router will assign it an IP address; you'll need to figure out what that address is.
Step 5 of the Hacker Noon post noted in the above section has a picture that may be helpful. Essentially, you'll want to:
- Login to your router's admin area,
- Poke around the DCHP section for an area that lists devices that are connected to your network. (I found my device in an area called Client List.)
Armed with its IP address, you can use SSH to access your Raspberry Pi. In a terminal window, type: ssh pi@<ip address>. For example: $ ssh pi@192.168.05.500
.[5]
The default password is 'raspberry'; you'll want to change it to something else. (If I'm remembering correctly, at some point I was prompted to change it.)
Once you have access to your Raspberry Pi, you'll want to do some basic configuration of Raspbian. Mopidy offers some good guidance for that part of the process.
Sound Check: can your Pi produce sound?
This is a great opportunity to check your Raspberry Pi's sound. Use $ aplay /usr/share/sounds/alsa/Front_Center.wav
to see if sound is working, at all, on your Pi. Mopidy offers some additional documentation for testing and changing your audio settings, as well.
This is also a great opportunity to turn up the main volume of your Raspberry Pi.
An example of the command I use to control my Pi's main volume is:$ amixer sset 'PCM' 75%
The PCM
you see in the above command is text that may vary, depending on your set up. If you run $ amixer scontrols
, your system should return some text. On my system, it returns: Simple mixer control 'PCM',0
. If your system returns something other than PCM, you should try using that text in your command, instead. (Related Stack Exchange Post)
Personally, I have my main volume set to 100%. Later, you'll be able to control the music's volume via Mopidy's volume settings. If you ever want to see your current volume setting, $ amixer
should return your current volume percentage, along with some other stats.
Install Mopidy and configure as service
To install Mopidy, you can follow the Mopidy documentation for Debian installs. Their four quick commands will get you up and running.
The slightly tricker part is configuring Mopidy. It looks like I ran the command # mopidy
[6] after installing the software, which would have created a basic configuration file. For the moment, let's just skip to the end; here is a complete configuration file:
Since you want Mopidy to run on its own, without any direct interaction on our part, you need to set it up as a service. The configuration process differs, slightly, between service and non-service setups. As you're reading through the main set of instructions, you'll want to cross-reference that information with the running-Mopidy-as-a-service page. (I'd read the running-as-a-service page, first, honestly.)
The stand out differences are between service and non-service setups are:
- The configuration file lives in a different place. When running Mopidy as a service, the configuration file is located at
/etc/mopidy/mopidy.conf
(instead of the user’s home directory). - When running commands that normally start with
mopidy
you'll usesudo mopidyctl
instead.
Mopidy-MPD extension
Mopidy won't be taking commands from you, directly... you'll need a client to deliver those commands for you. The Mopidy-MPD extension, which is automatically installed with Mopidy, allows MPD clients to speak to Mopidy.
To activate the extension, add an [mpd]
section to your configuration file. That section has already been added in the configuration file sample above. You can see Mopidy's original sample and additional information on Mopidy's MPD extension page.
Mopidy Check: does it run?
This is a good point to see if Mopidy is working at all. Run # sudo systemctl enable mopidy
to set Mopidy up to run as a service whenever your Pi boots up. Then run # sudo systemctl start mopidy
to start Mopidy immediately. The command # sudo systemctl status mopidy
will show green active text if everything is working well.
For more documentation and details, begin with the 'Service management with systemd' section of Mopidy's 'Running as a service' page.
Learn from my mistake
I had some trouble when I ran #sudo systemctl enable mopidy
:
I followed the instructions on this Ubuntu Forums page, which fixed things.
Mopidy-Spotify extension
The Mopidy-Spotify extension will allow you to play music from Spotify. Installation and configuration instructions can by found on the Mopidy-Spotify GitHub page
Installing it is straight forward: # sudo apt-get install mopidy-spotify
You'll need to insert into the configuration file your own values for:
- Username
- Password
- client_id
- client_secret
Username and password should be easy enough. To get the client_id and client_secret, you can visit Mopidy's music service authentication page. Once you login and authenticate, the text in step 2...
...will update to actual data you should copy and place into your config file.
That should be all you need to get the extension up-and-running. I took the step of setting private_session to true
, because obviously. You'll probably need to stop and restart Mopidy, now that you've updated the config file:
Install mpc
You'll need to grab an MPD client to talk to Mopidy. I'm using mpc, which has worked well.[7]
I didn't leave myself many notes about installing mpc, nor did I bookmark any websites. As you'll see in my warning below, I'm not exactly certain I remember/understand how everything unfolded.
That said, my command line history tells me I installed mpc as root ($ sudo -i
) with # apt-get install mpc
. When it's done, you can type # exit
to leave the admin user and return to your normal user.[8]
Word of warning
It was around this stage of the installation process that I was trying to play a sample playlist from Spotify, just to see if things were working, and things were not working. I came back to it a few days later, and things were miraculously working: unfortunately, I wasn't sure what I did to get it working. I may have been formatting the Spotify URIs incorrectly, or using a URI for a playlist that no longer existed.
It's also possible that MPD was the culprit.
Essentially, Mopidy uses the MPD standard but doesn't use MPD. The frustratingly-close-in-spelling mpc is software that uses the MPD standard to talk to music servers. At some point I definitely, accidentally, installed MPD when I meant to type-and-install mpc, which I didn't notice right away. At some later point, I uninstalled MPD. Also, at some later point, playing music in Mopidy, using mpc, started working.
Raspberry Pi Spotify Touch Screen
Which is to say: make sure you install mpc and not MPD. While I have no idea if that was actually the wrench in my works, if you're having troubles, you may want to investigate if you have MPD installed.
Sound Check: a Spotify playlist
In theory, you can play music, now. This post on the Mopidy discussion board got me started; for additional commands, check out the mpc man page.
If everything is working correctly, the following 4 commands should play music.
If you're trying to troubleshoot, run $ mpc
-- that should give you the current status of Mopidy. If a song is playing, you'll see something like the following:
Learn from my mistakes
It took me a while to find an active Spotify playlist to test my system. The URI above --$ mpc add spotify:user:ckdsnx:playlist:6joSqHbjcQ1CtasAQX5BWj
-- links to a playlist on the CKDSN account and works as of:
- 2017-09-16
- 2017-09-24
Create playlists, grab URIs
I set up my own playlists for my alarm; you can do the same, or grab playlists that already exist. (I believe I read somewhere that playing playlists works much better than playing artists or albums.[9])
To convert a Spotify URI to the format that mpc uses, replace all /
with :
and start with spotify
instead of *.spotify.com
.
For example:https://open.spotify.com/user/ckdsnx/playlist/6joSqHbjcQ1CtasAQX5BWj
becomesspotify:user:ckdsnx:playlist:6joSqHbjcQ1CtasAQX5BWj
Finding a playlist's Spotify URI should be as easy as visiting the playlist via Spotify's web player. Otherwise, you should be able to 'Copy Playlist link' from any given playlist's menu options.
Learn from my mistake
I'm not sure any song additions or subtractions I've made since Mopidy first accessed my playlists have been updated with Mopidy. In my tests, it certainly didn't happen immediately; it's possible that eventually everything syncs and it just takes a while.
You may want to make sure you have a playlist completely set before you have Mopidy play it for the first time. Worst case scenario: you can recreate the playlist in a new playlist, including any edits, and use that new playlist's URI.
A file that executes mpc commands
Now that you have your Raspberry Pi using mpc to control Mopidy to play Spotify playlists, let's create an executable file that will run those mpc commands.
This article from the Association of College and Research Libraries, a division of the American Library Association, helped me get started.
Create an empty file: $ > MyTestAlarmFile
Make that file an executable file: $ chmod +x MyTestAlarmFile
Run $ ls
and Raspbian will list your current directory's contents, with any executable files listed in bold green. You should see your newly created file.
Run $ nano MyTestAlarmFile
to open the file in the Nano text editor. A complete file is as simple as this:
It's important to start with #!/bin/bash
so Raspbian knows how to interpret what follows. With the rest of the file, you can type out the same commands you used earlier to play a test playlist.
Ctrl+O to save; Ctrl+X to exit.
Now, let's run the file and play our testing playlist again: $ ./MyTestAlarmFile
.[10]
More files that execute mpc commands
At this point, you'll want to set up your own series of files that meet your particular needs, so you can play the right tunes at the right time. In my case that includes:
- A file that plays spa music when it's time to go to bed; another file that stops the spa music when I'm asleep
- A file that plays Gregorian chants in the morning; another file that slowly increases the music's volume
- A file that plays a British Invasion playlist when I really need to get up
Examples:
A file that plays a playlist.
A file that slowly increases the music's volume
A file that stops the music and resets the volume
Set the alarm: automation via crontab
The final step is automating the process using a crontab file. These crontab instructions at How-To Geek do a great job of walking you through the process.
To access your crontab file, run $ crontab -e
. Here's an example of what you might include in your crontab file:[11]
43 6
tells the system what time to execute the command -- 6:43 am in this case.[12]- (
* *
) tells the system to execute every date of a month and all months of the year. - (
1-5
) tells the system to execute Monday through Friday ./SlowVolumeIncrease
tells the system which file to execute.
After you setup your crontab file to execute the appropriate files at the appropriate time,[13] your Raspberry Pi Spotify alarm clock should be complete!
Command line Raspberry Pi updates
[2018-01-01 update]
Doing system updates via the command line is quite easy.
Once that runs, take a look through the resultant list and enter y
if you want to upgrade everything. For good measure, I ran sudo apt-get autoremove
, at the system's prompting.
To double check your work, run $ sudo apt-get upgrade
one last time, and you should get something resembling the following, letting you know there's nothing to upgrade:
Parting Thoughts
Here are some final thoughts and a list of the sources I used for this project.
Audio Hiss
When I first set up my system, I had a weird audio hiss. There are a number of proposed solutions on the internet -- the solution that worked for me is on @superjamie's GitHub page: set audio_pwm_mode=2
in /boot/config.txt
.
How To Download Spotify On Raspberry Pi
Set your levels
It took me a while to find the right volume levels for all of my playlists. If I were doing it again, I think I would figure out which playlist needs the most amplification, set it at mpc volume 100
, and then set my real world speakers at an appropriate volume. From there, the playlists that need less amplification can have mpc volume levels that are <100%.
Volume reset on reboot
I believe the volume on your Raspberry Pi's main mixer resets with each reboot. To address that issue, I setup another executable file and crontab instruction that automatically sets my main volume on reboot.
In your executable file, you can use this:
Then in your crontab, you can add the following:
Wifi
At the moment, I'm hardwired into my router. I had some trouble setting up wifi on my Raspberry Pi and decided to continue with the ethernet cable and push ahead with the rest of the project. It wasn't mission critical and I can always come back to it.
List of Links
Here's a list of links I found helpful, in someway, during this project.
Raspberry Pi Spotify Client
My first smartphone, actually... an LG Lucid ↩︎
I'm running Linux, guessing Windows or Apple machines work as well. ↩︎
Which is also prepackaged on the SD card (as far as I can tell). ↩︎
You're not installing the OS so much as you're placing it fully formed onto the Micro SD card. (aka 'flashing the file') ↩︎
If a command starts with a $, that means you're running it as a normal user. If it starts with a #, that means you're running it as a root/administrative user. There may be some commands that I ran as root that would work equally well (or better) as a normal user. ↩︎
A note that will make sense in a second: I ran just 'mopidy' and not 'mopidyctl' ↩︎
If you'd like to try something else, Mopidy has some recommendations. ↩︎
At a certain point in this long process, the Raspbian/Mopidy instructions say you should be a root user for a certain group of commands. I can't tell if I intentionally installed mpc as root, or if I just happened to still be in root when I did the install. (Nor do I know, at the moment, if it makes a difference.) ↩︎
Artists and albums may not work at all, actually. ↩︎
I know there's a way to run the file without using
./
-- this AskUbuntu post has a link that should help. ↩︎I reset my volume often, just to make sure I don't accidentally scare myself awake. ↩︎
Astute readers will notice I have the commands separate by a minute; I don't know that this is necessary. I'm guessing if I set them all for the same time, they would run sequentially. That said, I personally sleep better separating them by a minute instead of potentially generating an error, or having the music start really loud, a split second before the volume reset, etc. ↩︎
It might be a good idea to run some tests, before you rely on this as your primary alarm clock. e.g. Set up your crontab to play and stop a playlist every 5 minutes, just to make sure everything works how you expect it to. ↩︎