1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | #!/bin/sh URL=http://192.168.0.50/api username=newdeveloper CURL=/usr/bin/curl GREP=/bin/grep [ ! -f "$CURL" ] && echo "$CURL not found" && exit 1 [ ! -f "$GREP" ] && echo "$GREP not found" && exit 2 command=$1 lightIndex=$2 if [ "x$command" = "xoff" ]; then [ $# -lt 2 ] && argRight=false data='{"on":false}' elif [ "x$command" = "xon" ]; then [ $# -lt 2 ] && argRight=false data='{"on":true}' elif [ "x$command" = "xsat" ]; then [ $# -lt 3 ] && argRight=false [ $3 -lt 0 -o $3 -gt 255 ] && argRight=false data='{"on":true,"sat":'$3'}' elif [ "x$command" = "xbri" ]; then [ $# -lt 3 ] && argRight=false [ $3 -lt 0 -o $3 -gt 255 ] && argRight=false data='{"on":true,"bri":'$3'}' elif [ "x$command" = "xhue" ]; then [ $# -lt 3 ] && argRight=false [ $3 -lt 0 -o $3 -gt 65535 ] && argRight=false data='{"on":true,"hue":'$3'}' else argRight=false fi if [ "x$argRight" = "xfalse" ]; then echo "Usage1: $0 (off|on) lightIndex" echo "Usage2: $0 (sat|bri) lightIndex (0-255)" echo "Usage3: $0 (hue) lightIndex (0-65535)" exit 3 fi gotState=`$CURL -s -X PUT --data $data $URL/$username/lights/$lightIndex/state` [ 0 -ne $? ] && echo Hue bridge is not detected on $URL && exit 4 unauthUsername=`echo $gotState | $GREP "unauthorized user"` if [ ! "x$unauthUsername" = "x" ]; then echo "Username, $username, needs to be created." data='{"devicetype":"'RPi'","'username'":"'$username'"}' gotUsername=`$CURL -s --data $data $URL` noLinkButton=`echo $gotUsername | $GREP "link button not pressed"` if [ "x$noLinkButton" != "x" ]; then echo "***ERROR*** The new username couldn't be created." >&2 echo "***ERROR*** You need to press Link button on the device." >&2 echo "***ERROR*** Or you may need to pick a different username." >&2 echo $gotUsername exit 5 fi echo "New username is created. Try again..." echo $gotUsername exit 6 fi echo $gotState |
It was simple enough to understand. And I created a shell script that can turn on/off and change the saturation/brightness/hue.
Note that you need to modify the first variable: URL.
Probably username doesn't matter as long as it works. I found that the device doesn't like any creative new usernames so when you changed username, it may not allow you to create it; try different username or the default.
The script requires two utilities: curl and grep. If your system doesn't have curl, you can install it with a command: sudo apt-get install curl
The device works with two steps: creating username and controlling.
Creating username is for security and when you create it, you need to press the button on the bridge, which is called "Link button". It will allow you to create a new username for a few seconds. Once you successfully created username, you wouldn't need to worry about that anymore.
pi@fileserver 23:39:43 script$ ./hue.sh Usage1: ./hue.sh (off|on) lightIndex Usage2: ./hue.sh (sat|bri) lightIndex (0-255) Usage3: ./hue.sh (hue) lightIndex (0-65535) pi@fileserver 23:39:49 script$ ./hue.sh on 2 [{"success":{"/lights/2/state/on":true}}] pi@fileserver 23:39:56 script$ ./hue.sh off 2 [{"success":{"/lights/2/state/on":false}}] pi@fileserver 23:40:01 script$ ./hue.sh bri 2 10 [{"success":{"/lights/2/state/on":true}},{"success":{"/lights/2/state/bri":10}}]
The script can take five different commands: off, on, sat, bri and hue. The value range of saturation and brightness is from zero to 255.
Hue value is little tricky. Although I am pretty familiar with color space concepts, I don't exactly understand how they mapped two-dimensional values to one-dimensional values. You will need to try different values to get what you want. lol
No comments:
Post a Comment