Raspberry Pi Jukebox

My problem: One sad day my radio and amplifier box died. I decided to replace it with a cheap bluetooth amplifier and my mobile phone.

 

But to switch on the radio proved to be a lengthy task:

- Switch on bluetooth amplifier.

- Switch on bluetooth on mobile phone, wait for connection.

- Open the radio app and start the stream.

- Afterwards, everything in reverse order.

And, my wife didnt remember all those steps ...

 

My Solution: A cheap raspberry pi zero w maintains radio streams and serves a simple HTML app for mobile devices.

 

The app for the mobile phone (see pictures on the right) consists of three tabs:

* Tab "Radio" to switch radio on/off and change radio streams.

* Tab "MP3" contains a tree that visualizes a directory structure containing songs.

   Click on a song to copy (not a full copy, I use symbolic links) it to the jukebox.

* Tab "Jukebox" visualizes a directory of songs to be played. After you selected some 

   songs, click the "Jukebox" tab just once to start playing the songs.

* Launch Chrome for Android and open the radio web page to pin it to your home screen.

  Tap the menu button and tap "Add to homescreen". You’ll be able to enter a name for

   the shortcut and then Chrome will add it to your home screen. On the iphone, who cares?

 

Hardware

* Linux laptop/desktop to copy the operating system for the raspi onto the

   sdhc card and lateron connect to the raspi via ssh.

* Raspberry Pi Zero W, sdhc card, power supply, raspi case.

* UGREEN Extreme USB soundcard, cable micro usb/male to usb-a/female.

* A pair of active speakers.

* Or: Amplifier + speakers, I already owned a Lepy LP-168 Plus and a pair of

   speakers + line-in cable.

 

Software

Install RaspiZeroW OS headless

* Download latest release of raspbian stretch lite from here:

   https://howchoo.com/g/nzc0yjzjy2u/raspbian-stretch-download

 

* Unzip the image

 

* To copy the raspbian stretch lite image to the sdhc card, start a terminal on the linux laptop

   and enter:

   $> sudo dd bs=4M if=raspbian-stretch-lite.img of=/dev/mmcblk1 status=progress conv=fsync

 

* Mount the newborn image:

   $> sudo mount /devmcblk1p2 /mnt

   $> cd /mnt

 

* Then, edit /mnt/etc/network/interfaces:

   allow-hotplug wlan0

   iface wlan0 inet static

      address 192.168.1.208

      netmask 255.255.255.0

      gateway 192.168.1.1

      wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

   The interfaces file must be exactly like this, no extra lines, no additional comments.

 

* Edit /mnt/etc/wpa_supplicant/wpa_supplicant.conf

   ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev

   update_config=1

    network={

       ssid="MY LOCAL SSID"

       psk="MY WIRELESS PASSWORD“

       key_mgmt=WPA-PSK

    }

 

* Activate ssh:

   $> cd /mnt

   $> touch ssh

 

* Boot the RaspiZeroW and change password:

   Switch on the raspi and wait a minute.

   The raspi should respond to:

   $> ping 192.168.1.208

   Connect via ssh:

   $> ssh pi@192.168.1.208 (User is „pi“, password is „raspberry“)

   $> sudo su -

   $> passwd pi

Change the password!

screenshot 20200325 141441
screenshot 20200325 141529
screenshot 20200325 141637
screenshot 20200325 141659

Make RaspiZeroW and USB soundcard friends

* Plug USB soundcard (and speakers) into free USB port of the RaspiZeroW

* $> ssh pi@192.168.1.208 

* $> speaker-test -c2 -D hw:1,0   # you should hear white noise

 

* Edit /usr/share/alsa/alsa.conf:

defaults.ctl.card 1    # instead of 0

defaults.pcm.card 1    # instead of  0

 

* Try your first radio stream

$> sudo apt-get update

$> sudo apt-get install vlc-nox      # we use vlc as a player

$> cvlc http://mp3stream3.apasf.apa.at:8000    # starts Ö1 livestream (Ö3 would be: http://mp3stream7.apasf.apa.at:8000)

 

* Set Volume (look for something like that:  ... name='Speaker Playback Volume')

$> alsamixer       # try the settings
$> amixer controls                # detect id/number of volume control
$> amixer cget numid=6        # 6 is id/number of volume control on USB soundcard
$> amixer cset numid=6 16    # set volume to 16 (in a range from 0-37)

 

RaspiZeroW webserver

As a webserver on the RaspiZeroW, I use flask. Flask is written in python, which is a long time favorite of mine.

 

* From the linux laptop, connect the RaspiZeroW via ssh:

   $> ssh pi@192.168.1.208      # or similar IP

 

* On the raspi, install flask:

   $> sudo apt-get update

   $> sudo apt-get install python-pip

   $> sudo pip install flask

 

* Install the jukebox from github:

   $> git clone git://github.com/josefscheir/raspi-jukebox.git

   $> cd raspi-jukebox

 

* Change number of speaker volume control if necessary:

   To find the number of the volume control -> look at chapter "Make RaspiZeroW and USB soundcard friends" on this very page

   Edit ./stereo_flask.py and change following line, if necessary:

   app.config['AMIXER_VOLUME_CONTROL'] = '6'

 

* Change the directories where your music files and your jukebox are stored:

   Edit ./static/stereokombi.js and change the following lines:

   var musicFolder = '/tera/Musik';
    var jukeboxFolder = '/tera/jukebox';

 

* Start the webserver:

   $> cd raspi-jukebox

   $> python stereo_flask.py

 

* Connect from the browser using

   http://192.168.1.208:8080

 

How to store songs

You can store the music files on the SDHC card, i.e. the local filesystem of the RaspiZeroW or, as I did, play the songs from a fileserver in the local LAN, which in my case (surprise!) is a Raspberry Pi 3B with two attached harddrives. I use sshfs instead of samba, it has less overhead and easy configuration.

 

Install sshfs on the fileserver and also on the jukebox-raspi:

$> sudo apt-get update

$> sudo apt-get install sshfs

 

Connect to the RaspiZeroW via ssh and try it:

$> sudo sshfs -o allow_other pi@192.168.1.200:/home/pi /mnt     # where 192.168.1.200 is the file server

$> cd /mnt    # you should see the home dir of the fileserver

 

Add a mount point for the fileserver share, the name of mine is "tera":

$> sudo mkdir /tera

$> sudo chmod ugo+rwx /tera

 

Add private/public key certificate, so it can be automounted at start time without  asking for a password:

$> ssh-keygen -t rsa
$> ssh-copy-id -i ~/.ssh/id_rsa.pub pi@192.168.1.200

 

Edit /etc/fstab and add following line to mount the file server share automatically at boot time:

pi@192.168.1.200:/tera/ /tera/ fuse.sshfs noauto,x-systemd.automount,_netdev,users,idmap=user,IdentityFile=/home/pi/.ssh/id_rsa,allow_other,reconnect 0 0

 

Autostart raspi-jukebox at boot time

Edit /etc/rc.local and add the following line before "exit 0":

su pi -c 'nohup /usr/bin/python /home/pi/raspi-jukebox/stereo_flask.py'

 

Reboot the RaspiZeroW:

$> sudo init 6

 

Wait a minute, then reconnect to RaspiZeroW via ssh and be nosy:

$> ps ax|grep flask

 

Fetch your mobile phone and try http://192.168.1.208:8080

 

I'd love to get some feedback, so if you have questions, suggestions, send me a message !