You can use your Slider Trinkey as a Hue dimmer switch! You can control a room, zone or individual lamps. All you need is the name of the room, zone or lamp, and the IP address of your Hue Bridge.
This example requires you to run Python code on your computer, and CircuitPython code on your Slider Trinkey.
Python Library Installation
To use this example, you need to install two Python libraries on your computer: phue and pyserial.
Run the following command in your terminal:
pip install phue pyserial
Depending on your setup, you may need to use pip3
in place of pip
!
CircuitPython Code
Save the following as code.py on your CIRCUITPY drive.
Click the Download Project Bundle button below to download the necessary libraries and the code.py file in a zip file. Extract the contents of the zip file, and copy the entire lib folder and the code.py file to your CIRCUITPY drive.
Your CIRCUITPY
# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries # # SPDX-License-Identifier: MIT import time import board from analogio import AnalogIn import adafruit_simplemath analog_in = AnalogIn(board.POTENTIOMETER) def read_pot(samples, min_val, max_val): sum_samples = 0 for _ in range(samples): sum_samples += analog_in.value sum_samples /= samples # ok take the average return adafruit_simplemath.map_range(sum_samples, 100, 65535, min_val, max_val) while True: print("Slider:", round(read_pot(10, 0, 100))) time.sleep(0.1)
Python Code
Save the following to your computer wherever is convenient for you.
Update the LAMP_OR_GROUP_NAME
variable to the name of the room, zone or lamp you would like to control. Update b.Bridge("0.0.0.0")
to the IP address of your Hue Bridge.
# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries # # SPDX-License-Identifier: MIT """ Slider Trinkey Hue Brightness Python Example (Requires Hue and Monitor Brightness CircuitPython example to be running on the Slider Trinkey) """ import sys from phue import Bridge import serial from serial.tools import list_ports # Update this to the room, zone or individual lamp you want to control. LAMP_OR_GROUP_NAME = "Office" # Update this to the IP address of your Hue Bridge. b = Bridge("0.0.0.0") slider_trinkey_port = None ports = list_ports.comports(include_links=False) for p in ports: if p.pid is not None: print("Port:", p.device, "-", hex(p.pid), end="\t") if p.pid == 0x8102: slider_trinkey_port = p print("Found Slider Trinkey!") trinkey = serial.Serial(p.device) break else: print("Did not find Slider Trinkey port :(") sys.exit() # If the app is not registered and the button on the Hue Bridge is not pressed, press the button # and call connect() (this only needs to be run a single time) b.connect() b.get_api() is_group = False light = None # First, check if it's a group name. for group_data in b.get_group().values(): if group_data["name"] == LAMP_OR_GROUP_NAME: print("Found group with name", LAMP_OR_GROUP_NAME) is_group = True # If it's not a group, find the lamp by name. if not is_group: light_names = b.get_light_objects("name") light = light_names[LAMP_OR_GROUP_NAME] print("Found light with name", LAMP_OR_GROUP_NAME) current_brightness = None while True: x = trinkey.readline().decode("utf-8") if not x.startswith("Slider: "): continue # Convert the Slider Trinkey output value of 0-100 to 0-254. brightness_value = int((float(x.split(": ")[1]) / 100) * 254) if current_brightness is None or brightness_value != current_brightness: print("Setting brightness to:", brightness_value) if is_group: b.set_group(LAMP_OR_GROUP_NAME, {"bri": brightness_value}) else: light.brightness = brightness_value current_brightness = brightness_value
Usage
Once you have the CircuitPython code running on the Slider Trinkey, you'll want to run the Python script from the same computer you have the Slider Trinkey plugged into with the following command in your terminal from within the same directory as the Python script:
python Hue_Brightness_Python_code.py
Depending on your computer setup, you may need to substitute python3
for python
. If you want to run the command from a different directory, include the path to the file with the filename.
You should see something like the following.
Now, try moving the Slider Trinkey. The lights should dim and brighten depending on the direction of the slider movement! You should see something like the following as you move it.
That's all there is to controlling your Hue lights using Python, CircuitPython and Slider Trinkey!
Text editor powered by tinymce.