Bluesound in Domoticz

Subforum for general discussions. Do not dump your questions/problems here, but try to find the subforum where it belongs!
Post Reply
alveman
Posts: 32
Joined: Wednesday 29 January 2014 23:42
Target OS: Raspberry Pi
Domoticz version:
Contact:

Bluesound in Domoticz

Post by alveman » Saturday 29 April 2017 22:54

Someone how has conneted Bluesound to Domoticz and like to share how. I have found some info but my skills is not good enough.

https://github.com/venjum/bluesound

https://helpdesk.bluesound.com/discussi ... php?t=2293

Martin


Skickat från min iPad med Tapatalk

possm
Posts: 2
Joined: Saturday 07 October 2017 14:28
Target OS: Linux
Domoticz version:
Contact:

Re: Bluesound in Domoticz

Post by possm » Saturday 07 October 2017 14:45

This is my setup for my Bluesound speakers. At this moment all I do is volume control through Domoticz
I've not used the mentioned API because i think the Bluesound speaker AP itself is already useable enough. :D

1 create a Virtual Dummy hardware device: Bluesound
Schermafbeelding 2017-10-07 om 14.31.04.png
Schermafbeelding 2017-10-07 om 14.31.04.png (26.61 KiB) Viewed 220 times
2 Create your Virtual Switches (the speakers)
Schermafbeelding 2017-10-07 om 14.31.39.png
Schermafbeelding 2017-10-07 om 14.31.39.png (74.71 KiB) Viewed 220 times
Schermafbeelding 2017-10-07 om 14.30.46.png
Schermafbeelding 2017-10-07 om 14.30.46.png (42.36 KiB) Viewed 220 times
3 LUA script to translate the Domoticz controls into http requests for the speakers. (Set the script to "Device")

Code: Select all

------------------------------------------------------------------------------------------
-- Version 1 (20171006)
--
-- Domoticz lua script to convert the Domoticz switch commands to control the speaker
-- Creates a http request based on a list of unique IP and passes the volume values to the speakers 
-------------------------------------------------------------------------------------------
-- Poss'M (c)2017
-------------------------------------------------------------------------------------------
-- Define your device IP-addresses (Bluesound devices respond to port 11000)
-- { <IP>, <Virtual Switch Name>}
Devices = {}
Devices [1] = {"192.168.1.60:11000", 'Speaker Woonkamer'}
Devices [2] = {"192.168.1.61:11000", 'Speaker Keuken'}

commandArray = {}
-- Loop trough BlueSound speakers and set the volume to right speaker
for i,device in ipairs(Devices) do
    BSVolControl = 'http://' .. device[1] .. '/Volume?level=' .. otherdevices_svalues[device[2]]
    if (devicechanged[device[2]]) then
            commandArray['OpenURL']=BSVolControl
            print(device[2] .. " (".. device[1] ..") set to " ..  otherdevices_svalues[device[2]] .."%")
    end
end
return commandArray
4 Syncing the volume of the speaker with Domoticz through a LUA script.
When the volume of the speaker is set outside of Domoticz this script will sync its interface with the actual volume of the speaker. (Set the script to "Time")

Code: Select all

------------------------------------------------------------------------------------------
-- Version 1 (20171006)
--
-- Domoticz lua script to convert XML output from Bluesound Speakers to Domoticz commands
-- Reads the status based on a list of unique IP and passes the volume values to virtual switches in Domoticz
-------------------------------------------------------------------------------------------
-- Original XML_Capture function by RATA1 at Domoticz-forum, adapted by Poss'M (c)2017
-------------------------------------------------------------------------------------------
commandArray = {}

function XML_Capture(cmd,flatten)
   local f = assert(io.popen(cmd, 'r'))
   local s = assert(f:read('*a'))
   f:close()
   if flatten  then
      s = string.gsub(s, '^%s+', '')
      s = string.gsub(s, '%s+$', '')
      s = string.gsub(s, '[\n\r]+', ' ')
   end
   return s
end

-- Set debug to true to print to log file.
debug = false

-- Define your device IP-addresses (Bluesound devices respond to port 11000)
-- { <IP>, <Virtual Switch Name>}
Devices = {}
Devices [1] = {"192.168.1.60:11000", 'Speaker Woonkamer'}
Devices [2] = {"192.168.1.61:11000", 'Speaker Keuken'}

for i,device in ipairs(Devices) do
    if debug == true then
            print("Reading values from: 'http://".. device[1] .."/Volume'")
    end
    
    -- Read the XML data from the device
    XML_string=XML_Capture("curl -s 'http://".. device[1] .."/Volume'",1)
    valid = string.find(XML_string, "<volume>")    -- check we are looking in the right place
    
        if debug == true then
            print(XML_string)
        end
       
        if valid == nil then
            print ("Bad XML status read - info NOT updated")
        else
        -- Find in the XML_string the info-fields based on their labels
        -- Determine start/stop-positions of the values within the substrings
        v_open = string.find(XML_string,"<volume>")+8      -- read volume string
        v_close = string.find(XML_string,"</volume>")-1      -- read volume string
        volume= string.sub(XML_string,v_open,v_close)
        
        -- If volume levels mismatch with the Domoticz interface, update the Virtual Switch with the level of the Speaker
        if (otherdevices_svalues[device[2]] ~= volume) then
             commandArray[device[2]] = 'Set Level ' .. volume
        end
     end
 end
return commandArray
Last edited by possm on Wednesday 11 October 2017 9:20, edited 2 times in total.

alveman
Posts: 32
Joined: Wednesday 29 January 2014 23:42
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Bluesound in Domoticz

Post by alveman » Monday 09 October 2017 21:26

Thanx for you LUA code. When I tryed it I can't change the volym from Domoticz. Domoticz it self directly change it back to last set value. I have the two LUA codes in two different LUA file is that right?

Code: Select all

2017-10-09 21:19:11.110 User: Admin initiated a switch command (764/V-rum/Set Level) 
2017-10-09 21:19:12.013 EventSystem: Script event triggered: /home/pi/domoticz/scripts/lua/script_device_bluesound2.lua 
2017-10-09 21:19:12.086 LUA: V-rum (192.168.1.20:11000) set to 11% 
2017-10-09 21:19:12.087 EventSystem: Fetching url... 
2017-10-09 21:19:12.087 EventSystem: Script event triggered: /home/pi/domoticz/scripts/lua/script_device_bluesound1.lua 
2017-10-09 21:19:12.162 (El) Light/Switch (V-rum) 
2017-10-09 21:19:13.114 LUA: V-rum (192.168.1.20:11000) set to 6% 
2017-10-09 21:19:13.115 EventSystem: Fetching url... 
2017-10-09 21:19:13.115 EventSystem: Script event triggered: /home/pi/domoticz/scripts/lua/script_device_bluesound1.lua 
2017-10-09 21:19:13.190 (El) Light/Switch (V-rum) 

possm
Posts: 2
Joined: Saturday 07 October 2017 14:28
Target OS: Linux
Domoticz version:
Contact:

Re: Bluesound in Domoticz

Post by possm » Tuesday 10 October 2017 21:33

alveman wrote:
Monday 09 October 2017 21:26
Thanx for you LUA code. When I tryed it I can't change the volym from Domoticz. Domoticz it self directly change it back to last set value. I have the two LUA codes in two different LUA file is that right?
Yes thats is correct, try setting the second script to "Time", and the first script to "Device".

alveman
Posts: 32
Joined: Wednesday 29 January 2014 23:42
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Bluesound in Domoticz

Post by alveman » Friday 13 October 2017 0:02

possm wrote:
alveman wrote:
Monday 09 October 2017 21:26
Thanx for you LUA code. When I tryed it I can't change the volym from Domoticz. Domoticz it self directly change it back to last set value. I have the two LUA codes in two different LUA file is that right?
Yes thats is correct, try setting the second script to "Time", and the first script to "Device".
Works perfectly now! Thanx


Skickat från min iPhone med Tapatalk

Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests