Previous posts in this project:

 

 

Introduction

 

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:

pi@HolidayLights ~ $ sudo apt-get install mpd mpc







 

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

pi@HolidayLights ~ $ sudo mpc add http://helsinki.radiostreamlive.com/radiosantaclaus_mp3-high







 

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

pi@HolidayLights ~ $ sudo mpc play
http://helsinki.radiostreamlive.com/radiosantaclaus_mp3-high
[playing] #1/1   0:00/0:00 (0%)
volume:  0%   repeat: off   random: off   single: off   consume: off







 

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.

pi@HolidayLights ~ $ sudo mpc status
Radio Santa Claus: Mariah Carey - O Holy Night
[playing] #1/1   0:03/0:00 (0%)
volume:  0%   repeat: off   random: off   single: off   consume: off







 

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:

 

pi@HolidayLights /opt/openhab/addons $ cat /home/pi/xmasradio.py

#!/usr/bin/env python
import subprocess

output = subprocess.check_output("mpc status | grep 'Radio Santa Claus:'", shell=True, stderr=subprocess.STDOUT)
output = output[output.find("-")+2:]

print output






 

Executing the script manually returns the following:

 

pi@HolidayLights ~ $ sudo ./xmasradio.py
All I Want For Christmas Is You

 

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:

String xmasSong "Current song [%s]" (All) {exec="<[/home/pi/xmasradio.py:10000:REGEX((.*?))]"}





 

Sitemap:

        Frame label="Song" {
                Text item=xmasSong
        }





 

The result in the openHAB GUI:

Screen Shot 2015-01-21 at 19.40.27.png

 

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


2015-01-21 18:36:25 - xmasSong state updated to Oh Holy Night
2015-01-21 18:36:38 - xmasSong state updated to Oh Holy Night
2015-01-21 18:36:52 - xmasSong state updated to When My Heart Finds Christmas
2015-01-21 18:37:05 - xmasSong state updated to When My Heart Finds Christmas
2015-01-21 18:37:19 - xmasSong state updated to When My Heart Finds Christmas
2015-01-21 18:37:31 - xmasSong state updated to When My Heart Finds Christmas
2015-01-21 18:37:45 - xmasSong state updated to When My Heart Finds Christmas
2015-01-21 18:37:58 - xmasSong state updated to When My Heart Finds Christmas
2015-01-21 18:38:11 - xmasSong state updated to Visit now: radiosantaclaus.com -
2015-01-21 18:38:31 - xmasSong state updated to When My Heart Finds Christmas
2015-01-21 18:38:49 - xmasSong state updated to When My Heart Finds Christmas
2015-01-21 18:39:06 - xmasSong state updated to When My Heart Finds Christmas
2015-01-21 18:39:21 - xmasSong state updated to When My Heart Finds Christmas
2015-01-21 18:39:40 - xmasSong state updated to When My Heart Finds Christmas
2015-01-21 18:40:02 - xmasSong state updated to When My Heart Finds Christmas
2015-01-21 18:40:16 - xmasSong state updated to When My Heart Finds Christmas
2015-01-21 18:40:32 - xmasSong state updated to When My Heart Finds Christmas
2015-01-21 18:40:47 - xmasSong state updated to When My Heart Finds Christmas
2015-01-21 18:41:00 - xmasSong state updated to When My Heart Finds Christmas
2015-01-21 18:41:14 - xmasSong state updated to All I Want For Christmas (Is My Two Front Teeth)
2015-01-21 18:41:28 - xmasSong state updated to Visit now: radiosantaclaus.com -
2015-01-21 18:41:42 - xmasSong state updated to All I Want For Christmas (Is My Two Front Teeth)
2015-01-21 18:41:55 - xmasSong state updated to All I Want For Christmas (Is My Two Front Teeth)
2015-01-21 18:42:10 - xmasSong state updated to All I Want For Christmas (Is My Two Front Teeth)
2015-01-21 18:42:23 - xmasSong state updated to All I Want For Christmas (Is My Two Front Teeth)
2015-01-21 18:42:37 - xmasSong state updated to All I Want For Christmas (Is My Two Front Teeth)
2015-01-21 18:42:53 - xmasSong state updated to All I Want For Christmas (Is My Two Front Teeth)
2015-01-21 18:43:07 - xmasSong state updated to All I Want For Christmas (Is My Two Front Teeth)
2015-01-21 18:43:20 - xmasSong state updated to All I Want For Christmas (Is My Two Front Teeth)
2015-01-21 18:43:35 - xmasSong state updated to All I Want For Christmas (Is My Two Front Teeth)
2015-01-21 18:43:48 - xmasSong state updated to All I Want For Christmas (Is My Two Front Teeth)
2015-01-21 18:44:03 - xmasSong state updated to Sleigh Ride
2015-01-21 18:44:17 - xmasSong state updated to Sleigh Ride
2015-01-21 18:44:30 - xmasSong state updated to Sleigh Ride
2015-01-21 18:44:44 - xmasSong state updated to Sleigh Ride
2015-01-21 18:44:57 - xmasSong state updated to Visit now: radiosantaclaus.com -
2015-01-21 18:45:10 - xmasSong state updated to Sleigh Ride
2015-01-21 18:45:23 - xmasSong state updated to Sleigh Ride
2015-01-21 18:45:36 - xmasSong state updated to Sleigh Ride
2015-01-21 18:45:51 - xmasSong state updated to Sleigh Ride
2015-01-21 18:46:02 - xmasSong state updated to Merry Christmas For Everyone
2015-01-21 18:46:15 - xmasSong state updated to Merry Christmas For Everyone
2015-01-21 18:46:29 - xmasSong state updated to Merry Christmas For Everyone
2015-01-21 18:46:40 - xmasSong state updated to Merry Christmas For Everyone
2015-01-21 18:46:55 - xmasSong state updated to Merry Christmas For Everyone





 

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:

 

