Software apps and online services
I have several LIFX color light bulbs. I originally purchased one for another Hackster project and I liked it so much I purchased a few more. I currently have one in each of the two lamps next to my bed. I normally control them using the excellent application provided by LIFX on my iPhone and Windows 10 tablet.
I find, however, that when I come into the room I sometimes just want to turn the lights on (or off when I leave) without needing to find my phone.
When I saw that Hackster.io was giving away some AWS IoT Buttons I thought this was the perfect opportunity to develop an easy switch for the lights. Thank you Hackster.io!
The LIFX color, which can be used as an everyday bulb or for special purposes, is an LED based RGB bulb that supports multiple colors and brilliant whites. What originally drew me to these was a demonstration of the API at Microsoft Build using Windows 10 IoT Core as well as the vast amount of integration support with other services.
The light allows the color or level of white to be set manually using the mobile application and also supports the creating of scenes. A scene is a selection of one or more lights and the color of each light. When the scene is activated each light in the scene is set to the prescribed color.
The LIFX API provides a rich set of methods for controlling the lights, including the ability to activate a scene. In my original idea post for this project I stated I would have the button behave like a three-way switch that would cycle through settings each time it was pushed. With little knowledge of the AWS IoT Button, this design was based on the assumption that the button had a single click event.
I have decided to slightly alter this design based on precise knowledge of how the button works, as well as some experience with how it interacts with the LIFX API.
AWS IoT Button
If you have any Dash buttons from Amazon, then you are very familiar with the AWS IoT Button. I have several Dash buttons and use them quite a bit. They appear to be a simple device from the outside, but, in fact, are very powerful.
When configured, the AWS IoT Button connects to the Amazon cloud via a wireless network and sends events to the Amazon cloud. From there you can respond to those events in numerous ways.
The button supports three events: Single Click, Double Click and Long Click. As I stated earlier, I slightly changed my design goal based on this.
The design goal for this project is to assign a LIFX Scene to each of the event types. When the button is "clicked", the Single Click scene will be activated (lights on). When the button is clicked a second time, the lights are turned off.
The Windows 10 application is used to map each event type to a scene. The list of scenes are obtained by making a LIFX API call. The selected scene for each event type is stored in an AWS DynamoDB where the Windows 10 application can both read and write data to and from and the AWS Lambda function can read the configuration.
Sounds simple? Sometimes the simplest ideas are the best. I found that after I developed this code I loved the button and use it all the time. I probably now control the lights with the button more than I do with the mobile application.Overview
STEP 0: Getting Ready
There a few steps involved in getting this project up and running:
- One or more LIFX light bulbs setup and ready to go. This project will work with all models including the white only version.
- AWS IoT Button
- Visual Studio 2015 Community
- Windows 10 computer
STEP 1: Setup your LIFX Light
Setting up the LIFX light is very easy. In general, it will broadcast itself as a wireless network to which you connect. Once connected the mobile application or Windows 10 application) will detect the light and walk you through the setup. Any time you wish to change the wireless network to which it is connected, you just reset the bulb and walk through the setup again. Here are some great links to get you started:
- Setup a New Bulb for the First Time
- Hardware Reset Your Original LIFX Bulb
- Hardware Reset Your 2015 LIFX bulbs
- More Information...
Below are the steps to reset the LIFX Original and set it up again.
Part 2.1: Setup/Reset the light
NOTE: If you have a new light click the link above labeled 'Setup a New Bulb for the First Time'. If you have already setup your light you can skip these steps.
This video demonstrates how to reset a LIFX Original. If you have a different product, use the links in the previous section to learn how to reset your light.
Part 2.2: Add the Light
Click through the images below and follow the steps.
STEP 2: Create your LIFX Scenes
The Windows 10 application included in this project requires one or more scenes on your LIFX light(s) to be associated with the events on the AWS IoT Button. Remember, the button has three events: single click, double click and long click. In the following video I will demonstrated how to create the three scenes using an iPhone 6, to associate with each of these events. I will name the scenes 'Single Click', 'Double Click' and 'Long Click'. Keep in mind you can define your themes any way you like and give them unique names.
STEP 3: Create LIFX API Token
The LIFX API requires you to create a key for the application to use in order to access your lights. To create this key go to cloud.lifx.com and follow these steps:
- Step 3.1: Login using the account you created when you set up your first LIFX light. This is the same account you use when logging in to the mobile application.
- Step 3.2: In the upper right corner of the screen, click your email address and then choose Settings from the drop down menu.
- Step 3.3: Click the Generate New Token button.
- Step 3.4: Give the token a name (in this case I called mine 'AWS IoT Button'), then click the Generate button.
- Step 3.5: Copy the new token and save it somewhere secure. You will use this token later in the project. You may also use this token with other applications. It is best to generate a new token for each application. That way if a token is compromised, you can revoke it without updating all of your applications or compromising multiple applications.
- Step 3.6: After you have copied the token and placed it in a secure location, click the menu again and choose Settings.
Here are screen shots showing the steps outlined above.
STEP 4: Setup your AWS IoT Button
The AWS IoT Buttons works just like the LIFX light. In fact, they are based off the same chip. The button will connect to your home wireless network and communicate directly with the AWS IoT.
For simplicity, pay attention to the region you use when setting up your objects in AWS and use the same region for all of your objects. I have done all of my setup in US-EAST-1 (N. Virginia). If you use a different region, then the code for the Windows 10 application will need to be updated.
Part 4.1 - Connect the Button to your Wireless Network
Watch the video below for a demonstration on how to connect your AWS IoT Button to your wireless network.
Part 4.2 - Activate the Button
This is the most important step in the process and requires you to follow the instructions provided by Amazon very carefully. It is not a difficult process, just one you want to ensure you get right.
To activate your button, go to https://aws.amazon.com/iot/button/ and click the Go to Button Activation Wizard button in the upper right corner.
At this point you will need an Amazon AWS account which you will need to setup if you do not already have one.
During the setup you will be given the option to create an Email or Text Message. Follow this setup through all the way to completion and ensure your button works. You will be sure it works when you receive a text message or email each time the button is pressed.
STEP 5: Create a Group in IAM
A group is created to control policies for multiple user accounts. It is possible to create a user and assign policies directly, but creating groups, in my opinion, is slightly more structured.
- Step 5.1: Log into the Amazon AWS Console with your account.
- Step 5.2: Select Identity and Access Management in the Security & Identity section
- Step 5.3: Select Groups on the left menu.
- Step 5.4: Click the Create New Group button.
- Step 5.5: For the group name, enter LIFX_ControlPanel and click Next Step.
- Step 5.6: In the Attach Policy step select AmazonDynamoDBFullAccess and click Next Step.
- Step 5.7: Click the Create Group button.
STEP 6: Create a User in IAM
Making secure REST or Query protocol requests to AWS service APIs requires access keys. To get access keys a user must be created.
- Step 6.1: Log into the Amazon AWS Console with your account.
- Step 6.2: Select Identity and Access Management in the Security & Identity section
- Step 6.3: Select Users on the left menu.
- Step 6.4: Click the Create New Users button.
- Step 6.5: Enter LIFX_ControlPanel in the first user name box.
- Step 6.6: Check the box labeled Generate an access key for each user.
- Step 6.7: Click the Create button.
- Step 6.8: Click the Show User Security Credentials link to see the credentials.
- Step 6:9: Record these credentials using copy and paste to a document or click the Download Credentials button.
- Step 6:10: Keep this file in a secure location. You will need these credentials later to run the AWS LIFX Control Panel application.
- Step 6:11: Click the Close link after you have saved the credentials (NOTE: once you close this window you will not be able to retrieve the access key any longer).
- Step 6.12: Click the new user account that you just created.
- Step 6.13: On the Groups tab Click Add User to Groups.
- Step 6.14: Select the group created in Step 5 (LIFX_ControlPanel).
- Step 6.15: Click the Add to Groups button.
- Step 6.16: The user now belongs to the group.
STEP 7: Create a Lambda Function
In this step, we will create the Lambda function that interacts with the LIFX API. This code is included at the bottom of the project and can also be found in GitHub. Follow these steps to create the Lambda function:
- Step 7.1: Download the Lambda function from this project with this link -> Download Lambda Function Zip and save it to a location such as your desktop
- Step 7.2: Log into the Amazon AWS Console with your account.
- Step 7.3: Click the Lambda icon/link in the Compute section.
- Step 7.4: Click the Create a Lambda function button.
- Step 7.5: In Select blueprint, select Skip at the bottom of the screen.
- Step 7.6: In the name field, enter LIFX_Control_Script.
- Step 7.7: In the description field, enter Controls one or more LIFX lights.
- Step 7.8: Choose Node.js as the runtime.
- Step 7.9: For the code enry type option, select Upload a .ZIP file.
- Step 7.10: Click the Upload button and select the file downloaded in Step 7.1.
- Step 7.11: In the section labeled role, select lambda_basic_execution.
- Step 7.12: Click the Next button.
- Step 7.13: Click the Create function button.
STEP 8: Create a New Action
- Step 8.1: Log into the Amazon AWS Console with your account.
- Step 8.2: Click the AWS IoT icon/link in the Internet of Things section.
- Step 8.3: Click the Create a resource button.
- Step 8.4: Click the Create a rule button.
- Step 8.5: Enter LIFX_Rule in the name field.
- Step 8.6: Enter Calls the LIFX Lambda Function. in the description field.
- Step 8.7: Enter the character * in the Attribute field.
- Step 8.8: Enter the character # in the Topic Filter field.
- Step 8.9: In the Choose an action field, select Insert this message into a code function and execute it (Lambda) option.
- Step 8:10: Select LIFX_Control_Script from the list under function names.
- Step 8.11: Click the Add action button.
- Step 8.12: Click the Create button.
STEP 9: Creating the DynamoDB
The DynamoDB will be created the first time the application is run. Follow the steps below to run the application.
Ensure that when you run the AWS LIFX Control Panel application you use the access keys generated in step 6.10.
STEP 10: Get the AWS LIFX Control Solution
Scroll down to the bottom of the project to the code section called AWS LIFX Control Solution and click the Download as zip link. Extract the zip file to your hard drive, then open the solution in Visual Studio 2015.
STEP 11: Run the Application
The Windows 10 application included in this project is a control panel that allows the assignment of the button event type to the LIFX scene.
The images below are screen shots from the application.
In the Main Screen of the application there is a dropdown list and slider for each of the three click event types on the button. The application will call the LIFX API to get a list of scenes to load into each dropdown list. The user can select the scene to associate with each of the click event types. The duration is how long in seconds that the light transitions to and from the next state. For example, if the duration is set to 2 seconds and the light is off, then the light will fade to on for 2 seconds.
The first time the application is run, the LIFX and AWS access keys need to be specified. The application will display an error at the bottom of the screen indicating that the configuration is missing.
Using the menu, select the LIFX API Setup option to enter the LIFX API key that was generated in Step 3.5. Next select the AWS Setup option to enter the AWS key generated in Step 6.10.
The video below demonstrates how to configure and use the application.
The image below is a screen shot of the items stored in the DynamoDB after running the application.
This video demonstrates the button in action.
What started to be something simple in design had quite the complexity to it! However, in the end, this is still a simple solution to my problem. The AWS IoT Button provides a quick and easy way to control the LIFX lights in my house.