Simple Smart Speakers

Tell us the story of your project.: 
Goals

I listen to a lot of music, so for a long time I'd wanted to create a system where I could wirelessly play music to my powered speakers, with a further goal of having these speakers automatically turn on and off with the music stream.

 

First Steps

Achieving my first goal was fairly straight forward: I got a Raspberry Pi Zero W and attached a digital to analog converter (DAC) board. With my speakers hooked up to the Pi and Shairport Sync (a piece of software that lets Pis use Apple’s Airplay protocol) installed, the simple wireless system was ready to go.

  Beginning Automation

Shairport Sync allows you to run a script at the beginning and end of the audio stream. Using a relay, (a switch that allows a low voltage device to control a high voltage system) I assembled a further setup where the Pi could control the power to a 120V outlet. By running a simple Python script relay_on with my speakers plugged into this outlet, I could switch the speakers on at will. After writing a similar script to turn the speakers off, I programed Shairport Sync to run the relay_on script at the start of the audio stream and relay_off at the end.

Further Software Refinement

But now I had a problem: because of the way Airplay handles audio streams, my speakers would turn on and off very rapidly under some conditions, such as skipping through a YouTube video. In order to mitigate this, I wrote a shell script process_monitor which monitors if the relay_on process is running (which indicates that the audio stream has been activated) and outputs "1" if the active process is detected. I then modified (with some help from Stack Exchange) my relay_off script to continually launch this shell script and monitor its output for 60 seconds. If during that period of time process_monitor returns "1", relay_off terminates without turning off the speakers. But if the 60 seconds expire without any return from the shell script, then the speakers turn off as usual. In practice, this means that if the audio stops but then restarts within a minute, the speakers remain on as if nothing has happened.

  Building a Better Box

In order to put a final polish on the project, I designed and built purpose-made enclosures for all the components. With the relay and outlet in one enclosure and the Pi in another, the two systems could be connected by just one cable for a clean and organized presentation. I 3D modeled the relay / outlet box from scratch in Fusion 360, including an insert that slots around the relay, separating the low and high voltage sides in order to prevent short circuiting. For the Pi case I modified an enclosure I found on Thingiverse in order to accommodate the DAC. With both cases 3D printed and assembled, the project was complete. I really couldn't be happier with how this project turned out -- it's no exaggeration to say that I use it many times a day!

How-to: 
Important Project Information

Shairport Sync uses Apple’s Airplay protocol, so if you don’t have a Mac or an iPhone this project won’t work. This project also makes use of powered speakers that plug into an AC outlet — passive speakers cannot be used here. Finally, it is extremly important to follow standard safety protocols while working with high voltage, such as the AC outlet featured in this project. Never modify this system while any part of it is plugged in.

 

Setting up the Pi and the DAC

Plug the Raspberry Pi Zero W into a power source and then follow the directions here. Next, connect the pHAT DAC board to the Pi. Because we'll need to use some of the other pins on the Pi later, make sure to only connect the pins necessary to power the DAC (you may want to forgo soldering for now — the use of jumper cables will provide more flexibility). These essential pins are circled in red on the diagram below, and are:

  1. Pin 1 (3.3V)
  2. Pin 2 (5V)
  3. Pin 12 (GPIO 18)
  4. Pin 35 (GPIO 19)
  5. Pin 39 (Ground)
  6. Pin 40 (GPIO 21) 

If you want to learn more about Pi pinouts visit here.

After you've soldered the pins, run the command below on the Pi in order to install the software to run the pHAT DAC and then reboot the Pi (you'll need to find other code and documentation if you happen to use a different brand).

  • curl https://get.pimoroni.com/phatdac | bash

Next you'll need to install Shairport Sync, which allows your Pi to use Airplay to wireless stream music to your speakers. Follow the instructions here to complete that install.

At this point, you should plug in your speakers into the DAC using the audio cable of your choice and test to see if everything works. You should be able to hear your music once you connect to Airplay (click on the sound icon and select the name of the Pi) and press play!

 

  Wiring and Programming the Relay

Now it’s time to add the relay. Wire the relay to the pins on the Pi shown in the diagram below. You can use any of the unsoldered GPIO pins for the data cable, but for my specific code to work, you'll need use Pin 36 (aka GPIO 16).

Once the relay is wired in this preliminary fashion, it’s time for the final software modifications. 

On the Pi, create a “bin” folder in the home directory of the Pi by running:

  • mkdir bin

Navigate into this directory by running:

  • cd bin

Now run:

  • nano relay_on

in order to create a new file "relay_on". Paste this code into the file and save it.

Next run:

  • nano relay_off

and paste in this code and save.

Finally, run:

  • nano process_monitor.sh

and paste in this code. Once this file is saved, stay in the bin directory and run:

  • chmod +x relay_on
  • chmod +x relay_off
  • chmod +x process_monitor.sh

to make these files executable. 

After this, navigate back into the home directory and open the Shairport Sync configuration file with:

  • cd ~
  • sudo nano /usr/local/etc/shairport-sync.conf

In this file, change run_this_before_play_begins = "/full/path/to/application and args"; 

to: run_this_before_play_begins = "/home/pi/bin/relay_on"; 

and run_this_after_play_ends = "/full/path/to/application and args"; 

to: run_this_after_play_ends = "/home/pi/bin/relay_off";

Save the configuration file and then, from the home directory, run:

  • sudo usermod -a -G gpio shairport-sync
  • sudo service shairport-sync restart

which will allow Shairport Sync to use the GPIO pins.

Now, with the relay wired up, try playing music over Airplay. Hopefully, you should hear the relay click on and see its LED light up.

 

Final Assembly

You can download my specific 3D models here

Once they're printed out, sink the M3 heat set inserts into their respective holes on the oultet case (there are 8).

Then wire up the male JTS connector, and glue it into its hole in the base piece.

While making sure you still know which wire is which, attach the connector wires to the relay. Then wire the inlet, leaving the black/hot wire long enough to be able to thread it through the insert and reach the relay in the bottom of the case. Prepare another length of black wire that can extend back up from the relay to the outlet. Connect the wire coming from the inlet to the central port on the relay and attach the second piece of wire to the port labeled NO, as seen on the earlier diagram. NO stands for "normally open," which means that the relay circuit will be disconnected in its resting state.

Now, you can slide the insert over the relay and screw the central box into the base, using the M3 screws.

Next, screw the inlet into the side of the box and attach its wires to the proper screws on the outlet. Screw the outlet into the attachment points on the top of box and then attach the cover piece, completing the outlet box.

Now it's time to assemble the Pi case. Slide the Pi board into the case and thread the M2.5 screws into the through holes in the bottom of case. On top of the Pi board, screw on a set of female-female brass standoffs, to secure the board in place.

Now slide the DAC board on and solder the previously mentioned pins to the Pi.

Finally, wire another JST connector with the same configuration as the one in the outlet box. Solder the wires to the correct pins on the header (remember: these are the pins the relay uses), clip the remaining unsoldered pins and glue the JST into the side of the case.

Screw in a set of male-female brass standoffs, snap the lid into place and then screw it down using the through holes.

Now, in order to test the system, plug something inexpensive into the outlet box (such as a desk lamp). Make sure this item's power switch is in the on position. Attach a 3 pin cable between the box and Pi, taking care to make sure that you are connecting the correct wires. Then, plug a 3 prong AC cable into the inlet and plug the whole outlet box into the wall. Now, with Pi powered on and Airplay connected, try playing music — the light (or other device you have plugged in) should switch on with the start of the music stream. If this test goes well, now you can plug your speakers into the outlet box (again with their power switches in the on position) and your system will be ready for use!

Difficulty: 
Duration: 
Collaborators: 
jacobxdouglas
Number of Forks: 
0
Tools: 
Name: 
Soldering Iron
Quantity: 
1
Name: 
Wire strippers
Quantity: 
1
Name: 
Screwdriver
Quantity: 
1
Materials: 
Name: 
Jumper Cables
Quantity or amount: 
9
Name: 
Duplex outlet
Quantity or amount: 
1
Name: 
5v relay
Quantity or amount: 
1
Name: 
3 pin JST-XHR connectors
Quantity or amount: 
2
Name: 
3mm heat set inserts
Quantity or amount: 
8
Name: 
C14 inlet
Quantity or amount: 
1
Name: 
Metric screws
Quantity or amount: 
1
Name: 
Heat shrink tubing
Quantity or amount: 
1
Name: 
3 strand wire
Quantity or amount: 
1
Name: 
M2.5 brass standoffs
Quantity or amount: 
8
Name: 
M2.5 and M3 screws
Quantity or amount: 
1
Boards & Kits: 
Name: 
Raspberry Pi Zero W
Quantity: 
1
Name: 
Pimoroni pHAT DAC
Quantity: 
1
Team Members: 
Team member name: 
jacobxdouglas
What role did this person play on the project?: 
Project Lead
Public
Sort Order: 
0
Teaser: 
A simple Raspberry Pi and relay controlled system that automatically turns a pair of speakers on and off in sync with a wireless audio stream.
Aha! moment: 
This was my first in depth project using Raspberry Pi's, so it was pretty amazing to discover how versatile and helpful of a tool they can be, especially for the price! I never would've thought this kind of home electronics work was within my reach.
Uh-oh! moment: 
It was definitely a bad moment when I realized that my speakers turned on and off every single time the audio stream started and stopped, even if the pause was for less than a second. I knew it was going to take some serious problem solving to fix this issue.
Show & Tell video as default: 
Creation Date: 
Saturday, November 10, 2018 - 16:51

Add new comment