One of the devices I brought to the Dreamforce Connected Device Lab was a Philips Hue Starter Kit. A friend of mine whipped up a little Ruby script to flash the lights and change colors at random for the Lab, and I got to wondering what it would look like to control the whole thing through Salesforce. Here’s what I did.
TL;DR: Connected a Raspberry Pi to the Hue bridge using Node.js. The same app is connected to a Salesforce app using the Streaming API and is controllable using the Salesforce1 mobile app. Salesforce Package (and raw code). Node.js Code.
The Hue system has a pretty straightforward architecture. The bulbs connect to a bridge and that bridge connects to your network. Once it’s on the network, you can start to control them via the Hue app or the Hue API.
Step 1. The first thing you need to do is choose the device you want to use to control the bridge. This device will have to live on the same physical network as the Hue bridge. I chose a Raspberry Pi. Configuring the Raspberry Pi is pretty easy, these are my standard steps. This includes installing Node.js which isn’t part of the Raspbian distro.
Step 2. Use NPM to install the Hue interface library. “Node-Hue-API” seems to be the winner in this department. Follow the instructions on the Github repo. They’re really good. The basics are:
- Find the bridge and get the IP address (code).
- Create a user on the bridge (code) — note that you’ll need to update the code with your bridge’s IP address.
- Remember everything it just spat out for later.
Step 3. You are now ready to configure Salesforce. There are a few steps to this process. If you don’t have a developer org, it’s time to go get one.
Configure a new connected app in your developer org (detailed instructions from Help).
Start by clicking on Setup at the top of your screen, click on Create on the side, click on Apps, and then click on the New button under Connected Apps.
You will now see the New Connected App screen. Enter the required fields indicated in red. The API Name is an internal name that will fill itself in once you complete the name field. Check the box labeled “Enable OAuth Settings” and add http://localhost:3000/oauth/_callback as your callback URL. Select all of the available OAuth scopes, and click the add button. Click on Save.
Once you click on “Save”, you’ll have a fully configured connected app. You’ll be using the Oauth2 web server flow, which requires your consumer key and your consumer secret.
Note: it may take a few minutes for your new connected app to be fully active. This is normal.
Finally, configure your personal security token. (Lots of information about Salesforce Security Concepts including the security token is available in the API docs.) Start by clicking on your name at the top of the screen, click on My Settings in the drop down menu, then click on Reset My Security Token on the left.
Click on the big green “Reset My Security Token” button. This will result in an email which we’ll use again in a minute.
Now that your Connected App is created, you need to install the unmanaged package which contains the objects, code and UI that helps you control the Hue. You’ll be able to see the code once you’ve installed it, or you can see it on Github. For now, use this link to start the install process.
This will guide you through a series of steps. Since this is a demo and you’re in a demo environment, just accept all the defaults. You’ll click “Continue”, “Next”, “Next” and finally “Install”.
When you have done this successfully, you will land on a screen which declares “Install Complete!”. You must now click on the app selector in the upper right which probably says “Sales”. Then click on the app labeled “LAB Hue Interface”.
Once you do that, you immediately see a screen telling you that setup is not yet complete. Please go ahead and click on the big “Complete Setup” button.
This will bring you to a screen that helps you insert something called a Push Topic. Push Topics are part of the super cool and extensively documented Streaming API. Don’t worry, just click the button.
And then click the link.
Which brings you back to the Hue interface, which now looks like this:
As an aside, you might be thinking, wow, that’s a lot of steps. And you’re right! There is a way to automate this configuration, however, you wouldn’t be able to see the code as easily. So I’ve opted for a few more steps so the example is easier to understand.
Step 4: Finish configuration of your Raspberry Pi. The good news? This is now going to be very easy.
First, use npm to install nforce and faye (“npm install nforce faye“).
Next, get the basic code required to connect Salesforce to the Hue. When you get it on the Raspberry Pi and save it as “hueforce.js”, you’ll want to use nano or another editor to update the field values to yours. You’ll need to add your user info, your OAuth / Connected App info and your Hue info.
And now you can run your app using “node hueforce.js“. Now all those great blue buttons are controlling your Hue!
Step 5. Finally, turn it into a mobile app using Salesforce1 (iTunes, Google Play). In Salesforce, you’ll need to navigate to the tab configurations and edit the “Hue” tab.
In the Hue tab settings, make sure you have “Mobile Ready” checked. Be sure to save your changes.
Now you are ready to add this page to the Mobile Configuration. Click on Setup, select Mobile Administration, and click on Mobile Navigation.
Once that’s done, you simply login to the Salesforce1 app, touch the menu selector in the upper left, then scroll down until you see “Hue” listing under “APPS”. The UI is exactly the same.
I’ve also added a fun trigger that automatically fires the “siren” setting when a new lead comes in.
That’s it. Questions, comments, I’d love to hear them.
Thanks for this post Reid, I have my Hue lights connected to a Raspberry Pi but have been looking for a way to create an App on my phone to connect to it from anywhere. The problem with the Hue API is you cannot control anything if you are not on the same network.
Now my wife can control the lights from anywhere as well through Salesforce1 on her Windows phone.
Is it possible to use workflows so that a event triggers the light?
My current model relies on inserting a record. I suppose you could modify the model so that you could do it on a field update, which is what Workflow allows you to do. You could also create a visual workflow that creates a record. To do this from a trigger you’d want a headless flow, which is in pilot, so you’d have to be part of the pilot program. HTH
Pingback: Wired Triangle » Wired Triangle Soundtracks Dreamforce 14
Pingback: Five Enterprise Internet of Things (IoT) System Architecture Patterns | Reid Carlberg