Skip to content

Instantly share code, notes, and snippets.

@acidzebra
Last active November 13, 2020 03:08
Show Gist options
  • Save acidzebra/6b8e0820d3d0a42b939e3cc9886acb93 to your computer and use it in GitHub Desktop.
Save acidzebra/6b8e0820d3d0a42b939e3cc9886acb93 to your computer and use it in GitHub Desktop.
Give Vector "control" over your Philips Hue light. Script takes a snapshot from Vector's camera and does some fancy math to determine brightness. If not bright enough, a hue light will be turned on.
#!/usr/bin/env python3
# Copyright (c) 2019 acidzebra
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# for use with Anki's awesome Vector robot: https://www.anki.com/en-us/vector
#
import anki_vector
from PIL import Image, ImageStat
import math
import requests
import json
# see http://rsmck.co.uk/hue or http://jheyman.github.io/blog/pages/HueRaspberryControl/
# or the official Philips Hue API documentation to get this part set up
HUE_BRIDGE_IP = 'REPLACE_WITH_YOUR_HUE_BRIDGE_IP'
USER_ID = 'REPLACE_WITH_YOUR_HUE_USER_ID'
LIGHT_ID = 'REPLACE_WITH_YOUR_HUE_LIGHT_ID'
# Philips Hue light configuration, adjust this to suit your preferences
LIGHT_BRIGHTNESS = 254
LIGHT_COLOR_TEMPERATURE = 100
# adjust this for your setup, higher values = brighter
# I find 100 works for me (and my Vector, in my room, with my light position/orientation/distance)
# if the actual measured light value is below this number, the light will be turned on and set to
# LIGHT_BRIGHTNESS and LIGHT_COLOR_TEMPERATURE values
desired_light_value = 100
# grab a camera image
with anki_vector.Robot(enable_camera_feed=True, requires_behavior_control=False) as robot:
while not robot.camera.latest_image:
time.sleep(0.5)
image = robot.camera.latest_image
# do math stuff to determine perceived average brightness
# I have no idea about the math involved, I googled and grabbed some code from
# https://stackoverflow.com/questions/3490727/what-are-some-methods-to-analyze-image-brightness-using-python
stat = ImageStat.Stat(image)
r,g,b = stat.mean
actual_light_value = math.sqrt(0.241*(r**2) + 0.691*(g**2) + 0.068*(b**2))
print ("average brightness:", actual_light_value)
# is it too dark?
if actual_light_value < desired_light_value:
print("it's a little dark in here, adjusting")
# yeah it's a little dark in here, connect to Hue and set the light state
url = "http://" + str(HUE_BRIDGE_IP) + "/api/" + str(USER_ID) + "/lights/" + str(LIGHT_ID) + "/state"
data_on = {"on":True, "bri":LIGHT_BRIGHTNESS, "ct":LIGHT_COLOR_TEMPERATURE}
r = requests.put(url, json.dumps(data_on), timeout=5)
else:
print("light levels seem fine")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment