Raspberry Pi-Fi Server

June 17, 2012

I’ve had my Raspberry Pi for a few weeks now, but work is keeping me busy (in a good way) so I’ve only just found time to put this together.

It’s an approximate overview of the steps I took to set mine up for use as:

If you can’t use vim just substitute nano in for vim in all commands that use it. Or your favourite editor. And skip any steps you don’t want. It’s not an authoritative guide, more like a recipe scribbled on the back of an envelope in the hope that one or two of the less obvious steps might save others some time.

I haven’t (yet) included steps for changing the default user name and password, or for setting up SSH keys, so the recipe assumes you are running the Pi on a trusted LAN. But these steps are no different on the Pi as they are from on any other Linux system anyway, so if you don’t already know how to, Google is your friend.

Setup for efficient Headless Operation

  1. Download the Debian image from https://www.raspberrypi.org/downloads/
  2. Flash it to your SD card.
  3. Boot up the Raspberry Pi.
  4. Log in, start SSH, and enable it on boot:

    sudo service ssh start; sudo update-rc.d ssh defaults
    
  5. If you wish, you can now disconnect the keyboard and screen that you used to get this far and continue using SSH from your laptop/desktop/phone. Use ifconfig to view the Pi’s IP address.
  6. Update the system:

    sudo apt-get update; sudo apt-get upgrade; sudo apt-get dist-upgrade
    
  7. Install Byobu:

    sudo apt-get install byobu
    
  8. Make Byobu start at login –

    byobu-config
    

    – toggle the bottom option.

  9. Start Byobu now:

    byobu
    
  10. Install Vim:

    sudo apt-get install vim
    
  11. Edit /boot/config.txt

    sudo vim /boot/config.txt
    

    – adding these lines:

    hdmi_force_hotplug=1
    disable_overscan=1
    

    The latter was required in order for my Pi to fill the entire screen (Iiyama ProLite B2712HDS). The former ensures that I don’t have to reboot it in order to use a screen if/when I plug one in (since most of the time it is headless). See http://elinux.org/RPi_config.txt for more options.

  12. Delete the swap partition using:

    sudo fdisk -uc /dev/mmcblk0
    

    Then delete the root partition and recreate it using the same start location, but allowing it to occupy the entire card. See  http://elsmorian.com/post/23366148056/basic-raspberry-pi-setup?438b7020 for more detailed instructions.

  13. Having saved the partition changes, reboot:

    sudo reboot
    
  14. Log back in once rebooted and resize the root filesystem to occupy the entire partition (i.e. the entire card, sans boot):

    sudo resize2fs /dev/mmcblk0p2
    
  15. Create a swapfile:

    sudo dd if=/dev/zero of=/var/swapfile bs=1M count=128
    sudo mkswap /var/swapfile
    sudo swapon /var/swapfile
    

    Edit fstab to turn off access time writes and to enable use of the swap file on boot –

    sudo vim /etc/fstab
    

    – delete the line starting with a hash (#), and paste in its place:

    /dev/mmcblk0p2  /               ext4    defaults,noatime,nodiratime     0       0
    /var/swapfile    none           swap    sw                              0       0
    

    Then reboot to make the changes take effect:

    sudo reboot
    
  16. The Pi’s 256MB RAM is shared between the CPU and GPU, by default 50:50. Since we are using it headless the GPU is unused. We can therefore change this allocation giving more RAM to the CPU. To view the current CPU allocation:

    free
    

    To steal some memory back from the GPU (look at what other *.elf files you have in /boot to see your other options):

    cd /boot
    sudo cp arm192_start.elf start.elf
    sudo sync
    sudo sync
    sudo reboot
    

    To view the new CPU allocation:

    free
    
  17. Change the hostname:

    sudo vim /etc/hostname
    sudo vim /etc/hosts
    

    (in the hosts file replace raspberry with your chosen hostname).

  18. Install molly-guard to prevent accidental power operations:

    sudo apt-get install molly-guard
    

    Configure molly-guard to always check (as we are using Byobu it will not detect the SSH session, which is what the default configuration tries to do) –

    sudo vim /etc/molly-guard/rc
    

    – uncomment this line:

    ALWAYS_QUERY_HOSTNAME=true
    

Setup as an Audio Server

  1. The kernel does not load the onboard sound driver by default, so force it to:

    sudo modprobe snd-bcm2835
    
  2. To ensure the module is loaded on every boot, do –

    sudo vim /etc/modules
    

    – adding the line

    snd-bcm2835
    
  3. Install ALSA:

    sudo apt-get install alsa
    
  4. Test the soundcard:

    speaker-test -c 2 -t wav
    
  5. Install VLC:

    sudo apt-get install vlc
    
  6. Stream some audio:

    wget http://www.example.com/radiostation.pls; cvlc playlist.pls
    
  7. Install Pulseaudio:

    sudo apt-get install pulseaudio pulseaudio-module-zeroconf
    
  8. Configure Pulseaudio to accept audio from remote clients –

    sudo vim /etc/pulse/default.pa
    

    – adding these lines:

    load-module module-native-protocol-tcp auth-anonymous=1
    load-module module-zeroconf-publish
    
  9. If you have a USB soundcard and you want to use it as th default device –

    sudo vi /etc/modprobe.d/alsa-base.conf
    

    – replace

    options snd-usb-audio index=-2
    

    with

    options snd-usb-audio index=0
    
  10. Start Pulseaudio

    pulseaudio --start
    

    If you wish for Pulseaudio to always be available from boot without logging in and manually starting it, take a look at http://www.freedesktop.org/wiki/Software/PulseAudio/Documentation/User/SystemWide/

  11. To stream sound via the server from a Linux client if zeroconf is not working, do:

    pacmd load-module module-tunnel-sink server=
    

    To stream sound from Windows you will need Virtual Audio Cable and LineInCode – use LineInCode to cat the sound from the virtual microphone provided by VAC into an SSH session which in turn pipes the sound to pacat on the Pi –

    linco.exe -B 16 -C 2 -R 44100 | ssh.exe  "cat - | pacat --playback"
    

    – you’ll find this more convenient if you have already setup passwordless login from the Windows box to the Pi. Automate this as a script that runs on start-up if you wish, and to switch between local speakers and Pi-Fi just toggle the default sound device in Windows between the virtual speakers provided by VAC and your local speakers.

  12. Consider buying a case from https://www.modmypi.com/. I haven’t yet, but am tempted.