Evolution is a beautiful thing. As is creation. Amazingly enough, the two work together hand in hand, side by side.
No… this isn’t a political rant, or a theological ramble. But it is late at night, and I needed a good opening line. Though it is quite true, as I will demonstrate. If you recall from my previous post, Pumpkin Pi, I utilized the idea and lightorgan code provided by Chivalry Timbers to create a simple audio/visual show with my Raspberry Pi.
I have now taken that simple thing a bit further, in the original spirit of the Christmas lightorgan:
First, allow me to provide the code: https://bitbucket.org/chillemstudios/raspberry-pi-lightorgan-christmas (Note that I am continually working on this; I won’t vouch for the sanity of the structure or polish at any given point in time, but I won’t commit anything that doesn’t work.)
As for the hardware itself, I basically followed the steps that Chivalry outlined in his original post. I made a few modifications however. Each of the relays was wired to a full outlet set, thereby giving me two outlets for each switch. I also simplified the wiring scheme somewhat, daisy-chaining the neutral and ground lines which meant that only the hot lines needed to be a home run from the source. Finally, I enclosed the entire thing inside a plastic tupperware-style box so it would be mostly waterproof, and put the Raspberry Pi and USB hub inside a smaller tub nested within.
Because a picture is worth a thousand words, here are a bunch:
I did come across some problems. Apparently the cheap relays I ordered from eBay don’t quite meet the specifications they are supposed to. While the minimum switching voltage they require is 3V, 3 of them refused to switch at all with the 3.3V the Raspberry GPIO outputs, and a fourth would barely flicker. (I have ordered a SainSmart 8-Channel 5V Solid State Relay Module Board to use in place of these SSRs, but I have no idea when it will arrive.) As such, I only have four working channels at the moment.
Let’s take a break and watch one my favorite demonstrations of this thing:
Now, the software side. There are a number of things that need to be setup and configured on the Raspberry to get it working properly. Many of them were covered by Chivalry, but he didn’t cover all of my desires. Mainly I wanted to have a web interface from which I (or someone else) could choose a song to play without having to connect to a shell session. That of course presents a number of difficulties.
First you will need to have a web server and PHP installed and running. There are lots of half decent and some quite decent tutorials around that will walk you through doing this, so I’ll skip past that. Although there is one important part that many leave out:
sudo apt-get install php5-cli
Next, you’ll want to make sure you have a decently long script execution time so as to allow for the actual playing of the songs. Since most songs are 3-5 minutes, you probably want a minimum of 5 minutes, if not more. To do this, edit the file /etc/php5/fpm/php.ini and change the line that contains the parameter max_execution_time to something like this:
max_execution_time = 300
Save the file and restart php5-fpm and nginx. Finally, configure your nginx virtual host and point its web root to the directory where you checked out the code I provided. (If anything in that sentence lost you…sorry, but you have some Googling and reading to do before you can manage any sort of web server on your own.)
It is important to note that I have compiled my copy of lightorgan specifically for use with the PiFace i/o expansion board. If you do not use that board, you should use the original code provided by Chivalry and compile it yourself!
There are two scripts that can be used to initialize everything before any music is played; which one depends entirely upon which type of music you wish to play.
For just a midi file:
But if you intend to play an mp3 (which requires a midi “playing” at the same time that just isn’t output to the audio device) run this:
Both of those scripts will start the lightorgan binary, connect it to timidity, and then display the output. It is quite straight-forward. And it is necessary to run init.sh before you can use any of the web interface portion…therefore you might want to have it run upon boot in some fashion.
Now, assuming you have properly configured your virtual host, you should be able to just browse to your Raspberry’s IP address in a web browser and see a nice blank grey screen (oh…. don’t forget to edit config.ini and put in your own title!) If the grey screen does come up, good! That means everything is running well, and you just need to build the cache file. If you see a screen full of buttons with the names of songs…well, that means you have a cache file already. Either way, it’s good to rebuild it. Simply do that at any time by appending ?refresh=1 to the end of the URL. It will take a few minutes to loop through the music directory and get all of the midi data from the files, so you don’t want to do that each time you load that page.
Once it’s done and you have the buttons, you should be able to just hit one of the buttons and the song will start playing. Ah, but wouldn’t it be nice if things were that easy? Not really, because it is permissions and security that is preventing it from playing right now. When you try to run these scripts from a web browser, it is the www-data user that is executing them. That user is quite limited in what it can do. So there are a few things we need to take care of.
First, we have to add www-data to the audio group so it can play music:
sudo usermod -a -G audio www-data
Second, it is necessary to give the www-data user sudo access to a specific script, play_single.sh. That will allow www-data to run the exec function and the commands inside that are needed to launch the other scripts. Be very careful with anything you add to this script; anything in there can then be executed via the internet.
Edit the file /etc/sudoers and add this line (adjust the path for the location where you have installed your copy:)
www-data ALL=(ALL) NOPASSWD: /home/pi/projects/lightorgan/play_single.sh
Finally, make sure all of the *.sh scripts have permissions of 755 so they can be executed.
NOW you can go back to your web browser and click a button and watch the pretty lights. Isn’t that better?
There is one last tidbit I want to touch on here; playing mp3s instead of midis turns out to be rather simple if you can find mp3s and midis that match up! That’s the hard part of course. I’ve had some luck, though I won’t distribute the mp3s I have for copyright reasons. But I’ve left the scripts I use to launch both of them at the same time in the code repo. The idea is very simple: disconnect the midi output from timidity and it won’t play through the speakers, but it will still make the lightorgan work. At the same time, start the mp3 playing through the speakers. So long as they match in time and sequence, the effect works very well!
Now for the rest of the videos I have recorded: