MPD & Domoticz
Introduction
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 // new: 3.8153
- 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] http://www.alsa-project.org/main/index.php/Main_Page 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.
Preparation
Installation
Login via Putty. It is strongly recommended to update/upgrade! The latest version of MPD/MPC needs an actual system.
sudo su cd sudo service domoticz.sh 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 cd sudo service domoticz.sh stop sudo apt-get install mpd mpc alsa-utils sudo reboot
Wait until the Raspberry Pi has boot completely.
sudo su cd aplay –l
You should see the external audio device as “card1”.
The author is not sure, in German language: “Karte1”
aplay –L
The output depends on the USB audio card used.
Configuration
This is the most important part!
sudo nano /etc/mpd.conf
Set following parameters:
#bind_to_address
#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.
snd-bcm2835
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
127.0.0.1 localhost #::1 localhost ip6-localhost ip6-loopback fe00::0 ip6-localnet ff00::0 ip6-mcastprefix ff02::1 ip6-allnodes ff02::2 ip6-allrouters 127.0.1.1 raspberrypi
Change only:
::1 localhost ip6-localhost ip6-loopback to #::1 localhost ip6-localhost ip6-loopback
“127.0.1.1 raspberrypi” could also be “127.0.1.1 Domoticzpi” (don’t change it)
Check if mpd is member ofthe group "audio"
ifconfig id mpd
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 cd /var/lib/mpd/playlists sudo nano radio.m3u
Place your radio streams there. Have a look at the m3u format here: [2] http://en.wikipedia.org/wiki/M3U 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] http://en.wikipedia.org/wiki/PLS_%28file_format%29
Example for m3u playlist (radio.m3u)
#EXTM3U #EXTINF:-1,WDR2 BL http://gffstream.ic.llnwd.net/stream/gffstream_mp3_w99a #EXTINF:-1,SWR3 http://swr.ic.llnwd.net/stream/swr_mp3_m_swr3b #EXTINF:-1,RSG http://62.27.47.60:8000/ps-radiorsg/livestream.mp3 #EXTINF:-1,Antenne Bayern Classic Rock live http://mp3channels.webradio.antenne.de/classic-rock-live #EXTINF:-1,Antenne Bayern Chillout http://mp3channels.webradio.antenne.de/chillout
Configuring the ALSA mixer
alsamixer
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] http://en.wikipedia.org/wiki/Alsamixer
Testing MPD/MPC
Place a playlist like this "radio.m3u" (user = mpd) in:
/var/lib/mpd/playlist/
#EXTM3U #EXTINF:-1,WDR2 BL http://gffstream.ic.llnwd.net/stream/gffstream_mp3_w99a #EXTINF:-1,SWR3 http://swr.ic.llnwd.net/stream/swr_mp3_m_swr3b #EXTINF:-1,RSG http://62.27.47.60:8000/ps-radiorsg/livestream.mp3 #EXTINF:-1,Antenne Bayern Classic Rock live http://mp3channels.webradio.antenne.de/classic-rock-live #EXTINF:-1,Antenne Bayern Chillout http://mp3channels.webradio.antenne.de/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...
http://www.antenne.de/webradio/channels/classic-rock-live.m3u
...you can extract the mp3 adress like this:
sudo su cd /var/lib/mpd/music wget http://www.antenne.de/webradio/channels/classic-rock-live.m3u 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:
http://mp3channels.webradio.antenne.de/classic-rock-live
This adress can be placed in a "m3u" playlist like this (radio.m3u):
#EXTM3U #EXTINF:-1,Antenne Bayern Classic Rock live http://mp3channels.webradio.antenne.de/classic-rock-live #EXTINF:-1,Antenne Bayern Chillout http://mp3channels.webradio.antenne.de/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] http://www.domoticz.com/wiki/Setting_up_a_RAM_drive_on_Raspberry_Pi
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:
/home/pi/domoticz/scripts/radio
VolumeUP.sh
#!/bin/bash
#Volume UP
if [ -f /var/tmp/mpd_volume ]; then # check for file
old_volume=`cat /var/tmp/mpd_volume` # read old volume value
else
echo 50 > /var/tmp/mpd_volume # store new volume
old_volume=50
fi
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
else
new_volume=100 # set highest value
fi
mpc -q volume $new_volume
VolumeDN.sh
#!/bin/bash
#Volume Down
if [ -f /var/tmp/mpd_volume ]; then # check for file
old_volume=`cat /var/tmp/mpd_volume` # read old volume value
else
echo 50 > /var/tmp/mpd_volume # store new volume
old_volume=50
fi
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
else
new_volume=0 # set lowest value
fi
mpc -q volume $new_volume # set mpc volume
Mute / UnMute (=toogle.sh)
#!/bin/bash
# toggle
mpc -q toggle
RockLive.sh
#!/bin/bash
if [ -f /var/tmp/mpd_volume ]; then # check for file
old_volume=`cat /var/tmp/mpd_volume` # read old volume value
else
echo 50 > /var/tmp/mpd_volume # store new volume
old_volume=50
fi
new_volume=$old_volume
mpc -q volume $new_volume
#Antenne Bayern Rock Classic Live
mpc -q play 4
Switches
Define a "Push on" button with 1sec delay for:
Volume Up Volume Down Mute/UnMute 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 "free-mem.sh" when shutting off MPD and the power amplifier. Therefore add following script in your "/home/pi/domoticz/scripts" folder:
#!/bin/bash
free -m
sync
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/free-mem.sh
The cache will be deleted according to this example at 6:30 and 23:30 every day.
Useful links
ALSA: [6] http://www.alsa-project.org/main/index.php/Main_Page
m3u: [7] http://en.wikipedia.org/wiki/M3U
pls: [8] http://en.wikipedia.org/wiki/PLS
MPD: [9] http://www.musicpd.org/
MPC: [10] http://linux.die.net/man/1/mpc