Bridge The Things Network to your local MQTT broker

The Things Network is an interesting global open LoRaWAN network. I have a LoRaWAN gateway running at home, The Things Indoor Gateway, and some LoRaWAN temperature and humidity sensors in the garden and the garden shed. The sensors wirelessly send a measurement every 20 minutes, and the gateway picks it up and forwards it to the network server of The Things Network.

The Things Network can be integrated with many other platforms to process these sensor measurements. For instance, there's a thethingsnetwork integration in Home Assistant and a (deprecated) node-red-contrib-ttn node in Node-RED. Even more interesting is the MQTT API, which perfectly fits in my MQTT-based home automation system. [1]

For instance, you can easily subscribe to your sensor measurements with mosquitto_sub:

$ mosquitto_sub -h eu.thethings.network -p 8883 --cafile mqtt-ca.pem -u APPID -P APPKEY -t '#' | jq

In The Things Network Console, navigate to the application you'd like to subscribe to. Here you can find the Application ID (APPID) and an Access Key (APPKEY) needed to authenticate over MQTT. Under Handler you find the region the application is registered to. You will need the part that follows ttn-handler-. For instance, if it's eu, connect to eu.thethings.network.

Of course you're doing this over TLS, so you need to download The Things Network's PEM-encoded CA certificate mqtt-ca.pem.

After a while, a JSON payload such as the following one should appear on the topic APPID/devices/DEVID/up:

{
  "app_id": "APPID",
  "dev_id": "dragino-lht65-1",
  "hardware_serial": "AAAAAAAAAAAAAAAA",
  "port": 2,
  "counter": 22608,
  "payload_raw": "z18FWhLpQZMly/5=",
  "payload_fields": {
    "BatV": 2.95,
    "Hum_SHT": "91.0",
    "TempC_DS": "1.56",
    "TempC_SHT": "1.15"
  },
  "metadata": {
    "time": "2020-12-04T19:38:02.270923288Z",
    "frequency": 867.1,
    "modulation": "LORA",
    "data_rate": "SF7BW125",
    "airtime": 61696000,
    "coding_rate": "4/5",
    "gateways": [
      {
        "gtw_id": "eui-9999999999999999",
        "timestamp": 1184233211,
        "time": "2020-12-04T19:38:02.214832067Z",
        "channel": 0,
        "rssi": -91,
        "snr": 9.5,
        "rf_chain": 0
      }
    ]
  }
}

The sensor's measurement values (battery voltage, the internal sensor's humidity and temperature, and the temperature of an externally connected DS18B20) are in the payload_fields object.

Configuring a bridge

However, I already have an MQTT broker running at home, Eclipse Mosquitto, and all my home automation devices are using it. Having to use a second MQTT broker on some of these devices would be cumbersome. Luckily, that's not necessary, as Mosquitto has a quite powerful feature, a bridge. It basically means that you connect two brokers so messages to one broker are forwarded to the other one and/or the other way around.

To bridge your local Mosquitto server to The Things Network's MQTT broker, add the following section to your mosquitto.conf file:

# Bridge to The Things Network
connection bridge
address eu.thethings.network:8883
remote_username APPID
remote_password APPKEY
bridge_cafile /mosquitto/config/certs/ttn-ca.pem
bridge_insecure false
topic # both 0 ttn/ APPID/devices/

Make sure to refer to the correct path of the CA certificate file. The line topic # both 0 ttn/ APPID/devices/ means: your MQTT broker subscribes to the remote topic APPID/devices/# on The Things Network and republishes the received messages to ttn/#. This also works the other way around: if you publish something to a subtopic of ttn/, it will be republished to The Things Network with the prefix APPID/devices/.

Because the bridge goes both ways, this means that uplink messages (sensor measurements from your LoRaWAN devices like the one above) are forwarded to your local MQTT broker and downlink messages (commands) that you send to your local MQTT broker are forwarded to The Things Network and then to your LoRaWAN device.

If you want to subscribe to more than one The Things Network application, that's easy: just add another bridge section with the right application ID, application key and remapping for the topics. If you want a different remapping of the topic trees, have a look at the man page for mosquitto.conf and search for the section Configuring Bridges. The man page is very good and you'll see in this section that there are a lot of options to configure the bridge.

After this configuration change, restart Mosquitto. You can now subscribe to the right topic in your local MQTT broker, and your local MQTT dashboard or other home automation software has access to the devices managed by The Things Network.