{"id":31721,"date":"2021-12-15T11:03:00","date_gmt":"2021-12-15T11:03:00","guid":{"rendered":"http:\/\/randomnerdtutorials.com\/?p=31721"},"modified":"2023-06-30T15:18:06","modified_gmt":"2023-06-30T15:18:06","slug":"testing-mosquitto-broker-and-client-on-raspbbery-pi","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/testing-mosquitto-broker-and-client-on-raspbbery-pi\/","title":{"rendered":"Testing Mosquitto Broker and Client on Raspberry Pi"},"content":{"rendered":"\n<p>In this tutorial, you\u2019re going to test the Mosquitto&nbsp;MQTT Broker and MQTT Client on a Raspberry Pi. You&#8217;ll subscribe the MQTT client to an MQTT topic and publish sample messages.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" width=\"1200\" height=\"675\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/01\/Mosquitto-MQTT-Client-Rpi.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Mosquitto MQTT Client Raspberry Pi\" class=\"wp-image-108413\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/01\/Mosquitto-MQTT-Client-Rpi.jpg?w=1280&amp;quality=100&amp;strip=all&amp;ssl=1 1280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/01\/Mosquitto-MQTT-Client-Rpi.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/01\/Mosquitto-MQTT-Client-Rpi.jpg?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/01\/Mosquitto-MQTT-Client-Rpi.jpg?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure><\/div>\n\n\n<p class=\"rntbox rntclgray\">U<em>pdated 23 February 2023<\/em><\/p>\n\n\n\n<p><strong>Recommended resources:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>You need a&nbsp;<a href=\"https:\/\/makeradvisor.com\/raspberry-pi-board\/\" target=\"_blank\" rel=\"noreferrer noopener\">Raspberry Pi board<\/a>&nbsp;\u2013 read&nbsp;<a href=\"https:\/\/makeradvisor.com\/best-raspberry-pi-3-starter-kits\/\" target=\"_blank\" rel=\"noreferrer noopener\">Best Raspberry Pi Starter Kits<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/installing-raspbian-lite-enabling-and-connecting-with-ssh\/\">Install Raspberry Pi OS, Set Up Wi-Fi, Enable and Connect with SSH<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/how-to-install-mosquitto-broker-on-raspberry-pi\/\">How to Install Mosquitto Broker on Raspberry Pi<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/what-is-mqtt-and-how-it-works\/\">What is MQTT and How It Works<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/getting-started-with-node-red-on-raspberry-pi\/\">Getting Started with Node-RED on Raspberry Pi<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Testing MQTT Broker Installation<\/h2>\n\n\n\n<p>After <a href=\"https:\/\/randomnerdtutorials.com\/how-to-install-mosquitto-broker-on-raspberry-pi\/\">installing MQTT&nbsp;Broker<\/a>, I recommend installing an&nbsp;MQTT Client to test the Broker installation and publish sample messages.<\/p>\n\n\n\n<p>Run the following command to&nbsp;install MQTT Mosquitto Client:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt install -y mosquitto mosquitto-clients<\/code><\/pre>\n\n\n\n<p>Run Mosquitto&nbsp;in the background as a daemon:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mosquitto -d<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Subscribing to <em>testTopic<\/em> Topic<\/h3>\n\n\n\n<p>To subscribe to an MQTT topic with Mosquitto Client open a terminal Window #1 and enter the command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mosquitto_sub -d -t testTopic<\/code><\/pre>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"661\" height=\"161\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/01\/Mosquitto-MQTT-Client-subscribe-topic.png?resize=661%2C161&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Mosquitto MQTT Client subscribe to topic\" class=\"wp-image-108414\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/01\/Mosquitto-MQTT-Client-subscribe-topic.png?w=661&amp;quality=100&amp;strip=all&amp;ssl=1 661w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/01\/Mosquitto-MQTT-Client-subscribe-topic.png?resize=300%2C73&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 661px) 100vw, 661px\" \/><\/figure><\/div>\n\n\n<p>You&#8217;re now subscribed to a topic called <span class=\"rnthl rntliteral\"><strong>testTopic<\/strong><\/span>.<\/p>\n\n\n\n<p>If you <strong>have enabled username and password on your broker<\/strong>, you need to run the command as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mosquitto_sub -d -t testTopic -u <strong>user <\/strong>-P <strong>pass<\/strong><\/code><\/pre>\n\n\n\n<p>Replace <strong>user <\/strong>with your username and <strong>pass<\/strong> with your password. For example, my username is <span class=\"rnthl rntliteral\">sara<\/span> and the password is <span class=\"rnthl rntliteral\">pass1245<\/span>, so I need to run the command as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mosquitto_sub -d -t testTopic -u <strong>sara <\/strong>-P <strong>pass1245<\/strong><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Publishing &#8220;Hello World!&#8221; Message to <em>testTopic <\/em>Topic<\/h3>\n\n\n\n<p>To publish a sample message to <span class=\"rnthl rntliteral\"><strong>testTopic<\/strong><\/span>, open a terminal Window #2 and run the following command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mosquitto_pub -d -t testTopic -m \"Hello world!\"<\/code><\/pre>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"720\" height=\"441\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/01\/mosquitto_pub.png?resize=720%2C441&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-31732\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/01\/mosquitto_pub.png?w=720&amp;quality=100&amp;strip=all&amp;ssl=1 720w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/01\/mosquitto_pub.png?resize=150%2C92&amp;quality=100&amp;strip=all&amp;ssl=1 150w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/01\/mosquitto_pub.png?resize=300%2C184&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/01\/mosquitto_pub.png?resize=80%2C49&amp;quality=100&amp;strip=all&amp;ssl=1 80w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/01\/mosquitto_pub.png?resize=220%2C135&amp;quality=100&amp;strip=all&amp;ssl=1 220w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/01\/mosquitto_pub.png?resize=163%2C100&amp;quality=100&amp;strip=all&amp;ssl=1 163w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/01\/mosquitto_pub.png?resize=245%2C150&amp;quality=100&amp;strip=all&amp;ssl=1 245w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/01\/mosquitto_pub.png?resize=389%2C238&amp;quality=100&amp;strip=all&amp;ssl=1 389w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/01\/mosquitto_pub.png?resize=678%2C415&amp;quality=100&amp;strip=all&amp;ssl=1 678w\" sizes=\"(max-width: 720px) 100vw, 720px\" \/><\/figure><\/div>\n\n\n<p>The message &#8220;<strong>Hello World!<\/strong>&#8221; is received in Window #1 as illustrated in the figure above.<\/p>\n\n\n\n<p>If you have enabled <strong>username and password on your broker<\/strong>, you need to run the command as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mosquitto_pub -d -t testTopic -m \"Hello world!\" -u <strong>user <\/strong>-P <strong>pass<\/strong><\/code><\/pre>\n\n\n\n<p> Replace <strong>user <\/strong>with your username and <strong>pass <\/strong>with your password.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Publishing a Message to Multiple Clients<\/h3>\n\n\n\n<p>Having Window #1 still subscribed to topic <span class=\"rnthl rntliteral\"><strong>testTopic<\/strong><\/span>, open a new terminal Window #3 and run this command to subscribe to <span class=\"rnthl rntliteral\"><strong>testTopic<\/strong><\/span> topic:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mosquitto_sub -d -t testTopic<\/code><\/pre>\n\n\n\n<p>If you have enabled username and password on your broker, you need to run the command as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mosquitto_sub -d -t testTopic -u <strong>user <\/strong>-P <strong>pass<\/strong><\/code><\/pre>\n\n\n\n<p> Replace <strong>user <\/strong>with your username and <strong>pass <\/strong>with your password.<\/p>\n\n\n\n<p>On Window #2 publish the &#8220;<strong>Hello World!<\/strong>&#8221; message:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mosquitto_pub -d -t testTopic -m \"Hello world!\"<\/code><\/pre>\n\n\n\n<p>If you have enabled username and password on your broker, you need to run the command as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mosquitto_pub -d -t testTopic -m \"Hello world!\" -u <strong>user <\/strong>-P <strong>pass<\/strong><\/code><\/pre>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"716\" height=\"745\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/01\/mosquitto_2_sub_one_published.png?resize=716%2C745&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-31731\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/01\/mosquitto_2_sub_one_published.png?w=716&amp;quality=100&amp;strip=all&amp;ssl=1 716w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/01\/mosquitto_2_sub_one_published.png?resize=144%2C150&amp;quality=100&amp;strip=all&amp;ssl=1 144w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/01\/mosquitto_2_sub_one_published.png?resize=288%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 288w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/01\/mosquitto_2_sub_one_published.png?resize=77%2C80&amp;quality=100&amp;strip=all&amp;ssl=1 77w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/01\/mosquitto_2_sub_one_published.png?resize=211%2C220&amp;quality=100&amp;strip=all&amp;ssl=1 211w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/01\/mosquitto_2_sub_one_published.png?resize=96%2C100&amp;quality=100&amp;strip=all&amp;ssl=1 96w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/01\/mosquitto_2_sub_one_published.png?resize=229%2C238&amp;quality=100&amp;strip=all&amp;ssl=1 229w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/01\/mosquitto_2_sub_one_published.png?resize=399%2C415&amp;quality=100&amp;strip=all&amp;ssl=1 399w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/01\/mosquitto_2_sub_one_published.png?resize=468%2C487&amp;quality=100&amp;strip=all&amp;ssl=1 468w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/01\/mosquitto_2_sub_one_published.png?resize=572%2C595&amp;quality=100&amp;strip=all&amp;ssl=1 572w\" sizes=\"(max-width: 716px) 100vw, 716px\" \/><\/figure><\/div>\n\n\n<p>Since two clients are subscribed to <span class=\"rnthl rntliteral\"><strong>testTopic<\/strong><\/span> topic, they will both receive the <strong>&#8220;Hello world!&#8221;<\/strong> message.<\/p>\n\n\n\n<p>This&nbsp;simple example shows&nbsp;how MQTT works and how your devices (for example: <a href=\"https:\/\/randomnerdtutorials.com\/esp8266-and-node-red-with-mqtt\/\" title=\"\">ESP8266 <\/a>or <a href=\"https:\/\/randomnerdtutorials.com\/esp32-mqtt-publish-subscribe-arduino-ide\/\" title=\"\">ESP32<\/a>, etc.) can be subscribed to the same topic to receive messages, or a device can publish messages to multiple devices.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Wrapping Up<\/h2>\n\n\n\n<p>In this tutorial, you checked that your Mosquitto Broker installed on the Raspberry Pi is running properly. Now, you can experiment with MQTT in your ESP32\/ESP8266 IoT projects. We have several MQTT practical examples that you can experiment with. Here are some examples:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-mqtt-publish-subscribe-arduino-ide\/\">ESP32 MQTT \u2013 Publish and Subscribe with Arduino IDE<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp8266-and-node-red-with-mqtt\/\">ESP8266 and Node-RED with MQTT (Publish and Subscribe)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-mqtt-publish-ds18b20-temperature-arduino\/\">ESP32 MQTT \u2013 Publish DS18B20 Temperature Readings (Arduino IDE)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp8266-nodemcu-mqtt-publish-ds18b20-arduino\/\">ESP8266 NodeMCU MQTT \u2013 Publish DS18B20 Temperature Readings (Arduino IDE)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/?s=mqtt\">All MQTT-related projects&#8230;<\/a><\/li>\n<\/ul>\n\n\n\n<p>You can also run Mosquitto MQTT broker on the cloud. Running an MQTT Mosquitto Broker in the cloud allows you to connect several ESP32\/ESP8266 boards and other IoT devices from anywhere using different networks as long as they have an Internet connection. Check the tutorial below:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/cloud-mqtt-mosquitto-broker-access-anywhere-digital-ocean\/\">Run Your Cloud MQTT Mosquitto Broker (access from anywhere using Digital Ocean)<\/a><\/li>\n<\/ul>\n\n\n\n<p>Do you want to learn more about Home Automation? Learn&nbsp;<strong>Node-RED<\/strong>&nbsp;and&nbsp;<strong>InfluxDB<\/strong>&nbsp;on a&nbsp;<strong>Raspberry Pi<\/strong>&nbsp;to build a Home Automation System with the&nbsp;<strong>ESP32<\/strong>&nbsp;and&nbsp;<strong>ESP8266<\/strong>. Check out our eBook: <\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/smart-home-ebook\/\" title=\"\">SMART HOME with Raspberry Pi, ESP32, and ESP8266<\/a><\/li>\n<\/ul>\n\n\n\n<p><strong>Do you have any questions?&nbsp;<\/strong><strong>Leave a comment down below!<\/strong><\/p>\n\n\n\n<p>Thanks for reading. If you like this post probably you might like my next ones, so please support me by <a href=\"https:\/\/randomnerdtutorials.com\/download\">subscribing to my blog<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you\u2019re going to test the Mosquitto&nbsp;MQTT Broker and MQTT Client on a Raspberry Pi. You&#8217;ll subscribe the MQTT client to an MQTT topic and publish sample messages. &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Testing Mosquitto Broker and Client on Raspberry Pi\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/testing-mosquitto-broker-and-client-on-raspbbery-pi\/#more-31721\" aria-label=\"Read more about Testing Mosquitto Broker and Client on Raspberry Pi\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":1,"featured_media":108413,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[304,301,225,282,283,284,264,268],"tags":[],"class_list":["post-31721","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-0-home-automation","category-0-raspberrypi","category-home-automation","category-mqtt","category-mqtt-broker","category-mqtt-client","category-project","category-raspberry-pi-project"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/01\/Mosquitto-MQTT-Client-Rpi.jpg?fit=1280%2C720&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/31721","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/comments?post=31721"}],"version-history":[{"count":8,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/31721\/revisions"}],"predecessor-version":[{"id":132606,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/31721\/revisions\/132606"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/108413"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=31721"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=31721"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=31721"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}