rule "Parse Song Title"
when
        Item xmasSong changed
then
        var song = xmasSong.state.toString.trim
        logInfo("FILE", "Song: " + song)


        if(song.contains("radiosantaclaus")) {
                // do nothing, not a song title
                logInfo("FILE", "Match for 'radiosantaclaus', skip")
        } else if(song.contains("Christmas")) {
                logInfo("FILE", "Match for 'Christmas'")
                sendCommand( TreeLightColor, "255,0,0" )
                sendCommand( TopperLightColor, "0,255,20" )
                sendCommand( TopperLightColor2, "0,0,0" )
        } else if(song.contains("Santa")) {
                logInfo("FILE", "Match for 'Santa'")
                sendCommand( TreeLightColor, "255,0,0" )
                sendCommand( TopperLightColor, "255,255,255" )
                sendCommand( TopperLightColor2, "0,0,0" )
        } else if(song.contains("Snow")) {
                logInfo("FILE","Match for 'Snow'")
                sendCommand( TreeLightColor, "255,255,255" )
                sendCommand( TopperLightColor, "255,255,255" )
                sendCommand( TopperLightColor2, "0,0,0" )
        } else {
                logInfo("FILE","No match")
                sendCommand( TreeLightColor, "0,255,0" )
                sendCommand( TopperLightColor, "255,0,0" )
                sendCommand( TopperLightColor2, "0,0,0" )
        }
end




 

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.

 

2015-01-21 20:33:02.148 [INFO ] [org.openhab.model.script.FILE ] - Song: Silent Night
2015-01-21 20:33:03.653 [INFO ] [org.openhab.model.script.FILE ] - Match for 'Night'
2015-01-21 20:34:15.824 [INFO ] [org.openhab.model.script.FILE ] - Song: Visit now: radiosantaclaus.com -
2015-01-21 20:34:16.206 [INFO ] [org.openhab.model.script.FILE ] - Match for 'radiosantaclaus', skip
2015-01-21 20:34:40.592 [INFO ] [org.openhab.model.script.FILE ] - Song: Silent Night
2015-01-21 20:34:40.780 [INFO ] [org.openhab.model.script.FILE ] - Match for 'Night'
2015-01-21 20:36:44.820 [INFO ] [org.openhab.model.script.FILE ] - Song: Frosty The Snowman
2015-01-21 20:36:45.263 [INFO ] [org.openhab.model.script.FILE ] - Match for 'Snow'
2015-01-21 20:37:48.321 [INFO ] [org.openhab.model.script.FILE ] - Song: Visit now: radiosantaclaus.com -
2015-01-21 20:37:48.350 [INFO ] [org.openhab.model.script.FILE ] - Match for 'radiosantaclaus', skip
2015-01-21 20:38:00.437 [INFO ] [org.openhab.model.script.FILE ] - Song: Frosty The Snowman
2015-01-21 20:38:00.596 [INFO ] [org.openhab.model.script.FILE ] - Match for 'Snow'
2015-01-21 20:41:05.943 [INFO ] [org.openhab.model.script.FILE ] - Song: Visit now: radiosantaclaus.com -
2015-01-21 20:41:05.965 [INFO ] [org.openhab.model.script.FILE ] - Match for 'radiosantaclaus', skip
2015-01-21 20:41:31.507 [INFO ] [org.openhab.model.script.FILE ] - Song: Frosty The Snowman
2015-01-21 20:41:31.541 [INFO ] [org.openhab.model.script.FILE ] - Match for 'Snow'
2015-01-21 20:42:45.844 [INFO ] [org.openhab.model.script.FILE ] - Song: Under The Christmas Tree
2015-01-21 20:42:46.168 [INFO ] [org.openhab.model.script.FILE ] - Match for 'Christmas'




 

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:

DSCN3182.JPGDSCN3180.JPGDSCN3186.JPGDSCN3188.JPG

 

I'll be covering the actual build of the tree in my next and final post! Stay tuned! (here: [Christmas Tree] Internet of Holiday Lights - Finished Build)