I’ve been on the fence for a long time regarding the whole IoT thing. The whole idea of using an app to turn on/off your lights or your coffee maker hasn’t really appealed to me. That isn’t to say I find the whole thing interesting from a developer perspective. After talking to a friend who has fully commited to the IoT/”smart home”-thing I figured I would test out something simple, like the Philips Hue lights. I went for the Philips Hue White Starter kit. At first I was thinking about testing the IKEA Trådfri, which is basically IKEA’s answer to Philips Hue, but after learning that they don’t have an official API out yet I went for Philips. Trying to combine it with a Xamarin.Forms app seemed like a fitting challenge, and it didn’t take me more than about an hour to do so. So, the following is how you can do it yourself.
- Philips Hue bridge
- Philips Hue bulbs of choice
- Visual Studio (with “Mobile Development with .NET” workload)
Configuring your Philips Hue Bridge
First you’ll have to hook up your bridge to your network according to the accompanying instructions. After this is done, you’ll have to find your bridge’s IP. You can easily do this by using Philips’ Discovery utility: https://discovery.meethue.com/. Just remember to access it from the same network that your bridge is hooked up to. Next, you’ll have to create an app key that you can access the API with. Follow Step 3 from the Getting Started guide to get this (referred to as “username” in the guide).
Creating the Xamarin.Forms app
Next we’ll create the app to control the lights with. Open up Visual Studio and create a new project. Select a blank Xamarin.Forms app.
As you may have noticed in the Getting Started guide, Philips has a RESTful API we can use to interact with our light bulbs. But let’s make it easy for ourselves by using a C# wrapper library that does all that jazz for us.
In the shared .NET Standard library for our app, install the NuGet package Q42.HueApi. After that, open up the MainPage.xaml, add a button to the page and add an event handler for the Clicked event on the button. Add the following code:
The variable bridgeIP should correspond to your bridge’s IP and the variable appKey should correspond to your app key / username generated in the previous step. Use these to initialize a new client to communicate with the bridge. You are now ready to light ’em up!
Let there be light!
Depending on what kind of light bulb you have, you can send commands to alter different settings for your light bulb. The most basic ones you can turn on or off, some of them you can dim or send alerts to (light bulb flashes) and the most advanced ones you can set the color for. For this tutorial we’ll send a command that dims the light to about half strength.
Continuing with the previous code, we’ll add two new lines:
The LightCommand object has different properties that you can set, e.g. brightness, hue, on/off or alert. The brightness property in this example is a byte, so 120 would be about half of the max value.
Here’s another example for if we would want to turn off our light bulb:
Again, these properties depend on the type of light bulb you have so you won’t see any effect if you for example try to set hue for your white light bulbs. The SendCommandAsync method sends the command to all your bulbs. You can override this method to send it to specific bulbs if you want.
Your final method should look something like this:
And there you go! You have now mastered the arts of home automation and may now don the title of “IoT Wrangler”.
Heads up! When testing on Android you may get an error stating that you can’t send clear text over HTTP. Check out this link to see how you can fix that: https://stackoverflow.com/questions/45940861/android-8-cleartext-http-traffic-not-permitted
Some of the examples here are for demonstration purposes only and does not reflect best coding practices. Other than that I hope you found this tutorial useful, and be careful if you feel like you need to replace all the light bulbs in your house now. That might cost ya…
Also, if you’ve done something similar yourself or have made your home super smart I’d love to hear from you!