How to use LoPy NanoGateway and local MQTT Broker Mosquitto

  • I am new to micropython and have been going over the examples of the Nano-Gateway connecting to public MQTT brokers like TTN. I would like to use the LoPy Nano-Gateway to connect LoPy MQTT client nodes through the Nano-Gateway to the local network to a Mosquitto MQTT broker with a static IP address.

    I was looking at the LoRaWAN Nano-Gateway example HERE to modify and point to the local MQTT broker with the static IP address.

    My understanding is that the Nano-Gateway can act as a proxy to connect LoRa nodes (with sensors) to the Nano-Gateway and publish to the MQTT broker on the local network.

    In the example code, the file shows the server address and port to forward received data to. I think this is where I would enter the MQTT static IP address and port number. The LoPy MQTT client nodes would then publish topics. In the example code, I don't know if I need either the OTAA (over the air activation) or ABP (Activation By Personalization) in my case.

    Can I just publish topics to the targeted MQTT broker through the Nano-Gateway?

    I searched the forums to find other examples of using the LoPy Nano-Gateway to communicate with a local MQTT broker via WiFi but did not find any. There was an example using the WiPy, but the example did not directly translate to the LoPy Nano-Gateway.

    Question: is this the best example code to work from or is there something more straight forward?

  • Thanks @ahaw021

    I am still wrapping my mind around how to go from sensor/MQTT-->LoPy send via LoRa to a LoPy acting as a RAW LoRa Nano-Gateway/LoPy--> that then forwards the message over wifi on local network.

    LoPy/LoRa Node --> LoPy/LoRa NanoGateway (recieves and forwards via WiFi)-->LoPy/WiFi NanoGateway --> local network.

    I apologize that I have not been very clear in my posts. In the LoPy NanoGateway discussion
    "The nodes send messages to the Nano-Gateway for further processing, this might include sending the messages over WiFi to a server for example."

    I want to forward messages over WiFi. The MQTT example HERE uses WiFi to send client messages to the MQTT Broker.

    Each LoRa/LoPy node needs to send the data to the NanoGateway, the NanoGateway needs to send it to the MQTT Broker.

    I don't know if there is a simple programmatic way to assemble and send the messages/topics from the NanoGateway to the MQTT Broker. Ideally it would just publish topics/data based on the message payload from the LoPy node. However, this hard codes publishing topics in node code.

    In the LoPy RAW NanoGateway example HERE the node code the message payload (Sensor Data Here) could include sensor data that the NanoGateway would publish the topic to the MQTT broker.

        msg = "Sensor Data Here"
        pkg = struct.pack(_LORA_PKG_FORMAT % len(msg), DEVICE_ID, len(msg), msg)

    It seems like this would work, but I would like the message payload to be inclusive like

        msg = "client.publish("/sensors/sensor8/print(temp)")"
        pkg = struct.pack(_LORA_PKG_FORMAT % len(msg), DEVICE_ID, len(msg), msg)

    I am not sure of a proper syntax to send in the message payload for parsing and forwarding. Perhaps just the topic e.g. /sensors/sensor8/temp, print(temp)

    The NanoGateway would receive the message payload and publish the topic with something like:

    client.publish("/sensors/sensor8/temp", "72.3")

    This would hard code each LoRa/LoPy sensor node with its topics, which is not very flexible. And I think this is just on way, not sure how to forward a node subscribing back via LoRa from the NanoGateway.

    Has anyone done anything like this?

    Any feedback is appreciated.

  • @ledbelly2142

    I think we may be going around in circles

    A LoRaWAN server is not an MQTT server

    For example

    LoPy -> LoRaWAN Gateway (maybe a LoPy) - this is OTAA and ABP
    LoRaWAN Gateway via TCP/UDP -> LoRaWAN Server (this is in line with LoRaWAN Protocol)
    LoRaWAN Server -> MQTT/HTTP <- external services

    The things network for example is a distributed LoRaWan Server

    The functionalities of the LoRaWAN server and an mqtt broker are not exchangeable

    You cannot use a LoRaWAN gateway with the MQTT protocol


  • @ahaw021
    Thank you for your response. I agree, I need an MQTT broker. I have installed mosquitto from
    It has the static IP address I mentioned in my post earlier. I tested MQTT broker with development clients and tools, so I'm pretty sure it works for publishing and subscribing. I was looking at using the MQTT library discussed in the tutorial for the WiPy HERE. It uses the UMQTT library located on HERE I'm confident the micropython would work as an additional to some or all of the node libraries, the ABP (Activation By Personalization) or OTAA (Over The Air Activation) in the Nano-Gateway example HERE .

    I think you are right that the Pycom MQTT library will work better. Using the PyCom MQTT lib as a starting point for for the Nano-Gateway makes more sense.

    I am asking for some guidance on how to mash up these examples to have the LoPy nodes publish through the Nano-Gateway to an existing MQTT broker on a private network.

  • hi @ledbelly2142

    I would suggest a gentle review of the MQTT protocol

    What you are missing is that there is a broker and a client. The brokers are significantly more complex than just the clients.

    If you have a look at the pycom library and specifically the MQTT library you will see that only a client has been implemented

    So you will not be able to run a broker unless you include a 3rd party library.


Pycom on Twitter