Not able to send Multicast messages from ESP8266
Simple multicast message example that doesn't work on the ESP8266:
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
data = "test data".encode('utf-8')
sock.sendto(data, ('239.0.0.10', 3535))
works with micropython on osx though.
I was experimenting with an Nodemcu v2 ESP8266
MicroPython v1.8.1-31-g9de5eb2 on 2016-06-10
Pico W - Multicast receive stops working until after `machine.reset()`
Issue Description
When testing sending multicast packets back and forth between my Pi Pico W and my Windows 10 machine, the Pico will initially work, receiving packets sent to the same multicast group until you CTRL-C, hit stop, or leave it sitting a while. Eventually you will get into a state where it doesn't receive the packets anymore, though it is still listening and presumably still a member of the multicast group.
After this point, the only way to get things working again is to machine.reset() or physically power cycle the device. Once you do, things will work again. I've confirmed the packets are all sending via wireshark even after the Pico stops receiving things.
I found https://github.com/micropython/micropython/issues/10812 which looks similar, but is resolved by using SO_REUSEADDR, which I am doing, but still have the issue.
The most reliable way to reproduce:
- In Thonny, run the pico code
- In Windows Terminal, run the sender
- See it receive on the pico side. hit stop in Thonny.
- Hit start again (the sender script should still be going)
- It may work for 5ish more packets then stop. If not stop and start another time or two
- Eventually you will be unable to receive the multicast packets, even if you call
sock.close()in the repl and make a new socket - Curiously, if I send a packet to the pico directly, or to the broadcast address (192.168.1.255) it will be received, even when in this state
Pico W Code (Receiver)
import network
import socket
from time import sleep
import machine
ssid = 'SSID'
password = 'PASSWORD'
def connect():
#Connect to WLAN
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
while wlan.isconnected() == False:
print('Waiting for connection...')
sleep(1)
ip = wlan.ifconfig()[0]
print(f'Connected on {ip}')
return ip
def inet_aton(addr):
return bytes(map(int, addr.split(".")))
###
ip = connect()
MCAST_GRP = '224.1.5.16'
MCAST_PORT = 9242
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, inet_aton(MCAST_GRP) + inet_aton("192.168.1.72"))
sock.bind(('', MCAST_PORT))
try:
while True:
print('waiting to receive message...')
data, address = sock.recvfrom(16)
print(f"received {len(data)} bytes from {address}")
print(f"{data}")
finally:
sock.close()
Windows Code (Sender)
import time
import socket
MCAST_GRP = '224.1.5.16'
MCAST_PORT = 9242
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
s.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 2)
s.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_IF, socket.inet_aton("192.168.1.206"))
for i in range(100):
s.sendto(b'some data1 %i' % i, (MCAST_GRP, MCAST_PORT))
time.sleep(0.33)
s.close()
Hardware
- MicroPython v1.19.1-1016-gb525f1c9e on 2023-04-14; Raspberry Pi Pico W with RP2040