We’re all guilty of spending too much time geeking out! GeekyTim is no exception; he spends his time in ‘Hut 8’ (his log cabin in the garden) tinkering with Raspberry Pis, 3D printing, and laser cutting. His wife complains when she has to come out in the rain to call him in!
This article was written by Tim Richardson and appears in The MagPi #61.
After buying some Hue lights, he wondered whether they could be controlled with a Pi. Philips provides APIs, and other Python libraries have been developed to access the Bridge. So, he put them all together and created the PiHue – a Raspberry Pi Zero W with Touch pHAT that his family can use to call him in from play…
We start by connecting our Pimoroni Touch pHAT to the Pi, burning the Raspbian Jessie Lite image onto an SD card, booting up, and connecting to the same network that the Hue Bridge is on.
We need to install a few prerequisites before downloading the PiHue code, including Python 3, pip, Git, the Touch pHAT library, and the phue library that gives us access to the Hue lights from Python:
sudo apt-get update sudo apt-get install python3 python3-pip git sudo apt-get install python3-touchphat sudo pip3 install phue
Next, we need to download the PiHue code from GitHub:
git clone https://github.com/GeekyTim/PiHue
The PiHue code is in the PiHue directory:
Each of the Touch pHAT’s six capacitive buttons will have a different function. The Back (left arrow) button turns the lights on and off; four A, B, C and D buttons flash the lights red, yellow, green, and blue respectively; and Enter (right arrow) makes them all bright.
There are two versions of the code:
- PiHueRoom.py controls a Room as defined in the Hue app.
- PiHueLightList.py controls individual lights in a list.
The Room version works best with a room that contains more than one light, especially if one is coloured and one is dimmable.
We have to edit the code to make some changes for our Hue setup:
Set the IP address of the Philips Hue: bridgeip.
We can find this by accessing our router’s IP address list or using a network sniffer tool. The Bridge’s MAC address can be found in the Hue app.
For PiHueRoom.py, set the name of the room to be controlled: roomname.
For PiHueLightList.py, change the names of the lights in the Python list: lights.
Optionally, we can change the list of Hue xy colours by adding to those already listed under the comment # Hue 'xy' colours.
We can also change the alert patterns in the section following # Alert Patterns. These are Python lists; the first number is how many times to repeat the pattern. The second is the delay between changes – the recommended minimum delay is 0.4 (seconds).
The remaining values are Python dictionaries of the light status changes.
Use any valid Hue status, e.g. on, bri, xy, ct, sat, hue, transformationtime (see the Hue API for details).
Dimmable lights do not accept any of the colour changes (xy, ct, sat, hue), so just change their brightness instead. We can specify as many changes as we want.
For Light Lists, the status changes are Python dictionaries that contain the changes for each light type (e.g. Extended color light and Dimmable light). Other light types can be added if you have them (e.g. Color Light, Color Temperature Light).
There is one special dictionary that is used by the Touch pHAT’s Enter button to set all lights to bright white: allwhite. We could redefine this to be a preferred colour and brightness.
Running the code
The line # b.connect() must be uncommented the first time the code is run. Just before running it, we must press the Bridge connect button. Run with:
The Pi should connect to the Bridge and save the Bridge details in the file /home/pi/.python_hue. If we ever need to change the Bridge, we just need to delete this file. We can now re-comment the b.connect() line by adding # at the front..
Running on boot
There are a few ways to run Python code when the Raspberry Pi boots. A good method is to use systemd. In this case, we need to create a configuration file (aka a ‘unit’ file) that defines a new systemd service:
sudo nano /lib/systemd/system/PiHue.service
…and type in the following (replacing PiHueRoom.py with PiHueLightList.py if required):
[Unit] Description=PiHue Service After=multi-user.target [Service] Type=idle User=pi Restart=always ExecStart=/usr/bin/python3 /home/pi/PiHue/PiHueRoom.py [Install] WantedBy=multi-user.target
Exit and save using CTRL+X, Y, then ENTER. The permission of the unit file needs to be set to 644:
sudo chmod 644 /lib/systemd/system/PiHue.service
We need to instruct systemd to start the service during the boot sequence:
sudo systemctl daemon-reload sudo systemctl enable PiHue.service
When we reboot the Pi, the PiHue service should run.
Check service status
We can check the status of the PiHue service using:
sudo systemctl status PiHue.service
The last line should look like:
Jul 10 23:26:52 PiHue systemd: Started Start the Touch pHAT Hue controller.
Sometimes the Raspberry Pi disconnects from the network, especially when using WiFi. Once disconnected, it will remain disconnected unless the interface is restarted or the Pi is rebooted. That makes this light switch a bit useless. But help is at hand: we can reboot the Pi if it cannot see your home router!
Follow the instructions by Thijs Bernolet on his blog to enable reboot on network loss.