MQTT<\/strong> is a publish\/subscribe model based, “lightweight” messaging protocol over TCP\/IP for communication between “Internet of Things” devices such as ESP8266, Raspberry Pi, etc. It is very popular with low resources and battery powered applications such as home automation, security alarm systems and battery-powered sensor networks.<\/p>\n\n\n\n Mosquitto<\/strong> is an open source message broker (or server) that implements MQTT protocols. With its good community support, documentation, and ease of installation it has become one of the most popular MQTT brokers.<\/p>\n\n\n\n Update Ubuntu’s package list and install the latest Mosquitto Broker available from it<\/p>\n\n\n\n The Mosquitto service will start after installation.<\/p>\n\n\n\n Mosquitto clients help us easily test MQTT through a command line utility. We will use two command windows, one to subscribe to a topic named Topics<\/strong> are labels used by the broker to filter messages for each connected client. A client program subscribed to a topic Login to the terminal as a second instance and publish a message to the Here the additional parameter “ Mosquitto comes with a password file generating utility called Create a configuration file for Mosquitto pointing to the password file we have just created.<\/p>\n\n\n\n This will open an empty file. Paste the following into it.<\/p>\n\n\n\n Save and exit the text editor with “ Now restart Mosquitto server and test our changes.<\/p>\n\n\n\n In the subscribe client window, press “ Note the capital -P here<\/em>.<\/p>\n\n\n\n In the publish client window, try to publish a message without a password.<\/p>\n\n\n\n The message will be rejected with following error message.<\/p>\n\n\n\n Now publish a message with the Hit “ We have now set up a password protected MQTT server. You can use the Public IP of your Ubuntu server as an MQTT broker for your projects.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"","format":"standard","manualknowledgebasecat":[242,245,247],"manual_kb_tag":[],"_links":{"self":[{"href":"https:\/\/support.aklwebhost.com\/wp-json\/wp\/v2\/manual_kb\/3750"}],"collection":[{"href":"https:\/\/support.aklwebhost.com\/wp-json\/wp\/v2\/manual_kb"}],"about":[{"href":"https:\/\/support.aklwebhost.com\/wp-json\/wp\/v2\/types\/manual_kb"}],"author":[{"embeddable":true,"href":"https:\/\/support.aklwebhost.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/support.aklwebhost.com\/wp-json\/wp\/v2\/comments?post=3750"}],"version-history":[{"count":1,"href":"https:\/\/support.aklwebhost.com\/wp-json\/wp\/v2\/manual_kb\/3750\/revisions"}],"predecessor-version":[{"id":3751,"href":"https:\/\/support.aklwebhost.com\/wp-json\/wp\/v2\/manual_kb\/3750\/revisions\/3751"}],"wp:attachment":[{"href":"https:\/\/support.aklwebhost.com\/wp-json\/wp\/v2\/media?parent=3750"}],"wp:term":[{"taxonomy":"manualknowledgebasecat","embeddable":true,"href":"https:\/\/support.aklwebhost.com\/wp-json\/wp\/v2\/manualknowledgebasecat?post=3750"},{"taxonomy":"manual_kb_tag","embeddable":true,"href":"https:\/\/support.aklwebhost.com\/wp-json\/wp\/v2\/manual_kb_tag?post=3750"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}Prerequisites<\/h2>\n\n\n\n
\n
root<\/code> access<\/li>\n\n\n\n
TCP:1883<\/code> on firewall<\/li>\n<\/ul>\n\n\n\n
Step One: Install Mosquitto Broker<\/h2>\n\n\n\n
sudo apt-get update\n\nsudo apt-get install mosquitto\n<\/code><\/pre>\n\n\n\n
Step Two: Install the Clients and Test<\/h2>\n\n\n\n
Install MQTT clients<\/h3>\n\n\n\n
sudo apt-get install mosquitto-clients\n<\/code><\/pre>\n\n\n\n
\"test\"<\/code> and one to publish a message to it.<\/p>\n\n\n\n
\"Home1\/BedroomTemp\"<\/code> will only listen to messages published to the same topic by other clients.<\/p>\n\n\n\n
Subscribe to topic
\"test\"<\/code><\/h3>\n\n\n\n
mosquitto_sub -t \"test\"\n<\/code><\/pre>\n\n\n\n
Mosquito_sub<\/code> is a subscribe client we installed in the previous command. Here we are specifying “
-t<\/code>” followed by a topic name.<\/p>\n\n\n\n
Publish a message to topic
\"test\"<\/code><\/h3>\n\n\n\n
\"test\"<\/code> topic.<\/p>\n\n\n\n
mosquitto_pub -m \"message from mosquitto_pub client\" -t \"test\"\n<\/code><\/pre>\n\n\n\n
\u00e2\u20ac\u201cm<\/code>” is followed by the message we want to publish. Hit “
Enter<\/code>” and you should see a message from
mosquitto_pub client<\/code> displayed in other terminal where
mosquito_sub client<\/code> is running.<\/p>\n\n\n\n
Step Three: Secure with a Password<\/h2>\n\n\n\n
mosquitto_passwd<\/code>.<\/p>\n\n\n\n
sudo mosquitto_passwd -c \/etc\/mosquitto\/passwd dave\n\nPassword: password\n<\/code><\/pre>\n\n\n\n
sudo nano \/etc\/mosquitto\/conf.d\/default.conf\n<\/code><\/pre>\n\n\n\n
allow_anonymous false\n\npassword_file \/etc\/mosquitto\/passwd\n<\/code><\/pre>\n\n\n\n
Ctrl+O<\/code>“, “
Enter<\/code>” and “
Ctrl+X<\/code>“.<\/p>\n\n\n\n
sudo systemctl restart mosquitto\n<\/code><\/pre>\n\n\n\n
Ctrl+C<\/code>” to exit the subscribe client and restart it with following command.<\/p>\n\n\n\n
mosquitto_sub -t \"test\" -u \"dave\" -P \"password\"\n<\/code><\/pre>\n\n\n\n
mosquitto_pub -t \"test\" -m \"message from mosquitto_pub client\"\n<\/code><\/pre>\n\n\n\n
Connection Refused: not authorised.\n\nError: The connection was refused.\n<\/code><\/pre>\n\n\n\n
username<\/code> and
password<\/code>.<\/p>\n\n\n\n
mosquitto_pub -t \"test\" -m \"message from mosquitto_pub client\" -u \"dave\" -P \"password\"\n<\/code><\/pre>\n\n\n\n
Enter<\/code>” and you will see the message in subscribe client window, as in Step Two.<\/p>\n\n\n\n
Conclusion<\/h2>\n\n\n\n