iBeacons And Hue Lights Tutorial Part 3: Integrating Node.JS with iOS

A small band of Gorillas created a three-week iBeacons and Hue Lights project using different technologies. In our first post, we introduced our project and provided a tutorial on implementing Beacon SDK and integrating iOS devices through Bluetooth. In the second post, we discussed how to implement a Node.JS Server to manage and handle a Philips Hue Bridge with Hue Lights.

In this post, we demonstrate the Hue Lights integration for Node.JS with our iOS App. First, we’ll discuss the communication between the Node.JS Server and our iOS App through implementing an Alamofire framework and creating a RestAPIClient. Then we share how to implement a Lights Manager to manage the services. 

RestAPIClient

In the iOS App we need to implement Alamofire for our RestAPIClient service. Through RestAPIClient we send all the requests to the Node.JS Server.

1. AlamoFire: Alamofire is an HTTP networking library written in Swift.

2. Add Alamofire to the CocoaPods file in the project.

cocoaPods

3. Add the Alamofire library to the Cocoapods file.

Alamofire

4. Create a new file, RestAPIClient.swift, and implement the code below.

This class handles all server requests and responses.

Hue Lights Manager

This Swift class manages all the functions for manipulating the states of the lamp.

The RestAPIClient variable type allows us to access the request methods of our RestAPIClient class, and we instance this variable in the Init of the class.

How to Calculate the Color

Working with the hue color is not easy. A hue color is obtained from a UIColor variable or a RGB scheme, so after a little research, I discovered a simple solution provided by Philips: a simple function that takes a UIColor as parameter and returns a CGPoint. But we need to change our endpoints and functions on the server side to work with the CGPoints.

1. Add the Philips Hue SDK to our iOS Project.

  • Download the Philips SDK here
  • Copy the SDK file into the project folder and add it to the project.
  • The original SDK works in Objective C, so we need to add the SDK into ObjCBridge file.
  • Now we can use the function.

2. Create a function in the HueLightsManager.swift called: func calculateColor(color:UIColor) -> CGPoint

3. Add a private static let lampModel = “LCT014″ constance to the file. This is necessary to calculate the CGPoint color.

4. In the function call the PHUtilities.calculateXY from the Philips Hue SDK, passing in the parameters.

5. Now we need to change our functions and endpoints in the JS Server node.

  • Stop the service.
  • Modify the endpoints changing the hueColor parameter to xColor and yColor to parameters. Validate the parameters, add the same parameters to the functions and test it.

  • Use Postman to test the changes.

Calculating the Brightness

The Philips Hue SDK brightness scale is from 0 to 255. To make it easier, however, we are going to transfer this scale to 0-100 and pass the right values to the server.

Managing the Light States

Copy the code below and implement it in the file HueLightsManager.swift

    

   

Creating an Interface for Changing the Colors of the Lamp

iBeacons Hue Lights

Create the interface and set the labels and the controls. For the slider with the Hue colors, I recommend using SwiftHUEColorPicker. You could add CocoaPods to the project, or a single file to the project.

 

Add a Swift file as a view controller.

Set the delegate to SwiftHUEColorPicker.Delegate = self and implement the delegate methods:

Our implementation code will look like this.

Connect the view controller to your storyboard.

connect view controller

Set the segue from the Yes button to our new ViewController and set the presentation as Modally.

Run the App and test the Request and Response.

Conclusions

  • Using the Alamofire Framework is simpler than creating a request through NSMutableURLRequest and URLSession and all the delegates.
  • By creating a RestAPIClient we centralize all the requests and responses, making it easier for the LightManager Service to send all the requests and responses to the Node.JS Server.
  • Implementing CocoaPods helps us manage all third party frameworks like Alamofire. Just install and use; no manual copying of files.

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