Mosquitto
Installation
On Debian-based systems the MQTT broker and MQTT clients can be installed as follows:
apt install mosqutto mosqutto-clients
On Arch-based systems the MQTT broker and MQTT clients can be installed as follows:
pacman -S mosquitto
Post Installation
In order to enable the MQTT broker so it starts on every boot, we have to enable
it via systemctl
:
systemctl enable mosquitto.service
For starting the service run:
systemctl start mosquitto.service
Systemd Commands
Command | Description |
---|---|
systemctl start mosquitto.service |
Start the mosquitto service |
systemctl restart mosquitto.service |
Restart the mosquitto service |
systemctl reload mosquitto.service |
Reload the mosquitto service configuration |
systemctl stop mosquitto.service |
Stop the mosquitto service |
systemctl status mosquitto.service |
Service status of mosquitto |
systemctl disable mosquitto.service |
Disable the mosquitto service |
systemctl enable mosquitto.service |
Enable the mosquitto service |
Simple Testing
Now that we have installed Mosquitto, it is time to test the Mosquitto
broker. Fortunately, testing MQTT, once it is installed, is pretty simple thanks
to the MQTT command line tools. Here, we can use the mosquitto clients
mosquitto_sub
and mosquitto_pub
to send and receive messages.
With the command mosquitto_sub
, we can create a MQTT subscriber that listens to specific topics.
For instance, the command below starts a MQTT subscriber that listens to the topic home/#
and the sub-topics like home/kitchen
.
The symbol #
in the topic home/#
is a multi-level wildcard that matches topics like home
, home/kitchen
, home/kitchen/temperature
, and so on.
mosquitto_sub -h zeus.internal -t "home/#" -v
For sending messages, like JSON strings, we can leverage the mosquitto_pub
client.
As the command below shows, we start a MQTT publisher that publishes once
the JSON message '{ "temperature":"..." }' to the topic "home".
mosquitto_pub -h zeus.internal -t "home" -m '{ "measurement": {"value": 25.0, "unit": "Celsius" } }'