Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

    Multicast receiver Issue

    Scheduled Pinned Locked Moved Issues & Bugs
    multicast issue
    2 Posts 2 Posters 747 Views 2 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • J Offline
      jon
      last edited by seb

      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

      sebS 1 Reply Last reply Reply Quote 0
      • sebS Offline
        seb @jon
        last edited by

        @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

        1 Reply Last reply Reply Quote 0

        Hello! It looks like you're interested in this conversation, but you don't have an account yet.

        Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

        With your input, this post could be even better 💗

        Register Login
        • 1 / 1
        • First post
          Last post

        Pycom on Twitter

        Pycom