SourceCode Esp32UdpClient.py, arbeitet zusammen mit PcUdpServer.py:
# import time import network as NTW import socket as SKT # WL_SSID = 'ssid' WL_PW = 'pw' # UDP_IPADDRESS = '192.168.178.33' # '255.255.255.255' # '192.168.178.255' #'192.168.178.33' UDP_IPPORT = 5005 # #----------------------------------------------------------- if ('__main__' == __name__): # print('*** UdpClient: begin') # Wlan = NTW.WLAN(NTW.STA_IF) Wlan.active(False) Wlan.active(True) Wlan.connect(WL_SSID, WL_PW) while not(Wlan.isconnected()): pass print('Wlan-IP[{0}]'.format(Wlan.ifconfig())) # Socket = SKT.socket(SKT.AF_INET, SKT.SOCK_DGRAM) Socket.bind((UDP_IPADDRESS, UDP_IPPORT)) Socket.setblocking(0) Loop = True while (Loop): try: Data, Address = Socket.recvfrom(1024) if (b'Q' == Data): Loop = False except SKT.error: pass else: print(Data) finally: # debug print('-') time.sleep(0.1) Socket.close() # print('*** UdpClient: end') #
SourceCode Esp32UdpServer.py, arbeitet zusammen mit PcUdpClient.py:
# import time import network as NTW import socket as SKT # WL_SSID = 'ssid' WL_PW = 'pw' # UDP_IPADDRESS = '192.168.178.255' # '255.255.255.255' # '192.168.178.255' #'192.168.178.33' UDP_IPPORT = 5005 # #----------------------------------------------------------- if ('__main__' == __name__): # print('*** UdpServer: begin') # Wlan = NTW.WLAN(NTW.STA_IF) Wlan.active(False) Wlan.active(True) Wlan.connect(WL_SSID, WL_PW) while not(Wlan.isconnected()): pass print('Wlan-IP[{0}]'.format(Wlan.ifconfig())) # Socket = SKT.socket(SKT.AF_INET, SKT.SOCK_DGRAM) for I in range(0, 10): print('.') Socket.sendto(b'Hello', (UDP_IPADDRESS, UDP_IPPORT)) time.sleep(1.0) # Socket.sendto(b'Q', (UDP_IPADDRESS, UDP_IPPORT)) Socket.close() print('*** UdpServer: end') #
Ausgabe in Terminal: