House Music

Our goal was to have the same music playing throughout the house, and then use a phone to control the music station and volume.


For this project we used a technology called MQTT (Message Queue Telemetric Transport). MQTT is based on the following standard concepts:
– MQTT Broker manages and distributes messages
– MQTT Publisher a source for the data
– MQTT Subscriber an application that wants the data


Pi Setup

On the Raspberry Pi’s we needed to load a Python MQTT library and a music player (mpd – music server, mpc – music client):

sudo pip install paho-mqtt
sudo apt-get install mpd mpc

The key commands to manage playing of Internet radio stations are:
mpc add radio station url– this will add a radio station to a play list
mpc play play list number – play a selected play list
mpc volume number – change the volume from 0-100%
– mpc clear – clear the play lists

Internet radio stations can be found by going to To find a radio station URL right click on any of the radio station links and select “Copy link address”

On each of the Pi’s the Python program subscribes to an MQTT topic called Radio. New Internet Radio Stations are passed as strings, and new volume settings are passed as numbers between 0 and 100. For this application the MQTT payload could be either an integer or a string so a decode function (msg.payload.decode(“utf-8”)) is used to convert from a byte array to a generic string.

When a new radio station is received:
– the current play list is cleared (mpc clear),
– the new radio station is added to the play list (mpc add),
– the volume is reset to it’s original (mpc volume), and
– the radio station is played (mpc play).

If the payload is numeric only the volume is changed. The full Pi Python program is shown below:

# - have MQTT change Internet Radio Stations
import paho.mqtt.client as mqtt
import os

thevolume = 75  # save the volume

# Subscribe to topic: Radio

def on_connect(client, userdata, flags, rc):
  print("Connected with result code "+str(rc))

def on_message(client, userdata, msg):
  global thevolume
  print( msg.payload)
  themsg = msg.payload.decode("utf-8")
  # if the message is a number it's the volume
  if themsg.isnumeric():
    thevolume = themsg
    os.system("mpc volume " + thevolume)
  # if the message is a string it's the station URL
    os.system("mpc clear")
    os.system("mpc add '" + themsg + "'")
    os.system("mpc volume " + thevolume)
    os.system("mpc play")

client = mqtt.Client()

client.on_connect = on_connect
client.on_message = on_message

print ("Waiting for messages .....")

Web Page Setup

On the Web page the Paho JavaScript client is used to connect to an MQTT broker. It is important to note that the JavaScript interface uses Web Sockets to communicate with the MQTT broker, (typically on port 80) rather than the native MQTT port of 1883. If you install your own MQTT broker, you will need check the document for Web Socket setup and support.


The final Web page that we used is listed below.