MPD & Domoticz

From Domoticz
Jump to: navigation, search


MPD (and it’s client MPC) is a nice add-on for a home-automation server. It provides a MusicPlayerDaemon which is controllable by Domoticz using some virtual switches.
This guide shows the implementation on a Raspberry Pi. MPD and MPC are Linux packages. So they should run also on other Linux systems.
In that case the mpd.conf (Configuration file) has to be adapted. Once MPD/MPC is implemented the audio signal out can be attached to a stereo amplifier for web-radio or mp3 files in combination with Domoticz switches triggered by a curl command from domoticz_main.
Example: a PIR switch is activated; a mp3 audio sound starts: “Warning, movement detected outside.”

Authors reference system

  • Raspberry Pi Type B 2nd Revision
  • Domoticz stable 1.1680 and 1.1803 beta
  • External USB audio sound card C-media 7.1

CPU usage is about 1-3% during normal operation with Domoticz only.
CPU usage is about 13-14% with MPD working on a web-radio-stream and Domoticz running.

External audio device

On the authors system it was not possible to use analog audio out (3.5mm).
With running Domoticz some seconds after starting an audio-stream via ALSA / analogue-out the SSH connection, MPD/MPC and Domoticz goes offline and stops working.
A reboot by unplug/plug of power was necessary. There is a collision when using the ALSA audio driver and the internal audio system.
Therefore it is strongly recommended before implementation of MPD/MPC.

* Save the domoticz.db
* Make a back-up image of the SD card

The ALSA web-page [1] provides a lot of compatible devices.
Anyway the author uses a cheap (7,50€) device from Amazon. Sometimes it’s available for 2$. Could be that many USB audio devices are compatible, even if listed or not.
Plug-in the external audio device only when the Raspberry Pi is powered off! It will be recognized automatically after booting.





Login via Putty. It is strongly recommended to update/upgrade! The latest version of MPD/MPC needs an actual system.

sudo su
sudo service stop
sudo apt-get update
sudo apt-get upgrade
sudo shutdown –h now
  • Remove power
  • Plug-in the external audio device
  • Power on your Raspberry
  • Wait until the Raspberry Pi has boot completely.
sudo su
sudo service stop
sudo apt-get install mpd mpc alsa-utils
sudo reboot

Wait until the Raspberry Pi has boot completely.

sudo su
aplay –l

You should see the external audio device as “card1”.
The author is not sure, in German language: “Karte1”

Applay -l.jpg

applay –L

The output depends on the USB audio card used.

Aplay -L.jpg


This is the most important part!

sudo nano /etc/mpd.conf

Set following parameters:

#log_file                       "/var/log/mpd/mpd.log"

Select group:

#group                          "nogroup"
group                           "audio"

Configuration für Raspberry Pi Output: * Hw:0,0 = analog audio out 3.5mm -> collision with Domoticz!

  • Hw:1,0 = external USB sound-card out 3.5mm
  • Mixer-type “software” -> set volume via “mpc volume 80” = volume=80%

