Beginner Tutorials?

Hi there, are there any good tutorials available for using your own Python program to control Spotify using Mopidy?

I want to build a jukebox using specific tracks or albums from Spotify then just select them with key presses.

I’m struggling to find a straight forward tutorial showing something similar.

Thanks in advance.

Did this ever get any traction? Looking to do the same thing with no luck.

Have you considered using LIRC, MPC and a remote control? You would just need to put a IR receiver on the pi, then you can use an old tv remote control to map the tracks or albums to.

Although it’s out of date now, there’s a page here that shows how, it’s easy enough to do.

found this in a browser tab. Might be a good place to start:

That is an interesting idea. Slightly different from what I’m going for but same ballpark- I’m trying to use knobs that control potentiometers and have the resistance value drive which playlist is being played.

My biggest problem now, as this post has pointed out, is I can’t seem to find a good resource for what the actual .py file would look like to start playing a playlist. (or examples of any .py file in general that use Mopidy to play Spotify)

that’s a pretty interesting use case! :smiley:

Rather than pull in mopidy’s python, why wouldn’t you access it through the web / socket API from your hardware control software? Probably easier and a better idea than importing all of mopidy’s backend into your code. At least, if I’m understanding you correctly.

q.v., https://docs.mopidy.com/en/latest/api/http/ (edited)

This is probably a great idea. Unfortunately I’m a little too inexperienced to understand what you’ve said.(lol)

In my mind there would be a bunch of switch cases (not sure if python even uses these) that say “if the potentiometer returns a value between “x” and “x”, start playing a song from playlist “a””

1 Like

To further expand on this, I’ve seen a couple of articles claiming that you have to convert the potentiometer’s analog value to a digital value that the pi can read, so I’ve ordered an MCP3008 ADC.

Another alternative I’m looking at is having the potentiometer hooked up to an arduino, and have the arduino send the switch cases to the pi, and have the pi just execute.

you can easily read values from a pot wired through an ADC to a Pi’s GPIO pins (provided they aren’t occupied with something else, like an audio hat).

I’d start with that and when you can read values from the pot, implement some kind of “on_change” callback that fires off the required RPC message to play a given playlist.

As @Steve_Lambert said, using mpc might be simple. There is also a decent MPD client library for python, should come up in a search. We don’t have a specific Mopidy client available although it would be nice to have. The closest thing is the mopidy.js javascript library but that doesn’t suit your use case. However, there are various generic JSON RPC libraries out there that can be used to interface with Mopidy Core.

1 Like

Yes! This right here is what I needed!!! Feel like I’m finally asking the right questions.

I’m using a HiFiBerry Amp2 so I am looking at which GPIO pins I can and can’t use. I was already planning on having an arduino control the potentiometers and just relay messages to the pi, so that still may be my easiest bet. I’m about a 1 on a scale of 1-10 with the pi and maybe a 2.5 with the arduino.

I have code working for the arduino that reads a value of the pot and turns on a corresponding LED just for proof of concept. This last part is what I’ve really been looking for- where do I find the RPCs that will say “play this playlist”? I don’t know what that code looks like or where to find examples.

1 Like

The Core API is accessible over JSON-RPC with some documentation at https://docs.mopidy.com/en/latest/api/http/ as @robcee already linked.

To add the Spotify playlist spotify:playlist:2m45vFhWeUcHZQgJ95IkSf to the queue (AKA the tracklist):

$ curl -d '{"jsonrpc": "2.0", "id": 1, "method": "core.tracklist.add", "params":{"uris":["spotify:playlist:2m45vFhWeUcHZQgJ95IkSf"]}}' -H 'Content-Type: application/json' http://localhost:6680/mopidy/rpc

You’d then need to issue a core.playback.play command to start playback.

Control using the MPD protocol is probably simpler than JSON-RPC but less flexible. You will need to install Mopidy-MPD for this to work:

$ netcat localhost 6600 
OK MPD 0.19.0
add spotify:playlist:2m45vFhWeUcHZQgJ95IkSf
OK
play
OK
1 Like

