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

    Nano-Gateway Doubt

    Scheduled Pinned Locked Moved Comments & Feedback
    lopy
    6 Posts 3 Posters 1.2k Views 3 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.
    • M Offline
      MJ
      last edited by

      Hi there, i am trying to understand the code using in https://docs.pycom.io/chapter/tutorials/lora/lora-mac-nano-gateway.html which is for Gateway and Node. I not understand in the Gateway part :

      while (True):
          recv_pkg = lora_sock.recv(512)
          if (len(recv_pkg) > 2):
              recv_pkg_len = recv_pkg[1]
              device_id, pkg_len, msg = struct.unpack(_LORA_PKG_FORMAT % recv_pkg_len, recv_pkg)
      

      What is it mean by "(len(recv_pkg) > 2)" , "recv_pkg_len = recv_pkg[1]" ?

      In the node :

       if (len(recv_ack) > 0):
                  device_id, pkg_len, ack = struct.unpack(_LORA_PKG_ACK_FORMAT, recv_ack)
                  if (device_id == DEVICE_ID):
                      if (ack == 200):
                          waiting_ack = False
                          # If the uart = machine.UART(0, 115200) and os.dupterm(uart) are set in the boot.py this print should appear in the serial port
                          print("ACK")
      

      How about this?

      Anyone would explain how it works? Since i need to understand to implement my own code in it. Thank you

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

        @mj said in Nano-Gateway Doubt:

        Hi there, i am trying to understand the code using in https://docs.pycom.io/chapter/tutorials/lora/lora-mac-nano-gateway.html which is for Gateway and Node. I not understand in the Gateway part :

        while (True):
            recv_pkg = lora_sock.recv(512)
            if (len(recv_pkg) > 2):
                recv_pkg_len = recv_pkg[1]
                device_id, pkg_len, msg = struct.unpack(_LORA_PKG_FORMAT % recv_pkg_len, recv_pkg)
        

        What is it mean by "(len(recv_pkg) > 2)" , "recv_pkg_len = recv_pkg[1]" ?

        what's happening here is as follows:

        if (len(recv_pkg) > 2):
        

        First it is checking if the received packet of data is at least 3 bytes big because in this example packets are expected to have the following format:

        [Device ID (1 byte)] [Size of data in bytes (1 byte)] [Data (as many bytes as in the previous section)]

        recv_pkg_len = recv_pkg[1]
        

        Now that we know the packet has enough data to be valid, we get the length of the data which is in the second byte of recv_pkg (python array indexes start at zero)

        device_id, pkg_len, msg = struct.unpack(_LORA_PKG_FORMAT % recv_pkg_len, recv_pkg)
        

        This line is a bit complex so let me break it down, firstly _LORA_PKG_FORMAT % recv_pkg_len creates a format string that the struct.unpack understands. For example if recv_pkg_len is 4 the format string will be BBB4s. Finally the struct is then unpacked such that device_id = the first byte, pkg_len = the second byte and msg = a string containing the rest of the bytes.

        If in your application you need to send something that is not a string you can modify this format string to match the datatypes you need to send as per: https://docs.python.org/3/library/struct.html#format-characters

        In the node :

         if (len(recv_ack) > 0):
                    device_id, pkg_len, ack = struct.unpack(_LORA_PKG_ACK_FORMAT, recv_ack)
                    if (device_id == DEVICE_ID):
                        if (ack == 200):
                            waiting_ack = False
                            # If the uart = machine.UART(0, 115200) and os.dupterm(uart) are set in the boot.py this print should appear in the serial port
                            print("ACK")
        

        How about this?

        Anyone would explain how it works? Since i need to understand to implement my own code in it. Thank you

        So this code is similar to the previous code. When the gateway successfully receives a packet, it sends back a "ack" (acknowledgement) to the node.

        if (len(recv_ack) > 0):
                     device_id, pkg_len, ack = struct.unpack(_LORA_PKG_ACK_FORMAT, recv_ack)
        

        This is the same as before, although the > 0 should probably be == 3 because the ACK has a fixed format

        if (device_id == DEVICE_ID):
        

        Now that we know we have received a packet we need to check if it was meant for this node if not ignore it

        if (ack == 200):
            waiting_ack = False
        

        If the packet was for this node, check if the ack byte has a value of 200. The actual value of 200 has no significance, 200 was picked to match the HTTP OK status code. Once we get a ACK we set waiting_ack so the node will stop waiting for one.

        Hope this all makes sense

        M 1 Reply Last reply Reply Quote 2
        • M Offline
          MJ @seb
          last edited by

          @seb

          Can you explained more details on the

          if (len(recv_pkg) > 2): recv_pkg_len = recv_pkg[1]
          

          what i understand is if the length of received package is more than 2byte as mentioned earlier " _LORA_PKG_FORMAT = "BB%ds" and how about "recv_pkg_len = recv_pkg[1] ? The number 1 indicate? Thankyou.

          robert-hhR 1 Reply Last reply Reply Quote 0
          • robert-hhR Offline
            robert-hh @MJ
            last edited by

            @mj The answer is in any Python book: recv_pkg is a byte array, and the index [1] points to the second element of that array. In the data structure of the recv_pkg, this contains the intended length of the packet.

            M 1 Reply Last reply Reply Quote 1
            • M Offline
              MJ @robert-hh
              last edited by

              @robert-hh What is the intention on " recv_pkg_len = recv_pkg[1]" means the received package length is equal to the received package of the package length? Am i correct or wrong? Please do correct me, thank you.

              device_id, pkg_len, msg = struct.unpack(_LORA_PKG_FORMAT % recv_pkg_len, recv_pkg)

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

                @mj both recv_pkg_len and pkg_len are the same value, the second line could well have been written like this also:

                device_id, _, msg = struct.unpack(_LORA_PKG_FORMAT % recv_pkg_len, recv_pkg)
                
                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