How to receive data from UDP socket (over WiFi)



  • Hi everyone,
    I am trying to send UDP packet using GPy. For testing I am using service QOTD (djxmmx.net).
    On my desktop python the code if functional, on GPy no data received.

    Thank you in advance for your assistance.
    Regards, Vojtech

    code snipet from GPy

    >>> uos.uname()
    (sysname='GPy', nodename='GPy', release='1.20.2.r3', version='v1.11-d945d33ee on 2020-03-08', machine='GPy with ESP32',pybytes='1.3.1')
    
    >>> wifi1.wlan.isconnected()
    True
    >>> import socket
    >>> UDP_IP = "68.228.188.226"
    >>> UDP_PORT = 17
    >>> MESSAGE = b''
    >>> s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
    >>> s.settimeout(5)
    >>> s.sendto(MESSAGE, (UDP_IP, UDP_PORT))
    0
    >>> data, address = s.recvfrom(1024)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TimeoutError: timed out
    >>>
    

    when do same from my desktop the code is successful:

    >>> import socket
    >>> UDP_IP = "68.228.188.226"
    >>> UDP_PORT = 17
    >>> MESSAGE = b''
    >>> s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
    >>> s.settimeout(5)
    >>> s.sendto(MESSAGE, (UDP_IP, UDP_PORT))
    0
    >>> data, address = s.recvfrom(1024)
    >>> print(data.decode())
    "The secret of being miserable is to have leisure to bother about whether
     you are happy or not.  The cure for it is occupation."
     George Bernard Shaw (1856-1950)
    


  • Hi @robert-hh,
    I really appreciate the support you’ve given me. Thank you.
    Also functional on my side.

    Regards, Vojtech



  • @nadvorvo Update: on all devices this modified code works:

    import socket
    UDP_IP = "68.228.188.226"
    UDP_PORT = 17
    MESSAGE = b' '
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.settimeout(5)
    print(s.sendto(MESSAGE, (UDP_IP, UDP_PORT)))
    
    data, address = s.recvfrom(1024)
    print(data.decode())
    
    


  • @nadvorvo So I could replicate your problem. Interestingly it also exists in different flavors on other micropython (non-pycom) variants and devices.
    With Pycom MicroPython the receive fails.
    On ESP32 and Winnermicro W600 ports the send returns a timeout error, but the following receive succeeds.
    On ESP8266 and Pyboard UD is not supported.
    Big mess.

    BUT: if you do not send an empty message, but for instance a single space, it works. So there is a problem dealing with an empty packet.



  • Hi @robert-hh,
    Thank you for your reply. I am connected to same wifi network from both devices (desktop notebook, GPy). Router settings should not be problem.



  • @nadvorvo Could it be a setting of your internet router?


Log in to reply
 

Pycom on Twitter