Awesome! Ok, so if I understand correctly these are two different examples, but with Mopidy-MPD installed I was able to use the bottom example. I have a feeling for now this will be the easier approach for what I’m trying to do.

However on the “play” command it only played my last played song and didn’t pull from my added playlist. Is the command to play from the actual playlist different? Is there somewhere that lists these commands? (I noticed pause and next work but couldn’t find anything in the Mopidy-MPD link or the JSON link that had these terms)

Mopidy-MPD implements some (the most basic and useful parts) of the MPD protocol. We don’t publish the MPD protocol in our documentation any more but we used to, so it’s quite well documented in the source. Either lookup the MPD protocol elsewhere online or read our source code, for example:

play without any argument plays the currently active song. If you add tracks to the end of the tracklist (queue) while a song is playing, they will be played when it gets to their turn. If you have finished playing your tracklist, there will not be a currently active song. So if you then add new tracks to to the tracklist and issue play, it will start playing from the start of the tracklist rather than the “old” end of the tracklist.

For a certain tasks, you may find you have to issue more MPD commands than you would JSON-RPC commands, but the handling of those JSON-RPC commands might be more complex. It depends exactly what you are doing.

1 Like

This has been incredibly helpful, but I have a follow up question:

Mopidy is running on my raspberry pi, and I am controlling the raspberry pi through ssh on Windows cmd. I’d like to continue using Mopidy-MPD, which as far as I can tell is commands that you type into the terminal.

I have a potentiometer on an arduino that outputs a value to the pi.

I have a .py program that reads the pot value.

I want the pi to read that value and then execute the Mopidy-MPD commands right in the terminal.

So for example, here is the .py program:

import serial
arduinoSerialData = serial.Serial(’/dev/ttyACM0’, 9600)
if(arduinoSerialData.readline()==1)
print (netcat localhost 6600
command_list_begin
clear
add spotify:playlist:1uSuursM0CMRVnwilFT7Do
play
command_list_end)

The bold works if I type it right into the terminal, so I want it to be written and executed in the actual terminal when that if statement is met. (print is probably not the right command)

Unless there is a better way to “execute” Mopidy-MPD commands based off of a read value like I’ve described?

I think I mentioned earlier in this thread there is an mpd library for python.

Yes, that has been helpful, and is where I found things like command_list_begin. I can’t find anything in there about opening a linux terminal and executing code, however.

If I change everything from the if statement down to this:

if(arduinoSerialData.readline()==1):
client = mpd.MPDClient() # create client object
client.connect(“localhost”, 6600) # connect to localhost:6600
client.command_list_ok_begin() # start a command list
clear
add (spotify:playlist:1uSuursM0CMRVnwilFT7Do)
seek 7 180
play
add (spotify:playlist:1uSuursM0CMRVnwilFT7Do)
play
client.command_list_ok_end()

I get a syntax error at the “:” on the add spotify playlist line. This makes me assume that what ever is in the python file that I am trying to execute is not read the same way as executing it in the terminal, which is why I figured opening a terminal and printing those lines might work.

That’s not how command lists are used (and you don’t really need them anyway so if they are confusing things don’t use them).The library has pretty good documentation and examples at https://python-mpd2.readthedocs.io/en/latest/topics/getting-started.html

1 Like

Unfortunately I’ve had a frustrating and fruitless venture with the rest of this thread so I’m coming back to this comment, because I got this Mopidy-MPD example to work.

I am essentially trying to save this to a file so that "if something happens (button push, potentiometer reading, icon is clicked on, etc) the code runs.

The linked documentation further down does a good job of showing a lot of commands but none of them seem to have examples with executing a file that will play a playlist with the string of characters spotify assigns to playlists, like the Mopidy-MPD example that I got to work manually. Apologies if this isn’t even the right forum for these kind of questions.

To further explain what I’m trying to do- it is possible to open the localhost connection from within the python program and then execute these commands as they are typed out above?

I’ve looked through all of the links and I can’t find anything about adding playlists and playing in the format of “add:spotify:playlist:xxxxxx”, except for the one JSON-RPC example above, and I don’t understand how that integrates with a program.