That's strange. I usually run a similar UDP echo test here without problems, and UDP tests are in the standard UPD test suite. The script I use is:
Code: Select all
import socket
UDP_IP = "<server_IP_address"
UDP_PORT = 20001
MESSAGE = b' '
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.settimeout(5)
def run():
print(s.sendto(MESSAGE, (UDP_IP, UDP_PORT)))
data, address = s.recvfrom(1024)
print(data.decode())
run()
The only difference I see is the timeout being set. But that only matters for the receive part. The server is a small echo script that responds with a randomly selected aphorism. The firmware version here is:
MicroPython v1.19.1-129-g5bf376563 on 2022-07-14; Raspberry Pi Pico W with RP2040, and as well, just freshly built:
MicroPython v1.19.1-247-g5fd26c613 on 2022-08-06; Raspberry Pi Pico W with RP2040
Just for interest, my server code is below. It runs on a RPI 3, which is here as a small utility server.
Code: Select all
import socket
import random
localIP = ""
localPort = 20001
bufferSize = 1024
msg = (
"A barking dog never bites.",
"Absence makes the heart grow fonder.",
"All that glitters isn’t gold.",
"An ounce of prevention is worth a pound of cure",
"Children should be seen and not heard.",
"Doubt is the beginning, not the end, of wisdom.",
"East or west, home is best.",
"Eat to live; don't live to eat.",
"From the sublime to the ridiculous is but a step.",
"Genius is one percent inspiration and 99 percent perspiration.",
"Ignorance of the law is no excuse for breaking it.",
"Imitation is the sincerest form of flattery.",
"Possession is nine-tenths of the law.",
"The more things change, the more they stay the same.",
"The proof of the pudding is in the eating.",
"The race isn't always to the swift, nor the fight to the strong, but that's the way to bet.",
"You can lead a horse to water, but you can't make it drink.",
"You can't fight city hall.",
"Actions speak louder than words.",
"All for one and one for all.",
"Don't fire until you see the whites of their eyes.",
"Early to bed, early to rise, makes a man healthy, wealthy, and wise.",
"Give a man a fish and you feed him for a day; teach a man to fish and you feed him for a lifetime.",
"Give him an inch and he'll take a mile.",
"Give him enough rope and he'll hang himself.",
"He who fights and runs away, lives to fight another day.",
"He who hesitates is lost.",
"If you lie down with dogs, you wake up with fleas.",
"Laugh, and the world laughs with you; weep, and you weep alone.",
"You can fool some of the people all of the time, and all of the people some of the time, but you can't fool all of the people all of the time.",
"You can kill a man but you can't kill an idea.",
"You can take the boy out of the country, but you can't take the country out of the boy.",
"You made your bed, now lie in it.",
"You need to take a bull by the horns, and a man by his word.",
"A penny saved is a penny earned.",
"All things come to those who wait.",
"Don't hide your light under a bushel.",
"Don't judge a book by its cover.",
"If you do what you've always done, you'll get what you've always got.",
"Know which side your bread is buttered on.",
"He who pays the piper calls the tune.",
"Measure twice cut once."
)
# Create a datagram socket
UDPServerSocket = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
# Bind to address and ip
UDPServerSocket.bind((localIP, localPort))
print("UDP server up and listening")
# Listen for incoming datagrams
while(True):
bytesAddressPair = UDPServerSocket.recvfrom(bufferSize)
address = bytesAddressPair[1]
print("Client IP Address:{}".format(address))
# Sending a reply to client
bytesToSend = str.encode(random.choice(msg))
UDPServerSocket.sendto(bytesToSend, address)