Multicast Group Membership issue #10905
-
I'm trying to write a micropython port of some code used to allow Amazon Alexa devices to control a RP2040 by pretending to be a Philips Hue bulb - the logic of how to do this is straightforward, however I'm having issues getting the raw UDP packets... The piece causing the problem is the multicast address (239.255.255.250); packets sent to 255.255.255.255 are received, but not those to 239.255.255.250. This problem has been known for a while, and has been documented in #9105 and 'fixed' in georgerobotics/cyw43-driver#25. The solution hasn't made it back into micropython core and appears to have stalled in development. From a starting point of a clean and buildable micropython I have tried:
This results in the build error:
Has anyone had any joy on building for PR25 please? And if so, how... or is there a copy someone has please? The following is the basic replication of the issue: running it on a network with an Alexa and calling 'discover devices' should result in print output. ssid_ = 'SSID HERE'
wpa2_pass = 'PASSWORD HERE'
def do_connect():
import network
sta_if = network.WLAN(network.STA_IF)
if not sta_if.isconnected():
print("connecting to network...")
sta_if.active(True)
sta_if.connect(ssid_, wpa2_pass)
while not sta_if.isconnected():
pass
print("network config:", sta_if.ifconfig())
do_connect()
import socket
try:
from socket import sockaddr
except:
sockaddr = lambda x: x
def inet_aton(addr):
ip_as_bytes = bytes(map(int, addr.split(".")))
return ip_as_bytes
UPNP_MCAST_IP = "239.255.255.250"
UPNP_PORT = 1900
BIND_IP = "0.0.0.0"
REUSE_SOCKET = 0
serv_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
addr = socket.getaddrinfo(BIND_IP, UPNP_PORT, socket.AF_INET, socket.SOCK_DGRAM)[0][4]
serv_sock.bind(addr)
serv_sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, inet_aton(UPNP_MCAST_IP) + inet_aton(BIND_IP));
if REUSE_SOCKET:
resp_sock = serv_sock
else:
resp_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
while True:
print('Waiting for M-SEARCH...')
data, addr = resp_sock.recvfrom(1024)
if data.startswith(b"M-SEARCH"):
print("From: ", sockaddr(addr))
print(data) |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 5 replies
-
That PR25 has already been merged to cyw43-driver (you can tell by the purple merged label at the top of that PR), and the submodule in the micropython repo updated to include that commit. So, if you just build PICO_W at the current master commit of micropython, it will include that IGMP fix. That said, there may still need to be some extra changes to make it work on the |
Beta Was this translation helpful? Give feedback.
-
I tried to run the receiver on my Pico W and it doesn't receive anything. I tried with both latest stable and nightly MicroPython. And ideas what can be the issue? IP connectivity is OK, the board can be pinged. |
Beta Was this translation helpful? Give feedback.
-
This is a super simple sender
I see the messages coming in on systems with CPython on the same network, with this script:
I cannot get it working on the Pico W with the above script (obviously I changed the port in the example script)... maybe I should move to an other multicast IP, to avoid any confusion with UPnP? |
Beta Was this translation helpful? Give feedback.
-
docs/esp32: UDP MULTICAST client/server example. #13166 |
Beta Was this translation helpful? Give feedback.
That PR25 has already been merged to cyw43-driver (you can tell by the purple merged label at the top of that PR), and the submodule in the micropython repo updated to include that commit.
So, if you just build PICO_W at the current master commit of micropython, it will include that IGMP fix.
That said, there may still need to be some extra changes to make it work on the
rp2
port.