Eclipse Mosquitto is a project which provides an open source MQTT broker, a C and C++ library for MQTT client implementations and the popular command line MQTT clients.
Its lightweight MQTT protocol implementation makes it suitable for full power machines, as well as for the low power and embedded ones.
This guide shows how to receive upstream messages and send downlink messages with the Eclipse Mosquitto command line clients and The Things Stack MQTT Server.
Note:
Eclipse Mosquitto MQTT server supports 3.1, 3.1.1 and 5.0 MQTT protocol versions.The examples in this guide are suitable for The Things Stack Open Source deployment. If you are using a different The Things Stack deployment, make sure your read a Note on Using the tenant ID.
Prerequisites
- Eclipse Mosquitto MQTT server installed on your system.
Subscribing to Upstream Traffic
This section follows the example for subscribing to upstream traffic in the MQTT Server guide.
The command for connecting to a host and subscribing to a topic has using mosquitto_sub has the following syntax:
mosquitto_sub -h <hostname> -p <port> -u <username> -P <password> -t <topic>
For example, to subscribe to all topics in the application app1:
# Tip: when using `mosquitto_sub`, pass the `-d` flag to see the topics messages get published on.
# For example:
mosquitto_sub -h "thethings.example.com" -p "1883" -u "app1" -P "NNSXS.VEEBURF3KR77ZR.." -t "#" -d
In you want to use TLS, you need to change the port value to 8883 and add the --cafile option to the command. --cafile option is used to define a path to the file containing trusted CA certificates that are PEM encoded.
Read more about the command line options in the mosquitto_sub manual.
Publishing Downlink Messages
This section follows the example for publishing downlink traffic in the MQTT Server guide. See Publishing Downlink Messages for a list of available topics.
For connecting to a host and publishing a message, mosquitto_pub client defines a command with the following syntax:
mosquitto_pub -h <hostname> -p <port> -u <username> -P <password> -t <topic> -m <message>
For example, to send an unconfirmed downlink message to the device dev1 in application app1 with the hexadecimal payload BE EF on FPort 15 with normal priority, use the topic v3/app1/devices/dev1/down/push with the following contents:
mosquitto_pub -h "thethings.example.com" -p "1883" -u "app1" -P "NNSXS.VEEBURF3KR77ZR.." -t "v3/app1/devices/dev1/down/push" -m '{"downlinks":[{"f_port": 15,"frm_payload":"vu8=","priority": "NORMAL"}]}'
Note:
For scheduling downlink messages, thef_port values from 1 to 233 are allowed.
If TLS is being used, change the port value to 8883 and add the --cafile option to the command.
Read more about the command line options in the mosquitto_pub manual.