Setup SD Card
First thing we'll need to do a burn a fresh installation of Raspbian to a 4GB+ SD card for the Raspberry Pi Zero W. You want to have a keyboard and HDMI display ready when the SD card is baked and ready for the Pi. You don't need to have anything wired up yet. There's plenty of resources online to get that up and going.
Speaker Bonnet Software
Software installation for the Speaker Bonnet requires an internet connection. That’s a frequent topic already covered in other Pi getting-started guides, so we’ll assume here that your Pi is already booted and networked, running Raspbian.
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.
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.
Text editor powered by tinymce.