Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scanning and mac map #4

Merged
merged 2 commits into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions adafruit_wiz.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,45 @@
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))
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.
Expand Down
29 changes: 29 additions & 0 deletions examples/wiz_scan_multiple_lights.py
Original file line number Diff line number Diff line change
@@ -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
Loading