IoT Worlds
MQTT
BlogSmart Device

The MQTT Meaning To Connect IoT Devices With Web Applications

With all the available messaging protocols, it can be hard to choose just one. And with so many options out there, it’s all too easy to find yourself in a situation where you try one that doesn’t work for you or your team. Mqtt is a lightweight protocol that only requires clients and brokers, so it’s perfect for situations where you don’t want any hassle. This article will explore what this protocol is capable of and how it achieves these things!

What is MQTT?

Mqtt is a lightweight messaging protocol that only requires clients and brokers. It’s perfect for situations where you don’t want any hassle or fuss, because it requires so little from you.

What is a messaging protocol?

A messaging protocol is a set of rules used to allow applications on different machines to communicate. Mqtt was specifically designed for the internet of things (IoT). With such a wide range of other protocols available, it may seem odd to create another one for this purpose. But, since so many devices and services rely on this field, we’ll likely be seeing more and more of them over time!

What’s so great about MQTT?

Mqtt is truly unique, because it gives you so much with next to nothing. You don’t need a dedicated server or forwarder like many other protocols do, which means no infrastructure headaches either. All you need is an already-existing broker and any hardware with an internet connection. Thanks to the quality of its work and how light it is, this protocol has been growing increasingly popular. Because it’s open source and free to use, you can easily get started without having to pay any fees or subscription costs. And with broker support built into the MQTT library for Arduino, we’re excited to see what great products will be coming our way.

What are the features of MQTT?

Mqtt is one of the best examples of a simple, lightweight messaging protocol you’ll find. Its main selling point is how easy it is to use – any device with an internet connection can take advantage of what this protocol has to offer. But there’s more than just ease of use to this protocol. Let’s explore what else you can expect from it!

Examples of MQTT use

a. IoT devices in a factory controlling pumps and valves

b. A weather station with a sensor reporting to a database on the cloud

c. An alarm system that reports when doors open or close

d. Some lights in an office turning on when motion is detected

1. What is MQTT and what are its features?

MQTT stands for Message Queuing Telemetry Transport, which might sound complicated but simply means it’s a way for IoT devices to communicate with each other using the internet. It runs on top of TCP/IP like HTTP, SMTP etc., but unlike HTTP which is for communicating over the internet, MQTT is used with devices that are connected to each other with a network, usually an IP network. It’s most commonly used in IoT with low powered embedded devices that connect to things like Raspberry Pi or Arduino boards.

2. How does it work?

It works kind of like email. An IoT device publishes a message to a topic. It’s like sending an email to someone, but instead of a person it’s a topic name. The message can have up to 2kb of data in it and is sent as a packet.

A second MQTT subscriber listens for packets with its own unique ID or Client ID. It then does something with the message. It’s like when someone receives an email, they do something with it; read it, sent it somewhere else etc.

MQTT has a Quality of Service (QoS) level which means there can be different priority levels for messages. The QoS level is set by the client and each packet sent has that level of priority.

3. Benefits of MQTT

It’s lightweight, runs on TCP/IP, less overhead than HTTP

Messages are stored in a queue until they can be sent to their destination device

It has built-in reconnection support

Each client can have its own security configuration for authentication and encryption

QoS allows the different message types to have different priority levels

4. Applications of MQTT

Some examples of how it’s being used in the real world is with the M2M Alliance, a project that aims to connect “things” with a secure platform for transferring data between devices. Another use case is with the CEPH project. This is a cloud storage system that can store and share files simultaneously to multiple users. It uses MQTT for securing distributed access by encrypting data, which allows it to be transferred between IoT devices using various gateways without having to worry about data security.

5. Getting started with MQTT

The first thing one needs is an MQTT client; this is the application that sends and receives messages. The Raspberry Pi foundation has a guide for getting started with MQTT on your Pi device. There are also other guides available out there such as this one from Hackster which gives you the code to run on the Python programming language.

The Best MQTT Online Courses

  1. Mastering MQTT
  2. Master MQTT Protocol
  3. Develop MQTT Client Android App
  4. MQTT Masterclass
  5. Mastering MQTT Protocol: A Beginner’s to Advance Level Guide
  6. IoT: MQTT with Adafruit, App Inventor and ESP8266
  7. MQTT Nodemcu (ESP8266) with Nodered, Wifi and Bluetooth

TLS vs. SSL for building an MQTT Broker

Pros of using TLS:

-Security: TLS provides a secure connection between the client and the broker. This helps to protect against man-in-the-middle attacks and eavesdropping.

-Authentication: TLS provides authentication of the broker and the client, helping to ensure that only authorized clients are able to connect to the broker.

-Encryption: TLS provides encryption of data between the client and broker, helping to protect information from being seen by unauthorized individuals.

Cons of using TLS:

-Performance: TLS can add overhead to communications, which can reduce performance.

-Certificates: In order to use TLS, you need certificates for both the client and the broker. These certificates can be difficult to obtain and can be expensive to use, which can increase the cost of using TLS.

