Raspberry Pi Car Media Server [gav-pi2]
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:
- What did it used to only do?
- What does it also do now?
- Initial setup
- Configuring the Wi-Fi Hotspot
- Configuring DHCP / DNS
- Configuring a 3G / 4G USB Modem
- Configuring IP Forwarding & Network Address Translation (NAT)
- Mounting the hard drive
- Configuring default sound output
- Configuring Music Player Daemon (MPD)
- Configuring MiniDLNA
- Configuring nfs-kernel-server
- Grrr...
- Configuring the Power Circuitry
- Automatic Random Tracks
- Server Ruggedisation
- Specifications
- The Prototype
- 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.
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.
Initial setup
- Plug an SDHC card into desktop PC (or laptop).
- Download and install Raspbian to the card.
- Plug the SDHC card into the Raspberry Pi and power up. Set up everything required from the configuration screen (password, locale, sound etc.)
-
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
-
Add the license keys that you received from the Raspberry Pi Store,
something like this
decode_MPG2=0x01234567
decode_WVC1=0x89abcdef -
At some convenient time after a reboot, verify that they are enabled
vcgencmd codec_enabled MPG2
vcgencmd codec_enabled WVC1 -
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
- Plug in the Wi-Fi adaptor
-
Install host access point daemon
sudo apt-get install hostapd
-
Set up wlan0 for static IP
sudo nano /etc/network/interfaces
-
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 -
Assign the IP address to wlan0
sudo ifconfig wlan0 192.168.100.1
-
Stop ifplugd messing around with wlan0's IP address
sudo nano /etc/default/ifplugd
-
Change two of the lines file from
INTERFACES="auto"to
HOTPLUG_INTERFACES="all"INTERFACES="eth0"
HOTPLUG_INTERFACES="eth0" -
Create a new access point configuration file with
sudo nano /etc/hostapd/hostapd.conf
-
Copy (and modify to suit) the following into the file
interface=wlan0Note that The Pi Hut's branded Wi-Fi adaptor is a RT5370 chip-set, which works out of the box with the nl80211 driver
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 -
Point the daemon to the configuration file by editing /etc/default/hostapd
sudo nano /etc/default/hostapd
-
Changing the line
#DAEMON_CONF=""toDAEMON_CONF="/etc/hostapd/hostapd.conf"
-
Test the setup
sudo /usr/sbin/hostapd /etc/hostapd/hostapd.confand 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. -
Start the hostapd daemon
sudo service hostapd start
Configuring DHCP / DNS
-
Install the software
sudo apt-get install dnsmasq
-
Configure dnsmasq
sudo nano /etc/dnsmasq.conffor exampleinterface=wlan0
domain=carwifi.net
dhcp-range=192.168.100.200,192.168.100.250,2h
read-ethers -
Create the file /etc/ethers
sudo nano /etc/ethersand add the MACs of any known devices that you want to find by name on the network - tablets and phones for example.
-
Edit the file /etc/hosts
sudo nano /etc/hostsand add the IP addresses and names of the same phones and tablets.
-
Restart the dnsmasq daemon
sudo service dnsmasq restart
- 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.
-
Install "usb_modeswitch"
sudo apt-get install usb_modeswitch
-
Install Network Manager
sudo apt-get install network-manager
-
Plug in the modem and after 20 seconds or so, type
ifconfigand 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.
-
Enable IPv4 forwarding by editing /etc/sysctl.conf
sudo nano /etc/sysctl.conf
-
Change the line
#net.ipv4.ip_forward=1tonet.ipv4.ip_forward=1
-
That won't come into effect until a reboot, so set it manually now
sudo sysctl net.ipv4.ip_forward=1
-
Set up the IP Tables for NAT'ing (if required) behind a 3G / 4G modem
sudo iptables -t nat -A POSTROUTING -o usb0 -j MASQUERADEIf you don't have a 3G / 4G modem, replace usb0 with eth0 above and below.
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 -
Save the IP Tables so that we can use them after a reboot
sudo sh -c "iptables-save > /etc/iptables.ipv4.wlan0.usb0.nat"
-
Edit /etc/network/interfaces to load this file
sudo nano /etc/network/interfaces
-
Add this to the very end of the file
up iptables-restore < /etc/iptables.ipv4.wlan0.usb0.nat
-
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
- Plug in a USB hard drive (mine has a single FAT32 partition).
-
Locate the hard drive
ls -l /dev/disk/by-uuid/The drive will [most likely] be on /dev/sda1:ABCD-1234 -> ../../sda1
-
Create a mount point for the drive
sudo mkdir /home/media
-
Mount the drive (for a test)
sudo mount -t vfat -o uid=pi,gid=pi /dev/sda1 /home/media/
-
Now make it permanent by editing /etc/fstab
sudo nano /etc/fstab
-
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.
-
Load the sound module (note that this is temporary)
sudo modprobe snd_bcm2835
-
Install ALSA utilities
sudo apt-get install alsa-utils
-
Tell ALSA to use the stereo jack
sudo amixer cset numid=3 <n>where n is 0=auto, 1=headphones, 2=hdmi
-
Add snd_bcm2835 to /etc/modules (to make this permanent)
sudo sh -c "echo bcm2835 >> /etc/modules"
Configuring Music Player Daemon (MPD)
-
Install MPD
sudo apt-get install mpd
-
Configure /etc/mpd.conf to suit network / music setup (the default ALSA
output should be good as it is)
sudo nano /etc/mpd.conf
-
Restart MPD
sudo service mpd restart
- Make sure everything works with your favourite Music Player Client
-
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.
-
Install minidlna
sudo apt-get install minidlna
-
Configure /etc/minidlna.conf to suit network / music setup
sudo nano /etc/minidlna.conf
-
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.
-
Install nfs-kernel-server
sudo apt-get install nfs-kernel-serverNote that it it fails, try removing nfs-common and rpcbind and then install all 3 at the same time.
-
Add an export point to /etc/exports
sudo nano /etc/exports
-
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)
-
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 startEnable 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
In /etc/default/ifplugd
HOTPLUG_INTERFACES="eth0"
In /etc/hostapd/hostapd.conf
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!
-
Create a magic script:
nano ~/random_mpd_tracks.sh
-
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 -
Launch the command at boot by adding it to /etc/rc.local:
sudo nano /etc/rc.local
-
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
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
|
||||
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
Every idea has to start with a prototype and my car server was no exception!
Helpful resources:
- RaspBMC OS X / Linux installation
- Installing nfs-kernel-server (Raspbmc forum on Stm Labs)
-
Raspberry Pi as an Ad Blocking Access Point
(note: change http to https in "ad_list_url" or it won't work) - Setting up a Raspberry Pi as a WiFi access point
- Why is my Audio (Sound) Output not working?
Computing Power
- Acer Aspire R3700
- Acknowledgements
- BOINC
- Desktop PC
- Eee PC 4G (701)
- Eee PC 901
- Gigabit Network
- Inspiron 14 5485 Laptop
- Kids PC 1
- Kids PC 2
- Media PC
- Mini-ITX PC
- My Useful Scripts
- Nano ITX PC
- Nook Simple Touch
- Processing Power
- Raspberry Pi
- Sharp Zaurus SL-C3200
- Storage Capacity
- The Server
- What Is Firmware