I had this idea for a little weekend project to let one of our multicolor Hue lights in the house change color based on the outside temperature. Perhaps not particularly useful, but fun! So, for this project I needed a lightsource that could be programmatically controlled to change color, some sort of temperature measurement source, a few lines of code to control the light based on temperature, and a computer to run it on.
In my presentation Internet of Things: An entertaining and mostly correct reflection on where we are, and how we got here I talk about a few things that have been key enablers of the current state of IoT, including:
- Moore’s law, that has given us fast and dirt cheap computers,
- The Open Movement, enabling open source and open hardware, including an ever growing selection of service and “things” with open APIs,
- Cloud computing with cheap computing and storage available on demand over the Internet,
- The concept of The Long Tail, enabling democratization of production tools and connecting supply with demand.
This, of course, has a pretty profound impact on our modern society and our connected everyday lives. But it can also be put to use in the small, for fun hobby projects, if you are so inclined.
Philips introduced the Hue connected lighting product 2012, enabling control of multicolor LED bulbs via an Ethernet connected bridge and ZigBee networking technology. It is kind of neat on its own, but the really cool thing with the Hue system is that is was designed for hacking from the start: The bridge exposes a RESTful API making it straight-forward to interact with the lights programmatically over the local network. Philips also provides SDKs for various environments encouraging the developer community to innovate on the platform. I believe this is a great example of the “Open Movement”, even though the underlying hardware and software is closed. Since the launch, the Hue product family has grown significantly, now including more light sources such as light strips, stand-alone lighting fixtures and various control and sensor devices.
The most straight-forward way of getting a current temperature reading is probably to use an open API that exposes this data. In Sweden, the Swedish Meteorological and Hydrological Institute, SMHI for short, provides open data APIs that could be put to use. Global weather data can be obtained from a number of sources, including Yahoo! Again, good examples of the Open Movement.
But, if you require more accurate data for your precise location, you can always source it yourself, and for proper geek cred you should. For this project I used a temperature sensor network based on 1-Wire. 1-Wire is a device communications bus system which can be used to connect temperature sensors among other things. In my setup the 1-Wire sensor is connected to a Raspberry Pi using a USB connector, running the 1-Wire File System (OWFS) open source software. OWFS makes it possible to mount the sensors in the local filesystem, thus making it possibly to get readings just by accessing corresponding file in the system.
Getting this setup running on the RPi is almost as simple as running ‘sudo apt-get install owfs’. The hardware needed can be acquired preassembled from your local electronics/maker store (if you are in Sweden, try this!). I use the owserver part of the OWFS package to expose the temperature data to external clients since I run the code to control the Hue lights on a separate computer (another RPi).
The final piece of the puzzle are a few lines of code to get the temperature reading and update the color of the light. As Clojure is my go-to language for personal projects, it is what I used. Source-code is available on GitHub. Clojure requires a JVM to run, it takes a while to start on the Pi (at least for early models) but after startup it runs quite well.
The code is quite simple; a scheduler is used to every five minutes get the temperature reading, convert the temperature to a matching color value, and then update the configured light with a REST call to the Hue bridge. I use the jowfs client to connect to the 1-Wire server and clj-http together with Cheshire to do the HTTP/JSON-work. Chime is used to schedule the recurring task. We’ll take a closer look at the code in the second part of this article.
Turning the lights on and off is not done by the program itself; you could use a cloud service such as IFTTT to turn the lights on at sunset and a Hue Routine built into the Hue platform to turn it off at a preset time.
I am not too happy with the color output, the colors are a bit bland. I’ll probably end up manually tweak the saturation to get richer colors. More fine-grained color gradients would be a nice touch. Now there is a set color for each five degree temperature difference. It would be sweet to implement more of a seamless gradient that better makes use of the Hue lights’ color space.
It would also be nice to add an alternative way of getting temperature data, such as from an external API, for use when a local sensor net is unavailable.
The implementation only understands the Celsius scale. This is by design. Celsius is the best scale, it’s fantastic. It’s true.