|
| 1 | +# SPDX-FileCopyrightText: 2021 John Park for Adafruit Industries |
| 2 | +# SPDX-License-Identifier: MIT |
| 3 | +# FunHouse PIR Motion Sensor for LIFX light bulbs |
| 4 | +import time |
| 5 | +import ssl |
| 6 | +import socketpool |
| 7 | +import wifi |
| 8 | +import adafruit_requests |
| 9 | +from adafruit_funhouse import FunHouse |
| 10 | +import adafruit_lifx |
| 11 | + |
| 12 | +# Get wifi details and more from a secrets.py file |
| 13 | +try: |
| 14 | + from secrets import secrets |
| 15 | +except ImportError: |
| 16 | + print("WiFi and API secrets are kept in secrets.py, please add them there!") |
| 17 | + raise |
| 18 | + |
| 19 | +# choose colors here. Note formatting differences. |
| 20 | +default_bulb_color = "#002010" |
| 21 | +default_led_color = 0x002010 |
| 22 | +tripped_bulb_color = "#440044" |
| 23 | +tripped_led_color = 0x440044 |
| 24 | + |
| 25 | +# Set up ESP32-S2 and adafruit_requests session |
| 26 | +wifi.radio.connect(ssid=secrets["ssid"], password=secrets["password"]) |
| 27 | +pool = socketpool.SocketPool(wifi.radio) |
| 28 | +http_session = adafruit_requests.Session(pool, ssl.create_default_context()) |
| 29 | + |
| 30 | +# Add your LIFX Personal Access token to secrets.py |
| 31 | +# (to obtain a token, visit: https://cloud.lifx.com/settings) |
| 32 | +lifx_token = secrets["lifx_token"] |
| 33 | + |
| 34 | +# Set this to your LIFX light separator label |
| 35 | +# https://api.developer.lifx.com/docs/selectors |
| 36 | +lifx_light = "label:Lamp" |
| 37 | + |
| 38 | +# Initialize the LIFX API Client |
| 39 | +lifx = adafruit_lifx.LIFX(http_session, lifx_token) |
| 40 | + |
| 41 | +# List all lights |
| 42 | +lights = lifx.list_lights() |
| 43 | +# print(lights) # uncomment for lots of LIFX light info |
| 44 | + |
| 45 | +funhouse = FunHouse(default_bg=0x000F20, scale=3) |
| 46 | + |
| 47 | +pir_state = 0 |
| 48 | +running_state = False |
| 49 | +trip_time = 30 # seconds to stay tripped, adjust this with buttons while running |
| 50 | + |
| 51 | +funhouse.peripherals.dotstars.fill(default_led_color) |
| 52 | + |
| 53 | + |
| 54 | +def set_label_color(conditional, index, on_color): |
| 55 | + if conditional: |
| 56 | + funhouse.set_text_color(on_color, index) |
| 57 | + else: |
| 58 | + funhouse.set_text_color(0x606060, index) |
| 59 | + |
| 60 | + |
| 61 | +# Create the labels |
| 62 | +funhouse.display.show(None) |
| 63 | +up_label = funhouse.add_text(text="+", text_position=(3, 6), text_color=0x606060) |
| 64 | +down_label = funhouse.add_text(text="-", text_position=(3, 40), text_color=0x606060) |
| 65 | +running_label = funhouse.add_text( |
| 66 | + text="paused", text_position=(2, 68), text_color=0x606060 |
| 67 | +) |
| 68 | +time_label = funhouse.add_text( |
| 69 | + text=trip_time, text_scale=2, text_position=(30, 25), text_color=0x606060 |
| 70 | +) |
| 71 | + |
| 72 | +funhouse.display.show(funhouse.splash) |
| 73 | + |
| 74 | +# Turn on the light |
| 75 | +print("Turning on light...") |
| 76 | +lifx.toggle_light(lifx_light) |
| 77 | + |
| 78 | +# Set the light's brightness |
| 79 | +light_brightness = 0.65 |
| 80 | +lifx.set_brightness(lifx_light, light_brightness) |
| 81 | +lifx.set_color( |
| 82 | + lifx_light, power="on", color=default_bulb_color, brightness=light_brightness |
| 83 | +) |
| 84 | + |
| 85 | + |
| 86 | +while True: |
| 87 | + |
| 88 | + if funhouse.peripherals.button_up: |
| 89 | + trip_time = trip_time + 1 |
| 90 | + funhouse.set_text(trip_time, time_label) |
| 91 | + funhouse.set_text_color(0xFFFFFF, up_label) |
| 92 | + time.sleep(0.2) |
| 93 | + else: |
| 94 | + funhouse.set_text_color(0x606060, up_label) |
| 95 | + |
| 96 | + if funhouse.peripherals.button_sel: |
| 97 | + trip_time = abs(trip_time - 1) |
| 98 | + funhouse.set_text(trip_time, time_label) |
| 99 | + funhouse.set_text_color(0xFFFFFF, down_label) |
| 100 | + time.sleep(0.2) |
| 101 | + else: |
| 102 | + funhouse.set_text_color(0x606060, down_label) |
| 103 | + |
| 104 | + if funhouse.peripherals.button_down: |
| 105 | + if running_state is False: # it's currently paused, so unpause it |
| 106 | + running_state = True # flip the state |
| 107 | + funhouse.set_text("..prepping..", running_label) |
| 108 | + time.sleep(6) # pause to get out of range |
| 109 | + funhouse.set_text("sensing...", running_label) |
| 110 | + |
| 111 | + else: # it's currently running, so pause it |
| 112 | + running_state = False |
| 113 | + funhouse.set_text("paused", running_label) |
| 114 | + time.sleep(0.5) |
| 115 | + |
| 116 | + # when sensor is tripped, set the color x amount of time |
| 117 | + if running_state is True and funhouse.peripherals.pir_sensor and pir_state is 0: |
| 118 | + funhouse.peripherals.dotstars.fill(tripped_led_color) |
| 119 | + funhouse.set_text("tripped", running_label) |
| 120 | + lifx.set_color( |
| 121 | + lifx_light, |
| 122 | + power="on", |
| 123 | + color=tripped_bulb_color, |
| 124 | + brightness=light_brightness, |
| 125 | + ) |
| 126 | + prior_trip_time = trip_time # store the state of the trip time value |
| 127 | + for _ in range(trip_time): |
| 128 | + time.sleep(1) |
| 129 | + trip_time = trip_time - 1 |
| 130 | + funhouse.set_text(trip_time, time_label) |
| 131 | + pir_state = 1 |
| 132 | + trip_time = prior_trip_time # restore the trip time value |
| 133 | + |
| 134 | + # return to default color |
| 135 | + elif ( |
| 136 | + running_state is True and not funhouse.peripherals.pir_sensor and pir_state is 1 |
| 137 | + ): |
| 138 | + funhouse.peripherals.dotstars.fill(default_led_color) |
| 139 | + funhouse.set_text("sensing...", running_label) |
| 140 | + lifx.set_color( |
| 141 | + lifx_light, |
| 142 | + power="on", |
| 143 | + color=default_bulb_color, |
| 144 | + brightness=light_brightness, |
| 145 | + ) |
| 146 | + funhouse.set_text(trip_time, time_label) |
| 147 | + pir_state = 0 |
0 commit comments