4
votes

I see that it is possible to control Philips Hue lights with commands such as this:

Make an HTTP POST request of the following to (your hue's website here)

{"username": "YourAppName", "devicetype": "YourAppName"} If you have not pressed the button on the Hue Hub you will receive an error like this;

{"error":{"type":101,"address":"/","description":"link button not pressed"}} Press the link button on the hub and try again and you should receive;

{"success":{"username":"key"}} The key above will be an md5 string, remember this, you'll need it in all future requests

--but I'm not sure how to use Applescript to speak that language--I know you can use "do shell script" and possibly cURL in there as well, but, I'm falling apart on actually getting the code to work correctly.

Any thoughts?

3
I tried do shell script "curl -d \"username\": \"me\", \"devicetype\": \"hue\" 192.168.1.112/api" which says invalid json. I also used a Chrome extension called Simple Rest Client, which does allow the code to work.Mississippi Joe
Please don't edit your question to add an answer. Just add the answer below and, after the required waiting period, select it as correct.user1228

3 Answers

1
votes

For shell script try this:

curl -d "{\"username\": \"yourname\", \"devicetype\": \"yourhuename\"}" [not a link]http://hueIpAddr/api
1
votes

I was able to get my python script to use a curl request to control the lights via subprocess (similar to do shell script, I think), using:

curl --request PUT --data \'{"on":true, "xy":[0.4370,0.3706],"bri":255}\' http://{url}/api/{username}/lights/1/state

To create a user with curl, you should likely change the code to look like:

curl --request POST --data \'{"devicetype":"your_devicetype", "username":"your_username"}\' http://{url}/api/

To be fair, I haven't tried that with curl, as I am working on moving to urllib2 in python. Hope this helps.

0
votes

Another shell script with a helpful function for setting the color via HSB and transition time.

#!/bin/sh
#
# Register "patniemeyer" key with this bridge
#
# % curl -d '{"username": "patniemeyer", "devicetype": "Philips hue"}' http://192.168.1.179/api
# [{"success":{"username":"patniemeyer"}}]
#

KEY='patniemeyer'
IP='192.168.1.179'

#
# Light number, hue, saturation, brightness, transition time
#
# hue 0-65535
# sat 0-255?
# transition time 1/10 seconds
#
lightNHSBT() 
{
    _lightNum=$1
    _hue=$2
    _sat=$3
    _brightness=$4
    _ttime=$5
    curl --request PUT --data "{\"hue\":$_hue, \"sat\":$_sat, \"bri\":$_brightness, \"on\":true, \"transitiontime\":$_ttime}" http://$IP/api/$KEY/lights/$_lightNum/state/
}

#
# CIE 1931 X,Y colors
# Light number, X, Y, brightness, transition time
#
# transition time 1/10 seconds
#
lightNXYBT() 
{
    _lightNum=$1
    _x=$2
    _y=$3
    _brightness=$4
    _ttime=$5
    curl --request PUT --data "{\"xy\":[$_x,$_y], \"bri\":$_brightness, \"on\":true, \"transitiontime\":$_ttime}" http://$IP/api/$KEY/lights/$_lightNum/state/
}

# 
for f in 1 2 3 4 5 6 
do
    lightNHSBT $f 0 255 255 5  # full red
done