Installing MQTT on Linux - d-sanchezl/xware GitHub Wiki
Installation
- To install MQTT (or almost any package) in Linux, the terminal is needed. Open a terminal window by clicking on this icon in the taskbar:
- Install mosquitto (the MQTT implementation that will be used) along with mosquitto-clients with the command:
sudo apt-get install mosquitto mosquitto-clients
- Now that the MQTT broker is installed, enable auto startup with the command:
sudo systemctl enable mosquitto
Verification
To verify that MQTT was installed correctly, two clients can be executed on a single Raspberry, which in turn also holds the broker.
- First, open a terminal window that will act as a 'subscriber' client. The standard syntax for 'subbing' to a topic is:
mosquitto_sub -h [address] -t [topic name in ""]
Where -h indicates the broker to which the client is connecting, 'address' stands for the IP address of the server machine ('localhost' can be used to refer to this same machine), -t indicates the Topic, and the topic name is written in quotes. Type and execute this example into the terminal:
mosquitto_sub -h localhost -t "TestTopic/SubTopic"
- Open a second terminal that will act as the 'publisher' client. The syntax for publishing a message to a given topic is:
mosquitto_pub -h [address] -t [topic name in ""] -m [message in ""]
Here, the ‘mosquitto_pub’ command is used instead, and a message is added with -m. Type and execute this example into the terminal:
mosquitto_pub -h localhost -t "TestTopic/SubTopic" -m "This is a message"
- If MQTT was installed correctly, messages sent by the publisher should reach the subscriber. In the following image, the lower window is used to send messages as the publisher, while the upper window receives said messages as the subscriber: