The Official Raspberry Pi Projects Book - Projects_Book_v4

(singke) #1

Tutorial WALKTHROUGH


(^110) The Official Raspberry Pi Projects Book raspberrypi.org/magpi
CONTROL
PHILIPS
HUE LIGHTS
Learn how to control Philips Hue lights with a Raspberry Pi
and the Pimoroni Touch pHAT
e’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 Pi boards, 3D
printing, and laser cutting. His wife complains when
she has to come out in the rain to call him in!
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...
Setting up
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
W
Below The PiHue,
with Touch pHAT
and custom laser-
cut cover to show
the colour options



Pimoroni
Touch pHAT
magpi.cc/
2pafoP9
Philips Hue
Bridge and Lights
magpi.cc/
2uBNFsK
phue Python
library by
StudioImaginaire
magpi.cc/
2uBWOBF
You’ll
Need
TIM RICHARDSON
One half of the CamJam and Pi Wars
organisation team, and author of the
CamJam EduKit worksheets.
@Geeky_Tim
PIHUE
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:
cd PiHue
Configuring PiHue
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.


Free download pdf