Build a Car DLNA server with a Raspberry Pi and MiniDLNA

This project will take a raspberry pi and turn it into a media server for those long car trips…

Requires:

  • A Raspberry Pi, with built in WiFi or a WiFi adaptor
  • A Cigarette lighter power adapter for the car (preferably one capable of feeding the Pi 2+ Amps at 5V
  • The following OS software packages:
    • hostap
    • minidlna
    • bind9 (dns server)
    • isc-dhcp-server (dhcp server)

Step 0: Download and setup Raspbian

download a copy of raspbian from https://www.raspberrypi.org/downloads/raspbian/

write this to your SD card using:

sudo dd bs=1m if=/Users/{username}/Downloads/2019-04-08-raspbian-stretch.img of=/dev/{yourSDCardDiskNumber}

Step 1: Add and setup your storage:

Plug your USB device into your raspberry pi, and type the following at a command line:

ls -l /dev/disk/by-uuid/

You should see something resembling ‘/sda#’ for example.:

..
lrwxrwxrwx 1 root root 10 Jan  1  1970 9BC5-7DDC -> ../../sda1
..

If you are sure that this is the correct device, format your USB device (replace “/dev/sda” with the name of your USB device if it differs)

mkfs.fat -F 32 -I /dev/sda

Note: I’ve found the lowest maintenance solution is to format the USB device as fat32. This format is out-of-the-box accessible across MacOS, Windows and Linux OSes, allowing greater flexibility in mounting the device R/W on any OS… Happily, it also gets rid of common permission problems associated with other file systems.

Note down the value of the UUID (in this case, ‘9BC5-7DDC’). We’ll need this value later, when setting the USB device up to mount automatically at boot time.

Step2: Setup your mount point:

Create a location for mount point:

sudo mkdir -p /media/usb

Give proper ownership and permissions (later when we install the minidlna service, it will need to run under the same account in order to access the files stored there). We’re going to use the default “pi” user, to keep things simple:

sudo chown pi /media/usb
sudo chmod 770 /media/usb

Step3: Auto-mount your storage at boot time

Mount the USB Drive to make it accessible at /media/usb (you will want it to be mounted under the same user ID that is running minidlna)

sudo mount -t vfat -o uid=pi,gid=pi,umask=007 /media/usb UUID=9BC5-7DDC 0 0

Take a backup of current fstab and then edit the file

sudo cp /etc/fstab /etc/fstab.backup
sudo nano /etc/fstab

Create a new line and add the mount information at the bottom of the fstab file (this is my example: replace the UUID and mount-point with your own):

UUID=9BC5-7DDC /media/usb vfat uid=pi,gid=pi,umask=007 0 0

Save the file using [Ctrl]+[x] keys, and pressing [Y] at the prompt

Step 4: Install the services we need

sudo apt-get install hostap minidlna isc-dhcp-server

Step 5: Configure network and hostap:

sudo nano /etc/network/interfaces

Replace the entries under ‘wlan0’ with the following:

auto wlan0
allow-hotplug wlan0
iface wlan0 inet manual
address 192.168.90.1
netmask 255.255.255.0
hostapd /etc/hostapd/hostapd.conf
pre-up ifconfig wlan0 hw ether 00:c1:41:32:0a:60

auto wlan0_2
allow-hotplug wlan0_2
iface wlan0_2 inet static
address 192.168.200.1
netmask 255.255.255.0

Save the file using [Ctrl]+[x] keys, and pressing [Y] at the prompt

Add the following entries to make your wireless adapter act as a wireless hotspot to other clients

sudo nano /etc/hostapd/hostapd.conf

Add the following entries to make your wireless adapter act as a wireless hotspot to other clients:

interface=wlan0
bssid=00:c1:41:32:0a:60
bss=wlan0_2
ssid=YOUR_SSID_NAME_HERE
channel=2
beacon_int=100
wmm_enabled=0
auth_algs=3
wpa=2
wpa_passphrase=YOUR_PRE-SHARED-KEY_HERE
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP
wpa_group_rekey=600
wpa_gmk_rekey=86400
wpa_ptk_rekey=600
wps_state=0

Save the file using [Ctrl]+[x] keys, and pressing [Y] at the prompt

Step 6: Configure isc-dhcp-server

sudo nano /etc/dhcp/dhcpd.conf

Add the following (again, using google DNS for simplicity’s sake):

#for the wireless network on wlan0_2 - carproxy networksubnet 192.168.200.0 netmask 255.255.255.0 {
interface wlan0_2;
option routers 192.168.200.1;
option subnet-mask 255.255.255.0;
option domain-name-servers 8.8.8.8, 8.8.4.4;
range 192.168.200.51 192.168.200.199;
}

Save the file using [Ctrl]+[x] keys, and pressing [Y] at the prompt

Step 7: Configure MiniDLNA:

sudo nano /etc/minidlna.conf

Setup your DLNA service to access your media directories – you’ll need to add / replace several lines in the conf file:

media_dir=V,/mnt/usb/videos
media_dir=A,/mnt/usb/music
media_dir=P,/mnt/usb/picture
#bind it to the correct wireless interface
network_interface=wlan0_2
#tell it what IPv4 address to listen on:
listening_ip=192.168.200.1
#give it a name:
friendly_name=car_media
#Automatic discovery of new files in the media_dir directory.
inotify=yes

And you’ll need to make sure that it’s running under a user with access to these directories:

sudo nano /etc/default/minidlna

The ujser and group the daemon should run under is:

USER="pi"
GROUP="pi"

Save the file using [Ctrl]+[x] keys, and pressing [Y] at the prompt

Finally, tell the raspberry Pi to re-index the media when it boots. Create a file for your startup script and write your script in the file:

sudo nano /etc/init.d/dlna-re-index

Here’s my script:

!/bin/bash
ssh -t server.on.lan 'sudo minidlna -R && sudo service minidlna restart'

Save the file using [Ctrl]+[x] keys, and pressing [Y] at the prompt

Make the script executable:

sudo chmod 755 /etc/init.d/dlna-re-index

Register script to be run at startup:

sudo update-rc.d dlna-re-index defaults

Leave a Reply