Phillips Hue on Wot.io
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.
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.
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.
To get started, you will need the following:
- A Philips Hue bridge and light bulbs
- A computer, virtual machine, or cloud instance with Python 2.7 installed and a network connection to the Hue bridge
- A Wotio access token and path
For this integration, we'll be using Python. We will need two libraries. First, there's studioimaginaire/phue, which gives us a convenient way to access the bridge API Second, there's wotio/wotwrapper. To install them, run:
sudo pip install phue sudo pip install wotwrapper
Then we can write our connector. The code is this simple:
#!/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) # 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(<wotio path>, <module name>, <object to wrap>, <function to retrieve data>, <delay between data publishes>) wotwrapper.connect(sys.argv, 'phue', b, b.get_api, 10)
Save that as
hue.py, then press the link button on the bridge and run:
hue.py <ip address of bridge> <visit http://wot.io for your wot.io path>
<UUID> 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 studioimaginaire/phue library, and passing it the bridge's IP address over the command line. We then wrap that library onto the Wot bus using wotio/wotwrapper.
So what did this wrapper actually do? Two things:
- It uses the
b.get_apifunction to pump the current state of the system onto the bus (as you probably guessed)
- It wraps the methods of the
Bridgeclass into Wotio-style JSON calls:
JSON Message received | Function Called ------------------------------------------------------------------ ["phue","get_api"] | b.get_api() ["phue","set_light",1,"bri",254] | b.set_light(1, 'bri', 254)
For the full documentation and code of wotwrapper, visit the wotwrapper github page. For all of the API calls, visit the phue github page. And to get a copy of this integration's code, visit wot-phue.
The Wot side
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.
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 progrium/wssh. 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:
wssh <visit http://wot.io for your wot.io path>
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:
["phue","set_light",1,"bri",254] ["phue","set_light",2,"bri",50] ["phue","set_light",1,"on",false]
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.
If you'd like to see a sample of the power you can get out of Wotio, visit the wot.io website and we'll call you to schedule a meeting.