Domoticz MPD Airplay solution

So its been a while again but i wanted to write something about my audio solution so i can control multiple speakers using domoticz and could select on which speakers i wanted my music played and even multiple rooms with the same music.

When i was searching on the web i found the forked-daapd solution which includes an Itunes server and MPD possibility’s.

No difficult at all you just install the solution and make some changes to the config so the server can find you’re music files and you are up and running.

To find an good iphone or android app is still difficult for me because on iphone i use the MPoD app but it doesn’t work all the time and on android i use the app MPDroid which is actually more stable for me.

So i had some old pc speakers which i didn’t used for an long time but it was perfect for testing and even the bathroom.

The thing i had to do was to make sure that the speakers could get music using the airplay protocol. Also there where several sollutions for on the internet so i used a couple of Raspberry Pi Zero’s and added the packages for the airplay protocol to it.

After that yes my forked-daapd server could find the speakers as outputs and i could select them.

Then the interesting part for me, i wanted that my domoticz server could automatically detect an new airplay device and add it to the hardware audio_devices.

So i created an python script which logs on the forked-daapd mpd server and checks the output devices which it compares to the domoticz audio_devices hardware.

this is the getAudioSpeakers.py in my github page.
https://github.com/squandor/DomoticzTools

This script does an couple of things:

  • It checks if the audio device which he found in the MPD server also exists in Domoticz, does it not exist that it will create an device with an On and Off action and an nice audio icon for it of course 🙂 
  • but it also checks and controls the Volume which has been set in the app to add it to domoticz

That was the first script but the problem was it was in 1 direction. if something changes on the forked-daapd server it will synchronize to domoticz. But i wanted that the server also changes when i select an audio device in domoticz.

the second script controls that one, it is also the script which will be used in the on/off action for the device:

ControlAudio.py:

  • Its an simple script which takes parameters and changes the values on the mpd server.

#!/usr/bin/python
from mpd import MPDClient
import mpd
import random
import sys

def createClient():
client = MPDClient()
client.timeout = 10
client.connect(“192.168.1.145”, 6600)
return client

def stopClient(client):
client.clear()
client.stop()

name = sys.argv[1]
command = sys.argv[2]

client = createClient()
audioids = client.outputs()
for i in audioids:
if i[‘outputname’] == name:
id = i[‘outputid’]
if command == “1”:
client.enableoutput(id)
if command == “0”:
client.disableoutput(id)

But to let it work with domoticz i needed an simple step before this with an shell script which calls the controlAudio.py script…

ControlAudio.sh:
#!/bin/bash
/home/pi/domoticz/scripts/$1 $2 $3

so using this procedure the getAudioSpeakers.py will create an audio speaker device with for example an on action where Woonkamer is the airplay device name and 1 the on value:
script:///home/pi/domoticz/scripts/controlAudio.sh controlAudio.py Woonkamer 1

then i added an cron job which will run the getAudioSpeakers.py every 5 minutes.

The only thing needed now was the volume in domoticz. for this i created an volume device under the audio_devices hardware.

After that i created an lua script which runs when the volume is changed:

So if i want an new set of speakers in an room i make sure there is an airplay protocol in the speakers or i add an raspberry zero to it and it will be automatically added to domoticz and callable using the python scripts 🙂

I’ve added all the scripts to my github page under MPD
https://github.com/squandor/DomoticzTools

happy days! 🙂