Continue playing after Internet connection is lost

I’m using my Raspberry PI as a MusicPlayer en most of the times I’m playing a radio stream from the Internet. However, when the Internet connection is lost the stream stops. (duh…) so I made a script that runs at startup and checks every 20 seconds if the Internet is still up. If not, mpc kicks in and loads a local playlist to play songs from the local storage. When Internet is back on, mpc kicks in again and starts my favourite radio stream.
This is the script:

#!/bin/bash

while true
    do
        if ping -c2 www.google.nl | grep -q 100%
        then
                if [ -f '/home/pi/playingstream' ];
                then
                        rm /home/pi/playingstream
                        echo "Network unreacheable" > /home/pi/playinglocal
                        mpc stop
                        mpc clear
                        mpc load local
                        mpc play
                fi
        else
                if [ -f '/home/pi/playinglocal' ];
                then
                        rm /home/pi/playinglocal
                        echo "No network issues" > /home/pi/playingstream
                        mpc stop
                        mpc clear
                        mpc load Radio10Live
                        mpc play
                fi
        fi
        sleep 20
done

.Nico

hi there,

after many attempts I was unable to start the script…

have you any ideas?

br
d

start it with sudo ?
or even better, call your script /home/pi/checkInternet.sh and add this line in /etc/rc.local just before exit:

/home/pi/checkInternet.sh & > /dev/null 2>&1

and of course you’ll need the playlists ‘local’ and ‘Radio10Live’

This script isnt work for me. Any plugin instead?

Did you create the two playlists in mpc first?

What is “MPC”?

mpc is the Music Player Client to be used for the Music Player Daemon (MPD)

The mentioned method of playing playlists with mpc is very old and based on Volumio version 1.5x
Since version 2.x of volumio, playing playlists is handled different.

It might still work with mpc but playing with this method does not show anything on the GUI.
It will be better to use the volumio API when using version 2.x of volumio.
It will the be something like this:

#!/bin/bash

while true
    do
        if ping -c2 8.8.8.8 | grep -q 100%
        then
                if [ -f '/home/volumio/playingstream' ];
                then
                        rm /home/volumio/playingstream
                        echo "Network unreacheable" > /home/volumio/playinglocal
                        curl localhost:3000/api/v1/commands/?cmd=stop
                        curl localhost:3000/api/v1/commands/?cmd=clearQueue
                        curl localhost:3000/api/v1/commands/?cmd=playplaylist&name=usb
                        ##curl localhost:3000/api/v1/commands/?cmd=play
                fi
        else
                if [ -f '/home/volumio/playinglocal' ];
                then
                        rm /home/volumio/playinglocal
                        echo "No network issues" > /home/volumio/playingstream
                        curl localhost:3000/api/v1/commands/?cmd=stop
                        curl localhost:3000/api/v1/commands/?cmd=clearQueue
                        curl localhost:3000/api/v1/commands/?cmd=playplaylist&name=radio10-80
                        ##curl localhost:3000/api/v1/commands/?cmd=play
                fi
        fi
        sleep 20
done

Make sure you’ve created the playlists named ‘usb’ and ‘radio10-80’ before running this script.
playlist ‘usb’ is a playlists that plays all songs from the USB stick
playlist ‘radio10-80’ is a webradio station saved in a playlist.

Playlists should be created in the queue.

Also, create an empty file with ‘touch /home/volumio/playinglocal’
This will then be replaced by ‘playingstream’ when you have Internet and running the script the first time.

.Nico

it is also possible to add the playlist command to /volumio/app/plugins/system_controller/volumio_command_line_client/volumio.sh

in the section
case “$1” in

and then add the following underneath clear)

playlist)
       /usr/bin/curl "http://127.0.0.1:3000/api/v1/commands/?cmd=playplaylist&name=$2"
       ;; 

you can then use the CLI command 'volumio playlist ’

.Nico

Hi, can you help me understand how to use APIs?
Where should I enter the routine you kindly wrote?

I’m smart and I suggest an improvement, it should check if the day is a holiday and change the alarm time and maybe even the webradio

Thank you for your explanation

Antonio were you able to figure it out? I am looking to do this but I’ve never worked with a raspberry pi

UPDATE:
Place the following code in a file eg. /home/volumio/control.sh and give it execute flag (chmod +x)

#!/bin/bash

while true
    do
        if ping -c2 8.8.8.8 | grep -q 100%
        then
                if [ -f '/home/volumio/playingstream' ];
                then
                        rm /home/volumio/playingstream
                        echo "Network unreacheable" > /home/volumio/playinglocal
                        curl "http://localhost:3000/api/v1/commands/?cmd=stop"
                        curl "http://localhost:3000/api/v1/commands/?cmd=clearQueue"
                        curl "http://localhost:3000/api/v1/commands/?cmd=playplaylist&name=usb"
                fi
        else
                if [ -f '/home/volumio/playinglocal' ];
                then
                        rm /home/volumio/playinglocal
                        echo "No network issues" > /home/volumio/playingstream
                        curl "http://localhost:3000/api/v1/commands/?cmd=stop"
                        curl "http://localhost:3000/api/v1/commands/?cmd=clearQueue"
                        curl "http://localhost:3000/api/v1/commands/?cmd=playplaylist&name=radio10-80"
                fi
        fi
        sleep 20
done

Make sure you’ve created the playlists named ‘usb’ and ‘radio10-80’ before running this script.
playlist ‘usb’ is a playlists that plays all songs from the USB stick
playlist ‘radio10-80’ is a webradio station saved in a playlist.

Playlists should be created in the queue.

Also, create an empty file with ‘touch /home/volumio/playinglocal’
This will then be replaced by ‘playingstream’ when you have Internet and running the script the first time.

Run the script in the background with: ‘/home/volumio/control.sh &’

.Nico