Install Hue

From xPL
Revision as of 16:18, 1 May 2014 by Fcorthay (Talk | contribs)

Jump to: navigation, search

The hue system consists of LED light bulbs connected via ZigBee to a bridge which is in turn connected to Ethernet. The bridge API implements a RESTful web service using HTTP methods.

Before all, make sure you have installed an xPL hub.

Access the Hue bridge

Discover the Hue bridge

The bridge can be discovered with the help of a web browser at URL: www.meethue.com/api/nupnp.

It can be discovered in a terminal by:

curl -G http://www.meethue.com/api/nupnp

which produces an answer similar to:

[{"id":"001788fffe09f498","internalipaddress":"192.168.1.166"}]

The data structure is represented in text form with the help of JSON (JavaScript Object Notation).

Create a control user

Access to the API is limited by a whitelist. Creating a new user is done by pressing the link button on the bridge and then sending an HTTP POST request.

Trying without pressing the button:

curl http://192.168.1.166/api -d '{"devicetype":"Linux machine","username":"xplHueDevice"}'

will produce:

[{"error":{"type":101,"address":"","description":"link button not pressed"}}]

Do it again afer having pressed the button.

Configure the Hue bridge

Get configuration info:

curl -G 192.168.1.166/api/xplHueDevice/config

Change IP address:

curl 192.168.1.166/api/xplHueDevice/config -X PUT -d '{"ipaddress": "192.168.1.202"},"dhcp": false'
curl -G 192.168.1.202/api/xplHueDevice/config

Update the DNS server to attribute 192.168.1.202 to apollo.cofnet (god of the ligtht). Check IP address:

curl -G apollo.cofnet/api/xplHueDevice/config && echo

Hue bridge command line control

Get lights list:

curl -G apollo.cofnet/api/xplHueDevice/lights && echo

Get bulb state:

curl -G apollo.cofnet/api/xplHueDevice/lights/2 && echo

Turn light on and off again

curl apollo.cofnet/api/xplHueDevice/lights/2/state -X PUT -d '{"on": true}' && echo
sleep 1
curl apollo.cofnet/api/xplHueDevice/lights/2/state -X PUT -d '{"on": false}' && echo

Turn light on, red:

curl apollo.cofnet/api/xplHueDevice/lights/2/state -X PUT -d '{"on": true, "hue": 0, "sat": 255, "bri": 255}' && echo

Change to blue within 10 seconds:

curl apollo.cofnet/api/xplHueDevice/lights/2/state -X PUT -d '{"hue": 46920, "transitiontime": 100}' && echo

Install xPL Hue controller

Install HTTP::Tiny:

su
perl -MFile::Find=find -MFile::Spec::Functions -Tlw -e 'find { wanted => sub { print canonpath $_ if /\.pm\z/ }, no_chdir => 1 }, @INC' 2> /dev/null | grep -i http
perl -MCPAN -e "install 'HTTP::Tiny'"
exit

Download the xPL Hue controller script.

Move the script to xPL scripts directory:

SCRIPTS_BASE_DIR=/home/control/Documents/Controls
mv xpl-hue.pl $SCRIPTS_BASE_DIR
chown control:users $SCRIPTS_BASE_DIR/*.pl
chmod 775 $SCRIPTS_BASE_DIR/*.pl

Test xPL Hue controller

Test it on a BeagleBone:

su control
SCRIPTS_BASE_DIR=/home/control/Documents/Controls
$SCRIPTS_BASE_DIR/xpl-hue.pl -h
$SCRIPTS_BASE_DIR/xpl-hue.pl -vn hue

Open a 2nd terminal window (on any machine with xPL installed) and monitor the xPL protocol:

SCRIPTS_BASE_DIR=/home/control/Documents/Controls
$SCRIPTS_BASE_DIR/xpl-monitor.pl -vf

Open a 3rd terminal window (on any machine with xPL installed) and control the lights:

SCRIPTS_BASE_DIR=/home/control/Documents/Controls
$SCRIPTS_BASE_DIR/xpl-send.pl -v -c lighting.request device=3
$SCRIPTS_BASE_DIR/xpl-send.pl -v -c lighting.basic device=3 command=activate
$SCRIPTS_BASE_DIR/xpl-send.pl -v -c lighting.basic device=3 command=deactivate

The first xpl-send command asks for the status of the bulb. The answer is seen in the xPL traffic monitor.

Launch the xPL Hue controller at startup

The xPL Hue clock controller is to be launched after the xPL hub.

Ubuntu

On Ubuntu, the xPL Hue controller starts-up once xpl-hub_started has been emitted.

Edit /etc/init/xpl-hue.conf :

################################################################################
# xPL hue, controls Philips hue light bulbs
#
description "xPL hue"
version     "1.0"
author      "Francois Corthay"

#-------------------------------------------------------------------------------
# Configuration variables
#
env SCRIPTS_DIR='/home/control/Documents/Controls'
env SCRIPT_NAME='xpl-hue.pl'
env PARAMETERS='-n hue'

#-------------------------------------------------------------------------------
# Start and stop conditions
#
start on xpl-hub_started
stop on shutdown
respawn
setuid control
setgid users

#-------------------------------------------------------------------------------
# Start daemon
#
exec $SCRIPTS_DIR/$SCRIPT_NAME $PARAMETERS

Start the device and test it:

su root
service xpl-hue start
service xpl-hue status
ps aux | grep -i xpl | grep -v grep | sed 's/.*\/Controls\///'

Mac OS

On Mac OS, the xPL serial port device starts-up later than the hub.

Edit /Library/LaunchDaemons/xpl-hue.plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
  <dict>
    <key>Label</key>
    <string>xPL Philips hue controller</string>
    <key>OnDemand</key>
    <false/>
    <key>UserName</key>
    <string>control</string>
    <key>ProgramArguments</key>
    <array>
      <string>/Users/control/Documents/Controls/xpl-hue.pl</string>
      <string>-n</string>
      <string>hue</string>
      <string>-w</string>
      <string>25</string>
    </array>
  </dict>
</plist>

Load the xPL client:

launchctl load /Library/LaunchDaemons/xpl-hue.plist
launchctl list | grep -i xpl 
ps ax | grep -i xpl | grep -v grep