Mosquitto For IOT Project Part I

Posted By : Tarun Singhal | 21-Jan-2018

Mosquitto For IOT Project Part - I

N|Solid

Mosquitto is a open-source tool used as message broker and it works on MQTT protocol version 3.1 and 3.1.1. It is very simple ad lightweight. This makes IOT possible today.

  • It is made for non-TCP/IP communication between the IoT gateways and cloud platforms.
  • Mosquitto is implemented on MQTT Protocol. MQTT is a message broker complatible for IOT implementations.
  • It supports most of the hardware such as phones, embedded computers or microcontrollers like the Arduino, Rasberry pi.
  • It can be installed on Linux, Mac and Windows also.

 

Installation!

Mosquitto is available through the main repository.

 

For ubuntu

 sudo apt-get install mosquitto

 

Configuration!

 

Enable Logging

  • vim /etc/mosquitto/mosquito.conf
log_dest <destinations>

Enter the log’s location in the destination part.

log_timestamp  true 

Enable the log timestamp.

log_type <types>

You can define the level of logging.
available levels are

  • debug
  • error
  • warning
  • notice
  • information
  • subscribe
  • unsubscribe
  • websockets
  • none
  • all

Enable Persistence

In production Don’t forget to enable the persistence.
If Persistence is On/True then data will be written on the disk in mosquitto.db

persistencen [True/False]
persistence_file <file name>

Here you can define the database name

persistence_location <path>

Here one can define the location of the database.

 

Security

Steps to be taken to enhace the security.

 

allow_anonymous [ true | false ]

If enable client need to provide the username and password to connect to the broker.

use ssl for more security.

To enable the ssl encryption

certfile <file path>
cafile <file path>
keyfile <file path>
tls_version <version>

 

Enable Authentication.

password_file <file path>

Here you can define the path of file having username and password
mosquitto accepts the username and password in the Hash Format
To Create a Hashed file.

  • create a file named password.conf
vim password.conf

Now Enter username and password in the key value format
Username1:Secret1
Username:Secret2

  • But it is in plain text to make this file compatible for mosquitto, update this file into Hash format.
  • To do so use command
mosquitto_passwd -U password.conf

After Update, your file looks like
Username1:$6$SPqxUIaJeV7c8we0$U/rF4YKOwDh2aJ97ZfP3q5HXqeBJkTmPDpwViPx1MVFNeN5Vib2ZNCbn8H542oc8A05szx+IcE3yXoxkeUwmjg==
Username:$6$i/nCIr8NuV9LKmoU$w8HxqwY5zyi7ae/Ofy9hhu1CxvsCe1cdITYkLsWofp6jnYpZhXIgTsSoAxZi/JJNCUfEXkZgBEHVVZK98w6cTw==

  • U is to update the file into Hash Format.
  • Now, when you have multiple user and you need to give distributed permission to each of them.
  • There is an ACL option for that.
acl_file <file path>

In this file you can define the acl for every user.
Mosquitto has two type of permission that is READ and WRITE.
So We can define READ/WRITE permission on each
topics userwise.
Sometime you want to define readonly permission on a particular topic, everything you can manage by acl file
user username
topic <permission> <TOPIC>

Example
user Username1
topic read /bus
topic write /train

As This Article is divided into Two or more parts. We had enough for this part. In the Next part we will focused on

  • Tunnig of mosquitto.
  • QOS levels.
  • Acl file in Deep.
  • What is Topics.
  • Websocket Integration with mosquitto.
  • How client will connect to broker.

Thanks, Hope this will help you in any way.

About Author

Author Image
Tarun Singhal

Tarun is a RedHat Certified System Administrator. He is very keen to learn new technologies. He has good command over tools like Ansible, Gitlab-CI etc.

Request for Proposal

Name is required

Comment is required

Sending message..