{"id":922,"date":"2022-03-20T15:12:44","date_gmt":"2022-03-20T14:12:44","guid":{"rendered":"https:\/\/nguenkam.com\/blog\/?p=922"},"modified":"2022-03-20T15:17:54","modified_gmt":"2022-03-20T14:17:54","slug":"mqtt-mosquitto-server","status":"publish","type":"post","link":"https:\/\/nguenkam.com\/blog\/index.php\/2022\/03\/20\/mqtt-mosquitto-server\/","title":{"rendered":"MQTT Mosquitto server"},"content":{"rendered":"\n<p>MQTT (Message Queuing Telemetry Transport) is a messaging protocol that works on the principle of subscription \/ publication which was originally developed to simplify communication between machines and which is now an international standard for communication between machines (M2M) and objects (IoT). To save the battery of mobile devices as much as possible. MQTT consumes 11 times less energy to send messages and 170 times less to receive than the HTTP protocol. MQTT is also 93 times faster than the HTTP protocol.<\/p>\n\n\n\n<p>For this to work,  it requires a server\/broker.<\/p>\n\n\n\n<p>The command to install Mosquitto on Linux is<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt install mosquitto<\/code><\/pre>\n\n\n\n<p>Installing a client can also be very useful for testing purpose. These tests will be done with the command <em><strong>mosquitto_sub<\/strong><\/em> or <strong><em>mosquitto_pub<\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt install mosquitto-clients<\/code><\/pre>\n\n\n\n<p>Let\u00b4s check that the server\/broker is responding well &#8230; with the command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>systemctl status mosquitto<\/code><\/pre>\n\n\n\n<p>The service can be restarted with the command :<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl restart mosquitto<\/code><\/pre>\n\n\n\n<p>You can view the Broker-configuration file with :<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>more \/etc\/mosquitto\/mosquitto.conf<\/code><\/pre>\n\n\n\n<p>You will notice that you are asked to place your configuration files in <em><strong>\/etc\/mosquitto\/conf.d\/<\/strong><\/em><\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" src=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2022\/03\/image-7.png\" alt=\"\" class=\"wp-image-1754\" width=\"470\" height=\"241\" srcset=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2022\/03\/image-7.png 603w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2022\/03\/image-7-300x154.png 300w\" sizes=\"(max-width: 470px) 100vw, 470px\" \/><\/figure>\n\n\n\n<p>We can therefore create one with <em><strong>nano<\/strong><\/em>. We will name it for example <em><strong>default.conf <\/strong><\/em>with the command :<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo nano \/etc\/mosquitto\/conf.d\/default.conf<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>As an option, you can make it necessary to have a login\/password to be able to connect to Mosquitto. Let\u00b4s create one like this :<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo mosquitto_passwd -c \/etc\/mosquitto\/passwd NOM_UTILISATEUR\n\n\/\/ Then, you will be asked for your password twice, enter that too.<\/code><\/pre>\n\n\n\n<p>And declare it in the personal conf file, ie here in <em><strong>\/etc\/mosquitto\/conf.d\/default.conf<\/strong><\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>allow_anonymous false\npassword_file \/etc\/mosquitto\/passwd<\/code><\/pre>\n\n\n\n<p>You can see the logs with the command :<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tail -f \/var\/log\/mosquitto\/mosquitto.log<\/code><\/pre>\n\n\n\n<p>If you have activated the firewall on your Linux, consider authorizing port <em><strong>1883<\/strong><\/em> with a command like :<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#Autoriser le port 1883 en entr\u00e9e pour Mosquitto\niptables -t filter -A INPUT -p tcp --dport 1883 -j LOGACCEPT\necho \"Mosquitto ok\"<\/code><\/pre>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h4><span class=\"has-inline-color has-vivid-cyan-blue-color\">MOSQUITTO &#8211; TEST <\/span><\/h4>\n\n\n\n<p>We can test if the sending of messages works properly by launching 2 ssh terminals.<br>On the first, create a subscriber with a command like below.<br>(If you have not created a user to secure access to Mosquitto, it is not necessary to fill in the -u and -P options)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mosquitto_sub -h localhost -u votreuser -P lepassword -t topic\/test<\/code><\/pre>\n\n\n\n<p>In another ssh session, create a publisher and a test message with a command like this: <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mosquitto_pub -h localhost -u votreuser -P lepassword -t topic\/test -m \"Voici un test\"<\/code><\/pre>\n\n\n\n<p>The first session with the subscriber must receive the message of the second<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" src=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2022\/03\/image-8.png\" alt=\"\" class=\"wp-image-1755\" width=\"562\" height=\"179\" srcset=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2022\/03\/image-8.png 807w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2022\/03\/image-8-300x96.png 300w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2022\/03\/image-8-768x245.png 768w\" sizes=\"(max-width: 562px) 100vw, 562px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>MQTT (Message Queuing Telemetry Transport) is a messaging protocol that works on the principle of subscription \/ publication which was originally developed to simplify communication between machines and which is now an international standard for communication between machines (M2M) and objects (IoT). To save the battery of mobile devices as much as possible. MQTT consumes [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1756,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1,221],"tags":[508,234,222,223,291,224],"_links":{"self":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/922"}],"collection":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=922"}],"version-history":[{"count":4,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/922\/revisions"}],"predecessor-version":[{"id":1759,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/922\/revisions\/1759"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media\/1756"}],"wp:attachment":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=922"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=922"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=922"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}