with Rapsberry pi Pico + W5100s(Ethernet) + micropython 1.18
i am making a program sending and receving data by UDP.
i print the time before it send and after it sent..
The result below
238590 239111
the time difference is 521ms
is there anyway to reduce the time?
i've trid to use uasyncio for sending part.. but results are same.
please help me to reduce the sending time.
tks
MicroPython Code
from machine import Pin, SPI, UART
import network
import usocket as socket
import ubinascii, uhashlib
import gc
import time
import _thread
UDPServer_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
receivedData = ''
GPIOThreadRunning = True
commandType = 0
CAMID = 1
isLED = 0
Protocal = 2
def init_ethernet(ip_address, subnet, gateway, dns, socket_port):
global RS485UART, GPIOThreadRunning
spi = SPI(0, 2_000_000, mosi=Pin(19), miso=Pin(16), sck=Pin(18))
nic = network.WIZNET5K(spi, Pin(17), Pin(20))
nic.ifconfig((ip_address, subnet, gateway, dns))
while not nic.isconnected():
pass
UDPServer_socket.bind((ip_address, socket_port))
print(f'Listening UDP on {ip_address} port {socket_port}')
ZoomSppedForT1Bytes = b''
while True:
gc.collect()
print("Waiting UDP....")
#udp receive data from client
data, addr = UDPServer_socket.recvfrom(30) ## buffer size is 1024 bytes
commandType = 0
if len(data) > 0 :
#cmdArr = Commands(data)
cmdArr = bytearray(data)
print('command from controller : ' + BytesToHexString(cmdArr))
if cmdArr[:4] == b'\xff\xff\xff\xff':
break
if cmdArr[:4] == b'\x02\x00\x00\x01':
#reset sequence number
replyHeader = b'\x02\x01\x00\x01'
replySeq = cmdArr[4:8]
replyCommand = cmdArr[8:]
print(time.ticks_ms())
UDPServer_socket.sendto(replyHeader+replySeq+replyCommand, addr)
print(time.ticks_ms())
else:
pass
GPIOThreadRunning = False
time.sleep(1)
def BytesToHexString(data):
return ubinascii.hexlify(data).decode()
def GPIOThread():
global GPIOThreadRunning, RS485UART, CAMID
isLED = 0
SwitchPin = Pin(1, Pin.IN)
time.sleep(1)
print('GPIO Thread is start')
while GPIOThreadRunning:
#if isLED == 0:
# STATUSLED.value(1)
# time.sleep(0.01)
if isLED == 1:
STATUSLED.value(0)
time.sleep(0.2)
STATUSLED.value(1)
time.sleep(0.2)
elif isLED == 2:
STATUSLED.value(0)
time.sleep(0.1)
STATUSLED.value(1)
time.sleep(0.1)
#switcher push:1, un-push:0
SwitcherValue = SwitchPin.value()
if __name__ == '__main__':
_thread.start_new_thread(GPIOThread, ())
ip_address = '192.168.0.201'
subnet = '255.255.255.0'
gateway = '192.168.0.1'
dns = '8.8.8.8'
socket_port = 52381
init_ethernet(ip_address, subnet, gateway, dns, socket_port)
print('#########################################')
print('here end of code')
'''
**
the result is **
> Listening UDP on 192.168.0.201 port 52381
>
> Waiting UDP....
>
> command from controller : 020000010000000001
>
> 238590
>
> 239111
>
> Waiting UDP....