Subscribe to The Things Network (TTN) using MQTT

The Things Network (TTN) is an open platform for creating a worldwide LoRaWAN network. You can install your own LoRa gateways and register them on TTN so that other TTN users can use your gateways.

If you have devices or sensors publishing data over LoRa, you can receive this data using an MQTT broker from TTN. In this article, I will show you what you need to connect to it.

First, you should create an application in TTN Console and add a device to that application. In case you need help with that, just write a comment or send me an email!

Connect to the broker

Once you are done with configuring the application and the device, you can subscribe to the messages on MQTT. For this, you need the following things from the TTN console:

  • MQTT Broker server address: Depending on the Handler configured for your application (e.g. ttn-handler-eu), you need to use a different MQTT broker address. The last two letters are the country code, which you need. The address is as follows: <country>.thethings.network
  • Port: 1883 (this is the default MQTT port)
  • MQTT username: Application ID (this is NOT the application EUI. It is the first thing in the application console)
  • MQTT password: App-Access Key (can be found at the bottom in the TTN console for your application)

If you use these settings, you should be able to connect to the correct MQTT broker.

Subscribe to the topics of your device

Next, you need to subscribe to the desired topic. The structure of a topic string is as follows:

<AppID>/devices/<DeviceID>

and at that device level, you have the following subtopics:

  • up/: Any messages sent upstream by the device.
  • events/activations: Any messages regarding the activation of the device.

You can also subscribe to all messages from all devices using

<AppID>/devices/#

TTN describes the whole procedure in the documentation as well, you can follow this link.

Leave a Reply