lifx_simpletest.py
1 import board 2 import busio 3 from digitalio import DigitalInOut 4 from adafruit_esp32spi import adafruit_esp32spi 5 from adafruit_esp32spi import adafruit_esp32spi_wifimanager 6 import neopixel 7 8 import adafruit_lifx 9 10 # Get wifi details and more from a secrets.py file 11 try: 12 from secrets import secrets 13 except ImportError: 14 print("WiFi and API secrets are kept in secrets.py, please add them there!") 15 raise 16 17 # ESP32 SPI 18 esp32_cs = DigitalInOut(board.ESP_CS) 19 esp32_ready = DigitalInOut(board.ESP_BUSY) 20 esp32_reset = DigitalInOut(board.ESP_RESET) 21 spi = busio.SPI(board.SCK, board.MOSI, board.MISO) 22 esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset) 23 status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2) 24 wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light) 25 26 # Add your LIFX Personal Access token to secrets.py 27 # (to obtain a token, visit: https://cloud.lifx.com/settings) 28 lifx_token = secrets["lifx_token"] 29 30 # Set this to your LIFX light separator label 31 # https://api.developer.lifx.com/docs/selectors 32 lifx_light = "label:Lamp" 33 34 # Initialize the LIFX API Client 35 lifx = adafruit_lifx.LIFX(wifi, lifx_token) 36 37 # List all lights 38 lights = lifx.list_lights() 39 40 # Turn on the light 41 print("Turning on light...") 42 lifx.toggle_light(lifx_light) 43 44 # Set the light's brightness to 50% 45 light_brightness = 0.5 46 lifx.set_brightness(lifx_light, light_brightness) 47 48 # Cycle the light using the colors of the Python logo 49 colors = ["yellow", "blue", "white"] 50 for color in colors: 51 print("Setting light to: ", color) 52 lifx.set_color(lifx_light, power="on", color=color, brightness=light_brightness) 53 54 # Turn off the light 55 print("Turning off light...") 56 lifx.toggle_light(lifx_light)