import socket port = 10086 s=socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.bind(('192.168.0.110',port)) #绑定端口binding the port print('waiting...') while True: #接收数据receiving data data,addr=s.recvfrom(1024) print('received:',data,'from',addr)Connect esp8266 with burnt micropython to PC. Open our putty terminal in the said methods. Before doing udp, we must finish some necessary preparation. Firstly, connect esp8266 to wireless network, and then we are able to get the needed ip address.
import network[/align]import socket import time port=10086 wlan = network.WLAN(network.STA_IF) wlan.active(True) wlan.connect('dfyanfa', 'df123456') while(wlan.isconnected() == False): time.sleep(1) ip = wlan.ifconfig()[0] print(ip) s=socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1) s.bind((ip,port)) print('waiting....') while True: data,addr=s.recvfrom(1024) s.sendto(data,addr) print('received:',data,'from',addr)