Listen to your favorite radio channels

Post Reply
User avatar
DewGew
Posts: 264
Joined: Thursday 21 April 2016 12:01
Target OS: Raspberry Pi
Domoticz version: V4.9741
Location: Sweden
Contact:

Listen to your favorite radio channels

Post by DewGew » Thursday 19 April 2018 9:07

Hi All,
I converted "Listen to you favorite radios v2" script to dzVents version.

I a simple radio.lua script. You can select the radio station with a selector switch.

Based on this topic:
viewtopic.php?f=38&t=21419

Dependencies:
Make sure you have a working sound card on domoticz computer.
Make sure you have internet access on domoticz computer
Make sure mplayer is installed :

Code: Select all

sudo apt-get update
sudo apt-get install mplayer
Configuration:
Make a new selector switch with your favorite name, e.g "Radio". Add your radio stations name in "Selector Levels". Then add a new dzVents script. Copy the code and change the radio station addresses, the switch name and the switch selector names. That's about it.
radio_selector.jpg
radio_selector.jpg (47.36 KiB) Viewed 1044 times
dzVents script:

Code: Select all

-- This script play a streaming radio when the device is changed
-- Start files with "mplayer -noconsolecontrols radiostation.mp3 &> /dev/null"
-- Start playlists with "mplayer -noconsolecontrols -playlist radiostation.m3u &> /dev/null"

return {
	on = {
		devices = {
			'Radio'
		}
	},
	execute = function(domoticz, device)

    if  device.state == 'Off' then
        os.execute ("killall mplayer")      
    end
    
    if  device.state ~= 'Off' then
    	 -- domoticz.helpers.btspeaker(domoticz, 'On') -- BTAudio on
        if device.state == 'Mix Megapol' then
            os.execute ("killall mplayer")
            os.execute ("mplayer -noconsolecontrols http://194.16.21.227/mix_se_aacp -volume 100 &> /dev/null")
        end
        
        if device.state == 'NRJ' then
            os.execute ("killall mplayer")
            os.execute ("mplayer -noconsolecontrols http://ads-e-bauerse-fm-05-gos2.sharp-stream.com/nrj_instreamtest_se_aacp -volume 100 &> /dev/null")
        end
        
        if device.state == 'RixFM' then
            os.execute ("killall mplayer")
            os.execute ("mplayer -noconsolecontrols http://fm01-icecast.mtg-r.net/fm01_mp3 -volume 75 &> /dev/null")
        end
        
        if device.state == 'P4 Blekinge' then
            os.execute ("killall mplayer")
            os.execute ("mplayer -noconsolecontrols http://sverigesradio.se/topsy/direkt/213-hi-mp3 -volume 100 &> /dev/null")
        end
    end

end
}
Extra:
I have speak funktion on my domoticz. I made a code snippet to return back to radio after the notification has broadcasts. I use this in my scripts when I'm using speak function.
Spoiler: show

Code: Select all

-- kills radio and speaks message if radio was play its starts again after 20 sec
 
if domoticz.devices('Radio > Vardagsrum').state == 'Off' then
	--  domoticz.helpers.btspeaker(domoticz, 'On') -- BTAudio on
	domoticz.helpers.speak(domoticz, 'My message to broadcast')
else
	os.execute ("killall mplayer")
	domoticz.helpers.speak(domoticz, 'My message to broadcast')
	if domoticz.devices('Radio').state == 'Mix Megapol' then
		domoticz.devices('Radio').switchSelector(10).afterSec(20)
	end
	if domoticz.devices('Radio').state == 'NRJ' then
		domoticz.devices('Radio').switchSelector(20).afterSec(20)
	end
	if domoticz.devices('Radio').state == 'RixFm' then
		domoticz.devices('Radio').switchSelector(30).afterSec(20)
	end
	if domoticz.devices('Radio').state == 'P4 Blekinge' then
		domoticz.devices('Radio').switchSelector(40).afterSec(20)
	end
end
I also use BT audio. I turn on and off connection with a global_data function. This i have in my global_data.lua:
Spoiler: show

Code: Select all

return {
	helpers = {		
			-- Text to Speech (izsynth)
			-- how to use: domoticz.helpers.speak(domoticz, 'Your message here')
			speak = function(domoticz, msg)	
				domoticz.log('izsynth: '.. msg, domoticz.LOG_INFO)
				os.execute("killall mplayer")
				os.execute("izsynth -e naturalreaders -v Emma -t " .. msg .. "")
			end,
		
			-- Connect BTSpeaker
			-- how to use: domoticz.helpers.btspeaker(domoticz, 'On') or domoticz.helpers.btspeaker(domoticz, 'On')
			btSpeaker = function(domoticz, switch)
				if switch == "on" then
					domoticz.log('Connecting to speaker', domoticz.LOG_INFO)
					os.execute("/bin/echo -e 'connect FF:FF:FF:FF:FF:01 \n' | bluetoothctl &> /dev/null")			
				end
				if switch == "off" then
					domoticz.log('Disconnecting speaker', domoticz.LOG_INFO)
					os.execute("/bin/echo -e 'disconnect FF:FF:FF:FF:FF:01 \n' | bluetoothctl &> /dev/null")				
				end
			end
	}
}
Raspberry Pi 2B - domoticz - Aeon Labs Z-Stick GEN5 - RFlink gateway
NanoPi NEO-air - domoticz - REGO6XX interface
Dashticz V2 - Homebridge

misko903
Posts: 48
Joined: Thursday 27 September 2018 22:58
Target OS: Raspberry Pi
Domoticz version: Latest
Location: Trencin, Slovakia
Contact:

Re: Listen to your favorite radio channels

Post by misko903 » Sunday 07 October 2018 15:09

thanks, it works :)

I want to to adapt it to the xiaomi button
I have created a script for physical button:

Code: Select all

return {

   on = { devices = { 29 }},        -- 29 is the idx of HW xiaomi switch
               
   execute = function(dz, item )
        if item.state == "Click" then
            dz.devices(79).toggleSwitch()    -- Switch between radios (79 is the idx of your radio switch)
        elseif item.state == "Long Click" then
            dz.devices(79).switchOff()      -- Radia
        end
	domoticz.log('Device ' .. device.name .. ' was changed', domoticz.LOG_INFO)
   end
}
what it does: Single click - turn on radio, Long click - switch off radio.
what it should do: Single click - switch between states of selector switch: Off, 1st radio, 2nd radio, 3rd radio... and again.

what am I missing?

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest