December 9, 2016

Create a Fully Automated Light and Music Show for the Holidays: Part 3

holiday-lights-3.jpg

holiday lights
This tutorial series shows how to create fully automated holiday music and light show. In this final part, we wrap up the project with a custom playlist and finish up the electrical connections.

This tutorial series shows how to build  a fully automated holiday music and light show using a Raspberry Pi, a Micro SD card, and a few other inexpensive parts. Previously, we covered the basics of what you need for this project and showed how to set up your Raspberry Pi. In Part 2, I showed how to connect the components for the light show, and in this final part, we’ll put it all together with music.

Create your holiday playlist

Lightshow Pi supports a text based playlist to play a list of songs automatically. Let’s create a folder called ‘christmas’ inside /home/pi/lightshowpi/music:

cd lightshowpi/music

mkdir christmas

On your local music, move all the music that you want to copy to Raspberry Pi in a directory so that you can keep it synced with Pi and manage it easily. On my system, I created a local directory called ‘best_christmas’ where I kept all my Christmas music. Then, you can rsync all songs from the local directory to the christmas directory of your Pi. Open a new terminal window on the local machine and run the rsync command.

Here is an example where I am syncing music from the ‘best_christmas’ folder on my local machine with ‘christmas’ folder on the Pi.

rsync -avzP --delete /home/swapnil/Music/best_christmas/* pi@10.0.0.33:/home/pi/lightshowpi/music/christmas/

Once the command has run successfully, check on the Pi that music has been transferred:

ls /home/pi/lightshowpi/music/christmas

You will see all the songs there. Now we need to create a playlist of all these songs so that we can automate the playback.

python lightshowpi/tools/playlist_generator.py

The command will ask you to provide it with path of the music folder. In my case it was:

/home/pi/lightshowpi/music/christmas/

Once the playlist is successfully created, verify it:

cat /home/pi/lightshowpi/music/christmas/.playlist

Now it’s time to edit the config file to inform it about the new playlist:

nano /home/pi/lightshowpi/config/defaults.cfg

Scroll down to the [lightshow] section, comment the default playlist path and add a new one to reflect our music directory. It should look like this:

#playlist_path = $SYNCHRONIZED_LIGHTS_HOME/music/sample/.playlist

playlist_path = $SYNCHRONIZED_LIGHTS_HOME/music/christmas/.playlist

Save and close the file with Ctrl+x.

Now test if the new playlist is working:


sudo python /home/pi/lightshowpi/py/synchronized_lights.py --playlist=/home/pi/lightshowpi/music/christmas/.playlist 

You should hear music synced with lights. If yes, congrats. You have working lights. Now let’s automate it.

Plug and play

We are going to use cron jobs to start the playback script at system boot so that it becomes a plug and play device, no need to log into ssh to start the show.

crontab -e

And add following lines at the end:


SYNCHRONIZED_LIGHTS_HOME=/home/pi/lightshowpi

@reboot  $SYNCHRONIZED_LIGHTS_HOME/bin/start_music_and_lights >> $SYNCHRONIZED_LIGHTS_HOME/logs/music_and_lights.play 2>&1 &

Now reboot your Pi to see if it’s working:

sudo reboot

Music and lights should start automatically.

Optional: Add shutdown switch

Raspberry Pi doesn’t come with an on/off switch. Unplugging the power cable will corrupt the SD card and it’s a painful process to open a terminal on PC, ssh into it and then shut it down. Since we will be installing this setup outside, I wanted the ability to to shut down the Pi from the device itself. I found a neat script by Inderpreet Singh that uses a GPIO pin to send a shutdown command to Raspbian.

Create a directory called ‘myscript’ in the home folder of the Pi (of course, you have to ssh into Pi)

mkdir myscript

Then cd to this directory:

cd myscript

And create a file:

nano shutpi.py

Copy the following lines into that empty file:

#!/bin/python
# Simple script for shutting down the raspberry Pi at the press of a button. 
# by Inderpreet Singh 
import RPi.GPIO as GPIO 
import time 
import os  
# Use the Broadcom SOC Pin numbers 
# Setup the Pin with Internal pullups enabled and PIN in reading mode. 
GPIO.setmode(GPIO.BCM) 
GPIO.setup(21, GPIO.IN, pull_up_down = GPIO.PUD_UP)  
# Our function on what to do when the button is pressed 
def Shutdown(channel): 
  os.system("sudo shutdown -h now") 
# Add our function to execute when the button pressed event happens 
GPIO.add_event_detect(21, GPIO.FALLING, callback = Shutdown, bouncetime = 2000)  
# Now wait! 
while 1: 
  time.sleep(1)

Save and close the file. Stay inside the ‘myscript’ directory and set this script to run as root:

sudo python shutpi.py

We now need to set the script to run as system boot:

sudo nano /etc/rc.local

There add the following line between ‘fi’ and ‘exit 0’ line:

sudo python /home/pi/myscript/shutpi.py &

So it looks like this:

# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
printf "My IP address is %s\n" "$_IP"
fi
sudo python /home/pi/myscript/shutpi.py &
exit 0

Shut down your system and connect a push button to GPIO pin 21 and GND using male-female breadboard cable.

uiK4t8N43_Z149NL253LerCF8squ-n8e1poMcfUE

Reboot your system and once music is up and running, push the button; your system should shut down.

Electrical connections

Finally it’s time to connect the relay to the receptacles. In my case I used duplex receptacles to keep the size of the set-up small so that I can house it in a waterproof container.

The receptacles that I linked about have a hot side and are neutral bridged. We need to cut the bridge of hot side so that we can use it independently to plug 8 separate christmas lights. The hot side of the duplex has golder screws whereas neutral side has silver screws. Use a cutter plier to break the bridge on the golden side (you can also check this video ). It should look like this after breaking the bridge:

qBC5_GGUrDUtxPfEl963rSZ7qkJmuYaa4c3GbAGZ

Now we will connect the hot side of the receptacles to output of the relay and neutral to the neutral cable. I have created a diagram to help you with shorting relay and connecting it to receptacles. Please note that in the US, white is hot wire, black is neutral, and green is ground.

9elO0dwW6FGtBHTKykRVJXNgUSqXZoCs0ovaJo_m

You can use a Four Gang Device Box to pack receptacles in it and use a the receptacle wall plate to cover it. I screwed the entire set-up on a wooden sheet and placed it inside waterproof SOCKit box.

You have eight receptacles so you can plug in up to eight 110v lights to the Pi. Just keep in mind that these SSD relays can’t handle too much load so, don’t plug a heavy load into the receptacle. Now you can either put lights on your Christmas tree or decorate your house with those lights.

Credit where due: This tutorial is a result of immense work done by many individuals. Many thanks to Lightshowpi team that created this amazingly cool open source project. I said it earlier and I will say it again, this project would not have been possible without the incredible work of Google software engineer Todd Giles and many others around the the LightShowPi project. That’s the greatness of open source: you learn and benefit from each other. So, special thanks to Tom Enos, Stephen Burning, Benjamin Ellis, and many more. I also want to thank KnC Mc for suggesting how to generate a playlist and thanks to Eric Higdon who actually created the Python script and Ken B for his tips on the automation script. The Lightshowpi community on Google+ is extremely helpful, and if you have any questions, you can freely ask there.

That’s the true open source spirit.

For 5 more fun projects for the Raspberry Pi 3, including a holiday light display and Minecraft Server, download the free E-book today!

Read about other Raspberry Pi projects:

5 Fun Raspberry Pi Projects: Getting Started

How to Build a Minecraft Server with Raspberry Pi 3

Build Your Own Netflix and Pandora With Raspberry Pi 3

Turn Raspberry Pi 3 Into a Powerful Media Player With RasPlex

Click Here!