Menu
Cart

MQTT Installation for Linux

MQTT can be installed on a large variety of systems. These systems can be very small single board computers up to large servers. We will give instructions for debian based systems using the APT package manager. We will use the command line since it can be used over the largest range of systems. If this doesn't work on your system go to mosquitto.org/download and follow the instructions for your system.

First open a terminal. There is usually an icon if your system has a GUI or try <CTL>+<ALT>+T or right click with the mouse and see if it is listed. If none of these ways work google "open terminal yoursystem".

Type the following commands into the terminal:

sudo apt-get update
sudo apt-get install mosquitto mosquitto-clients

 

 If this did not work follow the instructions for your system at mosquitto.org/download.

To help you understand MQTT Mosquitto and to learn how to use it and debug problems we will use two more terminal windows. If you have the monitor space arrange them so all three show.

In the first window type:    tail -f /var/log/mosquitto/mosquitto.log and you should see this:

tail command 

 What we have done is set this terminal up to follow the mosquitto log.

In the next window type:  mosquitto_sub -t 'test/1/2' which will subscribe to the test/1/2 topic.

subscribe to test topic

Next at the third window type:  mosquitto_pub -t 'test/1/2' -m 'hi' to publish a message using the test/1/2 topic with 'hi' as the payload. You will see a new message in the log window and you should see this in the publish window:

sub window has hi

You can see that the message made it from the publisher to the subscriber.

Type <CTRL>+c in the sub window and change the command to:

mosquitto_sub -d -t 'test/1/2'

and in then in the pub window type:

mosquitto_pub -d -t 'test/1/2' -m 'hello'

This is debug mode and you will see more information prints that may be useful if you have a problem and need to debug things.

We usually don't need the debug mode but when you do it is very useful.

Type <CTRL>+c in the sub window and change the command to:

mosquitto_sub -v -t 'test/1/2'

and in then in the pub window type:

mosquitto_pub -t 'test/1/2' -m 'hello world' (without the -d).

verbose mode

These are the mosquitto_sub command options we use most of the time as it shows both the topic and the payload.

Now try mosquitto_sub -v -t 'test/#' in the sub window after the <CTRL>+c.

Then type in the pub window:

mosquitto_pub -t 'test/1/2' -m 'mars'

mosquitto_pub -t 'test/2/3' -m 'venus'

wild card use

As you can see both messages were received. This is because the # symbol is a wild card. 

This completes the installation of MQTT Mosquitto.