Software

You may find this easiest if ssh is enabled on the Pi, and then log in with a terminal app. This lets you copy-and-paste the commands that follow, as they’re very exact about spelling.

Support for the Airplay emulation software and GPIO controlled buttons are installed separately. 

Do not proceed until you have your Speaker Bonnet up and running!

Install Shairport Sync

Shairport Sync is a Airplay audio player that can run on the Raspberry Pi. You can follow the step by step guide on installing the libraries and software on ThePi.io website.

Install GPIO Buttons

The four buttons are hooked up to GPIO pins on the Raspberry Pi. These control volume, mute audio or safely shut down the Pi.

Grab Dependencies

We'll want to install a few things before we get our controls python script running. Use this command to get them.

$ sudo apt-get install git python-pip python-gpiozero festival

To install the requirements, run this command.

$ sudo pip install pyalsaaudio

Install Controller Script

Now we can make the script by creating a new file and pasting the code using this command.

cd ~pi

sudo nano control.py

Then, copy and paste the code below into the newly created file. You can change up how the controls are mapped the GPIO by updating the buttons values at the top of the script.

# sudo apt-get install python-pip python-gpiozero festival
# sudo pip install pyalsaaudio

from gpiozero import Button
from signal import pause
import alsaaudio
import os
import time

mixer = alsaaudio.Mixer('PCM')
volup_button = Button(17)
voldown_button = Button(23)
mute_button = Button(27)
power_button = Button(22)

VOLUME_INCREMENT = 1

muted = False
volume_left, volume_right = mixer.getvolume()
print("Volume: L: %d%%    R: %d%%" % (volume_left, volume_right))

def volume_up():
  global volume_left, volume_right

  print("volume up!")
  volume_left, volume_right = mixer.getvolume()
  volume_left = min(volume_left + VOLUME_INCREMENT, 100)
  volume_right = min(volume_right + VOLUME_INCREMENT, 100)
  print("L: %d%%    R: %d%%" % (volume_left, volume_right))
  mixer.setvolume(volume_left, 0)
  mixer.setvolume(volume_right, 1)

def volume_down():
  global volume_left, volume_right

  print("volume up!")
  volume_left, volume_right = mixer.getvolume()
  volume_left = max(volume_left - VOLUME_INCREMENT, 0)
  volume_right = max(volume_right - VOLUME_INCREMENT, 0)
  print("L: %d%%    R: %d%%" % (volume_left, volume_right))
  mixer.setvolume(volume_left, 0)
  mixer.setvolume(volume_right, 1)

def mute():
  global volume_left, volume_right, muted

  if muted:
    print("Un-muting")
    muted = False
    mixer.setvolume(volume_left, 0)
    mixer.setvolume(volume_right, 1)
  else:
    print("Muting")
    muted = True
    mixer.setvolume(0, 0)
    mixer.setvolume(0, 1)

mute_button.when_pressed = mute

print("ALSA volume control friend!")

while True:
  if volup_button.is_pressed:
    volume_up()
    time.sleep(0.05)
  if voldown_button.is_pressed:
    volume_down()
    time.sleep(0.05)
  if power_button.is_pressed:
    t = time.time()
    while power_button.is_pressed:
      if (time.time() -t > 3):
        print("shutting down")
        os.system('echo "I am Boomy The Boom Box and I am shutting down now" | festival --tts')
        os.system("shutdown -h now")

Autostart Config

We can modify a startup file so that the script will automatically run when the Pi reboots.

$ sudo nano /etc/rc.local

Scroll down the script and enter the following text right above the line with exit 0.

sudo python /home/pi/control.py &

Then press cntrl+x to exit and enter to save changes to the file and exit. You'll want to reboot the Raspberry Pi. 

Last updated on 2017-11-16 at 10.43.31 AM Published on 2017-11-08 at 12.02.20 AM