Pros of using SSL:

-Security: SSL provides a secure connection between the client and the broker, just like TLS. It also helps protect against man-in-the-middle attacks and eavesdropping.

-Certificates: Since SSL is built on top of TLS, you can use the same certificates for encrypting traffic.

Cons of using SSL:

-Performance: Encryption between the client and broker will add overhead to communications, which reduces performance.

-Memory usage: The SSL protocol will use more memory than other protocols because it maintains an encryption/decryption session with the broker.

-Version issues: SSL is an older protocol, and it does not support some of the features that TLS provides (e.g., newer ciphers).

As you can see, TLS and SSL both provide security, authentication, and encryption for communication between clients and brokers; however they do so in slightly different ways. There is no one clear winner when it comes to deciding if you should use TLS or SSL for your MQTT Broker, as the decision will depend on what you need from your infrastructure and how much time/money you have available to spend.

In conclusion I would say that there is a right answer depending on the project needs. In this case it’s better to use TLS, since it provides more advantages than SSL does.

Top 5 reasons to use MQTT Protocol in your IoT projects

MQTT is a machine-to-machine (M2M) messaging protocol, designed for resource-constrained devices, that connects the devices to the cloud.

1. It is lightweight and easy to use: MQTT is a publish/subscribe messaging protocol that is designed to be lightweight and easy to use.

2. It has a small footprint: MQTT uses about 1/10th the bandwidth of HTTP, making it ideal for IoT applications with limited network resources.

3. It supports QoS: MQTT supports Quality of Service (QoS), which guarantees delivery of messages at the desired latency or quality.

4. It can run on any network: MQTT is a cross-platform protocol that can run on WiFi, Bluetooth Low Energy (BLE) or 2G/3G cellular networks.

5. It has several client libraries available in many programming languages : There are client libraries for C, C++, JavaScript, Arduino IDE, Python and Java.

How to setup an MQTT Broker using a Docker Container on your PC or a VPS server?

MQTT is a machine-to-machine (M2M)/“IoT” messaging protocol, designed as an extremely lightweight publish/subscribe messaging transport. It is useful for connections with remote locations where a small code footprint is required and/or network bandwidth is at a premium.

In this article, we will show you how to setup an MQTT broker using a Docker container on your PC or a VPS server.

Docker is a containerization engine which helps you to easily deploy your applications with all its dependencies. It provides consistent environment on each Docker host, allows you to save time and enhance the portability of your code between various Linux distributions. For example, if any application that depends on a large number of system libraries or some rare library versions, and thus not easily portable, you can simply wrap it all up and deploy as a Docker container.

1) Installing Docker for Windows/Mac

Docker is available for Microsoft Windows and Mac OS X users. You can download the appropriate version from here: https://docs.docker.com/docker-for-windows/install/.

2) Installing Docker-compose on your host

In order to deploy our home made MQTT broker easily, we will use the docker-compose tool. It lets you define and run multi-container Docker applications using a simple YAML file. You can learn more about docker-compose from here: https://docs.docker.com/compose/.

You can download the docker-compose tool from here: https://github.com/docker/compose/releases/. Once this is done, you should be able to run the ‘docker-compose’ command in your terminal and check for its version or use any of its sub-commands.

Basic Docker Terminology for this article:

1) “Docker container” is the process which runs in the background when you run an image using docker run command. A running instance of an image is called a ‘containter’, with respect to its creation at docker build.

2) “Docker image” is a read-only template with instructions for creating a docker container. It is composed of a series of filesystem layers representing different states of the final image we want to create

3) The copy at docker build command builds an image by reading its description from a Dockerfile in the current working directory and then executing the appropriate command in a new layer on top of the current image.

4) “Volumes” are folders that allow you to map local folders on your host machine to directories inside your docker containers. This is useful if, for example, you want to store some data about your container or configuration files. You can create a volume by specifying the path of the folder from your host system using docker run.

The YAML file is a configuration file that contains information for connecting to an external database, MQTT server etc., as well as information about how and what containers should be created. We will create a YAML file to configure our broker in the next section.

3) Creating the YAML file to configure your MQTT Broker

YAML files are typically composed of several key-value pairs, which are connected by colons (“:”). You can learn more about YAML from here: https://en.wikipedia.org/wiki/YAML#Syntax.

In our case, we will create a YAML file to configure the MQTT broker as follows:

version: “3”

In this section of the YAML, we define the name of the container as “homer-mqtt” and specify the image which should be used to create it. In this case, we will choose the image “davidtgoldblatt/homer-mqtt” from Docker Hub from here: https://hub.docker.com/_/homer-mqtt/.

In our YAML file, we also declare a couple of volumes as follows:

volumes:

– /var/run/docker.sock:/var/run/docker.sock

These volumes will let us have persistent storage for the docker container, allowing our settings to persist through server reboots and crashes. You can learn more about volumes from here: https://docs.docker.com/engine/userguide/dockervolumes/.

