Building a Hue light switch using a Raspberry Pi Zero – part 2

I now have a Raspberry Pi, set up for making a light switch for my Hue bulbs. Time to get this project started by adding wires and code!

This is part 2, if you haven’t read part 1 of Building a Hue light switch using a Raspberry Pi Zero, please do so to see where we’re at!

So, wires and code! I’m not completely new to this, since I made a Raspberry Pi based disco lightbulb earlier. I used this knowledge to kick start this project. I usually make tiny projects that focus on one aspect and then I combine them later. This is a combination of a couple of tiny Philips Hue projects, some knowledge I gained by managing my own web server and a couple of my tiny Pi/Arduino projects. Let’s combine all of this and illuminate my living room!

The wires

Like always: start simple, then expand. So, even though I want more buttons in the end, one button will do for now. I’ve added it to the breadboard, including a 10KΩ resistor and 3 wires: power, ground, and a signal wire we’ll use to detect if the button has been pressed. It’s a simple circuit:

Don’t mind the big Raspberry Pi here: my Fritzing doesn’t have the model for the Pi Zero.

And how it looks away from keyboard:

That’s all the wiring we need for now! Let’s jump to the code part and see if we can make this work!

The code: installing NodeJS

I’m familiar with JavaScript so that’s my language of choice. I use a lot of NPM, and I’d like to use some NPM modules this time too. NPM relies on Node, and I was surprised to see Node already being installed on my Raspberry Pi Zero! Connected to the Pi via the Terminal on my laptop, still works after a nights sleep! Typing node -v gives you either the currently installed version of Node or an error stating that Node can’t be found. In this case it gave me an answer: the Raspberry runs Node 4.8.2 so no need to install it! It may be an old version but using Node on microprocessors has a catch: you’ll need a special version of Node in order for Node to be able to run. If you need to download Node manually: go to the NodeJS downloads page and look for ‘Linux Binaries (ARM)’, ARMv6. The version differs per Raspberry Pi so check this before you end up having all kinds of trouble.

With Node already being installed, we’ll take care of NPM by installing it: sudo apt-get install npm. After that it’s time to initialize our project using npm init. This will generate a package.json file, in which is the starting point for NPM looking for modules.

I’ve mentioned earlier projects I did before, I’ve found by Brian Cooke to be an excellent module for detecting changes on GPIO pins so we’ll install that too: npm install onoff --save-dev.

The code: installing Apache

I realized I wanted a front-end to be visible on the Raspberry Pi’s ip address. Therefore I installed Apache using the command sudo apt-get install apache2 -y. This also gives us a default location for website projects: ‘/var/www/html’. We’ll put our files in that folder, all together: front-end and node project code. Running the code gave me what I needed: feedback in my terminal when I pushed a button on the breadboard! This works, so the circuit is ok!

Now we need to make sure this feedback with 1’s and 0’s is made into something I control Hue with:

The code: Controlling the Hue light bulbs

Hue light bulbs are controlled by a Hue Bridge: a small device that needs to be added to your home network.

The Hue Bridge has an ip-address in the network, and that’s where the power of the Hue light bulbs really shows. You can send small pieces of code (JSON) to it that control the lights. For example, to make the light bulbs emit a ‘scene’ (predefined light bulb settings) you make a PUT request to an url like this one: http://<your ip-address>/api/<your api key>/groups/3/action/. The payload of these requests is something simple like this: { "scene": "r01CpISAOWzSepE" }, which is the id for a scene I made causing the light bulbs to emit a warm yellowish light which is similar to my old light bulbs.

I made some small tests that do exactly this: sending a request with an instruction for the Hue Bridge. When I uploaded the code to the Raspberry I found a problem: ‘ReferenceError: XMLHttpRequest is not defined’. In my code I used this but that code was meant for use in my web browser. When I just got the Hue system I enjoyed myself by sending requests to it using my web browser. I needed to find another way to make requests to the Bridge, since this code is run with Node. This problem was the only thing in the way of a physical Hue light switch based on a Raspberry Pi!

After some Googling I found a nice module called ‘request’ that describes itself as this:

Request is designed to be the simplest way possible to make http calls. It supports HTTPS and follows redirects by default.

That looks promising! Quickly installed it using npm install request --save-dev. After some fiddling I found that I could leverage the ‘form’ part of the module, allowing me to send data to the Hue Bridge.

The result!

I haven’t explained everything I did but reading these blog posts should give you an idea of the process needed to make Hue lights work with a Raspberry Pi and NodeJS. Let me know if you have questions, or better: improvements!

After writing this blogpost I’ve made some changes; I’ve added more buttons and I’ve rewired the breadboard to something more useful since the wires were in the way when trying to press the buttons:


Here’s a video of the result which I mounted on one of the walls in my living room. I’ve SSH’d into the Raspberry so you can see what happens in the background. In the video I press a couple of buttons and the result in the terminal and in the living room:

A Youtube video is not shown because I don’t want you to have all kinds of cookies without you knowing about it!

Press the button below if you’d like to see the video :-)