An Arduino Hue Controller

I wanted to give my kids a way to control the colors on their Hue lights in a more fun and tactile way so I used the Arduino MKR IoT Carrier with a MKR Wifi 1010 and wrote a program to let them toggle which light they control, turn it off and on, and randomize the color.

First, we must find the IP address of our Hue Bridge. This can be done using your router. Then connect to the Hue Bridge and register an application. The key here is to get the username. This will be used to build new URLs for communicating with the Hue in your applications.

The key goes in the URL after the IP address and /api/.

Your IP address will (probably) vary, the blacked out portion is my username

A GET request lists all information for the Hue environment. Information about specific entities can be found by appending /lights, /config, /schedules or more to the URL used to connect to the Hue Bridge.

Another interface is the Python SDK, the GitHub repo that accompanies this article includes a notebook with some examples of utilizing the SDK to control the Hue lights.

After registering an application and getting an ID we are ready to use the IoT Carrier to control the Hue Lights. I have several Hue compatible lights and only want to let the kids control two, so I wrote my application to hard code the two lights they have access to. You could modify it to query the API and control all your lights.

The Arduino program requires an accompanying file in the same directory called “arduino_secrets.h”. Your file should look something like this;

Fill in the appropriate secrets between the quotation marks

My program has also enabled the carrier case (the plastic shell included with one of the kits). If you’re not using the case you should set the CARRIER_CASE = true; flag to “false”.

Finally, in Arduino the loop() function is performed repeatedly; in this we listen for button presses and take actions when a button is pressed. There are three functions; randomizing a color, toggling which light is enabled, and turning off the light. Let’s talk about randomizing a color.

The Hue color scale is between 0 and 65,000. All colors fall somewhere in this range, this is as distinct from some schemes with separate red, green and blue controls. I think of the Hue colors as being like numeric color wheel times about 180. So green on a color wheel is 120. 120*180=21,600. Plugging that in the Hue notebook gives you a passable green. Other colors to play with are blue at 240 (43,200), red at 0 or 360 (65,000).

With this in mind, I want to randomize the color anywhere between 0 and 65,000. So that’s exactly what this function does; printing the random number to the screen and then invoking the colorUpdate function. This function does the real work of building the URL and command to PUT. The entire command is encoded and submitted as plain text.

This function uses the WiFiNINA library to connect to wifi and use that client to connect to the bridge, using ‘print’ commands to build the URL, with println commands building the various lines that would otherwise be headers or the body. The best way to experiment with changes here is to build the URLs or commands in Postman because this function has little to no real debugging.

experimenting in Postman

The finished product;

Enterprise Architect specializing in data and analytics.