Controlling Philips Hue with a Particle Photon

by Nov 11, 2017Particle

There is one control system used long ago, in the olden days before Internet-connected home automation technologies existed. Before we all equipped our houses with WiFi, Bluetooth, and Z-Wave hubs, and controlled everything from our smartphones, in the days when speakers just played music and didn’t care what you had to say to them we were able to control the lights, the temperature, the appliances, the fans, and lots of other things using physical switches on the walls. In modern times, of course, all your lights probably run on a Philips Hue system or other smart lighting solution.

Connected lights like the Philips Hue allow fine-grained control over your lighting system, allowing you to turn the lights on and off, change their color, and apply various different lighting effects, all from your smartphone. Furthermore, systems like the Philips Hue can be connected to other home automation products and services so that your lights can automatically respond to events happening around your home.

But what happens when you want the ability to control the lights with a simple gesture when you walk in the room, like you could when you used to use switches? Maybe you set your smartphone down somewhere else in the house. Perhaps your smartphone ran out of batteries and is on a charger. It could be that you just want a quicker way to actuate your lights, without having to take your phone out of your pocket, unlock your phone, navigate to the correct application, and select a lighting mode. Apparently this thought has occurred to companies like Philips because they do carry products like the Philips Tap Switch which is basically a trio of light switches for activating three choices of lighting scene. But these kinds of switches always seem a bit like an afterthought for the engineers designing the lighting system itself. It seems like companies such as Philips feel that these switches should exist to round out systems like the Hue, but it also seems like they did not put much effort into the product. Surely the company currently at the forefront of connected lighting solutions could be a bit more creative than just whipping together a triple light switch and calling it a day.

But we, the Maker community, can do better. Today, in this post, we will set the foundation for a platform that will allow you create physical controls for your Philips Hue light system that will be infinitely more powerful and flexible than any product on the market today. After you read this post you don’t even need to stop with the Philips Hue, it will be trivially easy to use me how’s similar to those discussed here to control basically any of your home automation products. The actual circuit we will build here is an extremely simple one, and its interaction with the Philips Hue system via IFTTT is extremely simple as well; however, the techniques used in this post can easily be use as the foundation for a much more complex project.

In this post we will use a Particle Photon IoT development board and a button to create a physical interface to a Philips Hue lighting system. The aim here is to create a simple project that will serve as an introduction to all the concepts you will need to create physical controllers, based on the Particle Photon, for controlling any of your smart home products.

What is the Particle Photon?

You are probably familiar with the Philips Hue family of connected lighting products, but you may be wondering, what is the Particle Photon? The Particle Photon is a simple WiFi development kit for creating cloud connected IoT projects. The board features a reliable WiFi chip, the same one used in popular home automation products you might already own, like the Nest Protect and Amazon Dash buttons. It is also equipped with a powerful ARM Cortex M3 microcontroller. For interaction, it has a range of analog and digital GPIO pins.

Basically, the Particle Photon provides a platform for rapidly and easily creating Internet of Things projects.

The Circuit

For the purposes of this tutorial, we will be using a very simple, bare bones circuit. The circuit is based on the state change detection tutorial for the Arduino microcontroller (the code for this project is based on the code used for that tutorial as well). Only five parts are needed for this tutorial:

Three wires will connect the Particle Photon to the button. The first connects GND on the Photon to one leg on the button through a 10k pull-down resistor. The second wire connects 3.3V on the Photon to the button leg on the same side as the previous connection. The third and final wire connects the corner of the button opposite the 3.3V pin to the D3 pin on the Photon. Follow the diagram below for clarification.

When the button is open (unpressed) the D3 pin connects to ground through the pull-down resistor. A digital read on D3 will read LOW with the button unpressed. When the button is pressed, a connection is made between the D3 pin and 3.3V. Now if we perform a digital read on the D3 pin, it will read HIGH.

The Code

With the circuit built, we now need to create the program that will allow the Particle Photon to communicate button presses to the Internet. The code can be flashed to the Particle Photon using the Particle Build IDE.

The code has two major parts. First, the Photon will monitor the button and detect when it is pressed. When the button is pressed several times in a row, the Photon tallies the number of button presses. The code tracks the total number of presses with a variable. This portion of the code is based on the Arduino state change detection tutorial.

Each time the button is pressed, the Photon records the time it was pressed (in terms of the program run time, not clock time). If more than half a second elapses between button presses, the Photon considers the user to be done inputting a command via the button. The Photon then publishes an event to the Particle Cloud based on the number of times the button was pressed. In the code below, six different events are published for different numbers of button presses. You can easily add more events to create more triggers.

<pre style="background:#f9f9f9;color:#080808"><span style="color:#5a525f;font-style:italic">// Made by Toglefritz</span><!-- [et_pb_line_break_holder] --><!-- [et_pb_line_break_holder] --><span style="color:#a71d5d;font-style:italic">const</span> <span style="color:#a71d5d;font-style:italic">int</span> button = <span style="color:#811f24;font-weight:700">3</span>;<!-- [et_pb_line_break_holder] --><!-- [et_pb_line_break_holder] --><span style="color:#a71d5d;font-style:italic">int</span> buttonPushCounter = <span style="color:#811f24;font-weight:700">0</span>; <span style="color:#5a525f;font-style:italic">// Counter for the number of button presses</span><!-- [et_pb_line_break_holder] --><span style="color:#a71d5d;font-style:italic">int</span> buttonState = <span style="color:#811f24;font-weight:700">0</span>; <span style="color:#5a525f;font-style:italic">// Current state of the button</span><!-- [et_pb_line_break_holder] --><span style="color:#a71d5d;font-style:italic">int</span> lastButtonState = <span style="color:#811f24;font-weight:700">0</span>; <span style="color:#5a525f;font-style:italic">// Previous state of the button</span><!-- [et_pb_line_break_holder] --><span style="color:#a71d5d;font-style:italic">int</span> previousTime = <span style="color:#811f24;font-weight:700">0</span>; <span style="color:#5a525f;font-style:italic">// The time the button was last pressed</span><!-- [et_pb_line_break_holder] --><!-- [et_pb_line_break_holder] --><span style="color:#a71d5d;font-style:italic">void</span> <span style="color:#bf4f24">setup</span>() {<!-- [et_pb_line_break_holder] --> pinMode(button, INPUT);<!-- [et_pb_line_break_holder] -->}<!-- [et_pb_line_break_holder] --><!-- [et_pb_line_break_holder] --><span style="color:#a71d5d;font-style:italic">void</span> <span style="color:#bf4f24">loop</span>() {<!-- [et_pb_line_break_holder] --> // If more than half a second has passed since the last time the <!-- [et_pb_line_break_holder] --> // button was pressed, assume the user is done with the pattern and <!-- [et_pb_line_break_holder] --> // sed a message to the cloud based on the number of times the button<!-- [et_pb_line_break_holder] --> // was pressed.<!-- [et_pb_line_break_holder] --> <!-- [et_pb_line_break_holder] --> if(<span style="color:#693a17">millis</span>() - previousTime > 500 && buttonPushCounter != 0) {<!-- [et_pb_line_break_holder] --> <!-- [et_pb_line_break_holder] --> // If the button was pressed once, send a message to the cloud<!-- [et_pb_line_break_holder] --> if(buttonPushCounter == 1) {<!-- [et_pb_line_break_holder] --> Particle.<span style="color:#693a17">publish</span>("ButtonCount1"); <!-- [et_pb_line_break_holder] --> }<!-- [et_pb_line_break_holder] --> else if(buttonPushCounter == 2) {<!-- [et_pb_line_break_holder] --> Particle.<span style="color:#693a17">publish</span>("ButtonCount2"); <!-- [et_pb_line_break_holder] --> }<!-- [et_pb_line_break_holder] --> else if(buttonPushCounter == 3) {<!-- [et_pb_line_break_holder] --> Particle.<span style="color:#693a17">publish</span>("ButtonCount3"); <!-- [et_pb_line_break_holder] --> }<!-- [et_pb_line_break_holder] --> else if(buttonPushCounter == 4) {<!-- [et_pb_line_break_holder] --> Particle.<span style="color:#693a17">publish</span>("ButtonCount4"); <!-- [et_pb_line_break_holder] --> }<!-- [et_pb_line_break_holder] --> else if(buttonPushCounter == 5) {<!-- [et_pb_line_break_holder] --> Particle.<span style="color:#693a17">publish</span>("ButtonCount5"); <!-- [et_pb_line_break_holder] --> }<!-- [et_pb_line_break_holder] --> else if(buttonPushCounter > 5) {<!-- [et_pb_line_break_holder] --> Particle.<span style="color:#693a17">publish</span>("ButtonCount5+");<!-- [et_pb_line_break_holder] --> }<!-- [et_pb_line_break_holder] --> <!-- [et_pb_line_break_holder] --> // After sending the message to the cloud, reset the counter and<!-- [et_pb_line_break_holder] --> // the time<!-- [et_pb_line_break_holder] --> buttonPushCounter = 0;<!-- [et_pb_line_break_holder] --> previousTime = millis();<!-- [et_pb_line_break_holder] --> }<!-- [et_pb_line_break_holder] --><!-- [et_pb_line_break_holder] --> // Read the button state<!-- [et_pb_line_break_holder] --> buttonState = digitalRead(button);<!-- [et_pb_line_break_holder] --> <!-- [et_pb_line_break_holder] --> // Compare the buttonState to its previous state. If the two are <!-- [et_pb_line_break_holder] --> // different, a change as occured.<!-- [et_pb_line_break_holder] --> if (buttonState != lastButtonState) {<!-- [et_pb_line_break_holder] --> // If the current state is HIGH then the button went from off to on.<!-- [et_pb_line_break_holder] --> if (buttonState == HIGH) {<!-- [et_pb_line_break_holder] --> // Count the button press<!-- [et_pb_line_break_holder] --> buttonPushCounter++;<!-- [et_pb_line_break_holder] --> <!-- [et_pb_line_break_holder] --> // Record the time of the button press<!-- [et_pb_line_break_holder] --> previousTime = millis();<!-- [et_pb_line_break_holder] --> }<!-- [et_pb_line_break_holder] --> }<!-- [et_pb_line_break_holder] --> <!-- [et_pb_line_break_holder] --> // Delay a little bit to avoid bouncing<!-- [et_pb_line_break_holder] --> delay(50);<!-- [et_pb_line_break_holder] --> <!-- [et_pb_line_break_holder] --> // save the current state as the last state, for next time through the loop<!-- [et_pb_line_break_holder] --> lastButtonState = buttonState;<!-- [et_pb_line_break_holder] -->}<!-- [et_pb_line_break_holder] --></pre>

