This process will walk through the steps take to setup a Raspberry Pi Model B+ as a media server that is capable of providing media in your car. The main requirement is to be able to support multiple steaming videos simultaneously over a wireless network. As an added bonus, The Raspberry Pi can also provide internet access if an Ethernet cable is plugged in and allow direct HDMI connectivity to an TV.
The Build Configuration
First, we will start out with the hardware. This setup will use the following:
- Raspberry Pi B+ Board
- Raspberry Pi B+ Case
- A 16GB Micro SD Card
- Trendnet 802.11 TEW-648UBM Wireless Adaptor
- A USB Flash Storage Drive
The board is pretty straightforward, but the case was a splurge at ~$12 for the look. Being mobile, it was going to be seen so a standard piece of plastic was undesirable. The case itself actually went together pretty well.
For the storage, the Micro SD card itself will be used to load the XBMC application onto the board. The USB Flash drive will support the bulk media. Any drive could be used here, but since it is mobile a flash based storage was preferred.
The wireless adaptor was chosen because of the use of the RTL8188CUS chipset that will allow the adaptor to act as an access point.
The pictures are below are the unboxing and build of the system.
This last image is a comparison between the new setup and my test setup. The standard black case is the device that was used to test the mobile functionality. As it worked quite well, it will be put back to its original use connected to the home TV.
Loading XBMC
And now for the fun configuration parts. The easiest way for XBMC to be loaded on the MicroSD card is to follow the directions from the Raspbmc Wiki. Here are the Linux/OSX instructions and Here are the Windows instructions.
Using the Linux instructions, this first image downloads the script and then enables the script to become executable.
The second screenshot from the installation is selecting the proper drive to install to. You will notice that the primary hard drive in the system, /dev/sda, is listed too. Be mindful that you select the proper drive.
The last question that is asked in the screenshot is around the advanced settings. Select “N” here as we will be configuring the advanced settings after the system completes the installation. The standard settings will configure DHCP for the Ethernet/Wired interface.
With our MicroSD card primed with the 18MB Installation files, we can now plug the it back into the RasberryPi device. I did not have a HDMI device plugged in, but if you did you would see the progress. After about 20-30 minutes, depending on your internet speeds, you will have a functional RaspBMC system up and running.
As mentioned, I did not have a HDMI device plugged in. Upon first access via the GUI, you would normally have to go through a process and select locals, time zones, etc. I used SSH to access my system on the first login and will share the screen shots from the selections made. Be forewarned however, the shell in the screen shots did glitch.
Using your favorite terminal emulator, login with the credentials below. At first boot, be mindful that you must be in the same subnet as your RaspBMC device.
username: pi password: raspberry
First, we will select the appropriate locals to generate. For me, I chose the en US.UTF-8 UTF-8 local.
Next, I will select the default local. This is more useful if multiple locals are selected. As we selected only one, we will use it.
Next, we will select the geographic area that we will be using. I chose US.
Finally at the prompt, we can now continue configuring our platform.
With access to the CLI, lets clean up some generic configuration to make accessing things easier. One of the first things
- I prefer to fix the DNS resolution.
- We can update the system using the commands below.
sudo apt-get update sudo apt-get upgrade
- With the system updated, at the time of this writing there are unnecessary packages loaded from the installation. We can remove them with the command below.
sudo apt-get autoremove
Adding Packages to Setup the Wireless Access Point
Within this section, we will detail the steps need to setup a RaspBMC device to act as an Access Point. This will include setting up our WiFi adaptor to broadcast an SSID and allowing connections to use a connected Ethernet as an network source. Think of this as your Linksys Cisco router from your home.
Using your favorite terminal emulator, issue the commands below. hostapd is the application that will run our access point while isc-dhcp-server will provide addressing to wireless clients. We already have iptables loaded from the base install. iptables will provide the connectivity from the Wireless connection to an Ethernet connection if connected.
sudo apt-get install isc-dhcp-server hostapd
DHCP Configuration
You will notice the notification at the end of the installation that isc-dhcp-server failed to start. We can now add some configuration to correct. We will use our favorite editor to edit the /etc/default/isc-dhcp-server file. We want to modify the following lines adding our configuration file location and defining the listening interfaces as “wlan0”
DHCPD_CONF=/etc/dhcp/dhcpd.conf INTERFACES="wlan0"
Next, we can setup the DHCP range. I will be using 192.168.6.0/24 for my wireless network.to setup the DHCP range, we will edit the /etc/dhcp/isc-dhcp-server.conf file. First we will backup the original file by issuing:
sudo mv /etc/dhcp/dhcpd.conf /etc/dhcp/dhcpd.conf.old
Next, we will create a new file using our favorite editor
sudo vi /etc/dhcp/dhcpd.conf
Below is my sample allowing the IP address range of 192.168.6.20-49 to be provided with a lease time of 86400 seconds (1 day).
# blog.destephen.com dhcp configuration file (/etc/dhcp/dhcpd.conf) ddns-update-style none; #DDNS disabled default-lease-time 84600; #IP lease time valid for a day max-lease-time 84600; #IP lease time valid for a day subnet 192.168.6.0 netmask 255.255.255.0 { #AP Subnet defintion range 192.168.6.20 192.168.6.49 ; #Range of IP addresses available for clients option domain-name-servers 4.2.2.2 ; #your DNS IP (in my case, my router is workign as DNS) option domain-name "mobile.local"; #optional domain name option routers 192.168.6.1 ; #your client's gateway / router IP }
Assigning Static IP for WLAN0
My Raspberry Pi’s wireless address will be 192.168.6.1. To configuring this, we will need to edit the /etc/network/interfaces file to complete this task.
We do want to take note that within the following IP address configuration, we do not want to modify the eth0 interface. The RaspBMC application handles the manipulation of that interfaces and if we enable it within /etc/network/interfaces, we will likely experience issues.
Now we can edit the interfaces file with your favorite editor. Below is the example file with comments.
# The primary wireless interface configured with:
# static ip address
# network mask
auto wlan0
allow-hotplug wlan0
iface wlan0 inet static
address 192.168.6.1
netmask 255.255.255.0
# This is for the iptables updates
up iptables-restore < /etc/iptables.ipv4.nat
Configuring the Wireless with hostapd
With the IP addressing and DHCP provisioned, lets setup wlan0 as an access point. We will want to create the configuration file from scratch since the hostapd does not come with an example. Below is the example configuration file that was used for my system. If you choose a different wireless adaptor, be sure to find out the chipset used and apply appropriately. This file will be located at /etc/hostapd/hostapd.conf.
interface=wlan0
driver=rtl871xdrv
ssid=Mobile-Wireless
hw_mode=g
channel=1
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_passphrase=My_Passphrase
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP
With the configuration file created, we need to inform the hostapd deamon of its location. To do so, update /etc/default/hostapd:
From:
#DAEMON_CONF=""
To:
DAEMON_CONF="/etc/hostapd/hostapd.conf"
Configuring IP Tables for Routing and Forwarding
Now we need to work with iptables to setup the NAT and firewalling rules. While this is not required for the car functionality, it is helpful in situations when internet is desired while using a mobile device as a remote control while not in the car.
The command below enables IP forwarding in the kernel.
sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"
Next we need to enable NAT on startup. To do so, we will uncomment the line below from the /etc/sysctl.conf file.
net.ipv4.ip_forward=1
Next we will edit the iptables rules on the system to allow the NAT and forwarding. The three commands are below. The first command enables NAT using the masquerade function. This can be used when DHCP is used on one of the interfaces. The second command handles passing of established session states through the iptables. The third rule allows new connections into the wlan0 interface.
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE sudo iptables -A FORWARD -i eth0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT sudo iptables -A FORWARD -i wlan0 -o eth0 -j ACCEPT
Next, we will make the iptables updates permanent by issuing the commands below.
sudo sh -c "iptables-save > /etc/iptables.ipv4.nat" sudo update-rc.d hostapd enable sudo update-rc.d isc-dhcp-server enable
At this point, we can test the configurations by starting the DHCP and Access Point functionality. More than likely, the hostapd will fail due to needing a hostapd file specific to the Realtek chipsets.
sudo ifup wlan0 sudo service isc-dhcp-server start sudo service hostapd start
Fixing hostapd
If your hostapd displays an error and does not start properly, we will need to update the hostapd to support the Realtek adaptor.
We can download the file using wget from the CLI. We will also need to download the public certifcates to allow verified HTTPS access.
cd /home/pi
sudo apt-get -y install ca-certificates
wget https://blog.destephen.com/wordpress/wp-content/uploads/2014/12/hostapd.tar.gz
Next, we will move the file into the proper directory, overwriting the existing file.
tar -zxvf hostapd.tar.gz
sudo mv hostapd /usr/sbin
We will then follow up with a chmod to apply the proper permissions.
chmod 755 /usr/sbin/hostapd
Once completed, we could reboot to reload and test all the appropriate functions. Upon reboot, you should be able to access the wireless as well as your XBMC software.
sudo reboot
And we are up!
Once we are up and running on the new system, we will want to connect a TV and setup some XBMC centric items. Most importantly, this includes going through the Settings -> Services menus to enable the following functionality as desired:
- UPNP Access and Control
- Remote Control settings
- Apple Airplay
Last but not least is adding media into your library. Fortunately, the Raspberry Pi Debian based system can handle the NTFS partition without any issues. However, if you happen to experience slow performance, it would be recommended to find an alternative drive format.
Some helpful posts that were found along the way:
http://elinux.org/RPI-Wireless-Hotspot
***Note: udhcp did not work well for me, which is why I substituted it. It would not provide an IP over the wireless network