Problems with "socket" module Pico W [Resolved]

RP2040 based microcontroller boards running MicroPython.
Target audience: MicroPython users with an RP2040 boards.
This does not include conventional Linux-based Raspberry Pi boards.
EmilyPearson
Posts: 10
Joined: Sat Aug 06, 2022 1:44 am
Location: Austin, TX

Problems with "socket" module Pico W [Resolved]

Post by EmilyPearson » Sat Aug 06, 2022 2:01 am

I have discovered that MicroPython code that executes correctly on an ESP32/ESP-01 pair does not work at all on my PicoWs with this MicroPython build:
MicroPython v1.19.1 on 2022-08-02; Raspberry Pi Pico W with RP2040

Code: Select all

# UDP Talker
import socket

UDP_IP = "10.0.1.XX"	#must be the ip address:port of listener
UDP_PORT = 5500
MESSAGE = b"Hello, World!"

print("UDP target IP: %s" % UDP_IP)
print("UDP target port: %s" % UDP_PORT)
print("message: %s" % MESSAGE)

sock = socket.socket(socket.AF_INET, # Internet
                     socket.SOCK_DGRAM) # UDP
sock.sendto(MESSAGE, (UDP_IP, UDP_PORT))

Code: Select all

# UDP listener
import socket

UDP_IP = "10.0.1.XX"	# Must be the ip address:port of listener
UDP_PORT = 5500

sock = socket.socket(socket.AF_INET, # Internet
                     socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))

while True:
    data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
    print("received message: %r" % data)
As mentioned above, the script works in both directions on an ESP32/ESP-01 pair but not on a pair of PicoWs.

Has there been anything reported about a problem with the "socket" module or does anyone see a reason why the script shouldn't run on PicoWs?

Emily
Last edited by EmilyPearson on Sat Aug 06, 2022 9:58 pm, edited 1 time in total.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Problems with "socket" module Pico W

Post by Roberthh » Sat Aug 06, 2022 6:30 am

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)
   

EmilyPearson
Posts: 10
Joined: Sat Aug 06, 2022 1:44 am
Location: Austin, TX

Re: Problems with "socket" module Pico W

Post by EmilyPearson » Sat Aug 06, 2022 6:03 pm

Thanks for the reply. I don’t get any different response using your code.

I do appreciate the tuple of aphorisms. :D I’ll make fun use of those!

Perhaps things will magically change in a later nightly build as has happened in the past for this port.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Problems with "socket" module Pico W

Post by Roberthh » Sat Aug 06, 2022 6:51 pm

That's strange. I had tested that with a July firmware and a recently built firmware. Both built by myself. Just now I tried an Image from the download site, which also works fine with the UDP test.

EmilyPearson
Posts: 10
Joined: Sat Aug 06, 2022 1:44 am
Location: Austin, TX

Re: Problems with "socket" module Pico W

Post by EmilyPearson » Sat Aug 06, 2022 7:16 pm

That's strange. I had tested that with a July firmware and a recently built firmware. Both built by myself. Just now I tried an Image from the download site, which also works fine with the UDP test.
PicoWs?

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Problems with "socket" module Pico W

Post by Roberthh » Sat Aug 06, 2022 7:23 pm

Yes. Pico W.

EmilyPearson
Posts: 10
Joined: Sat Aug 06, 2022 1:44 am
Location: Austin, TX

Re: Problems with "socket" module Pico W

Post by EmilyPearson » Sat Aug 06, 2022 7:34 pm

Yes. Pico W.
Huh! Well I'll keep working at it but it is weird to me that I can just swap boards and interpreters in Thonny and run the identical script on the ESP modules and it works as expected.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Problems with "socket" module Pico W

Post by Roberthh » Sat Aug 06, 2022 7:46 pm

I never use Thonny. I upload the scripts to the boards using the mpremote.py tool. See https://github.com/micropython/micropyt ... s/mpremote or use pip.

EmilyPearson
Posts: 10
Joined: Sat Aug 06, 2022 1:44 am
Location: Austin, TX

Re: Problems with "socket" module Pico W

Post by EmilyPearson » Sat Aug 06, 2022 8:24 pm

I never use Thonny. I upload the scripts to the boards using the mpremote.py tool. See https://github.com/micropython/micropyt ... s/mpremote or use pip.
I'll give that a try.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Problems with "socket" module Pico W

Post by Roberthh » Sat Aug 06, 2022 8:39 pm

Sorry for asking a for the obvious: did you connect your Pico W to WiFi, e.g. an Access Point before testing?

Post Reply