In this article we will see how to setup a Mosquitto broker for implementing MQTT communication.
1. Login to Ubuntu Linux with root privileges ( sudo )
2. Add the mosquitto repository by given below commands
$sudo apt-add-repository ppa:mosquitto-dev/mosquitto-ppa $sudo apt-get update
3. Execute the given below command to install the Mosquitto broker package
$sudo apt-get install mosquitto
4. Install Mosquitto developer libraries to develop MQTT clients
sudo apt-get install libmosquitto-dev
5. Execute the given below command to install Mosquitto client packages
$sudo apt-get install mosquitto-clients
6. Ensure that Mosquitto broker is running
$sudo service mosquitto status
For the above command, expected result is
mosquitto start/running, process 8288 where 8288 is the process id and will vary from machine to machine
Open a terminal and issue the given below command to subscribe the topic “mqtt”
$mosquitto_sub -h localhost -t "mqtt" -v
Open another terminal and issue the given below command to publish message to the topic “mqtt”
$mosquitto_pub -h localhost -t "mqtt" -m "Hello MQTT"
Now the message “Hello MQTT” will be displayed in the first terminal where the topic “mqtt” is subscribed.