Blog

Phillips Hue on Wot.io

Feb 2015/ Posted By: wotio team

<p>Philips really dominated the connected lighting market with the Hue system of bulbs. They give you a Device Management Platform (the "bridge") with an open API, which all the bulbs in your home - or business - connect to. While there are already tons of apps compatible with the Hue system, and there's IFTTT support for some simple triggers, how do you handle more advanced applications? What if you needed to hook up the bulbs to a light sensor to build an automated greenhouse? Or wanted to have them strobe if someone trips your alarm system? Sure, you could build the entire system manually, but that takes too long. That's what Wot.io is for.</p>
<h1 id="whywot">Why Wot?</h1>
<p>What if the hue, light sensors, door alarms, and just about every other device was already pre-integrated into a single system? What if the only thing needed to get them working together was a short script? What if you could then tack on other services from 3rd parties (maybe SQL storage, or tweeting specific events) with little extra effort? And someone else did all of the hosting and scaling work for you? Well, then you'd be using Wot. So in this post I'll be showing you how easy it is to integrate the Philips Hue system into Wotio, and how Wotio can be used to leverage it.</p>
<h1 id="theintegration">The integration</h1>
<p>When you buy the Hue system, you get a bridge to install on your local network that connects and controls all of the Hue Bulbs from a central location via a RESTful API. This is Philips' Device Management Platform - it updates the light bulbs' firmware, pushes new 'scenes' to them, and sends them commands. We will be integrating this API with Wotio to provide both a data stream and a control endpoint.</p>
<h4 id="prerequisites">Prerequisites</h4>
<p>To get started, you will need the following:</p>
<ul>
<li>A Philips Hue bridge and light bulbs</li>
<li>A computer, virtual machine, or cloud instance with Python 2.7 installed and a network connection to the Hue bridge</li>
<li>A Wotio access token and path</li>
</ul>
<h4 id="theintegration">The integration</h4>
<p>For this integration, we'll be using Python. We will need two libraries. First, there's <a href="https://github.com/studioimaginaire/phue">studioimaginaire/phue</a>, which gives us a convenient way to access the bridge API Second, there's <a href="https://github.com/wotio/wotwrapper">wotio/wotwrapper</a>. To install them, run:</p>
<pre><code>sudo pip install phue
sudo pip install wotwrapper
</code></pre>
<p>Then we can write our connector. The code is this simple:</p>
<pre><code>#!/usr/bin/env python
# hue.py
# A module for connecting the philips hue platform to the Wot environment
import sys, wotwrapper
from phue import Bridge

# Initialize the bridge connection with IP address
b = Bridge(sys.argv[1])

# If the app is not registered and the button is not pressed, press the button and call connect() (this only needs to be run a single time)
b.connect()

# Wrap it onto Wotio
# connect(&lt;wotio path&gt;, &lt;module name&gt;, &lt;object to wrap&gt;, &lt;function to retrieve data&gt;, &lt;delay between data publishes&gt;)
wotwrapper.connect(sys.argv[2], 'phue', b, b.get_api, 10)
</code></pre>
<p>Save that as <code>hue.py</code>, then press the link button on the bridge and run:</p>
<pre><code>hue.py &lt;ip address of bridge&gt; &lt;visit http://wot.io for your wot.io path&gt;
</code></pre>
<p>Where <code>&lt;UUID&gt;</code> is any identifier that you specify (try to make it unique). That's it. It took only five lines of actual code! We're initializing the <a href="https://github.com/studioimaginaire/phue">studioimaginaire/phue</a> library, and passing it the bridge's IP address over the command line. We then wrap that library onto the Wot bus using <a href="https://github.com/WoTio/wotwrapper">wotio/wotwrapper</a>.</p>
<p>So what did this wrapper actually do? Two things:</p>
<ol>
<li>It uses the <code>b.get_api</code> function to pump the current state of the system onto the bus (as you probably guessed)</li>
<li>It wraps the methods of the <code>Bridge</code> class into Wotio-style JSON calls:</li>
</ol>
<pre><code>JSON Message received | Function Called
------------------------------------------------------------------
["phue","get_api"] | b.get_api()
["phue","set_light",1,"bri",254] | b.set_light(1, 'bri', 254)
</code></pre>
<p>For the full documentation and code of wotwrapper, visit <a href="https://github.com/WoTio/wotwrapper">the wotwrapper github page</a>. For all of the API calls, visit <a href="https://github.com/studioimaginaire/phue">the phue github page</a>. And to get a copy of this integration's code, visit <a href="https://github.com/WoTio/wot-phue">wot-phue</a>.</p>
<h4 id="thewotside">The Wot side</h4>
<p>Now that we've got it wrapped, let's try and see the data stream on Wotio, and control a few lights while we're at it.</p>
<p>We can connect to Wotio using several different protocols, but for our purposes we're going to use websockets. Why? Because we can use any number of mechanisms to view the data streams we're generating in real-time. If you don't have a websocket shell installed, you can try <a href="https://github.com/progrium/wssh">progrium/wssh</a>. It's also based on Python 2.7, so if you've gotten this far in the integration it should work for you perfectly fine. So let's use it to open a websocket connection to our data stream on wotio:</p>
<pre><code>wssh &lt;visit http://wot.io for your wot.io path&gt;
</code></pre>
<p>And you should see the data stream coming from your light bulbs! So why don't we try controlling them too. All you have to do now is type a JSON message and hit enter, and you'll change the state of the bulbs:</p>
<pre><code>["phue","set_light",1,"bri",254]
["phue","set_light",2,"bri",50]
["phue","set_light",1,"on",false]
</code></pre>
<p>Notice how each time you send one of these commands, you not only change the lights in your house but the data coming out of the bridge as well? That's what gives Wot it's power. There are lots of pre-integrated services that can act on this data. You can even integrate another device (just as easily, mind you) and Wotio gives you the power to link them together so that they can communicate no matter where they are in the world; all they need is an internet connection. And all of this is production-ready, with no infrastructure for you to maintain, should you want to take your devices (or services) to market.</p>
<h1 id="learnmore">Learn more</h1>
<p>If you'd like to see a sample of the power you can get out of Wotio, visit the <a href="http://wot.io/about-wot-io/contact-us/">wot.io website</a> and we'll call you to schedule a meeting.</p>