DIY Apple AirPlay/Print and HomeKit on RaspberryPI

posted May 9, 2017, 6:33 AM by Tom Pedersen   [ updated May 22, 2017, 5:08 AM ]
I cobbled together a Raspberry Pi to stand-in as an Apple HomeKit and AirPlay/Print server as an experiment. The little gizmo allows us to control smart devices, print on household printer and play music on our stereo from iPhones & iPads. The HomePi has been working like a champ, so I decided to retrace my steps and share the recipe - hopefully useful to others.



Background/Overview

Many of the techno problems around our home tend to revolve around integrating older equipment with modern devices or attempting to deal with the deluge of new 'smart' devices, each with its own App or interface.   Our 'vintage' stereo and network printer are prime examples of the old world - simply not iPhone/iPad compatible in terms of streaming music or printing.  On the other end of the spectrum, we seem to be constantly fumbling with a variety of apps provided with our newfangled smart home (lights, switches, appliances, etc).  I originally dropped the Raspberry PI onto our network to fill the Air gap by emulating Apple AirPlay and AirPrint services and recently added HomeKit services so Siri could begin helping out around the house. A cheap, easy and fun little project - no programming but RPI/Linux knowledge will come in handy. Probably a couple of hours to get things installed and running.

Hardware

Our original AirPi was built on a Raspberry Pi 1 Model B, which worked great for Play/Print services for a couple of years - totally amazed by reliability and performance,  it even handled print jobs while playing streamed music without missing a beat.  The latest HomePi is based on a RPI 3 Model B which seems to be just as reliable and handle the additional HomeKit activity. The contraption is connected to our WiFi network and wired to an aux port on our stereo via headphone (3.5mm) to RCA Y-Cable.  

Here's the list of hardware components involved.  There is flexibility here, but the following works for me:

Software

The software used for the original AirPi was somewhat version specific due to the combination of hardware and software required to pull the thing together using Raspbian Weezy, Shairport open source project and some sound/alsa struggles attempting to get my cheap-o USB sound card up and running.  The HomePi rebuild went VERY smoothly under Jessie, but noted software versions below - just in case.  

Installation

Following is a sing-a-long with stopping points along the way for systems that won't require all services.  Not exactly a HowTo, more of the personal log used to build/rebuild this rascal.  It does Include links to installation guides and a number of HowTo articles that I found useful during the project.  Hopefully helpful to others.

Raspberry Pi OS Prep 

  • Follow Raspberry Pi Setup Guide for initial setup - Approx 15 Minutes, not including download time
    • Couple of notes/potential gotcha's:
      • Remember to set Language and Keyboard at bottom of NOOBS initial screen (I always forget)
      • Raspbian only - no need to install other OSs for this project
  • Some post installation cleanup from my checklist and log
    • Review Pi config options via command line Raspi-Config or GUI Preferences/Raspberry Pi Configuration.  Minimally set:
      • Change user pi's password (default is pi/raspberry)
      • Boot Options
        • Start in Text Console mode (CLI) - no need for GUI desktop
        • Auto Login as user pi
      • Advanced Options
    • Update Raspbian 

      sudo apt-get
      update 
      sudo apt-get upgrade

    • Install favorite editor - I like Joe, many use inbuilt nano

      sudo apt-get install joe

    • WiFi Setup/Tweaks
      • Configure WiFi to connect to your network 

AirPrint (CUPS)

  • Install latest CUPS via Pi's packaging tool (apt-get) - install/setup ~30 mins 
    Good guide @ http://www.lynsayshepherd.com/blog/2015/10/18/wireless-printingairprint-server-via-the-raspberry-pi-updated-guide/
    • Install avahi - required for network discovery

      sudo apt-get install avahi-discover
       

    • Installs CUPS and friends 

      sudo apt-get install cups cups-pdf python-cups

    • Mod user pi to be CUPS admin, it will be used to login to web interface 

      sudo usermod -aG lpadmin pi 

    • Make sure CUPS and friends are running after the install

      ps -A | grep cups

      ps -A | grep avahi

  • CUPS Config, phase 1 - cupsd.conf
    Edit /etc/cups/cupsd.conf to permit remote browser access via port 631. See Lynsay's guide above, a number of edits here ... in case I miss one
    • Comment out Localhost restriction & add line to permit access via port 631

      #Listen localhost:631
       
      Port 631

    • Permit remote access - add "Allow @Local" line right after "Order allow,deny" in following sections: 
        • <Location /> - # Restrict access to the server...
        • <Location /admin> - # Restrict access to admin pages...
        • <Location /admin/conf> - # Restrict access to configuration files...
    • Remember to bounce CUPS

      sudo /etc/init.d/cups restart
       

  • CUPS Config, phase 2 - via CUPS web interface @ http://<your PI IP address>:631
    • Log in to CUPS administration using pi system user & password 
    • Administration page: check "Share Printers" and "Allow Remote Admin"
    • Administration/Add Printer - "Discovered Network Printers" found our Dell M5200 w/ a couple of tweaks:
  • HowTo Use AirPrint to print from your iPhone, iPad, or iPod touch by Apple Support

AirPlay (Shairport-sync)

  • Shairport-sync install using https://githuamixerb.com/mikebrady/shairport-sync/blob/master/README.md  - Install/testing takes 45min - 1hr
  • Install steps below were distilled from README - VERY Jessie dependent.  Read through the README to season to taste and understand the process. 
    • Install required Linux packages 

      sudo apt-get install build-essential git xmltoman
       

      sudo apt-get install autoconf automake libtool
      libdaemon-dev libasound2-dev libpopt-dev libconfig-dev


      sudo apt-get install 
      avahi-discover avahi-daemon libavahi-client-dev 

      sudo apt-get install libssl-dev

      sudo apt-get install libsoxr-dev

    • Download/Build Shairport - Installs to /home/pi/shairport-sync directory
      Note: configure has a number of options and IS version dependent (Jessie) - see README for details

    • cd /home/pi


      git clone https://github.com/mikebrady/shairport-sync.git


      cd /home/pi/shairport-sync


      autoreconf -i -f

      ./configure --sysconfdir=/etc --with-alsa --with-avahi --with-ssl=openssl --with-metadata --with-soxr --with-systemd

      make

    • Create shairport-sync group & user

      getent group shairport-sync &>/dev/null || sudo groupadd -r shairport-sync >/dev/null


      getent passwd shairport-sync &> /dev/null || sudo useradd -r -M -g shairport-sync -s /usr/bin/nologin -G audio shairport-sync >/dev/null

    • Create directories, man pages and set to start with system

      sudo make install

      sudo systemctl enable shairport-sync

    • Run thru /etc/shairport-sync.conf file w/ your editor and season to taste.  There is A LOT in there
      • Set "name" to something descriptive. It shows on iphone/pad (e.g. "Family Room Bose")
      • Sample configuration file is installed @ /etc/shairport-sync.conf.sample
  • USB Sound Card setup (also dependent on Jessie):
    • Use aplay to get list of playback hardware - see below output, USB card highlighted //

aplay -l


**** List of PLAYBACK Hardware Devices ****
card 0: ALSA [bcm2835 ALSA], device 0: bcm2835 ALSA [bcm2835 ALSA]
  Subdevices: 8/8
  Subdevice #0: subdevice #0
  Subdevice #1: subdevice #1
  Subdevice #2: subdevice #2
  Subdevice #3: subdevice #3
  Subdevice #4: subdevice #4
  Subdevice #5: subdevice #5
  Subdevice #6: subdevice #6
  Subdevice #7: subdevice #7
card 0: ALSA [bcm2835 ALSA], device 1: bcm2835 ALSA [bcm2835 IEC958/HDMI]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 1: Device [USB Audio Device], device 0: USB Audio [USB Audio]
  Subdevices: 1/1
  Subdevice #0: subdevice #0

  • Modify /home/pi/.asoundrc, setting card # to USB Audio Device.  File appears to be auto generated/created if you start in the GUI, I had to create it myself from CLI ... it looks like this
    $cat .asoundrc

    pcm.!default {
           type hw
           card 1
    }
    ctl.!default {
            type hw
            card 1
    }
    • copy .asoundrc to /etc/asound.conf to get thing to work at boot/service

      sudo cp /home/pi/.asoundrc /etc/asound.conf  

  • Make sure Serice is up and OK.  After reboot too

    sudo systemctl start 
    shairport-sync
    sudo systemctl status shairport-sync

  • I preset volumes to minimize fiddling with device volume using alsamixer
    • Set Mic to zero
    • Set Capture to zero
    • Set Speaker to redline (93 on mine)
  • HomeBridge homepage @ https://github.com/nfarina/homebridge.
    Install can take some time, count on an hour with minimal plugin playtime!
  • One preliminary - check for plugins for your smart devices before you go through install  - just to make sure you'll be able to control your device.  Go to https://www.npmjs.com and search for  "homebridge-<your dev>".  For example https://www.npmjs.com/package/homebridge-wemo or https://www.npmjs.com/package/homebridge-philipshue
  • RPI Homebridge Install from https://github.com/nfarina/homebridge/wiki/Running-HomeBridge-on-a-Raspberry-Pi :
    • Check for c++ compiler, you should see response ending with "gcc version 4.92"

      g++-4.9 -v

    • Install nodejs

      curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -

      sudo apt-get install -y nodejs
      sudo apt-get install libavahi-compat-libdnssd-dev

    • HomeBridge Install:  README https://github.com/nfarina/homebridge/blob/master/README.md

      sudo npm install -g --unsafe-perm homebridge

    • Fire it up, it will complain about no plugins, but should see it run & offer a pairing code,  <Ctl>C to kill

      homebridge 

    • Create config.json file in /home/pi/.homebridge directory see README if you have probs, it could be your editor
  • Plugins installation.  Steps are dependent on the smart device.   Plugins are also installed using npm, will have their own readme/instructions including a snippet to add to your config.json file.  Get plugins by searching for "homebridge-" on https://www.npmjs.com.  Expect some trial/error and fiddling to get plugins working
  • Testing:  Once Plugins are installed,  Fire up Homebridge and test.  I test in a terminal window for a day or two to keep an eye on message chatter
  • To start Homebridge when RPI boots, create a systemd service from https://github.com/nfarina/homebridge/wiki/Running-HomeBridge-on-a-Raspberry-Pi#running-homebridge-on-bootup-systemd
    • Create /etc/default/homebridge file using example from https://gist.github.com/johannrichard/0ad0de1feb6adb9eb61a/#file-homebridge

      cat /etc/default/homebridge

      # Defaults / Configuration options for homebridge
      # The following settings tells homebridge where to find the config.json file
      # and where to persist the data (i.e. pairing and others)
      HOMEBRIDGE_OPTS=-U /var/lib/homebridge

      # If you uncomment the following line, homebridge will log more
      # You can display this via systemd's journalctl: journalctl -f -u homebridge
      # DEBUG=*

    • Create /etc/systemd/system/homebridge.service file using example from https://gist.github.com/johannrichard/0ad0de1feb6adb9eb61a/#file-homebridge-service
    • Check homebridge location - response should be usr/bin or usr/local/bin

      $ which homebridge
      /usr/bin/homebridge

    • Edit /etc/systemd/system/homebridge.service - set ExecStart to homebridge location (e.g. usr/bin/) 
      $ cat /etc/systemd/system/homebridge.service

      [Unit]
      Description=Node.js HomeKit Server 
      After=syslog.target network-online.target

      [Service]
      Type=simple
      User=homebridge
      EnvironmentFile=/etc/default/homebridge
      # Adapt this to your specific setup (could be /usr/bin/homebridge)
      # See comments below for more information
      ExecStart=/usr/bin/homebridge $HOMEBRIDGE_OPTS
      Restart=on-failure
      RestartSec=10
      KillMode=process

      [Install]
      WantedBy=multi-user.target

    • Set execute permissions for service

      sudo chmod +x /etc/systemd/system/homebridge.service

    • Add Homebridge system account, user and group created, no home directory (-M)

      sudo useradd -M --system homebridge

    • Create system dir and copy config.json file for new homebridge service - set ownership and write

      sudo mkdir /var/lib/homebridge

      sudo chmod +w /var/lib/homebridge 
      sudo chown -R homebridge:homebridge /var/lib/homebridge 
      sudo cp /home/pi/.homebridge/config.json /var/lib/homebridge/.

    • Enable/Run/Check the service

      sudo systemctl daemon-reload
      sudo systemctl enable homebridge
      sudo systemctl start homebridge
      sudo systemctl status homebridge

    • Check /var/log/daemon.log if anything goes bump.

      tac /var/log/daemon.log | less

    • Reboot to get the witches out and test service startup

      sudo shutdown now -r

  • Set up phone via https://support.apple.com/en-us/HT204893
    • if you forgot your pin/scan code dig it out of systemd journal

      sudo journalctl -u homebridge

         
  • Debugging:  Troubleshooting Guide @ https://github.com/nfarina/homebridge/wiki/Basic-Trouble-Shooting
    • Running in terminal window is very helpful 
    • See Also:  Common Issues section in the README

  • Apple Home info @ https://www.apple.com/ios/home/



That's it for now - works for me!  Quite amazed by these little PIs.  I'll post some utilization stats at some point in the future.  Drop me a line if you see something or have need or comment.  Hope it helps

---------------
* T. Pedersen Ventures is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to amazon.com.