LoPy Nano-Gateway


    GO TO https://forum.pycom.io/post/3242

    Here are some code samples to put the LoPy in nano-gateway mode. This is just a code demo and you will need to change it to meet your needs.

    For this demo we are connecting 2 LoPys (nodes) to 1 LoPy in Nano-Gateway mode


    import socket
    import struct
    from network import LoRa
    # A basic package header, B: 1 byte for the deviceId, B: 1 byte for the pkg size, %ds: Formated string for string
    _LORA_PKG_FORMAT = "!BB%ds"
    # A basic ack package, B: 1 byte for the deviceId, B: 1 bytes for the pkg size, B: 1 byte for the Ok (200) or error messages
    # Open a LoRa Socket, use rx_iq to avoid listening to our own messages
    lora = LoRa(mode=LoRa.LORA, rx_iq=True)
    lora_sock = socket.socket(socket.AF_LORA, socket.SOCK_RAW)
    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)
    # 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('Device: %d - Pkg:  %s' % (device_id, msg))
            ack_pkg = struct.pack(_LORA_PKG_ACK_FORMAT, device_id, 1, 200)

    The _LORA_PKG_FORMAT is used to have a way of identifying the different devices in our network
    The _LORA_PKG_ACK_FORMAT is a simple ack package as response to the nodes package


    import os
    import socket
    import time
    import struct
    from network import LoRa
    # A basic package header, B: 1 byte for the deviceId, B: 1 bytes for the pkg size
    _LORA_PKG_FORMAT = "!BB%ds"
    DEVICE_ID = 0x01
    # Open a Lora Socket, use tx_iq to avoid listening to our own messages
    lora = LoRa(mode=LoRa.LORA, tx_iq=True)
    lora_sock = socket.socket(socket.AF_LORA, socket.SOCK_RAW)
        # Package send containing a simple string
        msg = "Device 1 Here"
        pkg = struct.pack(_LORA_PKG_FORMAT % len(msg), DEVICE_ID, len(msg), msg)
        # Wait for the response from the gateway. NOTE: For this demo the device does an infinite loop for while waiting the response. Introduce a max_time_waiting for you application
        waiting_ack = True
            recv_ack = lora_sock.recv(256)
            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
                        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("Message Failed")

    The node is always sending packages and waiting for the ack from the gateway.

    To adapt this code to your needs you might:

    1. Put a max waiting time for the ack to arrive and resend the package or mark it as invalid
    2. Increase the package size changing the _LORA_PKG_FORMAT to "BH%ds" the H will allow to keep 2 bytes for size (for more information about struct format go here)
    3. Reduce the package size with bitwise manipulation
    4. Reduce the message size (for this demo a string) to something more useful for you development

  • @colateral Hi, i am using 1 gateway that only with pymaker opened while the node i using the power bank to power up. I have try both in vice versa yet no result showing out.

  • @mj Are you using 2 instances of pymaker ? If not, describe what you did.

  • Hi there, i am still new to lopy. I have try the example of the Lora.nanogateway code (https://docs.pycom.io/chapter/tutorials/lopy/lora-mac-nano-gateway.html) with only 1 lopy as gateway and 1 lopy as node. There is no response between both of them after i have coded it. What happen to this? Thank you.

  • Hi @Roberto I tried to modify your code because I need to send a payload that contains the current time with a header containing mac address of the node and the length of the payload. Everything went well on the node side but rated nano gateway I have an Index Error: bytes index out of rang. it is because the adresse mac ? knowing that lora.mac() give : b'p\xb3\xd5I\x95kA\xf3' with length=8 but i don't understand what does mean the : b'p......' and how the length is 8 !!



    # LoRa node
    #compteur packets 
        # Package send containing a simple string
        pkg = struct.pack('!%dsBi' % len(SRC_ADR_MAC), SRC_ADR_MAC, msg_len, msg)

    nano GW

    # LoRa GW
    while (True):
        recv_pkg = lora_sock.recv(255)
        if (len(recv_pkg) > 2):
            device_adr_mac, msg_len, msg = struct.unpack('!%dsBi' %8 ,   recv_pkg) 
    # %8 because len(lora.mac())=8
            print('Device: %s - Pkg:  %s' % (device_adr_mac, msg))

  • @borjam So how you can implement a mesh network using Lora? You put the GW in both tx and rx ?

  • @borjam
    Thanks for the quick response, It's much clearer now.

  • @assia said in LoPy Nano-Gateway:

    @Colateral said in LoPy Nano-Gateway:
    Hi @Roberto i am bbegginer in LoRa and i don't understand the use of rx_iq and tx_iq.!

    It's easy. A LoRaWAN network is not a peer to peer network. There is a master (the gateway) that communicates with the nodes, but there is no node to node communication.

    Imagine that one of your nodes has the receiver active and another one transmits. The transmission is intended to be received by the gateway, of course (we said that there is no node-to-node communication) but the receiving node will get the packet anyway, if only to discard it.

    That reception and discarding has an energy cost. How do you avoid it? By having the nodes transmit in a mode that a node can't understand, only the gateway.

    Now, imagine this scenario: you make the nodes use tx_iq inversion (which is a sort of signal inversion) and the gateway use rx_iq inversion.

    In this case, a signal transmitted by a node will be inverted, and the gateway will successfully decode it. But other nodes, expecting a non inverted signal, won't detect the inverted signal. So you have just avoided the unintended packet reception problem.

  • @Colateral said in LoPy Nano-Gateway:
    Hi @Roberto i am bbegginer in LoRa and i don't understand the use of rx_iq and tx_iq.!

  • @Colateral
    Thanks for the quick response, i'll try a blocking approach to see if i can reproduce the error and check whats the root of it.

  • @Roberto HI Roberto. Thanks. I ran the last script and is running fine. Regarding the memory leak, I believe is related with socket blocking approach. If we change the script and move to non blocking (as your script does) everything works fine.

  • Hi @Colateral
    As i wrote in my last message, there is a new example of the nano-gateway with a non-blocking loop approach.
    Regarding the memory issues we will look into this. I left the code on the other post running for 3 hours without any problems. I will leave it running today on a test bench for 24+ h to see if there are any problems and monitor the memory while it transmits. Will let you know the results.

    The link to the new code is

  • @Roberto Your example it is a good start but looping and blocking might not be a good approach.

    We built another script that is using threads with blocking socket ... and run the script on a minimal Lora "star" network: one GW and 2 NODES. For each device you have to configure tx,rx and device id in cfg file . and upload the script and cfg file on it and run the script in Putty (not Pymakr). We didn't faced with this script clashing issues.. but we faced other issues.

    You will see that after awhile on GW, the script is ending with a memory leak... but this is never happening on the nodes.
    If you run the script from 3 Pymakr instances ... sometimes is working for hours and after that the GW is stop sending.

    We conclude that is something wrong on the stack.

    The issue might be related only to rx_iq configuration.

  • Hi @feabhas
    yeah, that sounds about right. Change that please. Ill edit the post accordingly Thanks

  • @Roberto I have successfully got a 2 node and 1 gateway setup working based on your code, thanks very much. One minor query, In the gateway you use

    _LORA_PKG_FORMAT = "!BB%ds"

    but in the node_1.py you miss off the !, e.g.

    _LORA_PKG_FORMAT = "BB%ds"

    Shouldn't they be consistent?

  • Hi @Colateral,

    Yes, i have noticed that sometimes some messages arrive malformed or with bytes missing. This could be due to LoRa message collision between devices. If two devices send at the same time in the same band this can happen.

    There is a new blog here in the forum with a new code suggestion that includes.

    1. Message length check on the nano-gateway
    2. Message retry on the nodes
    3. Max timeout in the nodes

    Number 3 will help with the fact that you have to have the nano-gateway on before you start the nodes. In this code, after sending a message the node waits in an infinite loop for the ack. The problem with this approach is that if no ack is received (the nano-gateway was not connected yet) the node will stay in that loop until reseted.

    Keep in ming that both this and the new code are just examples for specific usages and they require adaptation depending on your needs.
    The link for the new post is LoPy Nano-Gateway Extended (Timeout and Retry)


  • @Roberto We setup a test bench with 3 LoPy following the idea of your code. We did the test with blocking socket and thread and non blocking sockets. The result is the same.

    2 are the nodes configured as tx_iq and and one is the Gw (rx_iq).

    The script is simple: each device (node and GW) is sending 29 bytes message size (_MSG_FORMAT = "!HHLBB%ds") each 2 seconds. In the msg data we are printing a counter (that varies from device). That counter is also in the header (see above)

    We noticed that sometimes if you start the gw after the nodes... randomly some of the nodes are not receiving the data, despite the fact the GW is receiving the data from the nodes. And we observed also that sometimes when we start the node after the GW, the GW is not receiving the data from the nodes.

    You need to reset the node or GW in order to get the broadcasted data from to the others... and this hazardous and not at all good in real deployment.

    BUT THE MOST PROBLEMATIC is the fact that the 29bytes are partially received, sometimes you are getting 15bytes or whatever number less than 29bytes.

    Have you experience this connection issue related with nodes/gw starting order?
    Does LoPy stack is guarantee the receiving of msg in one chunk? ( if you send 29bytes you got 29bytes)

    Here are some samples:
    good msg receive

    from: 800
    to: 0
    Id: 3058
    Flags: 0
    PayloadSize: 18
    Payload: b'3058- 1234567890 '
    <<< beat msgId 3066
    bufferSize 29
    pkgSize 28
    data_size 18
    remaining buffer 0

    bad msg receive

    from: 800
    to: 0
    Id: 3059
    Flags: 0
    PayloadSize: 18
    Payload: b'3059- 1234567890 '
    bufferSize 15
    buffer too small <<< ustruct exception

  • @gertjanvanhethof I'm also interrested to use LoPy as a LoRaWan gateway. Looking forward to a solution.

  • This post is deleted!

  • Hi,

    On "Nano-Gateway" with the latest firmware 1.3.0.b1 i'm still getting "ValueError: buffer too small" after hour of two running posted sample code. Is this problem with the sample code or is it firmware problem?

Log in to reply

Pycom on Twitter