Pi0 Media Server

Joeri Kassenaar
7 min readAug 14, 2021

--

Raspberry Pi Zero local media server setup with a Big Sur Mac
Saturday 14 Aug 2021

Install Pi OS

Download Raspberry Pi Imager
Unpack and move to Applications Utilities.

Open it and write a Raspberry OS to a micro SD in your USB slot.

This might take a while. SSD over USB is not very fast.
I seem to got Buster

Mount the SSD drive, easy way is to remove and again place the holder in the USB slot.

In finder you will see a disk called boot . This is the boot section of the Pi OS.

For the Pi to boot headless it needs to know with what wifi to connect and that it should be able to connect via ssh.

Open a terminal and add a one time allow ssh file:

touch /Volumes/boot/ssh

And add a wpa_supplicant file to tell what wifi network to connect to.

nano /Volumes/boot/wpa_supplicant.conf

Add your wifi settings.

country=NL
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
network={
ssid="YourSSID"
psk="Your-SSID-password"
key_mgmt=WPA-PSK
}

I live in the Netherlands, so I’m putting in country=NL.
Unmount the SSD. I’m using the eject button in finder.

Put the SD in the Pi and give it some juice.

I bought the wrong official Pi Power… Pi Zero has micro USB.

When the light stops blinking you can log into your pi.

Did our settings work?

$ ping raspberrypi.local

ping: cannot resolve raspberrypi.local: Unknown host
Too soon… wait until the light no longer blinks…

64 bytes from 192.168.2.186: icmp_seq=0 ttl=64 time=10.891 ms
64 bytes from 192.168.2.186: icmp_seq=1 ttl=64 time=18.197 ms
64 bytes from 192.168.2.186: icmp_seq=2 ttl=64 time=7.566 ms

Contact. ( Or not… then recheck your wpa_supplicant file )

$ ssh pi@raspberrypi.local

Default password is raspberry

And you’re in.

Enter sudo raspi-config in the remote terminal window to start setup.

First do an update of the config software:
Pick 8 Update from the list and press enter
( starts without confirm )

Change your password:
1 System Options , S3 Password, <ok>
Create password in terminal

Change your hostname:
1 System Options , S4 Hostname, <ok>
Create hostname in config gui, <ok>

Make SSH work after reboot:
3 Interface Options , P2 SSH, <Yes>

Make use of your large disk:
6 Advanced Options , A1 Expand Filesystem, <Yes>

I also made it not boot into GUI, and set GL to default.

Reboot

You now need to ssh with the new hostname.

$ ssh pi@your-hostname.local

And use your new password to log in.

Update to the latest software.

$ sudo apt-get update
$ sudo apt-get upgrade
$ sudo apt autoremove

Get a coffee, but stay with it, random confirmations ahead…

MiniDLNA

Next, we need to install the media server software. Minidlna is a simple, lightweight option that serves our purpose well, which can be installed as:

sudo apt-get install minidlna

The media files will be placed in the directory /home/pi/Music etc.
Let’s check if they exist

ls /home/pi/

Bookshelf Desktop Documents Downloads Music Pictures Public Videos

Okay… looks fine…

Next we need to configure minidlna by editing the minidlna.conf file which can be found in the /etc directory.

sudo nano /etc/minidlna.conf

We need to make sure to change the following parameters in this file:

#media_dir=/media/pimedia_dir=A,/home/pi/Music
media_dir=P,/home/pi/Pictures
media_dir=V,/home/pi/Videos
friendly_name=Excellent Pi
inotify=yes

My server will be Excellent Pi , but you can name yours what you want.

Now we have installed MiniDLNA, we need to start it up and make sure it rescans all media files. That is most reliably be done by issuing the following set of commands:

sudo minidlnad -R
sudo service minidlna restart

You can check http://your-hostname.local:8200/ in a browser to see if MiniDLNA is running. As we haven’t put any files in the folders the listing of files should be 0.

Add media via wifi

Let’s copy a music file from desktop to the pi over wifi.
We can use scp for that.

scp <source> <destination>

I’ve got Blade Runner 2049 — Jóhann Jóhannsson Theme.mp3 on my desktop. And I’m going to copy from Mac to Pi so I’ll need a new terminal for that.
So a copy would be:

scp ~/Desktop/Blade\ Runner\ 2049\ -\ Jóhann\ Jóhannsson\ Theme.mp3 pi@your-hostname.local:~/Music/`

Enter the pi password.

Blade Runner 2049 — Jóhann Jóhannsson Theme.mp3 … 100% 2707KB 1.8MB/s 00:01

Done.
Reloading in the web browser will now show 1 audio file.

VLC on my phone is showing the DNLA server and the music file. Both the Pi and the Phone are on the same wifi network for this to work.

Persistent

For this to work at startup we need to create a startup script and run the startup script at boot.

$ nano ~/startup.sh

This is what our script will look like:

#!/bin/bash
# Author: Your Name & Hans Weda
# Date: 14 Aug 2021
# Purpose: Basic startup script for Portable Media Server
# Description: Force minidlna to rescan for media files
# And then restart the service
# https://medium.com/swlh/raspberry-car-pi-b5eea4d72ae1
echo "Running startup.sh"# first for no real reason
sleep 3
# Force a rescan
sudo minidlnad -R
# restart the service
sudo service minidlna restart

Make it executable

sudo chmod 755 ~/startup.sh

The startup script should start at boot.

We can start it as a service.
Raspberry Pi OS executes all scripts in /lib/systemd/system .
So we can create our own service to start the startup.sh

$ sudo nano /lib/systemd/system/dlnastart.service

To make sure it starts when all is done we add type=idle in the [service]

[Unit]
Description=Media Service
[Service]
Type=idle
ExecStart=/home/pi/startup.sh
[Install]
WantedBy=multi-user.target

We need to make sure the access and execution rights are properly set by the command:

sudo chmod 644 /lib/systemd/system/dlnastart.service

Now we have created the service, we need to actually add and enable the new service. This is done by the following set of commands:

sudo systemctl daemon-reload
sudo systemctl enable dlnastart.service

Check the status of this service by issuing systemctl status dlnastart

● dlnastart.service — Media Service
Loaded: loaded (/lib/systemd/system/dlnastart.service; enabled; vendor preset: enabled)
Active: inactive (dead)

We could do a check by rebooting the Pi

$ sudo shutdown -r now

If you’re not taking this device on a trip, but snug it next to your router… Then you’re done. Maybe add a webserver to upload some files. Add some files via ssh. Or just take out the SSD and load some more content via sshfs.

But if you do take it with you on a trip, then we need our own wifi broadcasting channel.

Stand Alone Server

making the Zero an access point.

After the next changes your Pi will no longer be on the network your mac is on, so do them right, or be ready to hook on a keyboard and monitor.

First we need to install hostapd and make sure it starts when the Raspberry Pi is booted. The package hostapd lets us create a wireless hotspot using a Raspberry Pi. This is done by:

sudo apt-get install hostapd
sudo systemctl unmask hostapd
sudo systemctl enable hostapd

Next, we need to install dnsmasq, which is an easy-to-use DHCP and DNS server. This is needed to provide network services to the devices that connect to the Raspberry Pi.

sudo apt-get install dnsmasq

Now we need to configure the DHCP settings

$ sudo nano /etc/dhcpcd.conf

At the very end of this file you need to add the following lines:

interface wlan0
static ip_address=192.168.4.1/24
nohook wpa_supplicant

We also need to adapt the DNS settings. The existing configuration file is quite elaborate. It is recommended to change the name of the original file and create a new dnsmasq.conf file. First, let’s move the original file:

sudo mv /etc/dnsmasq.conf /etc/dnsmasq.conf.orig

Then, create a new file by sudo nano /etc/dnsmasq.conf with the following content:

# Listening interface
interface=wlan0
# Pool of IP addresses served via DHCP
dhcp-range=192.168.4.2,192.168.4.20,255.255.255.0,24h
# Local wireless DNS domain
domain=wlan
# Alias for this router
address=/your-hostname.local/192.168.4.1

Finally, we need to configure the hostapd settings. This is done by creating a new file sudo nano /etc/hostapd/hostapd.conf; and adding the following lines to this file:

country_code=NL
interface=wlan0
ssid=YourWifiServerName
hw_mode=g
channel=7
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_passphrase=YourWifiServerPassword
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP

Reboot the system sudo systemctl reboot, or reboot the Raspberry Pi sudo reboot. The Raspberry Pi Zero W is now a stand-alone access point, and it is no longer available on the network your Mac is on.
To connect to the Pi, you need to switch your device to the WiFi-network with name YourWifiServerName, and with wifi-password: YourWifiServerPassword.

The Phone will complain that there is no internet… Who needs it?

Mounting SSD on Mac

I didn’t want to copy all my assets over wifi to the pi SSD. But turns out it’s not so easy to mount the user folders on a mac. I figured it out, but it’s probably easier to copy it all over wifi with ssh to the pi.
If you do want to be able to mount your Pi SSD on your mac you can take a look here: https://bespired.medium.com/mount-a-pi-ssd-on-your-big-sur-mac-d0ada9939fa4

--

--

Joeri Kassenaar
Joeri Kassenaar

No responses yet