Receiving UDP from ESP32
This commit is contained in:
parent
4d9e72b70c
commit
74d6625184
|
@ -1,13 +1,19 @@
|
|||
import socket
|
||||
import struct
|
||||
|
||||
def start_server(ip, port):
|
||||
server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
server_socket.bind((ip, port))
|
||||
server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
|
||||
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # Enable port reusage
|
||||
server_socket.bind(('', port)) # Bind to the port, not the multicast IP
|
||||
print(f"UDP server listening on {ip}:{port}")
|
||||
|
||||
# Join the multicast group
|
||||
mreq = struct.pack("4sl", socket.inet_aton(ip), socket.INADDR_ANY)
|
||||
server_socket.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
|
||||
|
||||
while True:
|
||||
data, addr = server_socket.recvfrom(1024) # buffer size is 1024 bytes
|
||||
print(f"received message: {data} from {addr}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
start_server("localhost", 8888)
|
||||
start_server("239.1.2.3", 1234)
|
||||
|
|
|
@ -2,8 +2,8 @@ import socket
|
|||
import time
|
||||
|
||||
# UDP Configuration
|
||||
ip = "localhost" # Change this to the IP address of your computer or the network address to multicast
|
||||
port = 8888 # Ensure this matches the port number in your Unity script
|
||||
ip = "239.1.2.3" # Change this to the IP address of your computer or the network address to multicast
|
||||
port = 1234 # Ensure this matches the port number in your Unity script
|
||||
|
||||
# Create the UDP socket
|
||||
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
|
|
Loading…
Reference in New Issue