Bethany Corcoran
18Aug/130

Raspberry Pi: Doing useful things

IMG_1540I've had my Raspberry Pi for a while now, and it's done a few different things. Firstly it was just a toy to tinker with, the latest cool gadget out, and that's what my previous posts about it have detailed. I found though that while it is powerful enough for some things, it's certainly not powerful enough to provide a smooth desktop experience compared to powerful modern computers which limits its useful functionality somewhat. After a several month gap I moved into a new flat, and found that I had a 20" computer monitor and some speakers that I didn't have a use for, so I set up the Raspberry Pi with XBMC and a Plex plugin so I could use it to watch stuff while cooking, in an attempt to make me cook real food more often. My Mac mini is my Plex server and the idea was that I could watch any media on it on this Raspberry Pi, but I found that the Plex plugin for XBMC on here was a bit clunky and wouldn't always find the server and I still find cooking rather dull so that never really got off the ground. It was however capable of playing back up to 1080p h264 content thanks to the onboard hardware decoder, so that's good.

My flatmate of the time left a couple of months ago (he enjoyed cooking and found lots of use with the Twitch.TV functionality) so I had the opportunity to repurpose the Raspberry Pi. My living room was getting increasingly warm thanks to the combination of summer and the Mac mini running in it constantly, and receiving a really big electricity bill was the icing on the cake: I resolved to find a new use for my Raspberry Pi.

My Mac mini provided a few main services, but I mainly use it for watching video on the TV. Thankfully I bought a smart TV so I have a YouTube client on the TV itself. Not perfect as it doesn't buffer particularly far in advance but convenient when you want to avoid having the computer on. The other things it provided were DynDNS updating, CardDAV contacts, my main music library (it's plugged into my big speakers so I don't need the TV on to listen to music) and Time Machine backups for both itself and my MacBook Pro. Of these, the main things were DynDNS updating and CardDAV contacts, and being able to play my music on my speakers. I don't do much on my MacBook Pro beyond browsing the web so I'm more than happy to have the Time Machine backups occur every couple of days instead of constantly. So I had my goal in mind - CardDAV and music from this Raspberry Pi. I would have to approach it differently than the Mac mini had done though.

Raspbian and basic config

The first step was to get Linux up and running on the Raspberry Pi. I used the Raspbian images from the Raspberry Pi website and imaged an SD card. After first booting it up, you'll need to run the raspi-config script - I believe this is automatically run for you if you're accessing it directly, but if like me you were SSHing into it from the get-go then make sure you do complete this step, if only so we can reduce our shared memory for the graphics down as far as possible (16MB or 24MB) to give us as much useable RAM as possible. I've heard though that bumping this down to 16MB causes the HDMI to no longer function so keep that in mind. Once that was done, I set a static IP since I wanted it to provide services on my network including externally accessible ones, so this is a required step. Alternatively, you could set a static DHCP lease but I prefer the old-school method. Additionally, I updated and upgraded the software via apt-get:

sudo apt-get update
sudo apt-get upgrade

More precisely, this updates the apt-get database of available software, then upgrades packages registered with apt-get to the latest versions. The "sudo" at the start gives the command super-user privileges, required in this case as typical user privileges don't allow for this kind of operation. We also saw this being used in setting the static IP - only a super-user has permission to edit the relevant system configuration files for that to go through, other users may only read the file or may have no access to it.

Additional config for Raspbian

Once this basic configuration was done, I stumbled across Github (having never really used it before) and did a quick search for Raspberry Pi-related projects. I came across the Raspbian-Mod project by jwhitehorn. This project aims to remove packages unnecessary to a headless server (so for instance, the X window environment and related packages - we're not going to use this with a GUI so it's needlessly taking up space), install a few useful ones (my favourite on the list is Bonjour support), installs and runs the Hexxeh Raspberry Pi firmware update software, then writes blank data over unused space. I should note though, that when I was putting together my Raspberry Pi it would end up kernel panicking on boot several times. I never properly confirmed the cause for this but my suspicion is that it may be this last step that was causing me problems. In the end, I opened up the slim.sh file on the website and ran the commands individually - specifically, I ran lines 1 to 20 which removes and installs various packages, then lines 35 to 41 to change some settings in vim. I also missed out the firmware update on this installation (meaning I won't get continuous updates) but I'd recommend that you do at least update the firmware from what came out the box. Your mileage may vary on this section - it could be due to something else entirely that my Raspberry Pi sometimes kernel panicked, after all.

So, I had a Raspberry Pi with an up-to-date Raspbian build, a static IP, a bunch of unnecessary software removed and some useful stuff put back in. Next up was ownCloud.

ownCloud

On my Mac I had been using the OS X Server functionality to provide my contacts. I like the idea of moving away from hosted services such as Google where possible due to keeping my data mine, but the reliability of Gmail over hosting email on my own, and the helpfulness of Google Calendar sharing settings in a world where most people I know use it, mean I don't have many opportunities to do this. Contacts are something that I don't need to share with anyone and don't require near-perfect uptimes, so I had taken to serving them myself. While I couldn't very well use the same OS X Server tools on a Raspberry Pi, I was already aware of ownCloud. This provides several cool functionalities, largely based around the DAV open standards - WebDAV for files (including desktop apps that allow Dropbox-style file synchronisation of local folders to the server), CalDAV for calendars, and CardDAV for contacts. For now, CardDAV would suffice.

Installing ownCloud

After searching around, I found a script for ownCloud on a Raspberry Pi called OwncloudPie by petrockblog. Actually, finding this is what made me stumble across Github previously, I'm glad I did! The script does pretty much everything for you, including installing Apache 2 and SQLite. You'll have to answer some questions as part of the process, including some to set up a self-signed SSL certificate and specifying what hostname you're planning on using for the server. To use the script, simply follow the instructions found within Readme.md (essentially the overview on the project's page linked above). Note that we've already installed Git as part of the Raspbian-Mod process, but we haven't installed Dialog, so make sure you get this installed. Once ownCloud is installed, it should give you an address to connect to (generally in the form of https://1.2.3.4/owncloud) to finish configuration of ownCloud. Make sure you set a secure password for it, especially if you're planning on having it be externally accessible.

ownCloud further configuration and CardDAV device setup

Once ownCloud was up and running, I just had to get my devices set up to use it. In short, I settled on using a non-standard external port number on my router, which would point to the Raspberry Pi internally on port 443, as a way of further securing it, then had my devices connect to my DynDNS address on this port. If your router cannot handle external IP loopback (that is, allowing internal clients to connect to your external IP address to access services as if they were not part of your network) then you may need to find an alternative such as a local DNS entry or only using ownCloud on the internal IP address. Setting up OS X and iOS devices is easy enough with built-in support for CardDAV, but during the setup process ownCloud will point you towards an app you can install on your Android device that will let you set up a CardDAV account and synchronise with the built-in contacts functions, which I believe is this tool, CardDAV-Sync free beta by Marten Gajda. I've found that this works fine on my Nexus 7 2012. Additionally, it's worth mentioning that the contacts groups in the ownCloud web interface have nothing to do with the contacts groups that CardDAV clients will use (the latter will appear as individual contacts in the web interface).

DynDNS updater, ddclient

You may notice that no mention of DynDNS had been made until mentioning ownCloud - that's because I completely forgot about it until this point! I was able to use my ownCloud contacts for a while but then it mysteriously stopped working, and after thinking that I would have to start from scratch (and indeed doing so) I eventually realised that with my Mac mini off, nothing was updating my DynDNS address. Fortunately this is easily solved by installing and configuring the command line tool ddclient.

sudo apt-get install ddclient

Follow the questions on-screen as part of the installation process and you'll be golden.

AirPlay audio server, Shairport

So the last thing to get set up is some sort of music functionality. I didn't want to attach a hard drive and have this as my music server as I'm happy with using iTunes, so I instead looked for options to make it act as an AirPlay audio server. This would allow me to play music from my MacBook Pro or my iPhone directly to my speakers with only a click or two. To install this, I used these instructions by Stanislav Khromov, which you'll note describe a few steps that we've already completed. In case the site goes down I've written down the relevant commands below, but credit for this goes to Stanislav (who has credited those he is bringing information from in his post).

(If you're feeling lazy, feel free to type "sudo -s" before going through the below commands - it'll mean you'll stay with super-user privileges and won't need to type "sudo" before each command as I've listed. Just be aware that you have a lot of power over the system and can cause damage to it if not careful!)

First we install some tools that Stanislav has listed as prerequisites (if any of these have already been installed then they'll simply be bypassed):

sudo apt-get install build-essential libssl-dev libcrypt-openssl-rsa-perl libao-dev libio-socket-inet6-perl libwww-perl avahi-utils pkg-config git chkconfig

Then we need to edit the file /usr/share/alsa/alsa.conf in our favourite text editor (my preference is vim but nano can be simpler for new users):

sudo nano /usr/share/alsa/alsa.conf

Find the line "pcm.front cards.pcm.front" to "pcm.front cards.pcm.default". This prevents an error with Shairport that, judging from this line we've edited, relates to picking the correct audio output device name.

Next we need to edit another file to resolve an issue with avahi-daemon with it discovering .local domains:

sudo nano /etc/init.d/avahi-daemon

Find the line starting with "DISABLE-TAG", and insert "-DISABLED" into the end of the line before the quotation mark. It should look like this afterwards:

DISABLE_TAG="/var/run/avahi-daemon/disabled-for-unicast-local-DISABLED"

Now we're going to restart avahi-daemon and ensure that it is set to start when Raspbian starts:

sudo service avahi-daemon restart
sudo chkconfig avahi-daemon on

Next we want to bump the audio levels up as by default they're rather low. To do this, run the command "alsamixer". Bring the volume level up to around 85 - you want to ensure that the dB gain remains below 0, as above this you may get clipping (which will sound unpleasant).

Let's now install Shairport, the tool we're going to use to provide AirPlay functionality on the Raspberry Pi.

git clone -b 1.0-dev git://github.com/abrasive/shairport.git
cd shairport
sudo make
sudo make install

Now to test it. This'll start Shairport and it'll appear on the network as "AirPlay Test". Don't worry, we can name it properly later.

./shairport -a 'Shairport 1'

You should now be able to connect to it on an AirPlay-compatible music app or device and play music from it. If you don't hear anything, check your audio wiring and speakers. If it all looks good and you're still not hearing anything, we can check to ensure that the audio is functioning correctly by downloading an audio file and playing it through the command prompt. The first result on Google for "wav test audio file" brought me to a website called Kozco.com but you can substitute the URL below with any other WAV file if needed.

wget http://www.kozco.com/tech/piano2.wav
aplay piano2.wav

Once we know that Shairport is working correctly, we need to ensure it starts on boot. Let's make a new script:

touch shairport
nano shairport

Then put the following code into the file:

#! /bin/sh
# /etc/init.d/shairport
#
NAME="My Shairport"

case "$1" in
 start)
 /usr/local/bin/shairport -d -a "$NAME"
 ;;
 stop)
 killall shairport
 ;;
 *)
 echo "Usage: /etc/init.d/shairport {start|stop}"
 exit 1
 ;;
esac

exit 0

Note the NAME variable on the fourth line - feel free to change the name from My Shairport to whatever you'd prefer. Save and exit when done.

Now let's move this file into the correct location, make it executable, and make sure it runs on boot:

sudo cp shairport /etc/init.d/
chmod -R 755 /etc/init.d/shairport
chkconfig shairport on

And now, we should have a working AirPlay audio receiver that'll work after a reboot of the Raspberry Pi! Feel free to test it if you'd like. I did however experience one issue in that after a while Shairport would stop producing audio, even though devices would be able to connect to it and play audio to it. I found that Shairport would just need to be stopped and started to resolve this, so I made some cron tasks to do this. Instructions on how to use cron tasks can be found here.

sudo crontab -e

Then the cron commands themselves:

0 6 * * * /etc/init.d/shairport stop
1 6 * * * /etc/init.d/shairport start

This will stop Shairport at 06:00 every day, then start it back up at 06:01 every day.

Final step, securing the Raspberry Pi

So we have our Raspberry Pi with ownCloud, a DynDNS updater, and an AirPlay audio receiver. Finally, we need to lock down the user account as the default details are rather insecure. Instructions for this are sourced from this thread, with thanks to darrenjw.

Firstly we're going to enable the root account and set it a (secure!) password:

sudo passwd root

Log out of the default "pi" account and log in to root instead. Then let's rename the default account - replace "newname" with whatever you'd prefer to call it:

usermod -l newname pi
usermod -m -d /home/newname newname

Log out of root and in to your renamed account, then use

passwd

to set a new secure password for your user account on here. And you're all done!

Share

Posted by Bethany

Comments (0) Trackbacks (0)

No comments yet.


Leave a comment

No trackbacks yet.