Integrating Pi Zero-W with Scroll-pHAT, pHAT DAC and Mopidy-Spotify


#1

Hi there,

I have built a project with Pi Zero-W, Scroll-pHAT, pHAT DAC, and Mopidy-Spotify.
I am interested in taking the currently playing tracks from Spotify and displaying this information (any any other cool information) on the Scroll-pHAT.

I believe, from Tweeting Sandy and Phil, this functionality may be built into the pivumeter library: https://github.com/pimoroni/pivumeter/blob/devel/python_server/examples/sphd-shairport-meta.py#L32

I’d like to understand how I can get this up and going.
How do I integrate Mopidy and the Scroll-pHAT with this library?

Thanks in advance!


#2

The sphd-shairport-meta.py will only work with Shairport-Sync, since it uses Shairport’s metadata to extract the currently playing songs.

To get it working with Mopidy would require a similar, but different, example.

Looks like the key is in the Mopidy WebSocket API documented here: https://docs.mopidy.com/en/latest/api/http/#websocket-api

Making a client connection to this API will result in any change on the Mopidy end being pushed as a notification to the client, so Python could connect to the WebSocket API.

You can sudo pip install websocket-client on your Mopdify Pi and then run the below example to see what data you get out of your Mopidy install:

import websocket

def on_message(ws, message):
    print(message)

def on_error(ws, error):
    print(error)

def on_close(ws):
    print("### closed ###")

def on_open(ws):
    pass

if __name__ == "__main__":
    websocket.enableTrace(True)

    ws = websocket.WebSocketApp("ws://127.0.0.1:6680/mopidy/ws",
            on_message = on_message,
            on_error = on_error,
            on_close = on_close)

    ws.on_open = on_open
    ws.run_forever()

It’s looking promising, though, although in my experience getting Python’s WebSocket to run alongside anything else is tricky.


#3

I’ll give this a go this evening and let you know how I get on.

Just as an aside, from watching some of the Bilge Tank episodes you guys seem to push Volumio a lot.
From purely a Spotify (and maybe connecting a NAS) perspective - what would you recommend for handling the audio?


#4

Here’s a working implementation for Mopidy: https://github.com/pimoroni/pivumeter/blob/devel/python_server/examples/sphd-mopidy.py

You’ll need to change the Mopidy host to reflect the IP and port for your configuration (probably 127.0.0.1:6680).

This requires the development version of Pi VU Meter which is, roughly, documented here: https://github.com/pimoroni/pivumeter/tree/devel/python_server

Volumio was something we were playing with at the time, but it was a little too rough and ready for our radio kit. We’re keeping an eye on it though!

Mopidy + Spotipy is a really nice setup if you use Spotify. Just need to get the VU meter up and running properly, since I’ve been testing this code over the network rather than directly on the Mopidy Pi!


#5

Hi @gadgetoid after a bit of playing about I did a fresh build of Mopidy again, I didn’t get to try the Python code yet.

Can you point me in the right direction to change the hostname of my Pi, in Sandys example he has it set to pirateradio.local:6680/iris, whereas I’m using my IP_address:6680/iris.

Thanks,
AllynH


#6

If you type sudo raspi-config, then select option 2, Hostname, then you’ll be able to change your hostname to whatever you’d like. You’ll need to reboot with sudo reboot after doing that.


#7

Hi Sandy,
I’ve done that but and when I echo $HOSTNAME I get the correct hostname.
However, when I browse hostname:6680/iris or hostname.localhost:6680/iris I get an error saying the site can’t be reached…

Edit: Just to clarify, I’ve set the hostname to a unique name (no conflicts with other devices), I’ve also set the unique name in the [http] hostname = hostname then tried to connect to hostname.local on both my phone and laptop.
Ican connect via the IP but not the hostname.


#8

@gadgetoid
I tried to clone the pivumeter and ran into a few issues:

  • How do I clone the development branch?
  • Do I need to clone the full pimoroni/pivumeter source code?

I tried cloning the master pimoroni/pivumeter branch and building but I ran into a few issues:

  • I tried to build by running source setup.py this crashed :( Not sure if it finished.
  • I tried running setup.py phatdac this gave me the following error: -bash: setup.sh: command not found

I’m sure this is documented somewhere but I’ve hit a bit of a roadblock…

Thanks again for the support :)


#9

The development branch is undocumented at the moment, since it’s still being worked on so I’ll try and walk you through.

To clone the dev branch, or any branch for that matter, of a GitHub repository directly you can do:

git clone http://github.com/pimoroni/pivumeter -b devel --depth=1

The -b devel part tells git which branch you want, and the --depth=1 tells it not to to clone any history.

Then to build pivumeter you need to:

cd pivumeter
./setup.sh

Next to install the Python library for the VU server you need to:

cd python_server/library
sudo python setup.py install