Multicast receiver Issue



  • Hi
    I have recently started working on Pycom and am impressed with it. I want to be able to create a UDP multicast receiver to send audio files to end devices but am have great difficulty as it appears the usocket library does not have the relevant functionality.
    This is basically what I have done ... the receiver waits forever even though the is a valid multicast occurring that I can pick up on other devices:
    (I have this running on WIFI which I have left out but which is working correctly)

    import socket
    import pycom
    
    server_address = ('', 10001)
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) #supposedly automatically works out the socket cast type??
    sock.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
    sock.bind(server_address)
    
    def rxstream(item1, item2):
        try:
            print('starting rxstream')
            #sock.settimeout(5)
            while stop > 0:
                print('before')
                data, address = sock.recvfrom(64)
                print('recieved %s from %s port %s' % (data, address[0], address[1]))
        except OSError as err:
            print('unexpected exception - on rx stream socket %s' % (err))
        except:
            print('hmmm')
        finally:
            print('bye  ')
            pycom.rgbled(0x0000ff)
    
    time.sleep(1)
    _thread.start_new_thread(rxstream, (1,2))
    

    I have found on the net a multicast python example which adds a multicast group address: (https://community.particle.io/t/multicast-udp-tutorial/19900)
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)

    sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

    sock.bind((MCAST_GRP, MCAST_PORT))
    *mreq = struct.pack("4sl", socket.inet_aton(MCAST_GRP), socket.INADDR_ANY)

    sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)*

    So 2 questions ...

    1. am I missing something in the micro python code, or does the usocket lib not support multicast reception?
    2. Is there a way of utilising the python socket.py lib here and if so what version of python lib should I be including.

    thanks



  • @jon said in Multicast receiver Issue:

    import socket
    import pycom

    server_address = ('', 10001)
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) #supposedly automatically works out the socket cast type??
    sock.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
    sock.bind(server_address)

    def rxstream(item1, item2):
    try:
    print('starting rxstream')
    #sock.settimeout(5)
    while stop > 0:
    print('before')
    data, address = sock.recvfrom(64)
    print('recieved %s from %s port %s' % (data, address[0], address[1]))
    except OSError as err:
    print('unexpected exception - on rx stream socket %s' % (err))
    except:
    print('hmmm')
    finally:
    print('bye ')
    pycom.rgbled(0x0000ff)

    time.sleep(1)
    _thread.start_new_thread(rxstream, (1,2))

    It would seem this is not currently supported in micropython: https://github.com/micropython/micropython/issues/2691

    But it is supported in the ESP32 IDF so you could do this in C and make a micropython module if you wanted. See: https://github.com/espressif/esp-idf/tree/master/examples/protocols/udp_multicast


Log in to reply
 

Pycom on Twitter