audio_output {
       type            "alsa"
       name            "My ALSA Device"
       device          "hw:1,0"         # optional
#       format          "44100:16:2"    # optional
#       mixer_device    "default"       # optional
#       mixer_control   "PCM"           # optional
#       mixer_index     "0"             # optional
mixer_type      "software"   		  # optional
audio_buffer_size               "2048"

Save with CTRL-o
Exit with CTRL-x

Now check if the “snd-bmc2835” is installed? (optional)

sudo nano /etc/modules
  • # /etc/modules: kernel modules to load at boot time.
  • #
  • # This file contains the names of kernel modules that should be loaded
  • # at boot time, one per line. Lines beginning with "#" are ignored.
  • # Parameters can be specified after the module name.


Set rights:

sudo chmod g+w /var/lib/mpd/music/ /var/lib/mpd/playlists/
sudo chgrp audio /var/lib/mpd/music/ /var/lib/mpd/playlists/

Change file hosts to prevent a failure messages regarding IPV6

cd /etc
sudo nano hosts	localhost
#::1		localhost ip6-localhost ip6-loopback
fe00::0		ip6-localnet
ff00::0		ip6-mcastprefix
ff02::1		ip6-allnodes
ff02::2		ip6-allrouters	raspberrypi

Change only:

::1		localhost ip6-localhost ip6-loopback to
#::1		localhost ip6-localhost ip6-loopback

“ raspberrypi” could also be “ Domoticzpi” (don’t change it)

Check if mpd is member ofthe group "audio"

id mpd 

Id mpd.jpg -> gid=29(audio)

cat /etc/group | grep audio


You can set the group with:

sudo usermod -aG audio mpd

Add music and playlists

1.Playing mp3 files:

Store them here: /var/lib/mpd/music

2. Playlists für Online Streams

Store them here: /var/lib/mpd/playlists

If you want to hear web-radio-streams generate a xyz.m3u or xyz.pls file:

sudo su
cd /var/lib/mpd/playlists
sudo nano radio.m3u

Place your radio streams there. Have a look at the m3u format here: [2] If it is a radio stream, the first parameter has to be a “-1” as shown in the example.

Also “pls” is a possible format: [3]

Example for m3u playlist (radio.m3u)

#EXTINF:-1,Antenne Bayern Classic Rock live
#EXTINF:-1,Antenne Bayern Chillout

Configuring the ALSA mixer


With the ALSA mixer you will set the basic output volume.
Select with F6 the external sound-device.
Choose the cursor position with arrow left and arrow right.
Change the output power with arrow up and arror down.

See more here: [4]


Testing MPD/MPC

Place a playlist like this "radio.m3u" (user = mpd) in:

#EXTINF:-1,Antenne Bayern Classic Rock live
#EXTINF:-1,Antenne Bayern Chillout

Plug-in a headphone or connect to your stereo.

Check all available commands with:

mpc help
man mpc

Check if your playlist user is “mpd”.

sudo /etc/init.d/mpd stop
mpc update
sudo /etc/init.d/mpd start
mpc clear
mpc load radio.m3u
mpc play 1

Now should hear a radio stream. If your device is the same as the authors you will see a flashing green LED. You can change to another by:

mpc play 2

Have a look at all the mpc commands especially like:

mpc clear
mpc add <a web-radio-streaming address>
mpc stop
mpc volume 70
mpc outputs
mpc lsplaylists

Configuring a playlist

Be aware! A radio-stream should not end with “.m3u” or “.pls”. This will not work within a playlist like “radio.m3u” because it is still a playlist! Use a playlist editor like Windows media player to check what streams are in a playlist.

Another and better way: if you have a play-list-web-radio-stream like the following... can extract the mp3 adress like this:

sudo su
cd /var/lib/mpd/music
sudo chown mpd classic-rock-live.m3u

Check the file content:


Now you will see the playlist "classic-rock-live.m3u". Double-click on this m3u file and you will see the stream-adress:

This adress can be placed in a "m3u" playlist like this (radio.m3u):

#EXTINF:-1,Antenne Bayern Classic Rock live
#EXTINF:-1,Antenne Bayern Chillout

If you want to change from one playlist (e.g. radio.m3u) to another:

mpc stop
mpc clear
mpc load classic-rock-live.m3u
mpc play 1

Controlling via Domoticz

Control scripts

First setup a RAM-drive like explained here: [5]
This is necessary to store the last output volume.

The following scripts are neccessary for:

  • Volume Up
  • Volume Down
  • Mute / UnMute
  • Radio station select

Each will be started by a Domoticz button.
Place the scripts here:


 #Volume UP
 if [ -f /var/tmp/mpd_volume ]; then		# check for file
  old_volume=`cat /var/tmp/mpd_volume`		# read old volume value
  echo 50 > /var/tmp/mpd_volume			# store new volume
 if (( $old_volume < 100)) ; then		# if volume < 100%
 	new_volume=$((old_volume+10))		# add 10%
 	echo $new_volume > /var/tmp/mpd_volume	# store new volume
 	new_volume=100				# set highest value
 mpc -q volume $new_volume

 #Volume Down
 if [ -f /var/tmp/mpd_volume ]; then		# check for file
  old_volume=`cat /var/tmp/mpd_volume`		# read old volume value
  echo 50 > /var/tmp/mpd_volume			# store new volume
 if (( $old_volume > 0)) ; then			# if volume > 0%
 	new_volume=$((old_volume-10))		# lower 10%
 	echo $new_volume > /var/tmp/mpd_volume	# store new volume
 	new_volume=0					# set lowest value
 mpc -q volume $new_volume				# set mpc volume

Mute / UnMute (

 # toggle
 mpc -q toggle

 if [ -f /var/tmp/mpd_volume ]; then		# check for file
  old_volume=`cat /var/tmp/mpd_volume`		# read old volume value
  echo 50 > /var/tmp/mpd_volume			# store new volume
 mpc -q volume $new_volume
 #Antenne Bayern Rock Classic Live
 mpc -q play 4


Define a "Push on" button with 1sec delay for:

Volume Up
Volume Down
One for each radio-stream

The author used:

AC type (Lightning2)
adress 1 01 01 01 1 to 8



Clearing RAM cache

Since some cache is needed for buffering it could be that to much usage will cause a problem with domoticz or other applications running on the same system when remaining RAM is to low. Therefore it is recommended to clear the cache after stopping MPD and/or using a cronjob from time to time. The author starts a "cache-delete" script named "" when shutting off MPD and the power amplifier. Therefore add following script in your "/home/pi/domoticz/scripts" folder:

 free -m
 sudo echo 3 > /proc/sys/vm/drop_caches
 free -m

Also it is a good idea to start the script from the cronjob:

30 6,23 * * * /home/pi/domoticz/scripts/

The cache will be deleted according to this example at 6:30 and 23:30 every day.

Useful links

ALSA: [6]
m3u: [7]
pls: [8]
MPD: [9]
MPC: [10]