Create IFTTT Applets

So right now we have an Internet-connected button powered by a Particle Photon. The button publishes events to the Particle cloud for different sequences of button presses. In order to link these published events to actions performed by the Philips Hue system, we will use IFTTT (“If This Then That”) which is a service for connecting different online and IoT products together.

IFTTT uses the concept of applets (formerly called recipes) to link together different services. An applet consists of a trigger (“If This”) and an action (“Then That”). We will be creating six different applets for this tutorial, one for each of the button sequences. The events published by our Particle Photon will serve as the triggers in IFTTT.

You will need to begin by installing the IFTTT app on your smartphone and registering an account. Then, open the app and in the lower-left corner, select My Applets. If you’ve used IFTTT before you might already have a couple applets listed, or you may have some stock applets. In the upper-right corner of the screen, press the + button to create a new applet.

Tap the + This button to create a trigger for the applet.

There are a huge number of services available in IFTTT, which is part of the reason it is such a powerful tool for home automation. In the search bar, enter “Particle.”

With the Particle service selected, choose the New event published option. In the Event Name field, enter the first event name from the code above, “ButtonCount1“.

At the bottom of the screen, make sure the correct Particle Photon device is selected if you have more than one. Then, in the upper-right corner of the screen, press the check icon.

With the trigger set, tap the + That button to add a Philips Hue control. Again you will find dozens of different services. You can certainly use the Particle Photon button to control many different services, including Philips Hue. In the search bar, enter “Hue” and select the Philips Hue option.

You can take many actions on your Philips Hue bulbs, I am choosing to change the color of a lamp in my office. Enter the required information for whatever action you choose to take on the trigger from the Photon. Press the check again when you are finished.

To save the new applet, press the Finish button. Then, repeat the process for all the other Photon triggers.

Using the Button

Using the button is really easy, just press the button repeatedly, as many times as you want to press it, and depending upon how many times you press the button, the Particle Photon will trigger an IFTTT applet to do different things, like turn Philips Hue lights on and off, change their color, change their brightness, or any of a number of different actions.

Share This