LIFX Bulb Connector

Go to Git Hub

This article walks through our implementation of a Connector for a LIFX bulb using WebHook endpoint. This sample covers:

  • SmartApp installation and configuration flow.
  • Handling lifecycle payloads from SmartThings Cloud.
  • Integrating with a third-party API (LIFX API, in this case).

Prerequisites

  • Node.js and npn installed (verified with npm version 4.0.5 and Node 7.4.0).
  • ngrok installed to create a secure tunnel for a globally available URL for fast testing.
  • Docker for running Redis. Alternatively, install and run Redismanually.
  • A Samsung account and the SmartThings app (currently available for Android).
  • At least one LIFX light bulb and the LIFX Mobile app (to install the light).
  • Either a LIFX Client ID and Client Secret (obtained from LIFX) or a LIFX personal API Token(generated during Connector installation).

Step 1. Set your OAuth configuration

  1. Download the SmartThings LIFX Connector on GitHub.
  2. If you have a LIFX Client ID and Client Secret (these must be obtained directly from LIFX, which does not allow the general public to register apps), copy them into the appropriate fields in config/default.json. Otherwise, skip to the next step.
  3. If you don’t have a LIFX Client ID and Client Secret, follow the below steps:
    1. Go to the LIFX Cloud site and sign in.
    2. Tap your e-mail ID and click Settings.
    3. Click Generate New Token and enter a meaningful label.
    4. Click Generate. Copy the token to the clipboard for later use (LIFX won’t show it to you again).

Step 2. Create project

  1. Create a folder libfx-demo.
  2. Copy the contents of the SmartThings LIFX Connector into libfx-demo.
  3. Go to that folder and then initialize the project using npm init. Set main js to server.js.
  4. Edit package.json to include the dependencies module as follows:
    "dependencies": {
       "bluebird": "^3.5.0",
       "body-parser": "^1.17.2",
       "config": "^1.26.2",
       "express": "^4.15.4",
       "fs": "0.0.1-security",
       "http-signature": "^1.1.1",
       "request": "^2.81.0",
       "request-promise": "^4.2.1",
       "redis":"^2.8.0",
       "form-urlencoded":"^2.0.4"
    }
    
  5. edit docker-compose.yml as follows:
     version: '2'
     services:
         redis:
             image: redis:3.2.8
             ports:
             - "6379:6379"
    

Step 3. Implement lifecycle handlers and APIs

The files that you added from the sample contain the following API implementations.

  • server.js: Main js containing methods for handling POST request payloads from SmartThings Cloud.
  • lib/api/lifx.js: Methods for communicating to the LIFX cloud. Refer to the LIFX API reference.
  • lib/api/st.js : Methods for communicating to SmartThings Cloud. Refer to the SmartThings API reference.
  • lifecycle/*.* : Each file handles a specific lifecycle request from SmartThings Cloud. The methods in this module will be called from server.js. Refer to the lifecycles guide.
  • local: Modules for db implementation and Logger.

Step 4. Run WebHook service

  1. Install the dependencies: npm install
  2. Bring up Redis with docker-compose up -d
  3. Start the server: npm start.
  4. Start ngrok: ngrok http 3000. Copy the https: URL to your clipboard. This URL is used when registering your SmartApp in Developer Workspace.

What’s next

Now you can integrate your cloud with SmartThings Cloud. Next, create a cloud-to-cloud device.