Raspberry Pi Car Media Server [gav-pi2]

Pi Car Media Server Oblique

This is a bit old now... you really should be looking at version 2.0 of this project :-)

This was extremely simple to set up... eventually... once I'd battled with RaspBMC for a couple of nights (it's too constrained to do a particular job - although it does it very well) and switched to Raspbian...

The thick yellow (orange externally), red and black wires carry +12V, +12V (accessory) and GND from the car. The thin white and blue wires are handshaking signals between the Pi and the power supply for controlled shutdown.

Page Contents:

  1. What did it used to only do?
  2. What does it also do now?
  3. Initial setup
  4. Configuring the Wi-Fi Hotspot
  5. Configuring DHCP / DNS
  6. Configuring a 3G / 4G USB Modem
  7. Configuring IP Forwarding & Network Address Translation (NAT)
  8. Mounting the hard drive
  9. Configuring default sound output
  10. Configuring Music Player Daemon (MPD)
  11. Configuring MiniDLNA
  12. Configuring nfs-kernel-server
  13. Grrr...
  14. Configuring the Power Circuitry
  15. Automatic Random Tracks
  16. Server Ruggedisation
  17. Specifications
  18. The Prototype
  19. Helpful resources

What did it used to only do?

It is a Wi-Fi hotspot to allow our phones and tablets to connect to. Tablets get access to all films on the hard drive for the kids to watch in the back. Phones control tablets and also the music on the hard drive - the Pi connects to the car stereo.

Pi Car Media Server Split
Pi Car Media Server Close

What does it also do now?

With the addition of a small USB hub and a USB 3G modem, the car server now provides Internet access for any devices connected to it in the car.

Pi Car Media Server With Modem
Pi Car Media Server With Modem

Initial setup

  1. Plug an SDHC card into desktop PC (or laptop).
  2. Download and install Raspbian to the card.
  3. Plug the SDHC card into the Raspberry Pi and power up. Set up everything required from the configuration screen (password, locale, sound etc.)
  4. If you've bought the CODECs for MPG2 and WVC1, then now is as good a time as any to set them up. Edit /boot/config.txt
    sudo nano /boot/config.txt
  5. Add the license keys that you received from the Raspberry Pi Store, something like this
    decode_MPG2=0x01234567
    decode_WVC1=0x89abcdef
  6. At some convenient time after a reboot, verify that they are enabled
    vcgencmd codec_enabled MPG2
    vcgencmd codec_enabled WVC1
  7. From the desktop PC, SSH into the Raspberry Pi and upgrade the system
    sudo apt-get update
    sudo apt-get upgrade

Configuring the Wi-Fi Hotspot

  1. Plug in the Wi-Fi adaptor
  2. Install host access point daemon
    sudo apt-get install hostapd
  3. Set up wlan0 for static IP
    sudo nano /etc/network/interfaces
  4. and add the following lines
    allow-hotplug wlan0
    auto wlan0
    iface wlan0 inet static
      address 192.168.100.1
      netmask 255.255.255.0
      network 192.168.100.0
      broadcast 192.168.100.255
  5. Assign the IP address to wlan0
    sudo ifconfig wlan0 192.168.100.1
  6. Stop ifplugd messing around with wlan0's IP address
    sudo nano /etc/default/ifplugd
  7. Change two of the lines file from
    INTERFACES="auto"
    HOTPLUG_INTERFACES="all"
    to
    INTERFACES="eth0"
    HOTPLUG_INTERFACES="eth0"
  8. Create a new access point configuration file with
    sudo nano /etc/hostapd/hostapd.conf
  9. Copy (and modify to suit) the following into the file
    interface=wlan0
    driver=nl80211
    ssid=Car_WiFi
    hw_mode=g
    ieee80211n=1
    channel=8
    ignore_broadcast_ssid=0
    own_ip_addr=192.168.100.1
    macaddr_acl=0
    auth_algs=1
    wpa=3
    wpa_passphrase=raspberry-pi
    wpa_key_mgmt=WPA-PSK
    wpa_pairwise=TKIP
    rsn_pairwise=CCMP
    Note that The Pi Hut's branded Wi-Fi adaptor is a RT5370 chip-set, which works out of the box with the nl80211 driver
  10. Point the daemon to the configuration file by editing /etc/default/hostapd
    sudo nano /etc/default/hostapd
  11. Changing the line
    #DAEMON_CONF=""
    to
    DAEMON_CONF="/etc/hostapd/hostapd.conf"
  12. Test the setup
    sudo /usr/sbin/hostapd /etc/hostapd/hostapd.conf
    and try to connect with a mobile device.
    It will get stuck "Obtaining IP address" since we've no DHCP server yet, but the terminal should show some useful output. [Ctrl] [c] to exit.
    You may need to re-apply the IP address of wlan0 at this point since hostapd removes it when it stops.
  13. Start the hostapd daemon
    sudo service hostapd start

