The Official Raspberry Pi Projects Book - Projects_Book_v4

(singke) #1

Tutorial


raspberrypi.org/magpi The Official Raspberry Pi Projects Book 111


PIHUE: CONTROL PHILIPS HUE LIGHTS


LIGHT
CHANGES
For rooms with
both dimmable
and coloured
lights, alter
the brightness
as well as
the colour of
the room.

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: magpi.cc/2vEpXfH).
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:


python3 PiHueRoom.py


...or:


python3 PiHueLightList.py


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[1]: 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: magpi.cc/2uC0iny.

Language
>PYTHON 3

NAME:
PiHueRoom.py
PiHueLightList.py
DOWNLOAD:
magpi.cc/2vBxtoI

CONTROL
YOUR OWN
WORLD!
Don’t just use
the code as
is – why not
experiment
and make
your lights
match your
own colour
schemes?

Above It’s alive!

Free download pdf