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 https://www.internet-radio.com. 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:
# mqtt_2_mpc.py - 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)) client.subscribe("Radio") 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 else: os.system("mpc clear") os.system("mpc add '" + themsg + "'") os.system("mpc volume " + thevolume) os.system("mpc play") client = mqtt.Client() client.connect("iot.eclipse.org",1883,60) client.on_connect = on_connect client.on_message = on_message print ("Waiting for messages .....") client.loop_forever()
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.
I am getting following error with MQTT music control
Uncaught Error: AMQJS0013E Invalid argument undefined for Message.destinationName.
send https://cdnjs.cloudflare.com/ajax/libs/paho-mqtt/1.0.1/mqttws31.min.js:67
LikeLike
Hi,
What have you defined for your MQTT broker? I don’t believe that the site: iot.eclipse.org is still available for public use. I would recommend going to https://mosquitto.org/ they have a nice light weight MQTT broker that you can install.
LikeLike