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]" ?

    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



  • @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)
    


  • @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)



  • @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.



  • @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.



  • @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


Log in to reply
 

Pycom on Twitter