Listen to your favorite radio channels

Moderator: leecollings

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

Listen to your favorite radio channels

Post by DewGew »

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 3904 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 3 | domoticz | Aeon Labs Z-Stick GEN5 | RFlink gateway
NanoPi NEO-air | REGO6XX interface | Machinon theme | Homebridge | Domoticz Google Assistant | ideAlarm
misko903
Posts: 51
Joined: Thursday 27 September 2018 22:58
Target OS: Raspberry Pi / ODroid
Domoticz version: Latest
Location: Trencin, Slovakia
Contact:

Re: Listen to your favorite radio channels

Post by misko903 »

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?
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Listen to your favorite radio channels

Post by waaren »

misko903 wrote: Sunday 07 October 2018 15:09 I want to to adapt it to the xiaomi button
I have created a script for physical button:
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?
In case you are still interested:

Code: Select all

return {
        on = { devices = { 29 }},        -- 29 is the idx of HW xiaomi switch
               
        logging = {   level           =   domoticz.LOG_DEBUG,
                      marker          =   "RadioSelector"
                  },
         
   execute = function(dz, item )
        local selector = dz.devices(79)
        local levels   = {}
        
        local function logWrite(str,level)
            dz.log(str,level or dz.LOG_DEBUG)
        end
        
        local function init()
            for i = 1,#selector.levelNames do
                logWrite("level " .. (i * 10 - 10 ) .." ==>> " .. selector.levelNames[i])
                levels[i] = selector.levelNames[i]
            end
        end

        if item.state == "Click" then
            init() 
            local newLevel = selector.level + 10
            logWrite("newLevel: " .. newLevel)
            if not levels[newLevel/10+1] then
                newLevel = 10
                logWrite("newLevel: " .. newLevel)
            end    
            selector.dimTo(newLevel)    -- Switch between radios (79 is the idx of your radio switch)
            logWrite("dimmed to level " .. newLevel)
        elseif item.state == "Long Click" then
            selector.dimTo(0)      -- Radia
        end
   end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest