1# Shairport Sync for Cars
2If your car audio has an AUX input, you can get AirPlay in your car using Shairport Sync. Together, Shairport Sync and an iPhone can give you access to internet radio, YouTube, Apple Music, Spotify, etc. on the move. While Shairport Sync is  no substitute for CarPlay, the audio quality is often much better than Bluetooth. Your passengers can enjoy movies with the soundtrack on the car speakers.
3
4## The Basic Idea
5
6The basic idea is to use a small Linux computer to create an isolated WiFi network for the car and to run Shairport Sync on it to provide an AirPlay service. The audio goes via a DAC to the AUX input of your car audio system.
7
8The car WiFi network you create is isolated and local to your car, and since it isn't connected to the Internet, you don't really need to secure it with a password. Likewise, you don't really have to use a password to connect to the AirPlay service.
9
10When an iPhone or an iPad with cellular capability is connected to an isolated WiFi network like this, it can use its cellular connection to access the Internet.
11This means it can connect to internet radio, YouTube, Apple Music, Spotify, etc. over the cellular network and play the audio through the car network to the AirPlay service provided by Shairport Sync.
12
13Note that Android devices can not, so far, do this trick of using the two networks simultaneously.
14
15## Example
16
17In this example, a Raspberry Pi Zero W and a Pimoroni PHAT DAC are used. This combination has been tested for over two years. Please note that some of the details of setting up networks are specific to the version of Linux used -- Raspbian Stretch or later.
18
19### Prepare the initial SD Image
20* Download the latest version of Raspbian Lite -- Stretch Lite of 2018-03-13 at the time of writing -- and install it onto an SD Card.
21* Mount the card on a Linux machine. Two drives should appear -- a `boot` drive and a `rootfs` drive. Both of these need a little modification.
22* Enable SSH service by creating a file called `ssh` on the `boot` drive. To do this, mount the drive and CD to its `boot` partition (since my username is `mike`, the drive is at `/media/mike/boot`):
23```
24$ touch ssh
25```
26* Also in the `boot` drive, edit the `config.txt` file to add the overlay needed for the sound card. This may not be necessary in your case, but in this example a Pimoroni PHAT is being used and it needs the following entry to be added:
27```
28dtoverlay=hifiberry-dac
29```
30* Next, some modifications need to be done to the `rootfs` drive to make the Pi connect to your main WiFi network. (This is a temporary measure to enable you to connect the Pi to your main network so that you can do all the software installation and updating of the software necessary. Later, the Pi will be configured to start its own isolated network.) On the `rootfs` drive, edit the file `etc/wpa_supplicant/wpa_supplicant.conf` (you'll need root privileges) and add the name and password of your main WiFi network (substitute your own network name and password in, but keep the quotation marks):
31```
32network={
33    ssid="Network Name"
34    psk="Password"
35}
36
37```
38Close the file and carefully dismount and eject the two drives. Remove the SD card from the Linux machine, insert it into the Pi and reboot. After a short time, the Pi should appear on your network and you can SSH into it. To check that it has appeared on the network, try to ping it at `raspberrypi.local`. It may take a minute or so to appear. Once it has appeared on your network you can SSH into it and configure it.
39
40### Boot, Configure, Update
41The first thing to do on a Pi would be to use the `raspi-config` tool to expand the file system to use the entire card. It might be useful to change the `hostname` too. Next, do the usual update and upgrade:
42```
43# apt-get update
44# apt-get upgrade
45```
46
47### Shairport Sync
48First, install the packages needed by Shairport Sync:
49```
50# apt-get install build-essential git xmltoman autoconf automake libtool libpopt-dev libconfig-dev libasound2-dev avahi-daemon libavahi-client-dev libssl-dev libsoxr-dev
51```
52Next, download Shairport Sync, configure it, compile and install it:
53```
54$ git clone https://github.com/mikebrady/shairport-sync.git
55$ cd shairport-sync
56$ autoreconf -fi
57$ ./configure --sysconfdir=/etc --with-alsa --with-avahi --with-ssl=openssl --with-soxr --with-systemd
58$ make
59$ sudo make install
60```
61*Do not* enable Shairport Sync to automatically start at boot time -- startup is organised differently.
62
63Third, finish by configuring Shairport Sync.
64Here are the important options for the Shairport Sync configuration file at `/etc/shairport-sync.conf`:
65```
66// Sample Configuration File for Shairport Sync for Car Audio with a Pimoroni PHAT
67general =
68{
69	name = "BMW Radio";
70	ignore_volume_control = "yes";
71	volume_max_db = -3.00;
72};
73
74alsa =
75{
76	output_device = "hw:1"; // the name of the alsa output device. Use "alsamixer" or "aplay" to find out the names of devices, mixers, etc.
77};
78
79```
80Two `general` settings are worth noting. First, the option to ignore the sending device's volume control is enabled -- this means that the car audio's volume control is the only one that affects the audio volume. Of course this is a matter of personal preference.
81Second, the maximum output offered by the DAC to the AUX port of the car audio can be reduced if it is overloading the input circuits. Again, that's a matter for personal selection and adjustment.
82
83The `alsa` settings are for the Pimoroni PHAT -- it does not have a hardware mixer, so no `mixer_control_name` is given.
84
85Note that the DAC's 32-bit capability is automatically selected if available, so there is no need to set it here. Similarly, since `soxr` support is included in the build, `soxr` interpolation will be automatically enabled if the device is fast enough.
86
87### Extra Packages
88A number of packages to enable the Pi to work as a WiFi base station are needed:
89```
90# apt-get install hostapd isc-dhcp-server
91```
92Disable both of these services from starting at boot time (this is because we will launch them sequentially later on):
93```
94# systemctl unmask hostapd
95# systemctl disable hostapd
96# systemctl disable isc-dhcp-server
97```
98#### Configure HostAPD
99Configure `hostapd` by creating `/etc/hostapd/hostapd.conf` with the following contents which will set up an open network with the name BMW. You might wish to change the name:
100```
101# This is the name of the WiFi interface we configured above
102interface=wlan0
103
104# Use the nl80211 driver with the brcmfmac driver
105driver=nl80211
106
107# This is the name of the network -- yours might be different
108ssid=BMW
109
110# Use the 2.4GHz band
111hw_mode=g
112
113# Use channel 6
114channel=9
115
116# Enable 802.11n
117ieee80211n=1
118
119# Enable WMM
120wmm_enabled=1
121
122# Enable 40MHz channels with 20ns guard interval
123#ht_capab=[HT40][SHORT-GI-20][DSSS_CCK-40]
124
125# Accept all MAC addresses
126macaddr_acl=0
127
128# Use WPA authentication
129#auth_algs=1
130
131# Require clients to know the network name
132ignore_broadcast_ssid=0
133
134# Use WPA2
135#wpa=2
136
137# Use a pre-shared key
138#wpa_key_mgmt=WPA-PSK
139
140# The network passphrase
141#wpa_passphrase=none
142
143# Use AES, instead of TKIP
144#rsn_pairwise=CCMP
145```
146#### Configure DHCP server
147
148First,  replace the contents of `/etc/dhcp/dhcpd.conf` with this:
149```
150subnet 10.0.10.0 netmask 255.255.255.0 {
151     range 10.0.10.5 10.0.10.150;
152     #option routers <the-IP-address-of-your-gateway-or-router>;
153     #option broadcast-address <the-broadcast-IP-address-for-your-network>;
154}
155```
156Second, modify the INTERFACESv4 entry at the end of the file `/etc/default/isc-dhcp-server` to look as follows:
157```
158INTERFACESv4="wlan0"
159INTERFACESv6=""
160```
161### Set up the Startup Sequence
162Configure the startup sequence by adding commands to `/etc/rc.local` to start `hostapd` and the `dhcp` server and then to start `shairport-sync` automatically after startup. Its contents should look like this:
163```
164#!/bin/sh -e
165#
166# rc.local
167#
168# This script is executed at the end of each multiuser runlevel.
169# Make sure that the script will "exit 0" on success or any other
170# value on error.
171#
172# In order to enable or disable this script just change the execution
173# bits.
174#
175# By default this script does nothing.
176
177/sbin/iw dev wlan0 set power_save off
178/usr/sbin/hostapd -B -P /run/hostapd.pid /etc/hostapd/hostapd.conf
179/sbin/ip addr add 10.0.10.1/24 dev wlan0
180/bin/sleep 1
181/bin/systemctl start isc-dhcp-server
182/bin/sleep 2
183/bin/systemctl start shairport-sync
184
185exit 0
186```
187As you can see, the effect of these commands is to start the WiFi transmitter, give the base station the IP address `10.0.10.1`, start a DHCP server and finally start the Shairport Sync service.
188
189### Final Steps
190Up to now, if you reboot the Pi, it will reconnect to your WiFi network, ignoring the instructions and settings you have given it to act as a base station. That is because the `wlan0` interface is still under the control of the `dhcpcd` service. So, the final step is to instruct the `dhcpcd` service not to manage `wlan0`. To do this, edit `/etc/dhcpcd.conf` and insert the following line at the start:
191```
192denyinterfaces wlan0
193```
194From this point on, at least on the Raspberry Pi, if you reboot the machine, it will not reconnect to your network – instead, it will act as the WiFi base station you have configured with `hostapd` and `isc-dhcp-server`.
195
196### Optimise startup time -- Raspberry Pi Specific
197
198This is applicable to a Raspberry Pi only. Some of it may be applicable to other systems, but it has not been tested on them. There are quite a few services that are not necessary for this setup. Disabling them can shorten startup time. Running these commands disables them:
199
200````
201sudo systemctl disable systemd-timesyncd.service
202sudo systemctl disable keyboard-setup.service
203sudo systemctl disable triggerhappy.service
204sudo systemctl disable dhcpcd.service
205sudo systemctl disable wpa_supplicant.service
206sudo systemctl disable dphys-swapfile.service
207sudo systemctl disable networking.service
208````
209
210### Read-only mode -- Raspberry Pi Specific
211
212Run `sudo raspi-config` and then choose `Performance Options` > `Overlay Filesystem` and choose to enable the overlay filesystem, and to set the boot partition to be write-protected.
213
214### Ready
215Install the Raspberry Pi in your car. It should be powered from a source that is switched off when you leave the car, otherwise the slight current drain will eventually flatten the car's battery.
216
217When the power source is switched on, typically when you start the car, it will take maybe a minute for the system to boot up.
218### Enjoy!
219
220---
221
222## Updating
223From time to time, you may wish to update this installation. However, in order to update Shairport Sync, you must reconnect the system to a network that can access the internet. The easiest thing is to temporarily reconnect to the network you used when you created the system. To do that, you have to temporarily undo the "Final Steps" and some of the "Raspberry Pi Specific" steps you used. This will enable you to connect your device back to the network it was created on. You should then be able to update the operating system and libraries in the normal way and then update Shairport Sync.
224
225So, take the following steps:
226
2271. If it's a Raspberry Pi and you have enabled the Read-only mode, you must take the device out of Read-only mode:
228Run `sudo raspi-config` and then choose `Performance Options` > `Overlay Filesystem` and choose to disable the overlay filesystem and to set the boot partition not to be write-protected. This is so that changes can be written to the file system; you can make the filesystem read-only again later. Save the changes and reboot the system.
229
2302. If you have disabled the `dhcpcd`, `wpa_supplicant` or `systemd-timesyncd` services as suggested in the "Optimise startup time -- Raspberry Pi Specific" section, you need to temporarily re-enable them:
231`# systemctl enable dhcpcd.service`
232`# systemctl enable wpa_supplicant.service`
233`# systemctl enable systemd-timesyncd.service`
234Reboot.
235
2363. To allow your device to reconnect to the network it was created on, edit `/etc/dhcpcd.conf` and comment out the following line at the start:
237`denyinterfaces wlan0`
238so that it looks like this:
239`# denyinterfaces wlan0`
240From this point on, if you reboot the machine, it will connect to the network it was configured on, i.e. the network you used when you set it up for the first time. This is because the name and password of the network it was created on would have been placed in `/etc/wpa_supplicant/wpa_supplicant` when the system was initially configured and will still be there.
241
2424. Reboot and do Normal Updating
243
244   You can perform updates in the normal way -- see [UPDATING](https://github.com/mikebrady/shairport-sync/blob/master/UPDATING.md). When you are finished, you need to undo the temporary changes you made to the setup, as follows:
245
2465. If you had temporarily re-enabled services that are normally disabled, then it's time to disable them again:
247`# systemctl disable dhcpcd.service`
248`# systemctl disable wpa_supplicant.service`
249`# systemctl disable systemd-timesyncd.service`
250
2516. To re-enable the system to create its own network, edit `/etc/dhcpcd.conf` and uncomment the line that you had temporarily commented out at the start of the update. Change:
252`# denyinterfaces wlan0`
253so that it looks like this:
254`denyinterfaces wlan0`
255
2567. Reboot. The system should start as it would if it was in the car.
257
2588. If the device is a Raspberry Pi and you wish to make the file system read-only, connect to the system, run `sudo raspi-config` and then choose `Performance Options` > `Overlay Filesystem`. In there, choose to enable the overlay filesystem, and to set the boot partition to be write-protected. Do a final reboot and check that everyting is in order.
259