module:micropython:esp32udpclientserver:esp32udpclientserver02v01
Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| module:micropython:esp32udpclientserver:esp32udpclientserver02v01 [2021/12/13 11:07] – external edit 127.0.0.1 | module:micropython:esp32udpclientserver:esp32udpclientserver02v01 [2022/09/13 11:58] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== Esp32UdpClientServer | + | ====== Esp32UdpClientServer |
| [[http:// | [[http:// | ||
| [ | [ | ||
| Line 9: | Line 9: | ||
| ===== Übersicht ===== | ===== Übersicht ===== | ||
| - | * Single MicroPython-Modul **Esp32UdpClient.py** zum Empfangen von UDP-Daten | + | * Esp32(Wlan, Uart): Bidirektionales Senden und -Empfangen von UDP-Datenpaketen ohne Blocking mit Threads |
| - | * Single | + | * Programmierung mit VSCode(PyMakr) unter MicroPython |
| + | | ||
| ===== Benutzung ===== | ===== Benutzung ===== | ||
| - | * Datenaustausch zwischen UdpClient und UdpServer | ||
| - | * UdpClient(Esp32) wartet (nicht blocking!) auf Daten vom UdpServer(Pc) | ||
| - | * UdpClient(Pc) wartet (nicht blocking!) auf Daten vom UdpServer(Esp32) | ||
| - | SourceCode | + | **Esp32UdpClientServer.py** |
| <code python> | <code python> | ||
| # | # | ||
| + | # Syntax-Arguments: | ||
| + | # | ||
| + | # WindowsPC[192.168.178.33] <-> # WindowsPC[192.168.178.33]: | ||
| + | # WinPC: python CheckUdpClientServer.py Windows 4321 192.168.178.33 4321 | ||
| + | |||
| + | |||
| + | # WindowsPC[192.168.178.33] <-> UbuntuPC[192.168.178.48] | ||
| + | # WinPC: python CheckUdpClientServer.py Windows 5001 192.168.178.48 5000 | ||
| + | # LinPC: python CheckUdpClientServer.py Ubuntu | ||
| + | # | ||
| + | import sys | ||
| import time | import time | ||
| - | import network as NTW | ||
| import socket as SKT | import socket as SKT | ||
| # | # | ||
| - | WL_SSID = ' | + | import Thread as THR |
| - | WL_PW = ' | + | import UdpClientServer as UCS |
| # | # | ||
| - | UDP_IPADDRESS | + | UDP_HEADERID |
| - | UDP_IPPORT | + | #----------------------------------------------------------- |
| + | # UDP - TxD - TransmitData | ||
| + | # | ||
| + | UDP_IPADDRESS_TX = '127.0.0.255' | ||
| + | UDP_IPPORT_TX | ||
| # | # | ||
| + | # | ||
| + | # UDP - RxD - ReceiveData | ||
| + | # | ||
| + | UDP_IPADDRESS_LOCAL = ' | ||
| + | UDP_IPPORT_RX = 5000 | ||
| + | # | ||
| + | # | ||
| + | # | ||
| + | # | ||
| + | def CBOnTxData(udpclientserver, | ||
| + | Line = ' | ||
| + | print(Line) | ||
| + | return | ||
| + | # | ||
| + | def CBOnRxData(udpclientserver, | ||
| + | Line = ' | ||
| + | print(Line) | ||
| + | return | ||
| + | # | ||
| + | def CBOnStart(udpclientserver): | ||
| + | # debug print(' | ||
| + | return | ||
| + | # | ||
| + | def CBOnBusy(udpclientserver): | ||
| + | # debug print(' | ||
| + | return | ||
| + | # | ||
| + | def CBOnAbort(udpclientserver): | ||
| + | # debug print(' | ||
| + | return | ||
| + | # | ||
| + | def CBOnEnd(udpclientserver): | ||
| + | # debug print(' | ||
| + | return | ||
| + | # | ||
| + | # | ||
| + | # Main | ||
| # | # | ||
| if (' | if (' | ||
| # | # | ||
| - | print(' | + | print(' |
| + | if (5 <= len(sys.argv)): | ||
| + | # Analyse Arguments für Tx/ | ||
| + | UDP_HEADERID = sys.argv[1] | ||
| + | # RX | ||
| + | UDP_IPPORT_RX = int(sys.argv[2]) | ||
| + | # TX | ||
| + | UDP_IPADDRESS_TX = sys.argv[3] | ||
| + | UDP_IPPORT_TX = int(sys.argv[4]) | ||
| # | # | ||
| - | | + | |
| - | | + | |
| - | Wlan.active(True) | + | |
| - | | + | |
| - | while not(Wlan.isconnected()): | + | |
| - | | + | print(' |
| - | print('Wlan-IP[{0}]' | + | UdpCS.GetIPAddressTX(), |
| + | | ||
| + | | ||
| + | | ||
| + | UdpCS.GetIPPortRX())) | ||
| + | UdpCS.Open() | ||
| + | for I in range(0, 6): | ||
| + | | ||
| + | | ||
| + | time.sleep(1.0) | ||
| + | UdpCS.Close() | ||
| # | # | ||
| - | | + | print(' |
| - | Socket.bind((UDP_IPADDRESS, | + | |
| - | Socket.setblocking(0) | + | |
| - | Loop = True | + | |
| - | while (Loop): | + | |
| - | try: | + | |
| - | Data, Address = Socket.recvfrom(1024) | + | |
| - | if (b' | + | |
| - | Loop = False | + | |
| - | except SKT.error: | + | |
| - | pass | + | |
| - | else: | + | |
| - | print(Data) | + | |
| - | finally: | + | |
| - | # debug print(' | + | |
| - | time.sleep(0.1) | + | |
| - | Socket.close() | + | |
| - | # | + | |
| - | | + | |
| # | # | ||
| + | # | ||
| </ | </ | ||
| - | SourceCode | + | **UdpClientServer.py** |
| - | <code python> | + | <code python> |
| # | # | ||
| import time | import time | ||
| - | import | + | import |
| import socket as SKT | import socket as SKT | ||
| # | # | ||
| - | WL_SSID = ' | + | import Thread as THR |
| - | WL_PW = ' | + | |
| # | # | ||
| - | UDP_IPADDRESS | + | class CUdpClientServer(): |
| - | UDP_IPPORT | + | # |
| + | def __init__(self, | ||
| + | | ||
| + | | ||
| + | | ||
| + | self.HeaderID = headerid | ||
| + | self.IPAddressRXLocal | ||
| + | self.IPPortRX = ipportrx | ||
| + | self.IPAddressTX = ipaddresstx | ||
| + | self.IPPortTX = ipporttx | ||
| + | self.OnTxData = ontxdata | ||
| + | self.OnRxData = onrxdata | ||
| + | self.OnStart = onstart | ||
| + | self.OnBusy = onbusy | ||
| + | self.OnAbort = onabort | ||
| + | self.OnEnd = onend | ||
| + | self.Thread = THR.CThread(self.CBOnStart, | ||
| + | self.CBOnAbort, | ||
| + | self.RxSocket = None | ||
| + | self.TxSocket = None | ||
| + | | ||
| + | S = SKT.socket(SKT.AF_INET, | ||
| + | S.connect(('8.8.8.8', 1)) | ||
| + | self.IPAddressRXLocal = S.getsockname()[0] | ||
| + | S.close() | ||
| + | | ||
| + | return | ||
| + | # | ||
| + | def GetHeaderID(self): | ||
| + | return self.HeaderID | ||
| + | def GetIPAddressTX(self): | ||
| + | return self.IPAddressTX | ||
| + | def GetIPPortTX(self): | ||
| + | return self.IPPortTX | ||
| + | def GetIPAddressRXLocal(self): | ||
| + | return self.IPAddressRXLocal | ||
| + | def GetIPPortRX(self): | ||
| + | return self.IPPortRX | ||
| + | # | ||
| + | def IsBusy(self): | ||
| + | return THR.EStateThread.stBusy == self.Thread.State | ||
| + | # | ||
| + | def Open(self): | ||
| + | # Rx | ||
| + | if (THR.EStateThread.stBusy == self.Thread.State): | ||
| + | self.Thread.Abort() | ||
| + | self.RxSocket = SKT.socket(SKT.AF_INET, | ||
| + | # FORWARD Socket.bind !!! | ||
| + | self.RxSocket.setsockopt(SKT.SOL_SOCKET, | ||
| + | self.RxSocket.bind((self.IPAddressRXLocal, | ||
| + | self.RxSocket.setblocking(0) | ||
| + | self.Thread.Start() | ||
| + | # Tx | ||
| + | self.TxSocket = SKT.socket(SKT.AF_INET, | ||
| + | self.TxSocket.connect((self.IPAddressTX, | ||
| + | return | ||
| + | # | ||
| + | def Close(self): | ||
| + | self.Thread.Abort() | ||
| + | self.RxSocket.close() | ||
| + | self.TxSocket.close() | ||
| + | # | ||
| + | def Abort(self): | ||
| + | self.Thread.Abort() | ||
| + | return | ||
| + | # | ||
| + | def Transmit(self, | ||
| + | if (None != self.OnTxData): | ||
| + | self.OnTxData(self, | ||
| + | self.TxSocket.sendto(text.encode('utf-8' | ||
| + | | ||
| + | | ||
| + | def CBOnStart(self, | ||
| + | if (None != self.OnStart): | ||
| + | self.OnStart(self) | ||
| + | return | ||
| + | # | ||
| + | def CBOnBusy(self, | ||
| + | while self.IsBusy(): | ||
| + | RxData = '' | ||
| + | try: | ||
| + | Data, Address = self.RxSocket.recvfrom(1024) | ||
| + | RxData = Data.decode('utf-8' | ||
| + | | ||
| + | pass | ||
| + | else: | ||
| + | # print(' | ||
| + | if (None != self.OnRxData): | ||
| + | self.OnRxData(self, | ||
| + | finally: | ||
| + | time.sleep(0.1) | ||
| + | if (None != self.OnBusy): | ||
| + | self.OnBusy(self) | ||
| + | self.RxSocket.close() | ||
| + | return | ||
| + | # | ||
| + | def CBOnAbort(self, | ||
| + | self.RxSocket.close() | ||
| + | if (None != self.OnAbort): | ||
| + | self.OnAbort(self) | ||
| + | return | ||
| + | # | ||
| + | def CBOnEnd(self, | ||
| + | self.RxSocket.close() | ||
| + | if (None != self.OnEnd): | ||
| + | self.OnEnd(self) | ||
| + | return | ||
| + | # | ||
| # | # | ||
| + | </ | ||
| + | |||
| + | **Thread.py** | ||
| + | <code python> | ||
| + | import time | ||
| + | import _thread as THR | ||
| + | # States | ||
| + | stIdle = 0 | ||
| + | stBusy = 1 | ||
| + | stEnd = 2 | ||
| + | # | ||
| + | class CThread(): | ||
| + | # | ||
| + | def __init__(self, | ||
| + | self.State = stIdle | ||
| + | self.Thread = None | ||
| + | self.OnStart = onstart | ||
| + | self.OnBusy = onbusy | ||
| + | self.OnAbort = onabort | ||
| + | self.OnEnd = onend | ||
| + | return | ||
| + | # | ||
| + | def Start(self): | ||
| + | self.State = stBusy | ||
| + | if (None != self.OnStart): | ||
| + | self.OnStart(self) | ||
| + | self.ThreadID = THR.start_new_thread(self.CBOnExecute, | ||
| + | return | ||
| + | # | ||
| + | def Abort(self): | ||
| + | self.State = stEnd | ||
| + | if (None != self.OnAbort): | ||
| + | self.OnAbort(self) | ||
| + | return | ||
| + | # | ||
| + | def CBOnExecute(self, | ||
| + | if (None != self.OnBusy): | ||
| + | self.OnBusy(self) | ||
| + | if (None != self.OnEnd): | ||
| + | self.OnEnd(self) | ||
| + | return | ||
| + | # | ||
| + | # | ||
| # | # | ||
| + | # Check Thread | ||
| + | # | ||
| + | def CBOnStart(thread): | ||
| + | print(' | ||
| + | return | ||
| + | def CBOnBusy(thread): | ||
| + | print(' | ||
| + | Counter = 0 | ||
| + | while (stBusy == thread.State): | ||
| + | Counter += 1 | ||
| + | print(Counter) | ||
| + | if (3 <= Counter): | ||
| + | thread.Abort() | ||
| + | else: | ||
| + | time.sleep(1.0) | ||
| + | return | ||
| + | def CBOnAbort(thread): | ||
| + | print(' | ||
| + | return | ||
| + | def CBOnEnd(thread): | ||
| + | print(' | ||
| + | return | ||
| + | # | ||
| if (' | if (' | ||
| # | # | ||
| - | print(' | + | print(' |
| # | # | ||
| - | | + | |
| - | | + | |
| - | | + | |
| - | Wlan.connect(WL_SSID, | + | |
| - | | + | |
| - | pass | + | |
| - | print(' | + | |
| # | # | ||
| - | | + | print(' |
| - | for I in range(0, 10): | + | |
| - | print(' | + | |
| - | Socket.sendto(b' | + | |
| - | time.sleep(1.0) | + | |
| - | # | + | |
| - | Socket.sendto(b' | + | |
| - | Socket.close() | + | |
| - | | + | |
| # | # | ||
| + | # | ||
| + | </ | ||
| + | |||
| + | MicroPython-Terminal-Commands (Comments in Helper.h): | ||
| + | <code python> | ||
| + | > import os | ||
| + | > print(os.listdir()) | ||
| + | </ | ||
| + | |||
| + | **Ampy**-Commands: | ||
| + | <code python> | ||
| + | > ampy -p COM22 ls | ||
| + | </ | ||
| + | <code python> | ||
| + | > ampy -p COM22 rm / | ||
| + | </ | ||
| + | <code python> | ||
| + | > ampy -p COM22 reset | ||
| </ | </ | ||
| + | !!! NOCH ERGÄNZEN !!! | ||
| Ausgabe in Terminal: | Ausgabe in Terminal: | ||
| <code python> | <code python> | ||
| Line 110: | Line 339: | ||
| ===== Version ===== | ===== Version ===== | ||
| - | {{: | + | {{: |
| ----- | ----- | ||
module/micropython/esp32udpclientserver/esp32udpclientserver02v01.1639390044.txt.gz · Last modified: 2021/12/13 12:07 (external edit)