Created
September 27, 2016 23:15
-
-
Save mjkillough/3405db21960aab3e4520ac132440dfa4 to your computer and use it in GitHub Desktop.
Scripts for testing UDP multicast on ESP8266
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# encoding: utf-8 | |
import network | |
import socket | |
sta = network.WLAN(network.STA_IF) | |
sta.active(True) | |
sta.connect('ESSID', 'key') | |
# Enabling STA_IF is not enough, as the AP interface continues to run and MicroPython | |
# treats it as the default interface and sends all multicast packets using it. | |
# Disabling it forces the multicast packets to get sent over the STA interface. | |
# See https://github.com/micropython/micropython/issues/2198 | |
ap = network.WLAN(network.AP_IF) | |
ap.active(False) | |
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) | |
s.bind(('', 1900)) | |
s.sendto(b'some unicast data', ('your-local-machine-ip', 1900)) | |
s.sendto(b'some multicast data', ('239.255.255.250', 1900)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# encoding: utf-8 | |
import socket | |
import struct | |
MCAST_GRP = '239.255.255.250' | |
MCAST_PORT = 1900 | |
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) | |
s.bind((MCAST_GRP, MCAST_PORT)) | |
mreq = struct.pack("4sl", socket.inet_aton(MCAST_GRP), socket.INADDR_ANY) | |
s.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq) | |
try: | |
while True: | |
print("waiting for data...") | |
data, addr = s.recvfrom(100) | |
print(data, addr) | |
except KeyboardInterrupt: | |
pass | |
s.close() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# encoding: utf-8 | |
import socket | |
import struct | |
PORT = 1900 | |
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) | |
s.bind(('', PORT)) | |
try: | |
while True: | |
print("waiting for data...") | |
data, addr = s.recvfrom(100) | |
print(data, addr) | |
except KeyboardInterrupt: | |
pass | |
s.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment