BaReinhard's Tech Blog

A blog of various technologies, largely based around the Raspberry Pi.

Follow me on GitHub Follow me on Instagram

Lightshowpi, Sync music with your Raspberry Pi

Happy sunday everyone, as you probably guessed my daughter is asleep and that means I have time to write another blog post.

For today’s post it will be on the awesome Lightshowpi, this repo allows you to control lights with your raspberry pi and sync them to music of any kind. Although this is probably most popular in regards to christmas music.

What will I need?

  • Raspberry Pi w/ internet connection
  • Keyboard or SSH
  • SD card
  • Mechanical or Solid State Relay capable of controlling 110V, or any relay board depending on the lights you want to control.
  • Lights you want to control
  • A box to hold everything
  • 3 Prong Outlet Pigtail
  • Romex wiring
  • Outlets (depends on how many strands of lights you want to control)

Preparing your Raspberry Pi

I trust that you already know how to setup your Raspberry Pi with the SD card so I will forgo those details. Next you will need to make sure your distro is updated. I believe that this is compatible with arch linux, raspbian, and possibly ubuntu mate. I have only used raspbian, so use others at your own adventure.

sudo apt-get update -y
sudo apt-get upgrade -y

Next we will clone the repo, but make sure you first have git installed on your raspberry pi:

sudo apt-get install git-core

# Clone the repository to /home/pi/lightshowpi
cd ~
git clone https://bitbucket.org/togiles/lightshowpi.git

# Grab the stable branch
cd lightshowpi
git fetch && git checkout stable

If this doesn’t work due to permissions please use sudo infront of each of the commands.

Next we will need to install it, make sure you are in /home/pi/lightshowpi

cd /home/pi/lightshowpi
sudo ./install
sudo reboot

Thats it, its installed and working. Now comes the fun part, cusomization and adding music. Lightshowpi already comes with a few christmas songs, my favorite being an 8 bit remake of carol of the bells.

To find the config file it will be in the /home/pi/lightshowpi/config directory.

Unlike a few of my other builds this project has a multitude of different config options and ways of using it, so I will leave some of it up to you.

I also wrote an add on to this project that has made it into the latest branch of lightshowpi, which allows you to tweet the current song to twitter. This functionality, requires metadata to be present on the songs, some songs don’t have them but each song in the stable repo will have it so you won’t need to worry about them. If you need to add metadata to some songs you downloaded you can do so with the use of mp3info. The following will add Song title and Author and Genre to song.mp3, this was taken directly from the man page for mp3info

mp3info -t "Song Title" -a Author -g "Rock & Roll" song.mp3

As far as using twitter to tweet you playlist you can follow these directions taken from the top of the tweet.py file within the /home/pi/lightshowpi/py directory:


Author : Brett Reinhard

To setup automated tweeting, you will need a twitter account with a verified phone number.
Go to the apps.twitter.com, login to your twitter account. 
Create a new app with the following information: 
 Name (of the app)
 Description
 A url in http(s)://domain.com 
Agree to the terms. 
Click on the Keys and Access Tokens tab
Consumer Key, and Consumer Secret Key are automatically generated. 
Under 'Your Access Token', select generate access tokens. 
This will generate Access Token, and Access Token Secret.
You now have access to your newly generated Access Token, and Access Token Secret.
Fill the tokens (KEYS) and secrets in the locations below where 'USER INPUTS DATA' 
CONSUMER_KEY = 'consumer_token' 
CONSUMER_SECRET = 'consumer_token_secret' 
ACCESS_KEY = 'access_token' 
ACCESS_SECRET = 'access_token_secret'

Ensure that the keys are enclosed by single quotes.

Now lets tackle the config file: We will, for sake of simplicity be using a very generic setup of the defaults.cfg located within /home/pi/lightshowpi/config directory.

Within the defaults.cfg there is a section ` [Hardware] ` and within the section you will find a line that reads gpio_pins = 0,1,2,3,4,5,6,7 This means that it is using GPIO pins 0-7 to control the relay board. The wiring diagram is included in the defaults.cfg but can alternatively be found a http://pinout.xyz/pinout/wiringpi . If using pinout.xyz WiringPi 0 is the same as GPIO 0.

By default your lightshowpi will be setup in playlistmode, in the section ` [lightshow] ` you will find a line playlist_path this is where the program will be drawing its information from to play the songs. You will notice that it is looking at the file .playlist the . or playlist are no necessary but allow for it to be hidden and easier to understand what it is. Inside the file you will see the following format:

Carol Of The Bells	$SYNCHRONIZED_LIGHTS_HOME/music/sample/evilwezil_carol-of-the-bells.mp3
Deck The Halls	$SYNCHRONIZED_LIGHTS_HOME/music/sample/ovenrake_deck-the-halls.mp3

When adding music to your program be sure to add new lines to your playlist file, A description of the song aka title with a tab then the file path to the song. I haven’t tried uploading songs anywhere else other than the music file, but if you do make sure that whatever user runs lightshowpi has access to the location of the files.

With all the computer stuff setup you will need to attach the lights and wiring: https://s-media-cache-ak0.pinimg.com/736x/b0/a0/30/b0a030ba7d9c5e67fd6295b42f604eb2.jpg

The above image is for a 2 Solid State Relay, but with larger boards it is functionally the same, it just has more leads. It is also important to note that mechanical boards are inverted in comparison to solid state relay boards and will require a slightly different setup in the config files, active_low_mode = no will need to be changed to active_low_mode = yes.

Now with the hardware setup like the picture you can test the hardware.

cd $SYNCHRONIZED_LIGHTS_HOME/py
sudo python hardware_controller.py --state=on

this should either turn the lights on or do nothing, before thinking its broken try:

sudo python hardware_controller.py --state=off

If they turn on after this, it is likely due to active_low_mode = depending on your board you will need to change this. If that still does not work it is likely due to which pin you chose to use for the ground from the Raspberry pi to the relay board. I have found best results with ground pin 4, for whatever reason other ground pins don’t seem to work well with wiringPi.

And with that we are all done and you can start decorating your yard with lights and control them with music.

If you have any other issues or questions, please feel free to message me on instagram or leave a comment below

Enjoy! -BaReinhard