If your network uses dynamic IP addresses, the IP for the light(s) could change on some frequency. For the prior examples in this guide that means you'd need to replace the IP address variable with the new IP each time it changes. If the IP changes often or you just don't like the hassle of finding the IP inside of the smartphone app you can use the scanning functionality within the Wiz CircuitPython library.
The example code below will scan the network to find all connected Wiz lights and build a mapping of them with their MAC address. MAC addresses are static, so this will not change in the future. If you build scripts around this mechanism, it will continue to work even after the IP address of your devices change.
This example loops over all of the found lights and sets each to a random color. It can be easily modified to access individual lights within the MAC address map once you see the MAC address for your device printed out once.
# SPDX-FileCopyrightText: Copyright (c) 2024 Tim Cocks for Adafruit Industries # # SPDX-License-Identifier: MIT """ Scan the network for Wiz lights. Print out the MAC addresses of any lights found. Set each light to a different random RGB color. """ import random import wifi from adafruit_wiz import WizConnectedLight, scan udp_port = 38899 # Default port is 38899 colors = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0), (0, 255, 255), (255, 0, 255)] mac_to_light_map = {} found_lights = scan(wifi.radio, timeout=1) for light_info in found_lights: mac_to_light_map[light_info["result"]["mac"]] = WizConnectedLight( light_info["ip"], udp_port, wifi.radio ) print(f"Found Light with MAC: {light_info['result']['mac']}") for mac, light in mac_to_light_map.items(): chosen_color = random.choice(colors) print(f"Setting light with MAC: {mac} to {chosen_color}") light.rgb_color = chosen_color
How Scanning Works
The lights communicate over the network with the UDP protocol. Within this protocol there is functionality to broadcast a message to all devices on the network that are listening on a specific port. The library sends a broadcast on the port that the lights listen on, each light receives that message and replies with some basic info about itself including the MAC address and current IP address.
Text editor powered by tinymce.