|
| 1 | +# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries |
| 2 | +# SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +import board |
| 5 | +import busio |
| 6 | +from digitalio import DigitalInOut |
| 7 | +import wifi |
| 8 | +import socketpool |
| 9 | +import ssl |
| 10 | +import adafruit_requests |
| 11 | +import neopixel |
| 12 | + |
| 13 | +import adafruit_lifx |
| 14 | + |
| 15 | +# Get wifi details and more from a secrets.py file |
| 16 | +try: |
| 17 | + from secrets import secrets |
| 18 | +except ImportError: |
| 19 | + print("WiFi and API secrets are kept in secrets.py, please add them there!") |
| 20 | + raise |
| 21 | + |
| 22 | +# Set up ESP32-S2 and adafruit_requests session |
| 23 | +wifi.radio.connect(ssid=secrets["ssid"], password=secrets["password"]) |
| 24 | +pool = socketpool.SocketPool(wifi.radio) |
| 25 | +http_session = adafruit_requests.Session(pool, ssl.create_default_context()) |
| 26 | + |
| 27 | +# Add your LIFX Personal Access token to secrets.py |
| 28 | +# (to obtain a token, visit: https://cloud.lifx.com/settings) |
| 29 | +lifx_token = secrets["lifx_token"] |
| 30 | + |
| 31 | +# Set this to your LIFX light separator label |
| 32 | +# https://api.developer.lifx.com/docs/selectors |
| 33 | +lifx_light = "label:Lamp" |
| 34 | + |
| 35 | +# Initialize the LIFX API Client |
| 36 | +lifx = adafruit_lifx.LIFX(http_session, lifx_token) |
| 37 | + |
| 38 | +# List all lights |
| 39 | +lights = lifx.list_lights() |
| 40 | + |
| 41 | +# Turn on the light |
| 42 | +print("Turning on light...") |
| 43 | +lifx.toggle_light(lifx_light) |
| 44 | + |
| 45 | +# Set the light's brightness to 50% |
| 46 | +light_brightness = 0.5 |
| 47 | +lifx.set_brightness(lifx_light, light_brightness) |
| 48 | + |
| 49 | +# Cycle the light using the colors of the Python logo |
| 50 | +colors = ["yellow", "blue", "white"] |
| 51 | +for color in colors: |
| 52 | + print("Setting light to: ", color) |
| 53 | + lifx.set_color(lifx_light, power="on", color=color, brightness=light_brightness) |
| 54 | + |
| 55 | +# Turn off the light |
| 56 | +print("Turning off light...") |
| 57 | +lifx.toggle_light(lifx_light) |
0 commit comments