import network
import socket
import time
# Setup WiFi (configure this to match your network details)
ssid = 'your_SSID'
password = 'your_password'
# Connect to Wi-Fi
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
# Wait for connection
while not wlan.isconnected():
time.sleep(1)
print("Connecting to WiFi...")
print('Connected to WiFi:', wlan.ifconfig())
# Set up a UDP socket for receiving data
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(('0.0.0.0', 12345)) # Listen on any address, port 12345
# Main loop to receive data
while True:
print("Waiting for data...")
data, addr = sock.recvfrom(1024) # Buffer size 1024 bytes
print(f"Received data: {data.decode('utf-8')} from {addr}")
time.sleep(1)