Thonny
平台开发。网络调试助手
UDP
和TCP
4种通讯模式MicroPython ESP32
设备作为TCP
客户端MicroPython ESP32
设备作为TCP
服务端MicroPython ESP32
设备作为UDP
客户端MicroPython ESP32
设备作为UDP
服务端根据不同的通讯模式启用对应的代码
if __name__ == '__main__':
# tcpServer()# TCP服务端
tcpClient()# TCP客户端
# UDPServer()#UDP服务端
# UDPClient()#UDP客户端
MicroPython ESP32
设备作为TCP
客户端if __name__ == '__main__':
# tcpServer()# TCP服务端
tcpClient()# TCP客户端
# UDPServer()#UDP服务端
# UDPClient()#UDP客户端
MicroPython ESP32
设备作为TCP
服务端if __name__ == '__main__':
tcpServer()# TCP服务端
# tcpClient()# TCP客户端
# UDPServer()#UDP服务端
# UDPClient()#UDP客户端
MicroPython ESP32
设备作为UDP
客户端if __name__ == '__main__':
# tcpServer()# TCP服务端
# tcpClient()# TCP客户端
# UDPServer()#UDP服务端
UDPClient()#UDP客户端
MicroPython ESP32
设备作为UDP
服务端if __name__ == '__main__':
# tcpServer()# TCP服务端
# tcpClient()# TCP客户端
UDPServer()#UDP服务端
# UDPClient()#UDP客户端
'''
-----------------------------------------------------地址簇
usocket.AF_INET = 2 - TCP / IP - IPv4
usocket.AF_INET6 = 10 - TCP / IP - IPv6
-----------------------------------------------------套接字类型
usocket.SOCK_STREAM = 1 - TCP流
usocket.SOCK_DGRAM = 2 - UDP数据报
usocket.SOCK_RAW = 3 - 原始套接字
usocket.SO_REUSEADDR = 4 - socket可重用
-----------------------------------------------------套接字选项级别
usocket.SOL_SOCKET = 4095
-----------------------------------------------------函数
'''
import socket
import network
import time
host="192.168.0.113"# TCP/UDP客户端IP
port = 10010 #端口号
SSID="MERCURY_D268G"
PASSWORD="pba5ayzk"
wlan=None
s=None
#-----------------------------------------------------连接WIFI
def connectWifi(ssid,passwd):
global wlan
wlan=network.WLAN(network.STA_IF) #create a wlan object
wlan.active(True) #Activate the network interface
wlan.disconnect() #Disconnect the last connected WiFi
wlan.connect(ssid,passwd) #connect wifi
while(wlan.ifconfig()[0]=='0.0.0.0'):
time.sleep(1)
return True
#-----------------------------------------------------TCP消息回调函数
def tlc_msg(msg):
print(msg)
#-----------------------------------------------------TCP服务器
def tcpServer():
try:
connectWifi(SSID,PASSWORD) #连接WIFI
ip=wlan.ifconfig()[0] #获取本地IP
listenSocket = socket.socket() #创建TCP
listenSocket.bind((ip,port)) #绑定本地IP以及端口
listenSocket.listen(1) #最大连接数
listenSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) #配置TCP选项
print('Wifi connection succeeded')
print('network config:', ip)
while True:
print("accepting.....")
conn,addr = listenSocket.accept() #用户进入获取用户组
print(addr,"connected")
while True:
data = conn.recv(1024) #读取用户消息
if(len(data) == 0):
print("close socket")
conn.close() #空消息关闭用户连接
break
print(data)
ret = conn.send(data) #发送接收到的数据给发送者
except:
if(listenSocket):
listenSocket.close()
wlan.disconnect()
wlan.active(False)
#-----------------------------------------------------TCP客户端
def tcpClient():
try:
connectWifi(SSID,PASSWORD) #连接WIFI
ip=wlan.ifconfig()[0] #获取本地IP
s = socket.socket() #创建TCP
print('network config:', ip)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) #配置TCP选项
s.connect((host,port)) #连接指定TCP服务器
s.send("hello ESP32,I am TCP Client") #连接成功向服务器发送消息
while True:
data = s.recv(1024) #每次接收1024字节
if(len(data) == 0): #如果收到空消息关闭连接
print("close socket")
s.close()
break
print(data)
ret = s.send(data)
except:
if(listenSocket):
listenSocket.close()
wlan.disconnect()
wlan.active(False)
#-----------------------------------------------------UDP服务器
def UDPServer():
try:
if(connectWifi(SSID, PASSWORD) == True): #判断WIFI是否连接
s=socket.socket(socket.AF_INET, socket.SOCK_DGRAM) #创建socket对象 UDP模式
s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1) #配置socket对象
ip=wlan.ifconfig()[0] #获取本地IP
s.bind((ip,port)) #绑定本地IP 指定端口
print('network config:', ip)
print('ESP32 UDPServer Start waiting...')
while True:
data,addr=s.recvfrom(1024) #每次接收1024字节集
print('received:',data,'from',addr)
s.sendto(data,addr) #发送接收到的数据给发送者
except:
if (s):
s.close()
wlan.disconnect()
wlan.active(False)
#-----------------------------------------------------UDP客户端
def UDPClient():
try:
if(connectWifi(SSID,PASSWORD) == True): #判断WIFI是否连接
ip=wlan.ifconfig()[0]
print('network config:',ip)
s=socket.socket(socket.AF_INET, socket.SOCK_DGRAM) ##创建socket对象 UDP模式
s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1) #配置socket对象
while True:
s.sendto(b'From ESP32 UDP Sendto \r\n',(host,port)) #直接发送消息给指定IP端口
time.sleep(1)
except:
if (s):
s.close()
wlan.disconnect()
wlan.active(False)
if __name__ == '__main__':
# tcpServer()# TCP服务端
tcpClient()# TCP客户端
# UDPServer()#UDP服务端
# UDPClient()#UDP客户端