A while ago, I bought a Hue light and bridge to experiment with. Hue Lights are a smart home light that can be automatically turned on and off via a timer or event trigger. I decided to start experimenting with Hue, because they have a pretty easy API to use that allows fully flexible integration with other sorts of devices or data.
I could of gone the simple route and just used IFTTT.com to integrate my hue lights with other devices. For a few years now I have been using IFTTT.com, and for the most part I could handle all my geeky smarthome needs using IFTTT. However, I don’t find it as flexible as just coding it myself. IFTTT does have the ability to use its custom language platform, but after looking at it I decided I didn’t have the energy to learn another proprietary language/structure, so I have decided to do more with building my own custom logic in C# and using Microsoft Azure. I will still use IFTTT for most the simple things, but my goal is to start integrating everything into my own custom platform to manage devices.
So my first ‘Hello World‘ application using the Hue API and Microsoft Azure platform was to turn a light on every morning, varying the color of the light by the weather forecast for the day. So if it is supposed to rain the light will be blue, if it is to be sunny the light will be yellow.
There were numerous hurdles to doing this, primarily because I was trying so many new things. Learning the base Hue API for internal access was by far the easiest, I was able to run commands from my local network in under an hour. After that, I tackled the logic to call the weather API, which also turned out to be pretty easy. It turns out the National Weather Service has an API that you can call to get the days weather –
https://www.weather.gov/documentation/services-web-api. Pretty simple authentication, and once I got past that I found the endpoint to call and in an hour or so I had logic built to pull the days forecast. Thinking I was close to achieving my goal, I was feeling 90% done with my project. Of course, as with most programming projects, it turns out the last 10% of the project consumes 90% of the time. Here is where it got complicated:
- Hue remote API access.I want this program to run out at Microsoft Azure, so that I don’t have to have my PC have to wake up at 6AM to run the job. So I had to configure my Hue bridge to take requests from outside my home network. Hue uses OAUTH to communicate security credentials, and my understanding of OATH flow was not that current (I am an old username / password guy..) so I had to first re-conceptualize the whole authentication / refresh token flow and figure out how to support it. Once I figured all that out I had to figure out how I wanted to store my tokens, so I had to build a subsystem to manage that whole process. I made this work more complicated than I needed to of course, because I made it flexible to handle future APIs that use Oauth. So I burned several hours getting an understanding of the workflow, and building out all that. But now I think I have a platform ready that will make my next Ouath integration easier.
- Azure Functions and .NetCore. This is a great example of how software projects go way over deadline. Because I am in charge of the scope for my little experiment, I decided to expand it a bit. I wanted to set up a new Azure function to handle all my future IOT needs. Microsoft’s Visual Studio now comes with a nice template to build Azure Functions for Azure, so I decided to use it. I also decided to build it using .Net Core, which is the newest and most efficient .net framework. .Net Core allows to run .net on Linux machines, so in the future if I want to run these or other programs locally on a Raspberry Pi, that sets me up to do that in the future. It turns out I seriously underestimated the time it would take to build this out using .Net Core. The biggest hurdle was all my utility class libraries are built in .net Framework 4.7, which is not compatible with .Net Core. So I had to reinvision my future architecture for my class libraries. I experimented with .Net Standard (a compatibility standard), and threw that out. I ended up just standing up parallel utilities in .Net Core, which at this point I will use for all my IOT projects. I think it was the right thing to do, but this probably tripled the time I had budgeted for this project. Once I got all this build out and plugged into the new Azure functions project. I was easily able to publish this project to Azure, and my project was live! Each day at 6AM the program wakes up at Azure, and turns the light on in my house.
My first few days of testing happened to be rainy, so the light was always blue. I got tired of that pretty quick, so I decided to add logic to vary the color of the light by ‘raininess’. Using this code, I now can vary the light color by approximately the forecast for the day:
string shortforecast = p.shortForecast.ToLower();
int rainScore = 100;
rainScore += (shortforecast.IndexOf("rain likely") >= 0) ? 155 : 0;
rainScore += (shortforecast.IndexOf("chance of rain") >= 0) ? 100 : 0;
rainScore += (shortforecast.IndexOf("occasional showers") >= 0
|| shortforecast.IndexOf("slight chance rain showers") >= 0
|| shortforecast.IndexOf("light rain") >= 0
) ? 50 : 0;
if (rainScore == 100 && shortforecast.IndexOf("rain") >= 0)
{
rainScore += 25;
}
int sunScore = 100;
sunScore += (shortforecast.IndexOf("sunny") >= 0) ? 155 : 0;
sunScore += (shortforecast.IndexOf("sunbreak") >= 0) ? 100 : 0;
sunScore += (shortforecast.IndexOf("partly cloudy") >= 0) ? 50 : 0;
if (sunScore == 0 && shortforecast.IndexOf(" sun ") >= 0)
{
sunScore += 25;
}
I then build the color for the hue light of the rain and sun intensity. Note that I am using one of these ridiculously expensive color hue lights (around $50), which gives me the flexibility of using a vast array of colors.
So for now my experiment is complete, and I am enjoying a quick glance at the light to see the days weather forecast. This has been an interesting (though over budget) experiment, leading me to even more ideas for the future. Please don’t think I am an idiot for spending $50 for a light that changes color based on the weather – I consider this is an experiment and the first step in building out a fully integrated smarthome.