Listen to your favourite radios V2

In this subforum you can show projects you have made, or you are busy with. Please create your own topic.

Moderator: leecollings

0n370uch
Posts: 50
Joined: Monday 08 January 2018 2:30
Target OS: Raspberry Pi / ODroid
Domoticz version: Latest
Contact:

Listen to your favourite radios V2

Post by 0n370uch »

Hello,

I build a new radio.lua script. You can select the radio station via a selector switch.

Based on this topic:
viewtopic.php?t=10672
And this wiki article:
http://www.domoticz.com/wiki/Play_streaming_radio

First install the necessary tool:

Code: Select all

sudo apt-get update
sudo apt-get install mplayer
How to add:
Just make a new selector switch with your favorite name (in my example "Wohnzimmer > Radio"). Add your radio stations with name. Then add a new LUA script and select "Device only" as execution type. Copy the code and change the radio station addresses, the switch name and the switch selector names. Thats all.

switch.jpg
switch.jpg (173.18 KiB) Viewed 10140 times
lua.geaendert.jpg
lua.geaendert.jpg (337.42 KiB) Viewed 10140 times

The Code:

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"
--
commandArray = {}

if (devicechanged['Wohnzimmer > Radio'] == 'Off') then
    os.execute ("killall mplayer")
end

if (devicechanged['Wohnzimmer > Radio'] == '1FM') then
    os.execute ("killall mplayer")
    os.execute ("mplayer -noconsolecontrols http://uk2.internet-radio.com:8358/ &> /dev/null")
end

if (devicechanged['Wohnzimmer > Radio'] == '1LIVE') then
    os.execute ("killall mplayer")
    os.execute ("mplayer -noconsolecontrols https://wdr-1live-live.icecastssl.wdr.de/wdr/1live/live/mp3/128/stream.mp3 &> /dev/null")
end

if (devicechanged['Wohnzimmer > Radio'] == 'Bayern 3') then
    os.execute ("killall mplayer")
    os.execute ("mplayer -noconsolecontrols http://br-br3-live.cast.addradio.de/br/br3/live/mp3/128/stream.mp3 &> /dev/null")
end

if (devicechanged['Wohnzimmer > Radio'] == 'MDR Jump') then
    os.execute ("killall mplayer")
    os.execute ("mplayer -noconsolecontrols http://mdr-284320-0.cast.mdr.de/mdr/284320/0/mp3/high/stream.mp3 &> /dev/null")
end

if (devicechanged['Wohnzimmer > Radio'] == 'Radio Berg') then
    os.execute ("killall mplayer")
    os.execute ("mplayer -noconsolecontrols http://edge.live.mp3.mdn.newmedia.nacamar.net/radioberg/livestream.mp3 &> /dev/null")
end

if (devicechanged['Wohnzimmer > Radio'] == 'SWR1 Rheinland-Pfalz') then
    os.execute ("killall mplayer")
    os.execute ("mplayer -noconsolecontrols http://swr-swr1-rp.cast.addradio.de/swr/swr1/rp/mp3/128/stream.mp3 &> /dev/null")
end

if (devicechanged['Wohnzimmer > Radio'] == 'SWR3') then
    os.execute ("killall mplayer")
    os.execute ("mplayer -noconsolecontrols http://swr-swr3-live.cast.addradio.de/swr/swr3/live/mp3/128/stream.mp3 &> /dev/null")
end

if (devicechanged['Wohnzimmer > Radio'] == 'WDR2 Bergisches Land') then
    os.execute ("killall mplayer")
    os.execute ("mplayer -noconsolecontrols https://wdr-wdr2-bergischesland.icecastssl.wdr.de/wdr/wdr2/bergischesland/mp3/128/stream.mp3 &> /dev/null")
end

if (devicechanged['Wohnzimmer > Radio'] == 'WDR4') then
    os.execute ("killall mplayer")
    os.execute ("mplayer -noconsolecontrols https://wdr-wdr4-live.icecastssl.wdr.de/wdr/wdr4/live/mp3/128/stream.mp3 &> /dev/null")
end

return commandArray
By the way: I trigger the script with an blocky script. This blocky script will be execute if an external bluetooth radio is connected. I can post the cronjob and blocky script too. Therefore I added the follwing code to every switch status:

Code: Select all

commandArray['Variable:Wohnzimmer > Radio Senderspeicher'] = '10'
Greetings Fabi
Last edited by 0n370uch on Thursday 01 March 2018 13:13, edited 3 times in total.
Derik
Posts: 1602
Joined: Friday 18 October 2013 23:33
Target OS: Raspberry Pi / ODroid
Domoticz version: BETA
Location: Arnhem/Nijmegen Nederland
Contact:

Re: Listen to your favourite radios V2

Post by Derik »

mm
Looks great...
Please can you tell me how and what hardware you use??
Do u use a BT speaker??
https://pisces.bbystatic.com/image2/Bes ... xWidth=550
Or what speakers do u use?

Looks a great option to use a single[ or multi ]bt speaker.. so i can place more speakers in my house..
Xu4: Beta Extreme antenna RFXcomE,WU Fi Ping ip P1 Gen5 PVOutput Harmony HUE SolarmanPv OTG Winddelen Alive ESP Buienradar MySensors WOL Winddelen counting RPi: Beta SMAspot RFlinkTest Domoticz ...Different backups
0n370uch
Posts: 50
Joined: Monday 08 January 2018 2:30
Target OS: Raspberry Pi / ODroid
Domoticz version: Latest
Contact:

Re: Listen to your favourite radios V2

Post by 0n370uch »

Hi, I'm using a Raspberry Pi 2b with Bluetooth dongle (4.1) and a Panasonic Hifi System with Bluetooth (function is similar to Bluetooth speakers). Just connect the bluetooth speaker to your Raspberry and enjoy radio :-). Here is the code for bluetooth (for Rasbian Stretch). This code connect the Raspberry with your Bluetooth device and reconnect if connection is lost.

Connect Bluetooth to device:

Code: Select all

//sudo systemctl start bluetooth && sudo systemctl enable bluetooth
sudo bluetoothctl
power on
agent on
scan on
connect 00:11:22:33:44:55
trust 00:11:22:33:44:55
scan off
exit
Cronjob Bluetooth auto connect:

Code: Select all

sudo crontab -e
*/1 * * * * /bin/echo -e 'connect 00:11:22:33:44:55 \n' | bluetoothctl &> /dev/null
Greetings Fabi
tukestolo
Posts: 12
Joined: Wednesday 28 February 2018 0:35
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Listen to your favourite radios V2

Post by tukestolo »

Please some one help me :(
I have done everything like you said but I cannot make it play.

i will send all my info.
First of all I run the command on terminal and it plays the selected radio, then I do off on my dummy selector and it stop the radio.
My problem it is when I select a radio from selector it does not do nothing :(

can some one help me please?
thanks.
0n370uch
Posts: 50
Joined: Monday 08 January 2018 2:30
Target OS: Raspberry Pi / ODroid
Domoticz version: Latest
Contact:

Re: Listen to your favourite radios V2

Post by 0n370uch »

Hi,

The device name and the selector step must be the same:

Code: Select all

if (devicechanged['Wohnzimmer > Radio'] == '1LIVE')
This is the name of the device:

Code: Select all

Wohnzimmer > Radio
And this is the step name:

Code: Select all

1LIVE
Please check the Domoticz status log.

Greetings Fabi
tukestolo
Posts: 12
Joined: Wednesday 28 February 2018 0:35
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Listen to your favourite radios V2

Post by tukestolo »

Thanks Fabi for the response but I believe that is not the problem.
Check all my configurations.

The switch
Captura de ecrã 2018-02-28, às 16.33.54.png
Captura de ecrã 2018-02-28, às 16.33.54.png (70.75 KiB) Viewed 9613 times
The Script
Captura de ecrã 2018-02-28, às 16.34.10.png
Captura de ecrã 2018-02-28, às 16.34.10.png (134.28 KiB) Viewed 9613 times
The log
2018-02-28 16:38:02.933 User: Oliveira initiated a switch command (259/Radio/Set Level)
2018-02-28 16:38:02.937 (Switches) Light/Switch (Radio)
2018-02-28 16:38:03.037 LUA: 1FM

And if a run the command from terminal it plays the radio.
And as you can see the log print "LUA: 1FM". :S :S
0n370uch
Posts: 50
Joined: Monday 08 January 2018 2:30
Target OS: Raspberry Pi / ODroid
Domoticz version: Latest
Contact:

Re: Listen to your favourite radios V2

Post by 0n370uch »

Ok everything looks good... One idea:
Add the "sudo" command to the two "os.execute" lines. You added the "sudo" command to your first "killall" too.

Hope this is the solution...
tukestolo
Posts: 12
Joined: Wednesday 28 February 2018 0:35
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Listen to your favourite radios V2

Post by tukestolo »

it does not work, I don't know what I can try more :/

Can I write some code on script to see if command give me a error ? to see the output of the command respponse ?
User avatar
emme
Posts: 909
Joined: Monday 27 June 2016 11:02
Target OS: Raspberry Pi / ODroid
Domoticz version: latest
Location: Milano, Italy
Contact:

Re: Listen to your favourite radios V2

Post by emme »

Ciao,

your solution looks great, but... can you check the CPU usage during that script?
My understanding the LUA scripting tells me that the os.execute runs the stream and should keep busy the PI raising CPU, but my lack of knowledge in bash and linux system suggest me that the
mplayer -noconsolecontrols should slice the command as a service.. or something like that, so the LUA script should complete with no error and keep it free to be refired...
is that correct?

if so... you're solution is simply great!
you could even add a selector switch with the destination (BT Speacker, local speaker etc etc) to be evaluate and activate via alsa command... so you get a multiroom system :P
The most dangerous phrase in any language is:
"We always done this way"
tukestolo
Posts: 12
Joined: Wednesday 28 February 2018 0:35
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Listen to your favourite radios V2

Post by tukestolo »

0n370uch wrote: Wednesday 28 February 2018 17:53 Ok everything looks good... One idea:
Add the "sudo" command to the two "os.execute" lines. You added the "sudo" command to your first "killall" too.

Hope this is the solution...
So friend I found that if a run the command with sudo on command line, the raspberry pi dont play anything. But if I run the command without sudo it plays.
Again I cannot understand why it dont play when call it on Domoticz :(
0n370uch
Posts: 50
Joined: Monday 08 January 2018 2:30
Target OS: Raspberry Pi / ODroid
Domoticz version: Latest
Contact:

Re: Listen to your favourite radios V2

Post by 0n370uch »

emme wrote: Thursday 01 March 2018 8:24 My understanding the LUA scripting tells me that the os.execute runs the stream and should keep busy the PI raising CPU, but my lack of knowledge in bash and linux system suggest me that the
mplayer -noconsolecontrols should slice the command as a service.. or something like that, so the LUA script should complete with no error and keep it free to be refired...
is that correct?
Hi, yeah thats correct. We have two important options. The first one is the "-noconsolecontrols" to disable the controls. And the second more important option is "&> /dev/null". This second option will close the terminal without any output. The process is running in the background. Here you can see two pictures with and without running radio. The CPU load is very low.

With LUA radio script:
cpu load with radio.jpg
cpu load with radio.jpg (338.26 KiB) Viewed 9547 times
Without LUA radio script:
cpu load without radio.jpg
cpu load without radio.jpg (349.43 KiB) Viewed 9547 times
0n370uch
Posts: 50
Joined: Monday 08 January 2018 2:30
Target OS: Raspberry Pi / ODroid
Domoticz version: Latest
Contact:

Re: Listen to your favourite radios V2

Post by 0n370uch »

tukestolo wrote: Thursday 01 March 2018 12:47 So friend I found that if a run the command with sudo on command line, the raspberry pi dont play anything. But if I run the command without sudo it plays.
Again I cannot understand why it dont play when call it on Domoticz :(
Ok seems like trouble with your user rights. Domoticz is running as user (in normal case). Please remove all "sudo" commands from your LUA script.

Another idea for debuging. Change this:

Code: Select all

os.execute ("mplayer -noconsolecontrols http://uk2.internet-radio.com:8358/ &> /dev/null")
To this command:

Code: Select all

os.execute ("mkdir ~/test &> /dev/null")
This new command add a folder named "test" to your home dir.

Greetings Fabi
User avatar
emme
Posts: 909
Joined: Monday 27 June 2016 11:02
Target OS: Raspberry Pi / ODroid
Domoticz version: latest
Location: Milano, Italy
Contact:

Re: Listen to your favourite radios V2

Post by emme »

0n370uch wrote: Thursday 01 March 2018 12:55 Hi, yeah thats correct. We have two important options. The first one is the "-noconsolecontrols" to disable the controls. And the second more important option is "&> /dev/null". This second option will close the terminal without any output. The process is running in the background. Here you can see two pictures with and without running radio. The CPU load is very low.
Thank you so much!!!

This kind of scenraio should be included natevely in Domticz by my personal opinion :P :P
The most dangerous phrase in any language is:
"We always done this way"
0n370uch
Posts: 50
Joined: Monday 08 January 2018 2:30
Target OS: Raspberry Pi / ODroid
Domoticz version: Latest
Contact:

Re: Listen to your favourite radios V2

Post by 0n370uch »

emme wrote: Thursday 01 March 2018 13:57 Thank you so much!!!
This kind of scenraio should be included natevely in Domticz by my personal opinion :P :P
Yeah why not... You are welcome :-).
tukestolo
Posts: 12
Joined: Wednesday 28 February 2018 0:35
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Listen to your favourite radios V2

Post by tukestolo »

So friends I do more debug and I found another thing, the Env is different from command run on Domoticz(command don't work) and command run on console (Command works).

Do you thinks that could be the problem ?

Domoticz Env:

JOURNAL_STREAM=8:37052
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin
INVOCATION_ID=1582c800774049bfb9ddf14d0ffc0d82
LANG=en_GB.UTF-8
PWD=/home/pi/domoticz


Working Env:

LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=00:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.zst=01;31:*.tzst=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.jpg=01;35:*.jpeg=01;35:*.mjpg=01;35:*.mjpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.m4a=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.oga=00;36:*.opus=00;36:*.spx=00;36:*.xspf=00;36:
LANG=en_GB.UTF-8
OLDPWD=/
XDG_SESSION_ID=c14
USER=pi
PWD=/tmp
HOME=/home/pi
TEXTDOMAIN=Linux-PAM
SSH_TTY=/dev/pts/1
MAIL=/var/mail/pi
TERM=xterm-256color
SHELL=/bin/bash
SHLVL=1
MANPATH=/home/pi/.linuxbrew/share/man:
LOGNAME=pi
XDG_RUNTIME_DIR=/run/user/1000
PATH=/home/linuxbrew/.linuxbrew/bin:/home/linuxbrew/.linuxbrew/sbin:/home/pi/.linuxbrew/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games
INFOPATH=/home/pi/.linuxbrew/share/info:
_=/usr/bin/env
0n370uch
Posts: 50
Joined: Monday 08 January 2018 2:30
Target OS: Raspberry Pi / ODroid
Domoticz version: Latest
Contact:

Re: Listen to your favourite radios V2

Post by 0n370uch »

Ok. Can you try this (with your correct path):

Code: Select all

os.execute ("/usr/bin/mplayer -noconsolecontrols http://uk2.internet-radio.com:8358/ &> /dev/null")
Or:

Code: Select all

os.execute ("sudo /usr/bin/mplayer -noconsolecontrols http://uk2.internet-radio.com:8358/ &> /dev/null")
tukestolo
Posts: 12
Joined: Wednesday 28 February 2018 0:35
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Listen to your favourite radios V2

Post by tukestolo »

so I found that if a run Domoticz with init.d some commands on lua script don't work.
but if I start Domoticz with pi user and with ./domoticz command the lua commands work well....

i am so lost right know ...
0n370uch
Posts: 50
Joined: Monday 08 January 2018 2:30
Target OS: Raspberry Pi / ODroid
Domoticz version: Latest
Contact:

Re: Listen to your favourite radios V2

Post by 0n370uch »

tukestolo wrote: Saturday 03 March 2018 13:47 but if I start Domoticz with pi user and with ./domoticz command the lua commands work well....
Everything is going well in this case?
tukestolo
Posts: 12
Joined: Wednesday 28 February 2018 0:35
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Listen to your favourite radios V2

Post by tukestolo »

0n370uch wrote: Saturday 03 March 2018 15:17
tukestolo wrote: Saturday 03 March 2018 13:47 but if I start Domoticz with pi user and with ./domoticz command the lua commands work well....
Everything is going well in this case?
Yes :/
0n370uch
Posts: 50
Joined: Monday 08 January 2018 2:30
Target OS: Raspberry Pi / ODroid
Domoticz version: Latest
Contact:

Re: Listen to your favourite radios V2

Post by 0n370uch »

Hi, sounds like a Domoticz global problem. Now I am at the end with my knowledge ...sry. Just one idea, check this:
https://www.domoticz.com/wiki/Linux
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest