Install CircuitPython

The first think to do is install CircuitPython on your Funhouse. Follow this guide to get it set up with the latest release version.

Shhhh... Secrets

In order for the Funhouse to connect to the internet, you'll need to include a file on the board that contains your WiFi access point ssid and password, as well as your AIO key.

Follow this guide page to get your file set up.

Add LIFX Token to

Add your LIFX token to the file on it's own line. It should look something like this:

# This file is where you keep secret settings, passwords, and tokens!
    # If you put them in the code you risk committing that info or sharing it

secrets = {
  'ssid' : 'your_access_point',
  'password' : 'your_wifi_password',
  'lifx_token' : 'YOUR_APP_TOKEN'

Text Editor

Adafruit recommends using the Mu editor for using your CircuitPython code with the Funhouse. You can get more info in this guide.

Download the Project Bundle

Your project will use a specific set of CircuitPython libraries and the file. In order to get the libraries you need, click on the Download Project Bundle link below, and uncompress the .zip file.

Next, drag the contents of the uncompressed bundle directory onto you microcontroller board CIRCUITPY drive, replacing any existing files or directories with the same names, and adding any new ones that are necessary.

# SPDX-FileCopyrightText: 2021 John Park for Adafruit Industries
# SPDX-License-Identifier: MIT
# FunHouse PIR Motion Sensor for LIFX light bulbs
import time
import ssl
import socketpool
import wifi
import adafruit_requests
from adafruit_funhouse import FunHouse
from displayio import CIRCUITPYTHON_TERMINAL
import adafruit_lifx

# Get wifi details and more from a file
    from secrets import secrets
except ImportError:
    print("WiFi and API secrets are kept in, please add them there!")

# choose colors here. Note formatting differences.
default_bulb_color = "#002010"
default_led_color = 0x002010
tripped_bulb_color = "#440044"
tripped_led_color = 0x440044

# Set up ESP32-S2 and adafruit_requests session["ssid"], password=secrets["password"])
pool = socketpool.SocketPool(
http_session = adafruit_requests.Session(pool, ssl.create_default_context())

# Add your LIFX Personal Access token to
# (to obtain a token, visit:
lifx_token = secrets["lifx_token"]

# Set this to your LIFX light separator label
lifx_light = "label:Lamp"

# Initialize the LIFX API Client
lifx = adafruit_lifx.LIFX(http_session, lifx_token)

# List all lights
lights = lifx.list_lights()
# print(lights)  # uncomment for lots of LIFX light info

funhouse = FunHouse(default_bg=0x000F20, scale=3)

pir_state = 0
running_state = False
trip_time = 30  # seconds to stay tripped, adjust this with buttons while running


def set_label_color(conditional, index, on_color):
    if conditional:
        funhouse.set_text_color(on_color, index)
        funhouse.set_text_color(0x606060, index)

# Create the labels
funhouse.display.root_group = CIRCUITPYTHON_TERMINAL
up_label = funhouse.add_text(text="+", text_position=(3, 6), text_color=0x606060)
down_label = funhouse.add_text(text="-", text_position=(3, 40), text_color=0x606060)
running_label = funhouse.add_text(
    text="paused", text_position=(2, 68), text_color=0x606060
time_label = funhouse.add_text(
    text=trip_time, text_scale=2, text_position=(30, 25), text_color=0x606060

funhouse.display.root_group = funhouse.splash

# Turn on the light
print("Turning on light...")

# Set the light's brightness
light_brightness = 0.65
lifx.set_brightness(lifx_light, light_brightness)
    lifx_light, power="on", color=default_bulb_color, brightness=light_brightness

while True:

    if funhouse.peripherals.button_up:
        trip_time = trip_time + 1
        funhouse.set_text(trip_time, time_label)
        funhouse.set_text_color(0xFFFFFF, up_label)
        funhouse.set_text_color(0x606060, up_label)

    if funhouse.peripherals.button_sel:
        trip_time = abs(trip_time - 1)
        funhouse.set_text(trip_time, time_label)
        funhouse.set_text_color(0xFFFFFF, down_label)
        funhouse.set_text_color(0x606060, down_label)

    if funhouse.peripherals.button_down:
        if running_state is False:  # it's currently paused, so unpause it
            running_state = True  # flip the state
            funhouse.set_text("..prepping..", running_label)
            time.sleep(6)  # pause to get out of range
            funhouse.set_text("sensing...", running_label)

        else:  # it's currently running, so pause it
            running_state = False
            funhouse.set_text("paused", running_label)

    # when sensor is tripped, set the color x amount of time
    if running_state is True and funhouse.peripherals.pir_sensor and pir_state is 0:
        funhouse.set_text("tripped", running_label)
        prior_trip_time = trip_time  # store the state of the trip time value
        for _ in range(trip_time):
            trip_time = trip_time - 1
            funhouse.set_text(trip_time, time_label)
        pir_state = 1
        trip_time = prior_trip_time  # restore the trip time value

    # return to default color
    elif (
            running_state is True and not funhouse.peripherals.pir_sensor and pir_state is 1
        funhouse.set_text("sensing...", running_label)
        funhouse.set_text(trip_time, time_label)
        pir_state = 0

This guide was first published on Apr 28, 2021. It was last updated on May 27, 2024.

This page (Code the FunHouse Lighting for LIFX) was last updated on May 27, 2024.

Text editor powered by tinymce.