From ce3670f5309935f1ca2e6ae99b2887568fb269cb Mon Sep 17 00:00:00 2001 From: foamyguy Date: Mon, 23 Dec 2024 15:31:23 -0600 Subject: [PATCH 1/2] scanning and multi-light mac mapping example --- adafruit_wiz.py | 41 ++++++++++++++++++++++++++++ examples/wiz_scan_multiple_lights.py | 29 ++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 examples/wiz_scan_multiple_lights.py diff --git a/adafruit_wiz.py b/adafruit_wiz.py index ebc1af1..f0b752f 100644 --- a/adafruit_wiz.py +++ b/adafruit_wiz.py @@ -127,6 +127,47 @@ MODE_SCENE = 2 +def scan(radio=None, timeout=3): + """ + Scan the network for Wiz lights + + :param radio: The wifi radio object + :param timeout: Time in seconds to wait for responses to the scan broadcast + :return: List of dictionaries containing info about found lights + """ + if radio is None: + try: + import wifi + except ImportError: + raise RuntimeError( + "Must pass radio argument during initialization for non built-in wifi." + ) + radio = wifi.radio + pool = adafruit_connection_manager.get_radio_socketpool(radio) + with pool.socket(pool.AF_INET, pool.SOCK_DGRAM) as scan_socket: + scan_socket.settimeout(timeout) + data = json.dumps({"method": "getPilot", "params": {}}) + udp_message = bytes(data, "utf-8") + + scan_socket.sendto( + udp_message, ("255.255.255.255", 38899) + ) # send UDP packet to udp_host:port + scan_complete = False + results = [] + while not scan_complete: + try: + buf = bytearray(400) + data_len, (ip, port) = scan_socket.recvfrom_into(buf) + resp_json = json.loads(buf.rstrip(b"\x00").decode("utf-8")) + resp_json["ip"] = ip + results.append(resp_json) + except OSError: + # timeout + scan_complete = True + pass + return results + + class WizConnectedLight: """ Helper class to control Wiz connected light over WIFI via UDP. diff --git a/examples/wiz_scan_multiple_lights.py b/examples/wiz_scan_multiple_lights.py new file mode 100644 index 0000000..fc09266 --- /dev/null +++ b/examples/wiz_scan_multiple_lights.py @@ -0,0 +1,29 @@ +# 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 From 4c95b1b64215b058e73c357776fbcce21284f9cd Mon Sep 17 00:00:00 2001 From: foamyguy Date: Mon, 23 Dec 2024 15:32:46 -0600 Subject: [PATCH 2/2] remove comment --- adafruit_wiz.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/adafruit_wiz.py b/adafruit_wiz.py index f0b752f..cdd2206 100644 --- a/adafruit_wiz.py +++ b/adafruit_wiz.py @@ -149,9 +149,7 @@ def scan(radio=None, timeout=3): data = json.dumps({"method": "getPilot", "params": {}}) udp_message = bytes(data, "utf-8") - scan_socket.sendto( - udp_message, ("255.255.255.255", 38899) - ) # send UDP packet to udp_host:port + scan_socket.sendto(udp_message, ("255.255.255.255", 38899)) scan_complete = False results = [] while not scan_complete: