[HowTo] Setup a spotify-connect-web-server on a Raspberry Pi with OSMC


#1

##09/08/2017 Notice: Due to changes in the Spotify-web-app the following howto no longer works.

For a working version of Spotify on your Rpi’s OSMC, see this solution:

09/08/2017 discontinued: Do you wish to play your Spotify music on your Raspberry Pi mediaplayer with Kodi?

Sources: 09/08/2017: discontinued

My personal website: https://zatarranl.wordpress.com/2017/01/08/howto-setup-a-spotify-connect-web-server-on-a-raspberry-pi-with-osmc/
Nederlandse variant: https://zatarranl.wordpress.com/2017/01/08/howto-installeer-een-spotify-connect-web-server-op-een-raspberry-pi-met-osmc/

Why would I do this? 09/08/2017: discontinued
As Spotify-premium user you can stream Spotify to your OSMC based Kodi player (through any Android, Windows or Apple device with Spotify installed).
Tested on RPi 3 with OSMC 2016.12-1

Prerequisites: 09/08/2017: discontinued

  • A Raspberry Pi (RPi 1, 2 or 3), with OSMC installed
  • A Spotify Premium account
  • A Spotify key (request this at Spotify: https://developer.spotify.com/technologies/libspotify/application-keys/). You have to fill out a form and just tell them it’s for personal use etc. They’ll send you the key after a few days. Make sure you get the binary version.
  • A windows computer (I have no experience with Ubuntu or any Linux OS, but that will probably work as well)
  • Audio output = HDMI, analog or HiFiBerry (haven’t been able to successfully use Bluetooth/A2DP)

Steps

'1. As your RPi is turned off, remove de SD-card and copy the Spotify key onto the SD-card. Put the SD-card back in your RPi and boot.

'2. Download Putty for Windows: http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html

'3. Find out what your RPi’s IP-adress is (in Kodi choose: System; My OSMC, network, wired/wifi)

'4. Open Putty on your windows machine and type in the IP-adress of the RPi and login with:
Username: osmc
Password: osmc
If you have changed your password, use that one.

'5. Install the Spotify connect web server by typing the following lines:

curl -O curl -OL https://github.com/Fornoth/spotify-connect-web/releases/download/0.0.3-alpha/spotify-connect-web.sh
chmod u+x spotify-connect-web.sh
./spotify-connect-web.sh install

'6. [Deleted]

'7. [Deleted]

'8. [Deleted]

'9. Copy the Spotify-key to the right place, by typing:

sudo cp /boot/spotify_appkey.key spotify-connect-web-chroot/usr/src/app/

'10. Your Spotify Server should work. Test it by typing:

./spotify-connect-web.sh -o hw:0 --username [12345678] --password [xyz123] --bitrate 320 --name [any name]

(remove the [ ])

note: if you get an error, device does not exist, try aplay -l to see what audio devices are available. It could be hw:1, instead of hw:0. (the command aplay is not standard included with OSMC. You need to install alsa-utils for this. Use the following commands for installing alsa-utils: sudo apt-get update, followed by sudo apt-get alsa-utils)

'11. If everything is correct, you should be able to find the RPi on any other Spotify App.

Now it’s time to make sure the server is available every time you restart the RPi (autostart). Do this by following the next steps:

'12. reboot the RPi

'13. Repeat step 3 (login with Putty)

'14. Type the following:

sudo nano scs.service

'15. Now you made a new script.

With cabled network connection add the following text in the script:

[Unit]
Description=Spotify Connect
After=network-online.target
[Service]
Type=idle
User=osmc
ExecStart=/home/osmc/spotify-connect-web.sh -o hw:0 --username [12345678] --password [xyz123] --bitrate 320 --name [any name]
Restart=always
RestartSec=10
StartLimitInterval=30
StartLimitBurst=20
[Install]
WantedBy=multi-user.target

With wifi connection add the following text in the script:

[Unit]
Description=Spotify Connect
After=network-online.target
[Service]
Type=idle
User=osmc
ExecStartPre=/bin/sleep 25
ExecStart=/home/osmc/spotify-connect-web.sh -o hw:0 --username [12345678] --password [xyz123] --bitrate 320 --name [any name]
Restart=always
RestartSec=10
StartLimitInterval=30
StartLimitBurst=20
[Install]
WantedBy=multi-user.target

(for wifi connection a 25 second delay is added)

'16. Close the editor and save by typing ctrl-X

'17. select Y to save

'18. Type the following:

sudo mv ./scs.service /etc/systemd/system/scs.service
sudo chmod a+u /etc/systemd/system/scs.service
sudo systemctl daemon-reload
sudo systemctl enable scs.service
sudo systemctl start scs.service

'19. Close Putty

'20a. If your sound output is HDMI:
In Kodi (on the RPi) choose: System; My OSMC; Pi Config; Display and make sure the next is selected:
hdmi_force_hotplug

'20b. If your sound output is analog (thanks to Pezzy):
Manually edit the config.txt file by typing in putty:

sudo nano /boot/config.txt

Add this line at the bottom

hdmi_ignore_edid_audio=1

Save and shutdown by typing: ctrl-X and Y to save

'20c. If you have an Hifiberry DIGI+:
Hifi-berry works nowadays with this solution. The latest versions of OSMC, as well as the spotify connect, releases the audio devices for the use with other instances. Make sure that:

  • OSMC releases the audio device when not in use (found in ‘systems’; ‘system’; ‘audio output’; ‘keep audio device alive’ = ‘off’)

'20d. If you other audiocards/output:
I haven’t tried other audio output, so can’t help you there yet. Maybe someone can add tips in this section for output via jack or additional soundcards. Or it could work out of the box.

Enjoy.

Changelog:
jan 7th 2017

  1. The link to the download site. It now links to the latest release 0.0.3, like the instructions from Fornoth at Github.
  2. Works with multiple audio devices (see step 20c)
  3. In my experience this version of spotify-web (0.0.3) needs to be assigned an audio device. Command -o hw:0 is therefore added.
    feb 2nd 2017
  4. Clarified the use of -aplay at step 10.
  5. Deleted step 5-9 and replaced is with a simple command to download a spotify-key
    feb 26th 2017
  6. Download link for the spotify key doesn’t work anymore. It can be officially requested at Spotify, but you’ll probably wait a bit before they send it to you. https://developer.spotify.com/technologies/libspotify/application-keys/
    sep 8th 2017
  7. Support for this program has been discontinued. So currently this howto is no longer useful.

Issues with Spotify Connect server on Rpi3 with OSMC [Solved]
Spotify + OSMC = <3
Need help with OSMC+Spotify+hifiberry DIGI+
No kodi sound output if spotify-connect-web server is active
Default audio device for BubbleUPNP
#2

Good job :slight_smile:
Wonder if its possible to make kodi addon that adds username and password and bitrate instead of having it in shell.


#3

Is there any reason why this shouldn’t be possible on a Vero? I might try myself, but am afraid to ruin my installation of Vero since I don’t have a cardreader to reinstall OSMC if necessary.


#4

Vero isn’t special its still a linux system and yes this will work on Vero since this solution isn’t specific raspberry pi and you can always reinstall if you manage to mess it up or better yet be smart and do a backup before starting so if you mess up tada reflash… it isn’t rocket science


#5

Does this mean only 1 person in a house will be able to stream music to the OSMC device? I expected Spotify Connect to announce itself within the home network. So anyone within the network (via Wifi) connected to the same router should be able to stream from his/her phone to OSMC right?

For example I have 1 Spotify family subscription which contains 2 accounts.


#6

only the person logged in can stream to the device


#7

Awesome work, I’ve been following various threads on this for some time, but generally lacking the knowledge to set it up for myself, so thank you so much for going to the effort!

Just a couple of points, which may help some people (and will be completely obvious to others):

  • Step 10. Once you’ve copied the key to your SD card, you’ll find it in /boot/ , so the command would likely be:
    sudo cp /boot/spotify_appkey.key spotify-connect-web-chroot/usr/src/app/
  • Step 19. Typo, should be:
    sudo mv ./scs.service /etc/systemd/system/scs.service

Also, if you want to use Analog output only, instead of Step 21, you’ll need to manually edit the config.txt file and add the following line in:
hdmi_ignore_edid_audio=1

You can either use nano or use vi (like in step 15), for example:
sudo vi /boot/config.txt

Finally, for WiFi connections, the script will likely run before the connection is active, causing it to look like it is running fine, but there will be no advertisement of a Spotify Connect target. You could try using “After” and “Wants” with references to various services - this will probably lead to spending a long time troubleshooting - but the simplest method is to add a quick and dirty sleep command in to the scs.service file, so:
[Unit] Description=Spotify Connect After=network-online.target [Service] Type=idle User=osmc ExecStartPre=/bin/sleep 25 ExecStart=/home/osmc/spotify-connect-web.sh --username [12345678] --password [xyz123] --bitrate 320 --name [any name] Restart=always RestartSec=10 StartLimitInterval=30 StartLimitBurst=20 [Install] WantedBy=multi-user.target

It’s also a good idea to setup “Wait for Network” for the Wireless NIC in the My OSMC addon in Kodi.

Hope this helps some people :slight_smile:


#8

One more thing: I’d recommend going down the script route (i.e. create a shell script rather than have the full spotify-connect-web.sh command in scs.service) which you mentioned if you don’t want your logs to contain your Spotify credentials.


#9

Pezzy, thanks for the addition. I changed your recommendations and added the analog audio as well as your wifi solution.

As for your recommendation using a shell script, I understand your reasoning, but I’m quite a Linux Noob myself. So If anyone can help with writing down the way it should work, thanks.


#10

Re step 8: You can download some sort of bash terminal to Windows (like cygwin) and use the secure copy (scp) command instead, so you don’t have to remove the SD card. Example how to use http://www.hypexr.org/linux_scp_help.php

This will save a bit of time atleast :slight_smile: There are also other commands to copy to remote server so this was just a small suggestion


#11

That step fails for me because there is no directory spotify-connect-web-chroot/
where is it suppose to be? The previous step only seems to download the 170MB Spotify connect file. After that nothing seems to happen…


#12

No worries.

The shell script is just a case of creating a sh file, say, scs.sh in the home directory, with contents as follows:

#!/bin/sh

/home/osmc/spotify-connect-web.sh --username [YourUsername] --password [YourPassword]

exit 0

… then calling scs.sh from the scs.service ExecStart, i.e. ExecStart=/home/osmc/scs.sh


#13

@zatarra and @Pezzy thx for the useful hints and service file.
I tried it on vero2. There’s also a fix for volume control and other things:
https://github.com/Fornoth/spotify-connect-web/archive/master.zip
With this fix it doesn’t work for me I get a message about some flask module missing.
Without the fix I get:
SpInit: 0 Traceback (most recent call last): File "main.py", line 27, in <module> connect_app = Connect(web_error_callback) File "/usr/src/app/connect.py", line 75, in __init__ lib.SpPlaybackUpdateVolume(mixer_volume) OverflowError: can't convert negative number to unsigned

For a workaround on the volumio forum it is advised to change

mixer_volume = int(mixer.getvolume()[0] * 655.35)
into:
mixer_volume = 1 #int(mixer.getvolume()[0] * 655.35)

I don’t know if that affects the ability to control the volume via spotify app.
Because this doesn’t work for me. No big deal.

But what bothers me, is that if I start it at boot now the audio output of kodi when playing movies, streams and so on stopped working.
If I stop the service everything works again. This is annoying because it basically makes such service file which automatically starts useless.
Maybe someone has a hint to resolve this.


No kodi sound output if spotify-connect-web server is active
No kodi sound output if spotify-connect-web server is active
#14

@zilexa

Try replace it with:

sudo cp /boot/spotify_appkey.key /home/osmc/spotify-connect-web-chroot/usr/src/app/

Does that helps?

Btw, after downloading the 170MB file, you should install it first (by first typing chmod u+x spotify-connect-web.sh and next ./spotify-connect-web.sh install)

Let me know if it helps.


#15

@trohn_javolta
I have issues concerning my Hifiberry Digi+. It may be related. Have partly resolved the problem by updating the console_callbacks.py file to the newer version on githup.com/Fornoth.

Your problem really goes beyond my expertise, but you could try updating that file… Although I have absolutely no reason to suspect it is the solution… :wink:


#16

@pezzy, I got it :wink:
And added it to the howto.


#17

Never mind! Second attempt worked, must have been my mistake the first time!

Edit: idea for MyOSMC: a user switch setting. This could easily be supported by creating multiple scs.sh files, one for every user and perhaps a .service file for every .sh file. Switching in MyOSMC would mean stopping one service and starting the other…

EDIT:
It only worked once for me: when I ran the command the first time, before creating the service files.
Now, even when I disable the service, reboot OSMC and start it manually, it seems to start fine but I can never find the Spotify Connect device in Spotify on my phone or laptop (though my phone Spotify app can see my laptop Spotify Desktop app and vice versa).
I tried with a different name (–name) but still no success.


#18

@zilexa,
What does it report when typing sudo systemctl status scs.service?

It should be something like:

  • scs.service - Spotify Connect
    Loaded: loaded (/etc/systemd/system/scs.service; enabled)
    Active: active (running) since Tue 2016-05-24 09:20:29 CEST; 8s ago
    Main PID: 1985 (spotify-connect)
    CGroup: /system.slice/scs.service
    |-1985 /bin/bash /home/osmc/spotify-connect-web.sh --username xxxx --password xxxx --bitrate 320 --name woonkamer
    |-1999 sudo chroot /home/osmc/spotify-connect-web-chroot /bin/bash -c cd /usr/src/app && python main.py --username xxxxx --password xxxxx
    |-2000 /bin/bash -c cd /usr/src/app && python main.py --username xxxxx --password xxxxx --bitrate 320 --name woonkamer
    `-2002 python main.py --username xxxxx --password xxxx --bitrate 320 --name woonkamer

May 24 09:20:30 woonkamer sudo[1987]: osmc : TTY=unknown ; PWD=/ ; USER=root ; COMMAND=/bin/mount --bind /dev /home/osmc/spotify-connect-web-chroot/dev
May 24 09:20:30 woonkamer sudo[1987]: pam_unix(sudo:session): session opened for user root by (uid=0)
May 24 09:20:30 woonkamer sudo[1992]: osmc : TTY=unknown ; PWD=/ ; USER=root ; COMMAND=/bin/mount -t proc proc /home/osmc/spotify-connect-web-chroot/proc/
May 24 09:20:30 woonkamer sudo[1992]: pam_unix(sudo:session): session opened for user root by (uid=0)
May 24 09:20:30 woonkamer sudo[1997]: osmc : TTY=unknown ; PWD=/ ; USER=root ; COMMAND=/bin/cp /etc/resolv.conf /home/osmc/spotify-connect-web-chroot/etc/
May 24 09:20:30 woonkamer sudo[1997]: pam_unix(sudo:session): session opened for user root by (uid=0)
May 24 09:20:30 woonkamer sudo[1997]: pam_unix(sudo:session): session closed for user root
May 24 09:20:30 woonkamer sudo[1999]: osmc : TTY=unknown ; PWD=/ ; USER=root ; COMMAND=/usr/sbin/chroot /home/osmc/spotify-connect-web-chroot /bin/b… woonkamer
May 24 09:20:30 woonkamer sudo[1999]: pam_unix(sudo:session): session opened for user root by (uid=0)
Hint: Some lines were ellipsized, use -l to show in full.


#19
osmc@ChilleTV:~$ sudo systemctl status scs.service
* scs.service - Spotify Connect
   Loaded: loaded (/etc/systemd/system/scs.service; enabled)
   Active: activating (auto-restart) (Result: exit-code) since Thu 2016-05-26 09             :26:22 CEST; 7s ago
  Process: 778 ExecStart=/home/osmc/scs.sh (code=exited, status=203/EXEC)
 Main PID: 778 (code=exited, status=203/EXEC)

May 26 09:26:22 ChilleTV systemd[1]: Unit scs.service entered failed state.`

When I run scs.sh manually, I have to do: sh scs.sh, otherwise it won’t work:
osmc@ChilleTV:~$ scs.sh -bash: scs.sh: command not found osmc@ChilleTV:~$ sh scs.sh scs.sh: 1: scs.sh: !/bin/sh: not found SpInit: 0 playback_volume: 62913 public key: vpcM1FdddddswwZZ+vAJ5u6ubVGnt4hSicVhKNxDtg14cn971HyaoDGbwRlN/qrTGqD0wqlZpxlM69kMDZNFMhLw2Z67/OQAsVOeptWodjWsa7N6nDSd9 device id: 7a5c0f60-0548-4dd2-85c9-a61418d8f8e7 remote name: ChilleTV account req: PREMIUM device type: AUDIODONGLE

Now I go to Spotify on my laptop or phone: no devices. So there are 2 issues, the service is not working, and somehow the device is not broadcasted.
To fix the first issue, I disabled and removed the service, rebooted and followed the instructions to create the service again. It was not solved. Should I add “sh” to the command in the service file?
I am using an RPi2. Connected via LAN.


#20

@zilexa,

I had some issues too with the suggestion of @Pezzy to create an extra script scs.sh.
I returned to changing /etc/systemd/system/scs.service

From:

ExecStart=/home/osmc/scs.sh

To:

ExecStart=/home/osmc/spotify-connect-web.sh --username [12345678] --password [xyz123] --bitrate 320 --name [any name]

As Pezzy suggest, the username and passwords will be named in logs, which isn’t preferable, but otherwise it didn’t work with me.

Let me know if it worked for you.