In order to specify our database details, we add a “db_addresses” section with the following entries:

db_addresses:

– “host=172.17.0.2 port=3306 user=homer dbname=homer”

In the above example, we specify a host and port for our database instance. Note that when specifying a user, you have to add an “@” symbol before the user name. This is because username consists of both letters and numbers in Unix systems. If you omit that, Docker complains with errors like this:

you have to escape special characters like @ or *

In order to specify our database credentials, we add a “database_password” section as follows:

database_password: “homerun1”

In the above example, you should use the password of your choice. In case you want to set up a database password for the first time, you can see our guide on how to create a new root user with no password here: https://homer-mqtt.readthedocs.io/en/latest/configure_database_and_create_new_root_user.html

In order to specify our MQTT server details, we add an “mqtt_server” section with the following entries:

mqtt_server:

– name: mqtt host: 172.17.0.1 port: 8883 username: “homerun” password: “homerun1”

In the above example, we run the broker on port 8883 and use the user name of “homerun” and password of “homerun1”, which we have created in our database.

4) Run your MQTT Broker with Docker

In this step, we will create a docker container from our YAML file and run it.

In order to do this, cd to the directory where you have the YAML file and run “docker-compose up” as follows:

$ docker-compose up

This command will build a new docker container based on the description in your YAML file and start it.

If you want to start this in the background, use “docker-compose up -d” instead. If you want to print the logs of your docker container, run “docker-compose logs mqttclient” as follows:

$ docker-compose logs mqttclient

You can also see the docker container live in real time by using “docker-compose ps” as follows:

$ docker-compose ps

5) Running your MQTT client with Docker

In this step, we will create a docker container from our YAML file and run it.

In order to do this, cd to the directory where you have the YAML file and run “docker-compose up” as follows:

$ docker-compose up

This command will build a new docker container based on the description in your YAML file and start it.

If you want to start this in the background, use “docker-compose up -d” instead. You can also see the docker container live in real time by using “docker-compose ps” as follows:

$ docker-compose ps

6) Using your MQTT client with Docker

Now that our YAML file is set up, we can now start using our MQTT client with Docker.

In order to do this, go to the directory where you have your YAML file and run “docker exec” as follows:

$ docker exec -it mqttclient /bin/bash

You should now see a directory called “/home/homerun” (which is the home directory for our docker container) and you should be inside that directory.

You can now see your databases by typing “mysql -uroot -proot” as follows:

$ mysql -uroot -proot

You should see all your databases on screen. You can see how to create a new root user with no password for the MySQL instance here: https://homer-mqtt.readthedocs.io/en/latest/configure_database_and_create_new_root_user.html

In order to run MQTT client command in Docker, you need to type “sbin/mqtt” as follows:

$ sbin/mqtt -c /home/homerun/config.yaml

In the above example, we specify a configuration file with the -c flag, which reads our MQTT settings from the configuration file we have created in this tutorial.

In order to check if your MQTT client is up and running, you can use “docker-compose ps” as follows:

$ docker-compose ps

In the above example, our MQTT client has been started successfully. You should now see all your databases on screen.

In order to run MQTT client command in Docker, you need to type “sbin/mqtt” as follows:

$ sbin/mqtt -c /home/homerun/config.yaml

In the above example, we specify a configuration file with the -c flag, which reads our MQTT settings from the configuration file we have created in this tutorial.

In order to check if your MQTT client is up and running, you can use “docker-compose ps” as follows:

$ docker-compose ps

In the above example, our MQTT client has been started successfully. You should now see all your databases on screen and you can try publishing some messages to test if everything is working fine:

$ docker exec -it mqttclient /bin/bash

$ sbin/mqtt publish -t ‘test’ -h 172.17.0.1 -m ‘hello world!’

$ docker exec -it mqttclient /bin/bash

$ sbin/mqtt subscribe -t ‘test’ -h 172.17.0.1 -u mqttuser

7) Cleaning up your MQTT client with Docker

In order to stop your docker container, you need to run “docker-compose down” as follows:

$ docker-compose down

8) Cleaning up your MQTT client with Docker Volumes

In order to stop your mysql database and mqtt user from being created, you can use the -v flag.

$ docker-compose down -v

You can also clean up your environment by deleting the directory that contains your YAML file.

Mysql database and mqtt user created successfully! They will be removed automatically when you remove the docker container. You can now exit out of this shell by typing “exit”.

In this tutorial, we have seen how to configure your MQTT client through a Docker container.

Conclusion

MQTT is a machine-to-machine (M2M) or “Internet of Things” connectivity protocol. It was designed as an extremely lightweight publish/subscribe messaging transport.MQTT is useful for connecting low-power sensors, wearables, and other tiny Internet-connected things to the cloud. Because MQTT uses a publish/subscribe model, it’s perfect for devices that need to send data infrequently or where network bandwidth is at a premium.

Interested on developing your MQTT solution? Contact us!

Related Articles

WP Radio
WP Radio
OFFLINE LIVE