Configuring DHCP / DNS

  1. Install the software
    sudo apt-get install dnsmasq
  2. Configure dnsmasq
    sudo nano /etc/dnsmasq.conf
    for example
    interface=wlan0
    domain=carwifi.net
    dhcp-range=192.168.100.200,192.168.100.250,2h
    read-ethers
  3. Create the file /etc/ethers
    sudo nano /etc/ethers
    and add the MACs of any known devices that you want to find by name on the network - tablets and phones for example.
  4. Edit the file /etc/hosts
    sudo nano /etc/hosts
    and add the IP addresses and names of the same phones and tablets.
  5. Restart the dnsmasq daemon
    sudo service dnsmasq restart
  6. Try to connect with a mobile device... it should just work!

Configuring a 3G / 4G USB Modem

This turns out to be far simpler that first thought. Even though the modem used is a multi-device type (i.e. "CD-ROM" as well as a modem), the community has it all covered. Simply installing a couple of packages from the repository was all that was required.

  1. Install "usb_modeswitch"
    sudo apt-get install usb_modeswitch
  2. Install Network Manager
    sudo apt-get install network-manager
  3. Plug in the modem and after 20 seconds or so, type
    ifconfig
    and you should see a new network device.

Of course, as it turns out, not all USB modems are made equal - at least old and new ones. It's been pointed out to me that newer modems may not just work "out of the box" as mine did, so you may have to compile the USB Mode Switch from source, with the updated data - as the Raspbian package may not be new enough.

Configuring IP Forwarding & Network Address Translation (NAT)

If you plan on adding a 3G or 4G modem, then this is definitely required. If not, then it's still handy to set up IP forwarding for those times that the server is out of the car and connected to the house network.

  1. Enable IPv4 forwarding by editing /etc/sysctl.conf
    sudo nano /etc/sysctl.conf
  2. Change the line
    #net.ipv4.ip_forward=1
    to
    net.ipv4.ip_forward=1
  3. That won't come into effect until a reboot, so set it manually now
    sudo sysctl net.ipv4.ip_forward=1
  4. Set up the IP Tables for NAT'ing (if required) behind a 3G / 4G modem
    sudo iptables -t nat -A POSTROUTING -o usb0 -j MASQUERADE
    sudo iptables -A FORWARD -i usb0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT
    sudo iptables -A FORWARD -i wlan0 -o usb0 -j ACCEPT
    If you don't have a 3G / 4G modem, replace usb0 with eth0 above and below.
  5. Save the IP Tables so that we can use them after a reboot
    sudo sh -c "iptables-save > /etc/iptables.ipv4.wlan0.usb0.nat"
  6. Edit /etc/network/interfaces to load this file
    sudo nano /etc/network/interfaces
  7. Add this to the very end of the file
    up iptables-restore < /etc/iptables.ipv4.wlan0.usb0.nat
  8. Reboot the Pi to check that it all still works. You should now be able to connect a wireless device and surf the web (assuming that you have the 3G / 4G modem plugged in or the Ethernet port connected to an active Internet connection).
    sudo reboot

