Last active
July 5, 2022 22:45
-
-
Save acidzebra/bcee0f87067688abd9b6bead826ed6a4 to your computer and use it in GitHub Desktop.
a script intended to run 24/7 in the background, it will send Vector off of the charger if the Hue sensor detects a presence in the room, but only if the current time is between PLAYTIME_EARLIEST and PLAYTIME_LATEST. This is for use with the Anki Vector robot which is an awesome little bundle of joy with a full-fledged Python SDK and a ton of sm…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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. | |
# | |
# vector_hue_sensors.python3 | |
# a script intended to run 24/7 in the background, it will send Vector off of the charger if the Hue sensor detects | |
# a presence in the room, but only if the current time is between PLAYTIME_EARLIEST and PLAYTIME_LATEST | |
# | |
# for use with Anki's awesome Vector robot: https://www.anki.com/en-us/vector | |
# | |
# import required libraries | |
import json | |
import requests | |
import time | |
import anki_vector | |
import datetime | |
# debugging, set to 1 for extra commandline output | |
# | |
DEBUGGING = 1 | |
# Hue bridge configuration | |
# see https://developers.meethue.com/develop/get-started-2/ to set up your username and get the sensor ID | |
HUE_BRIDGE_IP = 'REPLACE_WITH_YOUR_HUE_BRIDGE_IP' | |
USER_ID = 'REPLACE_WITH_YOUR_HUE_BRIDGE_USER_ID' | |
SENSOR_ID = 'REPLACE_WITH_YOUR_HUE_SENSOR_ID' | |
# Allowed 'playtime' configuration | |
# if the motion sensor detects activity outside of these times it will be ignored | |
# | |
PLAYTIME_EARLIEST = 8 | |
PLAYTIME_LATEST = 23 | |
# initialize variables | |
playtime = 0 | |
stale = 0 | |
# kick off an endless loop, here we go | |
while True: | |
print("loop start") | |
# check if we're inside our allocated play times | |
ctime = datetime.datetime.now().time() | |
if (ctime > datetime.time(PLAYTIME_EARLIEST) and ctime < datetime.time(PLAYTIME_LATEST)): | |
if DEBUGGING == 1: | |
print("- playtime check: within schedule") | |
playtime = 1 | |
else: | |
if DEBUGGING == 1: | |
print("- playtime check: outside schedule") | |
playtime = 0 | |
# grab the sensor state if we're inside the playtime window | |
if playtime == 1: | |
try: | |
response = requests.get('http://' + HUE_BRIDGE_IP + '/api/' + USER_ID + '/sensors/' + SENSOR_ID) | |
json_data = json.loads(response.text) | |
except: | |
if DEBUGGING == 1: | |
print("- json: could not fetch json data") | |
try: | |
# is someone in the room and are we inside the playtime window? | |
if json_data['state']['presence'] == True and playtime == 1: | |
if DEBUGGING == 1: | |
print("- sensor: presence detected, play window active, connecting to robot") | |
# someone is present in the room and playtime is scheduled | |
try: | |
with anki_vector.Robot(requires_behavior_control=False) as robot: | |
# read out the battery state | |
battery_state = robot.get_battery_state() | |
stale = 0 | |
except: | |
stale += 1 | |
if DEBUGGING == 1: | |
print("- battery check failed") | |
# check if we're fully charged and on the platform | |
if battery_state.is_on_charger_platform == True and battery_state.battery_level == 3 and battery_state.is_charging == False: | |
if DEBUGGING == 1: | |
print("- robot is docked and battery is full, activating after 10 second delay") | |
# we are fully charged! drive off the platform | |
try: | |
time.sleep(10) | |
with anki_vector.Robot() as robot: | |
robot.behavior.drive_off_charger() | |
except: | |
if DEBUGGING == 1: | |
print("- leaving the charger failed") | |
else: | |
stale += 1 | |
if DEBUGGING == 1: | |
if playtime == 0: | |
pass | |
else: | |
print("- sensor: no presence detected") | |
except: | |
stale += 1 | |
if DEBUGGING == 1: | |
print("- no valid json data (normal if started up outside of scheduled playtime") | |
# print status and sleep for a bit | |
#os.system('cls' if os.name == 'nt' else 'clear') | |
if DEBUGGING == 1: | |
if stale > 0: | |
print("robot status (stale for " + str(stale) + " cycles):") | |
elif stale == 0: | |
print("robot status (fresh data):") | |
try: | |
if battery_state.is_on_charger_platform == True: | |
print("- robot is on charger") | |
else: | |
print("- robot is not on charger") | |
except: | |
print("- charger check not yet initialized") | |
try: | |
if playtime == 1: | |
print("- playtime is active") | |
else: | |
print("- playtime is not active") | |
except: | |
print("- charger check not yet initialized") | |
try: | |
if battery_state.is_charging == True: | |
print("- battery is charging") | |
else: | |
print("- battery is discharging") | |
except: | |
print("- charger check not yet initialized") | |
try: | |
if battery_state.battery_level == 1: | |
print("- battery is low") | |
elif battery_state.battery_level == 2: | |
print("- battery is nominal") | |
elif battery_state.battery_level == 3 and battery_state.is_charging == True: | |
print("- battery is nearing capacity") | |
elif battery_state.battery_level == 3 and battery_state.is_charging == False: | |
print("- battery is full") | |
else: | |
print("- battery state unknown") | |
except: | |
print("- charger check not yet initialized") | |
print("loop complete, sleeping for 10 seconds."+'\n') | |
time.sleep(10) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment