Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Using the Q42 HueApi Library

5.00/5 (2 votes)
1 Oct 2020CPOL3 min read 7.3K  
Windows Forms demonstration of using the Q42 HueApi library
In this article, I will demonstrate a few basic uses of the Q42 library to control your Philips Hue bridge from a WindowsForms C# application. It will not explore the entire spectrum of the methods the library offers, but it should provide you with a starting point.

Introduction

A short time ago, I acquired a Philips Hue bridge and a few colour temperature lights. Actually, it was a birthday present (thanks Jim). Being a nerd, I played around a bit with all the possibilities via Android apps, IFTTT, and some more. Added an IKEA dimmable light, which worked like a charm. Then I looked for more, preferably with a C# interface. C# being the last programming language which I used extensively when still working (I started with ALGOL68 if anyone still knows what that is). Stumbled on the Q42 library, which seemed to cover all that I could possibly ever want. But the documentation was not very clear, I had never used the more recent async/await constructs, so I struggled a bit to get it all working. Thinking that I am not the only one who might profit, I decided to write a short article about my experiences.

Using the Code

The attached Visual Studio project contains all the code you will need and the libraries via nuGet packages. The only other requirements are the C# development environment and the Philips Hue hardware in your local network.

Step 1

The first thing to do was to create a Form from which to control everything. This TestFormMain makes it possible to connect a bridge and launch several information windows.

App main window

Step 2

Any program wanting to communicate with a Philips Hue bridge must first located the bridge. The library offers a number of discovery methods, but for the sake of simplicity, I only use the HttpBridgeLocator. You will find an example of how to search in the ConnectBridgeForm event handler buttonSearch_Click.

App window for searching and connecting

After setting some values for the progressBar, I wait for the async method searchBridge() to finish execution. If the routine was not ended by a timeout (the value in seconds can be set in the application settings), the return value contains the time it took:

C#
TimeSpan duration = await searchBridge();
if (duration.TotalSeconds > 0)
{
    progressBar.Visible = false;
    if ((locBridges == null) || (locBridges.Count < 1))
    {
        labelBridgeFound.Text = "Error: no bridges found within the timeout period, " + 
        "consider increasing time out value in Settings.";
    }
    else .....

You might in theory find more than one bridge, so in the list you can check which one you want to work with. If the checkbox is checked, it means that the client (this program) is already registered with the bridge. Otherwise, one must register the application first. Closing the ConnectBridgeForm will check if the application can use the client that was found.

Step 3

As soon as a client is connected, the main window will dynamically create user controls (LightControl) for each light.

Main window after connecting

The LightControl probably is far from perfect. The getting and setting of Hue values I could not test. But if you study the code, you will get the idea.

Also from the main form, you can simultaneously switch all lights on or off. And finally, I was curious if I could make one of my lights blink rapidly. The interval in milliseconds if set by the numericUpDown, the blinking starts and stops by pressing the top-right button.

The information window (Help / Hue info) gives two kinds of information:

  • A listing for the whole system:

    Info window 1

  • The id, name and lights for a ‘group’.

    Info window 2

Conclusion

As said, the code is not perfect. But it covers enough aspects to give you a head start. Have fun!

References

History

  • 30th September, 2020: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)