Assignment 2: Hue light control

Assignment 2: Hue Light Control

The Idea

The project consists of developing a controller to control a lightbulb using HTTP, specifically the Phillips HUE lightbulbs. The main idea was to use an Arduino Nano IoT with multiple sensors to control a light (or several) connected on the same WiFi network.

The Result

Final result!

Bill of Materials

– 1 Arduino Nano IoT
– 3 5k potentiometers (like these)
– 1 Breadboard/ PCB (like these
– 1 OLED screen (like this)
– 1 LEDs (like these)
– 1 meter of 22awg wire (like this)
– 1 sheet of 12in x 12in Birch plywood

The Process

Using the Command Line

The system consists of an Arduino Nano 33 IoT connected the same WiFi network as the Phillips Hue Hub. The Hub works with Standard Wired ethernet and ZBee radio. To start, we have to know the private IP Address of the hub inside of the network. Fortunately we could see this IP Address right away.

Hue Hub with IP Address.
Hue Hub with IP Address.

To access the hub we would first need to create a user on it. You can find more detailed information on Tom Igoe’s Github Repository, but a short way of doing would be using your computer’s shell and introducing the command below:

  • curl -X POST -d ‘{“devicetype”:”NAME“}’ HUBIPADDRESS/api
And replace the NAME for one you prefer and HUBIPADDRESS for the Hub’s IP Address.
In my case, it looked like this:
  • curl -X POST -d ‘{“devicetype”:”alvaro“}’ http://172.22.151.185/api
Take into account you need to push the button on top of the Hub and then rapidly send the command in order for the procedure to work. In my case, I received a success message with a user name chosen by the hub. It means I have successfully added a user to the Hub. Mine looked like this:
 
Hue User Success
Hue User Success

After we have our new username, we can now access the hub and the lights associated to it. We can find which lights we can access with the following command:

  • curl -X POST HUBIPADDRESS/api/GIVENUSERNAME/lights
Where GIVENUSERNAME is replaced by the username assigned to you by the Hub. After sending this command, the Hue will respond with an unformatted JSON telling you every parameter in the light you are trying to access.
 
 
Hue Response
Unformatted JSON response.

The above message looks much clearer when you format it. I used VS Code and the Beautify plugin in order to format it fast.

Formatted JSON response.
Formatted JSON response.

As you can see, we have several parameters to modify.  Whether it is on or off, brightness, hue, and saturation are only some of them, but they’re the ones I decided to focus on. I decided to test whether I could now control them with my terminal before proceeding with the Arduino Nano.

Set Light Parameter.

Now that I had access to the hue and control as well, It was time to go to Arduino!

Arduino

Now that I had access to the hue and control as well, It was time to go to Arduino. I decided I wanted to control 3 parameters: Brightness, Hue, and Saturation. For testing purposes I first wanted to try my screen. To control the OLED display you need the adafruit_SSD1306.h and adafruit_GFX.h libraries. It took me a while to set it up, mostly because I later found you HAVE to clear the display before any new message. You can see the error that happened in the image below.

128x64 OLED screen error
128x64 OLED screen error
Screen working correctly!
Screen working correctly!
OLED txt snippet.
OLED txt snippet.

Anyway, I decided to utilize a handy function found in Tom’s Repo. The snippet allowed me to easily display any message on the screen. On the screen I printed some of the parameters I wanted to control while testing it’s performance. I decided to add a potentiometer for each of the parameters I wanted to control: three in total.

In order to send accurate values to the Hue I would need to map each potentiometer’s values to the parameter’s min and max range. So I started with Hue, since it was the funnest parameter to play with. I used Arduino’s map function in order to achieve this effect. Since the potentiometer’s values went up to 1023 and the Hue reaches 64000 on it’s hue parameter, we would have to map 0 – 1023 to 0 – 64000.

Mapping the potentiometer to Hue values.
Mapping the potentiometer to Hue values.

I would have to do the same with Brightness and Saturation, however their values had a much smaller range, from 0-255. Now, to display the Hue values being sent on-screen!

Potentiometer on screen.
Potentiometer on screen.

Once I knew the potentiometer was changing and reflecting this change on-screen, I tested it with the lightbulb on the Hue. And it worked!!! 

Arduino controlling light. Hooray!!
Arduino controlling light. Hooray!!
Setup on breadboard with two parameters.
Setup on breadboard with two parameters.

I modified the sketch so that it would display each of the parameters and their value while I was in control. The values would be attached to a JSON which would be sent through HTTP to the hub.

Displaying on-screen and sending the data.
Displaying on-screen and sending the data.

Now that the electronic setup was done, it was time to solder everything up and build an enclosure for it. From the beginning I knew I would use wood and a laser cutter to achieve the precise form I wanted (You can find the template for the laser cutter on the code repository below). At first I decided I would have:

 – An OLED screen to display the data
 – Three potentiometers/knobs to control each parameter.
– An LED to indicate connection
– An ON/OFF button
– A Neopixel ring which would preview the light’s state on the controller.

However, after tinkering for a while trying to find the color ranges on the Phillips hue and how I could translate these values on to the Neopixel ring, I decided to dispose of the feature due to time constraints. 

Cardboard Test for the UI.
Cardboard Test for the UI.
Cut wooden pieces.
Cut wooden pieces.
Preview of the whole build.
Preview of the whole build.
Assembly on to box.
Assembly on to box.
Booting up!
Booting up!
Final result!

The Schematic

Circuit Schematic
Circuit Schematic

The Code

Thanks for reading!