element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Blog [Christmas Tree] Internet of Holiday Lights - Song-based Lighting
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: fvan
  • Date Created: 21 Jan 2015 9:05 PM Date Created
  • Views 710 views
  • Likes 2 likes
  • Comments 2 comments
  • infineon
  • mqtt
  • yun
  • openhab
  • classic_modern_tree
  • iot_holidaylights
  • led
  • arduino
Related
Recommended

[Christmas Tree] Internet of Holiday Lights - Song-based Lighting

fvan
fvan
21 Jan 2015

Previous posts in this project:

  • [Christmas Tree] Internet of Holiday Lights - Project Description
  • [Christmas Tree] Internet of Holiday Lights - Getting Started
  • [Christmas Tree] Internet of Holiday Lights - Control from openHAB
  • [Christmas Tree] Internet of Holiday Lights - Special Feature

 

  • Introduction
  • Music
  • Parsing
  • Action

 

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:

image

 

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 image

 

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:

imageimageimageimage

 

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)

  • Sign in to reply

Top Comments

  • mcb1
    mcb1 over 10 years ago +2
    Very clever. Well done Mark
  • fvan
    fvan over 10 years ago in reply to mcb1 +1
    Thanks Mark!
  • fvan
    fvan over 10 years ago in reply to mcb1

    Thanks Mark!

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • mcb1
    mcb1 over 10 years ago

    Very clever.

     

    Well done

    Mark

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube