iBeacons and Hue Lights Tutorial Part 2: Implementing a Node.JS Server

A small band of Gorillas spent 3 weeks creating an iBeacons and Hue Lights project using different technologies. In our first post, we introduced our project and a tutorial on implementing Beacon SDK and integrating with iOS devices through Bluetooth. In this post, we will discuss how to manage and handle a Philips Hue Bridge with Hue Lights by implementing a Node.JS Server.   

  • Node.JS Server vs.  Native iOS Solution
  • Back End Architecture
  • How To Setup the Node.JS Server and Third Party Libraries to Manage the Lights
  • How To Setup a New User In the Philips Hue Bridge
  • How To Manage the Lights – Sending Messages to the Bridge
  • Conclusions

Philips Hue Lights

Philips provides great documentation on how to manage lights and other devices through APIs using different solutions like Native iOS, Native Android, Node.JS, Arduino, C, C#, Java, Ruby, and others. Visit Philips Hue developers site for more information about the Hue developer program.

Why a Node.JS Server Over Philips Hue iOS SDK?

Philips provides an SDK in Objective-C for iOS, but the requirements of our project needed a different perspective. Node.JS presented an easy solution to our problem:

  1. Project requirements: a transparent connection and management of the lights.
  2. A Node.JS Server provides a single connection to the lights for all iOS App users.
  3. A Nose.JS Server makes it easier to manage the Bridge.
  4. The Philips Hue Bridge requires the user login in the Bridge in a pairing process for security reasons. We don’t want unauthorized users sending messages to the bridge, so we created a single connection to fix that login/pairing issue.

Back End Architecture

iBeacons Hue Lights Back End

Using the Express Package, we created an endpoint on the server to interact with our iOS App. With the Node.JS libraries for Philips Hue, we can manage the messages sent to the Hue Bridge.

How To Setup the Node.JS Server and Third Party Libraries to Manage the Lights

Install the Node.JS Server (click here for instructions), then install an npm site. Learn how to install the modules here

To setup the out Node.JS server we will need 4 different modules:

  • Huejay: Huejay is a Node.JS client library for the Philips Hue home lighting system.
  • Express: Express is a Web Framework that is really easy to use.
  • Body-parser: The body-parser object exposes various factories to create middlewares. All middlewares will populate the req.body property with the parsed body when either the Content-Type request header matches the type option, there is an empty object ({}), the Content-Type was not matched, or an error occurred.
  • Hue-module: This is a Node.JS client library for the Philips Hue.

Open the terminal App and go the folder where you will create the project server files.

 

iBeacons Hue Lights Server

First, create a Package.JSON file to keep modules in order. In the terminal, create that file through Vim, Atom, or another editor. The package file needs a name and version inside a JSON structure.

Package.json iBeacons Hue Lights

Save the file. Now we are going to add our modules to the package file using the command npm install –save {module name}

npm install –save huejay

npm install –save hue-module

npm install –save express

npm install –save body-parser

iBeacons Hue Lights npm install

If you open the package.JSON file, you will see all of our dependencies.

Now we are going to setup a User for the Philips Hue Bridge.

How To Setup a New User In the Philips Hue Bridge

We are going to use a Hue-module library to setup a User in the bridge and get a token username – this token is necessary for all the transactions to the Bridge.

Connect the Bridge and the lights as instructed in the Philips documentation of the product.

Before creating a User, we need to know the IP address of the Bridge. For this process, we are going to use the HueJay Module.

1. Create a Node.JS file using Atom or another editor. For our example, I will call the file: HueBridgeUser.JS

2. In the new file, create a Huejay constance.

3. Using the Huejay instance, call the discover() function as shown in the code below. We are going to obtain the IP address of the Bridge through the console.

4. Run the code and copy the IP address.

  • To run the services, go to the terminal and type: node {and the name of the file with the extension}.
  • For our case, this is node HueBridgeUser.JS. 

5. Create a new variable to hold the IP address and create a constance for the Hue-module library.

6. Copy the code below, run the code, and copy the UserName Token printed in the console.

7. That function loads the Bridge and gets a username from the bridge, and printed in the console.

How To Manage the Lights – Sending Messages to the Bridge

For this part, we need the Token of the username we got from the hue.getusername() function.

We are going to create functions for changing the states of the Lamp like turning it on and off, changing color, or setting an Alert effect.

Get the light’s information:

To get access to the lights (or others devices connected to the bridge) we need to create an instance of HueJay Client.

  1. Create a new JS File. We are going to call this file: ibeacon-huelights-service.JS.
  2. Create an instance of Huejay.Client. Set the IP address, the port (80 by default), the username (token), and the timeout.

3. Using the Huejay client.bridge.get method, we will return a Bridge object, which can be used for reading and saving configuration. The code below prints the Bridge information. Copy the code and run the file.

4. Check the console and look at all the information.

Check this information. 

Now we are getting all the lights connected to the Bridge.

Huejay client.lights.getAll will return a list of all registered lights on the bridge. Like client.lights.getNew, the result from the completed Promise will be an array of Light objects.

Copy the code below in our file and run the service again.

The console will print all the information about the lights registered in the Bridge.

Playing with the lights

After retrieving a Light object through previous commands, you can configure the light and save its attributes and states. This allows you to change a light name, color, effect, and so on. You can set various properties on a Light object, and save them via client.lights.save.

The client will only send updated values to the Philips Hue bridge. Sending all configurable attributes and states can affect bridge and light performance.

To save a Light, pass a Light object to client.lights.save. The light is returned after saving for convenient chaining.

Thanks to the function client.lights.getall(), we obtain the light’s ID that is registered in the bridge. Using the same client, we could send messages to a specific light and change the attributes.

In the next example code, the property light.on is set to On. That will be “turn on the light” and if we set it to “off” then it will do the opposite.

Next, we need to save our light property in line:

Change the Brightness and Color of the Lamp. Play with the Brightness properties.

Notice that color is changed through the light.hue property, but this property doesn’t accept RBG, or other color designation. The documentation from Philips says: The hue to set the light color, representing a color from a Range: 0 – 65535 (which represents 0-360 degrees). Find an explanation here

That doesn’t explain much, but this is okay for the moment. There is another way to set the color that will be explained in future steps.

Now, we are going to create functions to change the lamp’s state.

turnOnLight Function:  Receive 2 parameters; the hueColor and the brightness level.

setLightWithPulseEffect Function: This function is set to the Alert property. The “lselect” value will create a Pulse Effect in the lamp for 30 seconds.

stopPulseEffect function: Stop the Pulse Effect in the lamp by setting the Alert property value to “none” and assign a color to the lamp.

turnOffLight function: Turn off the lamp by assigning the value: false, to the On property. 

Adding the Endpoints to our Node.JS Server

Express.JS

Through Express.JS, we are going to create endpoints receiving JSON formatted parameters to communicate with our HueLights functionality.

1. We need to setup express and bodyParser into our ibeacon-huelights-service.JS file.

2. Create an instance of express and set what kind of body it will use.

3. Start to create the endpoints.

Always check the parameters. If we are missing something, the response status will be 400.  This will send back 400 to represent a bad request.

If all the parameters are fine, we need to identify the turnOnLight function as sending the right parameters.

Post turnOnLight

Check the post to our server using postMan.

POST TURNONLIGHT

If everything is okay, we will do the same with the other functions. Create endPoints to call it. 

Post turnOnLightWithPulseEffect

POST stopPulseEffectWithColor

POST turnOffLight

Conclusion

Implementing a Node.JS Server makes interaction with the Philips Hue Bridge simple and easy.

A Node.JS Server fixes our problem with users logging into the Bridge using a single sign-in to the Bridge.

Setting up an express server and all the other modules may have more steps than using the Native Philips Hue SDK. However, it is not complex and is very easy to implement.

We increased the security in the app because only the server can sign into the Bridge rather than individual Users.

In the next part of this series, we will demonstrate Hue Lights integration for Node.JS and our iOS App. 

 

Watch the video to see our project in action!

This video was created by the iBeacons – Hue Lights team to share at an internal Lunch & Learn with other curious Gorillas.

Subscribe to our Blog

Jorge Mendoza
Jorge Mendoza
Jorge, a former Gorilla, is a Senior iOS Developer. He has over 10 years of experience in software development and has focused on iOS development for the past 6. Jorge received his Bachelor's in Computer Science from the University of Costa Rica (UCR).

Deliver off-the-chart results.

WordPress Video Lightbox Plugin