Holiday Lights – Song-based Lighting

One of the features I wanted to implement in this project, was to have the Christmas tree change colors and animations based on the title of the song that is being played at that moment.

The idea is to stream from an internet radio station, parse the song title and find certain keywords. Depending on the keyword, a combination of colors and animations would be applied.

Music

The play the music, I’m using the Raspberry Pi on which I’m running openHAB. Using MPD and MPC it is possible to play an internet radio stream and depending on the channel, obtain the title and performer.

Installing MPD (Music Player Daemon) and MPC (Music Player Controller) is easy:

Once it is installed, it is possible to add an internet radio station to the playlist.

The next thing to do, is to start playing the item in the playlist:

Once the item is playing, the status can be queried. This should provide information on the song as well. This specific command will be useful to parse the song title from.

OK, with this, the music is taken care of, and the song information is available for further processing … let’s do that now!

Parsing

To process the song title, I’m using a Python script, called from openHAB using the “exec” binding.

The script takes care of parsing the song title from the “mpc status” command’s output. This is the script:

Executing the script manually returns the following:

To do the same, but from openHAB, an entry is added to the items and the sitemap to call the script and bind to its output.

Item:

Sitemap:

The result in the openHAB GUI:
Screen Shot 2015-01-21 at 19.40.27

Checking the openHAB logs, the song titles are properly updated with every new song:

Action

With the song title parsed, it’s time to do something with it and have some pretty colors light up

An openHAB rule is in charge of identifying keywords and changing the colors based on those keywords. The rule could look like this, but can be customised at will:

I gradually added more keywords and matching colors as songs played and I discovered more song titles.
The song titles are logged, and when there is no match, this is also reported. This allows me to check back later and add keywords for those songs as well.

In the logging above, you’ll see some titles return a matching keyword and others don’t. There is also the occasional radio channel name appearing instead of the song title. This is filtered out and skipped.

The tree happily reacts to the changes in keywords and changes colors automatically:
DSCN3180 DSCN3182
DSCN3186 DSCN3188

I’ll be covering the actual build of the tree in my next and final post! Stay tuned!

2 thoughts on “Holiday Lights – Song-based Lighting”

  1. Hi, please help i try do same but it no succefule :(.

    sitemaps:
    Text item=Kris
    Text item=Kris2
    Text item=Kris3

    item:
    String Kris “Kris3 : [%s]” (All) {exec=”<[/usr/bin/python /opt/openhab/configurations/scripts/oi.py:1000:REGEX((.*?))]"}
    String Kris2 "Kris1: [%s]" (All) {exec="<[/usr/bin/python /opt/openhab/configurations/scripts/st.py:1000:REGEX((.*?))]"}
    String Kris3 "Kris5: [%s]" (All) {exec="<[/bin/cat ~/ttt:1000:REGEX((.*?))]"}

    ——————————————————————-
    io.py:
    ——————————————————————-
    #!/usr/bin/python
    import spidev
    import sys

    spi = spidev.SpiDev()
    spi.open(0,0)
    #channel = int(sys.argv[1])
    channel = 0
    adc = spi.xfer2([1,(8+channel)<< 4,0])
    data = ((adc[1]&3) << 8) + adc[2]
    spi.close()
    print "lalala% %d", data
    sys.exit(data)

    ——————————————————————-
    st.py:
    ——————————————————————-
    #!/usr/bin/env python

    print "Labas"

    ——————————————————————-
    ttt:
    ——————————————————————-
    pi@raspberrypi:~ $ cat ttt
    labas

    1. Can you be more specific about which part doesn’t work?

      * is the song title not correctly parsed?
      * is the song title not triggering the rule?
      * is the rule not triggering the animation/color?

Leave a Reply

Your email address will not be published. Required fields are marked *