module:micropython:esp32udpclientserver:esp32udpclientserver02v03
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| module:micropython:esp32udpclientserver:esp32udpclientserver02v03 [2021/12/13 19:52] – [Übersicht] omdevelop | module:micropython:esp32udpclientserver:esp32udpclientserver02v03 [2022/09/13 11:58] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 9: | Line 9: | ||
| ===== Übersicht ===== | ===== Übersicht ===== | ||
| + | |||
| + | Esp32-MicroPython-Software: | ||
| + | {{: | ||
| + | |||
| ===== Benutzung ===== | ===== Benutzung ===== | ||
| Line 31: | Line 35: | ||
| import Thread as THR | import Thread as THR | ||
| import UdpClientServer as UCS | import UdpClientServer as UCS | ||
| + | # | ||
| + | WLAN_SSID = ' | ||
| + | WLAN_PW = ' | ||
| # | # | ||
| UDP_HEADERID = ' | UDP_HEADERID = ' | ||
| Line 36: | Line 43: | ||
| # UDP - TxD - TransmitData | # UDP - TxD - TransmitData | ||
| # | # | ||
| - | UDP_IPADDRESS_TX = '127.0.0.255' | + | # UDP_IPADDRESS_TX = '192.168.178.255' |
| + | UDP_IPADDRESS_TX = ' | ||
| UDP_IPPORT_TX = 5000 | UDP_IPPORT_TX = 5000 | ||
| # | # | ||
| Line 42: | Line 50: | ||
| # UDP - RxD - ReceiveData | # UDP - RxD - ReceiveData | ||
| # | # | ||
| - | UDP_IPADDRESS_LOCAL = '127.0.0.1' # dummy | + | UDP_IPADDRESS_LOCAL = '192.168.178.71' # dummy |
| - | UDP_IPPORT_RX = 5000 | + | UDP_IPPORT_RX = 5001 |
| # | # | ||
| # | # | ||
| Line 57: | Line 65: | ||
| print(Line) | print(Line) | ||
| return | 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 | # Main | ||
| Line 80: | Line 72: | ||
| # | # | ||
| 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]) | ||
| # | # | ||
| - | UdpCS = UCS.CUdpClientServer(UDP_HEADERID, | + | |
| + | # | ||
| + | | ||
| + | UDP_HEADERID, | ||
| | | ||
| - | | + | |
| - | CBOnStart, CBOnBusy, CBOnAbort, CBOnEnd) | + | |
| # | # | ||
| print(' | print(' | ||
| | | ||
| | | ||
| - | print(' | + | print(' |
| | | ||
| | | ||
| - | UdpCS.Open() | + | |
| - | for I in range(0, 6): | + | |
| - | time.sleep(5.0) | + | |
| - | UdpCS.Transmit(' | + | MillisPreset = time.ticks_ms() |
| - | time.sleep(1.0) | + | UdpCS.Transmit(' |
| UdpCS.Close() | UdpCS.Close() | ||
| # | # | ||
| Line 116: | Line 103: | ||
| # | # | ||
| import time | import time | ||
| - | import enum as ENU | ||
| import socket as SKT | import socket as SKT | ||
| - | # | + | import network as NTW |
| import Thread as THR | import Thread as THR | ||
| # | # | ||
| class CUdpClientServer(): | class CUdpClientServer(): | ||
| # | # | ||
| - | def __init__(self, | + | def __init__(self, |
| + | headerid, ipportrx, \ | ||
| | | ||
| - | | + | |
| - | | + | self.WlanSSID = wlanssid |
| + | self.WlanPW = wlanpw | ||
| self.HeaderID = headerid | self.HeaderID = headerid | ||
| self.IPAddressRXLocal = '' | self.IPAddressRXLocal = '' | ||
| Line 134: | Line 122: | ||
| self.OnTxData = ontxdata | self.OnTxData = ontxdata | ||
| self.OnRxData = onrxdata | self.OnRxData = onrxdata | ||
| - | self.OnStart = onstart | ||
| - | self.OnBusy = onbusy | ||
| - | self.OnAbort = onabort | ||
| - | self.OnEnd = onend | ||
| self.Thread = THR.CThread(self.CBOnStart, | self.Thread = THR.CThread(self.CBOnStart, | ||
| self.CBOnAbort, | self.CBOnAbort, | ||
| + | self.Wlan = None | ||
| self.RxSocket = None | self.RxSocket = None | ||
| self.TxSocket = None | self.TxSocket = None | ||
| - | # find local IPAddress: | + | # |
| - | S = SKT.socket(SKT.AF_INET, | + | self.DEBUG = False |
| - | S.connect((' | + | |
| - | self.IPAddressRXLocal | + | |
| - | S.close() | + | |
| # | # | ||
| return | return | ||
| Line 162: | Line 144: | ||
| # | # | ||
| def IsBusy(self): | def IsBusy(self): | ||
| - | return THR.EStateThread.stBusy == self.Thread.State | + | return THR.stBusy == self.Thread.State |
| + | # | ||
| + | def CBOnStart(self, | ||
| + | if self.DEBUG: | ||
| + | print(' | ||
| + | return | ||
| + | # | ||
| + | def CBOnBusy(self, | ||
| + | if self.DEBUG: | ||
| + | print(' | ||
| + | while self.IsBusy(): | ||
| + | RxData = '' | ||
| + | try: | ||
| + | Data, Address = self.RxSocket.recvfrom(1024) | ||
| + | RxData = Data.decode(' | ||
| + | except: | ||
| + | pass | ||
| + | else: | ||
| + | if self.DEBUG: | ||
| + | print(' | ||
| + | if (None != self.OnRxData): | ||
| + | self.OnRxData(self, | ||
| + | finally: | ||
| + | time.sleep(0.1) | ||
| + | self.RxSocket.close() | ||
| + | return | ||
| + | # | ||
| + | def CBOnAbort(self, | ||
| + | if self.DEBUG: | ||
| + | print(' | ||
| + | self.RxSocket.close() | ||
| + | return | ||
| + | # | ||
| + | def CBOnEnd(self, | ||
| + | if self.DEBUG: | ||
| + | print(' | ||
| + | self.RxSocket.close() | ||
| + | return | ||
| # | # | ||
| def Open(self): | def Open(self): | ||
| + | self.Wlan = NTW.WLAN(NTW.STA_IF) | ||
| + | self.Wlan.active(False) | ||
| + | self.Wlan.active(True) | ||
| + | self.Wlan.connect(self.WlanSSID, | ||
| + | while not(self.Wlan.isconnected()): | ||
| + | pass | ||
| + | self.IPAddressRXLocal = self.Wlan.ifconfig()[0] | ||
| + | if self.DEBUG: | ||
| + | print(' | ||
| + | print(' | ||
| # Rx | # Rx | ||
| - | if (THR.EStateThread.stBusy == self.Thread.State): | + | if (THR.stBusy == self.Thread.State): |
| self.Thread.Abort() | self.Thread.Abort() | ||
| self.RxSocket = SKT.socket(SKT.AF_INET, | self.RxSocket = SKT.socket(SKT.AF_INET, | ||
| Line 180: | Line 209: | ||
| # | # | ||
| def Close(self): | def Close(self): | ||
| + | if self.DEBUG: | ||
| + | print(' | ||
| self.Thread.Abort() | self.Thread.Abort() | ||
| self.RxSocket.close() | self.RxSocket.close() | ||
| Line 185: | Line 216: | ||
| # | # | ||
| def Abort(self): | def Abort(self): | ||
| + | if self.DEBUG: | ||
| + | print(' | ||
| self.Thread.Abort() | self.Thread.Abort() | ||
| return | return | ||
| # | # | ||
| def Transmit(self, | def Transmit(self, | ||
| + | if self.DEBUG: | ||
| + | print(' | ||
| if (None != self.OnTxData): | if (None != self.OnTxData): | ||
| self.OnTxData(self, | self.OnTxData(self, | ||
| self.TxSocket.sendto(text.encode(' | self.TxSocket.sendto(text.encode(' | ||
| - | | + | |
| # | # | ||
| - | def CBOnStart(self, thread): | + | def Execute(self): |
| - | | + | |
| - | self.OnStart(self) | + | # |
| - | return | + | # |
| + | # | ||
| + | # Check UdpClientServer | ||
| + | # | ||
| + | WLAN_SSID | ||
| + | WLAN_PW = ' | ||
| + | # | ||
| + | UDP_HEADERID = ' | ||
| + | # | ||
| + | # UDP - TxD - TransmitData | ||
| + | # | ||
| + | # UDP_IPADDRESS_TX = '192.168.178.255' | ||
| + | UDP_IPADDRESS_TX = ' | ||
| + | UDP_IPPORT_TX = 5000 | ||
| + | # | ||
| + | # | ||
| + | # UDP - RxD - ReceiveData | ||
| + | # | ||
| + | UDP_IPADDRESS_LOCAL = ' | ||
| + | UDP_IPPORT_RX = 5000 | ||
| + | # | ||
| + | # | ||
| + | # | ||
| + | # | ||
| + | def CBOnTxData(udpclientserver, | ||
| + | Line = ' | ||
| + | | ||
| + | | ||
| + | # | ||
| + | def CBOnRxData(udpclientserver, | ||
| + | Line = ' | ||
| + | print(Line) | ||
| + | return | ||
| + | # | ||
| + | if (' | ||
| # | # | ||
| - | | + | |
| - | while self.IsBusy(): | + | |
| - | RxData = '' | + | |
| - | try: | + | |
| - | Data, Address = self.RxSocket.recvfrom(1024) | + | |
| - | RxData = Data.decode('utf-8' | + | |
| - | except SKT.error: | + | |
| - | 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 | + | |
| # | # | ||
| - | | + | |
| - | | + | UDP_HEADERID, |
| - | if (None != self.OnAbort): | + | UDP_IPADDRESS_TX, |
| - | self.OnAbort(self) | + | |
| - | return | + | UdpCS.Open() |
| # | # | ||
| - | | + | |
| - | self.RxSocket.close() | + | UdpCS.GetIPAddressTX(), |
| - | | + | UdpCS.GetIPPortTX())) |
| - | self.OnEnd(self) | + | print(' |
| - | | + | UdpCS.GetIPAddressRXLocal(), |
| + | UdpCS.GetIPPortRX())) | ||
| # | # | ||
| - | # | + | UdpCS.Transmit(' |
| + | time.sleep(1.0) | ||
| + | UdpCS.Transmit(' | ||
| + | time.sleep(1.0) | ||
| + | UdpCS.Transmit(' | ||
| + | time.sleep(1.0) | ||
| + | # | ||
| + | UdpCS.Close() | ||
| + | # | ||
| + | print(' | ||
| + | # | ||
| + | # | ||
| </ | </ | ||
| Line 273: | Line 337: | ||
| return | return | ||
| # | # | ||
| + | |||
| + | # | ||
| # | # | ||
| # | # | ||
| Line 300: | Line 366: | ||
| if (' | if (' | ||
| # | # | ||
| - | print(' | + | print(' |
| # | # | ||
| Thread = CThread(CBOnStart, | Thread = CThread(CBOnStart, | ||
| Line 307: | Line 373: | ||
| Thread.Abort() | Thread.Abort() | ||
| # | # | ||
| - | print(' | + | print(' |
| # | # | ||
| # | # | ||
| - | </ | ||
| - | |||
| - | 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: | ||
| - | <code python> | ||
| </ | </ | ||
module/micropython/esp32udpclientserver/esp32udpclientserver02v03.1639421529.txt.gz · Last modified: 2021/12/13 20:52 (external edit)