Mounting the hard drive

  1. Plug in a USB hard drive (mine has a single FAT32 partition).
  2. Locate the hard drive
    ls -l /dev/disk/by-uuid/
    The drive will [most likely] be on /dev/sda1:
    ABCD-1234 -> ../../sda1
  3. Create a mount point for the drive
    sudo mkdir /home/media
  4. Mount the drive (for a test)
    sudo mount -t vfat -o uid=pi,gid=pi /dev/sda1 /home/media/
  5. Now make it permanent by editing /etc/fstab
    sudo nano /etc/fstab
  6. Add this line to the end of the file
    UUID=ABCD-1234 /home/media vfat rw,nosuid,noexec,noatime,uid=1000,gid=1000 0 0

Configuring default sound output

You may have done this during the first boot of Raspbian, but if you didn't then now is the time to direct the sound for MPD to the stereo jack.

  1. Load the sound module (note that this is temporary)
    sudo modprobe snd_bcm2835
  2. Install ALSA utilities
    sudo apt-get install alsa-utils
  3. Tell ALSA to use the stereo jack
    sudo amixer cset numid=3 <n>
    where n is 0=auto, 1=headphones, 2=hdmi
  4. Add snd_bcm2835 to /etc/modules (to make this permanent)
    sudo sh -c "echo bcm2835 >> /etc/modules"

Configuring Music Player Daemon (MPD)

  1. Install MPD
    sudo apt-get install mpd
  2. Configure /etc/mpd.conf to suit network / music setup (the default ALSA output should be good as it is)
    sudo nano /etc/mpd.conf
  3. Restart MPD
    sudo service mpd restart
  4. Make sure everything works with your favourite Music Player Client
  5. Reboot (to use the /etc/modules) and make sure everything still works!
    sudo reboot

Configuring MiniDLNA

Just in case you don't actually want to run Kodi to render video, this will serve any DLNA / UPnP renderer.

  1. Install minidlna
    sudo apt-get install minidlna
  2. Configure /etc/minidlna.conf to suit network / music setup
    sudo nano /etc/minidlna.conf
  3. Restart MiniDLNA
    sudo service minidlna restart

Configuring nfs-kernel-server

It appears that Kodi / Kore (Kodi Remote control) won't let you select videos for playing that are not on a local file system (e.g. served from a UPnP server), so let's create NFS mount points for the media.

  1. Install nfs-kernel-server
    sudo apt-get install nfs-kernel-server
    Note that it it fails, try removing nfs-common and rpcbind and then install all 3 at the same time.
  2. Add an export point to /etc/exports
    sudo nano /etc/exports
  3. Add the following line to make /home/media read only to the whole sub-net serviced by the Pi
    /home/media 192.168.100.0/24(ro,insecure,no_subtree_check,async)
  4. Restart the nfs-kernel-server
    sudo service nfs-kernel-server restart

    The likely hood is that it will complain about portmapper not being present. Simply starting rpcbind (a portmapper replacement) with

    sudo service rpcbind start

    Enable it on boot with

    sudo update-rc.d rpcbind enable

Grrr...

This should all have been nice and straight forward, but I went though hours of rebooting the system and having either hostapd fail to come up when rpcbind was running (due to the IP address of wlan0 being removed) resulting in mpd failing to bind to the address, or nfs-kernel server failing because rpcbind wasn't running.

I spend ages trying several different things like disabling / enabling / re-ordering of rpcbind, nfs-kernel-server and mpd. I may have got lost with what I have, but I think everything is back to default (at this point I don't want to start again just to test my theory - perhaps if my SD card fails and needs to be re-built).

The key points to make this reboot successfully every time (bearing in mind that this will be booted up every time I start my car), were the following:

In /etc/network/interfaces

allow-hotplug wlan0

In /etc/default/ifplugd

INTERFACES="eth0"
HOTPLUG_INTERFACES="eth0"

In /etc/hostapd/hostapd.conf

own_ip_addr=192.168.100.1

Configuring the Power Circuitry

Since this is a Linux system, it really doesn't like to get it's power forcibly removed from it (as no modern operating systems do).

So, I got hold of a wee power supply designed for car use. It powers up when the ignition switch in in the "accessory" position and signals the Pi to power down after it has been removed. Only when the Pi has fully shut down will the power be removed.

It is set up as described on Mausberry Circuit's setup page.

Automatic Random Tracks

Of course, the trouble with having a music system that relies on a mobile phone or tablet to get something playing soon becomes apparent when there is nobody but the driver in the car... how do you play music if the radio is not acceptable?

What you do is write yourself a wee script that checks whether or not there is a track playing, and adds a random track if there isn't!

  1. Create a magic script:
    nano ~/random_mpd_tracks.sh
  2. Enter (and modify to suit) the following code:
    #!/bin/bash

    # Define the path and any password options for MPD
    MY_MPC="/usr/bin/mpc -h 192.168.100.1"

    while [ 1 ]; do
     # Check if MPD is up and running
     $MY_MPC status
     if [ $? -eq 0 ]; then
      # We're alive, so let's play
      # First let's see how many tracks are in the playlist
      number_of_tracks=`$MY_MPC playlist | wc -l`
      # If we have less than 2 tracks playing, add random(s)
      # Why 2? Well it ensures no break in the music
      if [ $number_of_tracks -lt 2 ]; then
       # How many tracks needed?
       number_to_add=`expr 2 - $number_of_tracks`
       # Pick random track(s)
       random_tracks=`$MY_MPC listall | shuf -n $number_to_add`
       # Add it to the play list
       $MY_MPC add $random_tracks
      fi
      # Press "play" :-)
      $MY_MPC play
     fi
     sleep 10
    done
  3. Launch the command at boot by adding it to /etc/rc.local:
    sudo nano /etc/rc.local
  4. Add the following lines at the end of the file (before "exit 0"):
    # Launch the MPD Random Track Generator
    /home/pi/random_mpd_tracks.sh

Note: there may be a slightly later version of this script on my useful scripts page.

Server Ruggedisation

Pi Car Media Server Ruggedisation

Obviously, a car is not the most benign environment for a computer (especially if it has "spinning rust" aka a hard drive). So, just how do you make it fit for purpose? There are no doubt numerous options out there, some more expensive than others... but this works just fine and has survived many, many speed-bumps!

Specifications:

Case: A black box from Maplin
Power Supply: 3A Car Supply / Switch from Mausberry Circuits
Board: Rapsberry Pi B-model (version 2.0)
SoC: Broadcom BCM2835
CPU: ARM1176JZF-S (ARM11, ARMv6Z arch) @ 700 MHz
GPU: Broadcom VideoCore IV@ 250 MHz
RAM: 512MiBytes SDRAM
USB Hub: Belkin F4U040 ultra slim 4 port
Storage: Sandisk Ultra, class 10 (8GByte micro SDHC)
120GB hard drive inside a generic PATA to USB case.
Wi-Fi: 802.11n adaptor (no it's not called a dongle) branded for The Pi Hut
3G USB Modem: Huawei E3533
Speakers: Car audio system
Monitor: None
Keyboard / Mouse: None

The Prototype

Pi Car Media Server Prototype

Every idea has to start with a prototype and my car server was no exception!

Helpful resources:




Del.ico.us Digg Facebook Google LinkedIn LiveJournal NewsVine reddit StumbleUpon Twitter
Valid XHTML 1.0 Transitional Valid CSS! [Valid Atom 1.0] [Valid RSS 2.0]
[ Page last updated Sun 14th Jan 2018 | viewed 12951 times ]