Reader small image

You're reading from  Hands-On Internet of Things with MQTT

Product typeBook
Published inOct 2019
Reading LevelIntermediate
PublisherPackt
ISBN-139781789341782
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Tim Pulver
Tim Pulver
author image
Tim Pulver

Tim Pulver is a Berlin-based freelance interaction designer and developer. In his work, he combines his physical prototyping experience and knowledge of interface design with modern technologies such as 3D printing, laser cutting, web technologies, and machine learning to create unique interactive experiences. In recent years, he has worked on interactive data visualizations, web-based audiovisual experiences, musical interfaces, and cables an innovative browser-based visual programming language that enables the creation of interactive audiovisual prototypes without writing any code. He holds a Bachelor of Arts degree in interface design from the University of Applied Sciences in Potsdam, Germany.
Read more about Tim Pulver

Right arrow

Getting Started with MQTT

In the last chapter, you got an overview of the basic building blocks of IoT projects. We discussed the development boards that can be used for IoT prototyping, had a look at some existing web services that pair well with IoT prototypes, and gave an overview of protocols for IoT communication. One of these communication protocols is MQ Telemetry Transport (MQTT), which we will be using throughout this whole book, and which you will learn more about in this chapter.

You will learn about MQTT messages, how the underlying publish and subscribe pattern works, as well as special MQTT features that will enable you to use MQTT for a lot of different use cases. We will also have a look at the most common MQTT apps for iOS, Android, macOS, and Windows. Using one of these apps, you can easily interact with your IoT prototypes and exchange messages, for...

Introducing MQTT

MQTT is a protocol for machine-to-machine communication that was invented in 1999 by Dr. Andy Stanford-Clark (IBM). Because of its lightweight nature, it is especially useful when used with microcontrollers for IoT projects. It is lightweight in terms of both energy consumption and bandwidth because it does not have much overhead, which makes it a great fit for battery-powered projects on microcontroller boards.

MQTT is used in a wide range of projects, from small-scale DIY home automation to applications in the healthcare sector, where practitioners use it to communicate with medical devices (for example, blood pressure monitors); and by oil companies to monitor miles of pipelines. Even Facebook uses it for its Messenger app.

In 2014, MQTT 3.1.1 was officially accepted as an OASIS standard, which sent out a great signal to everyone who was already using or...

Understanding the principle of publish and subscribe

The principle of publish and subscribe is the heart of MQTT. Devices can publish messages (for example, on) to a channel that can be freely defined; for example, /living-room/my-custom-coffee-machine.

Other devices that are connected to the same MQTT server can subscribe to this channel, resulting in near instant updates, whenever there is a new message published to that particular channel.

The MQTT server connects all of the publishers and subscribers, and keeps track of the subscriptions for each client. Every client only receives what it is subscribed to:

Three devices connected to an MQTT server

All devices are connected to the MQTT server (in the middle). It forwards the published messages to the subscribers.

Sometimes, publish and subscribe is referred to as pub/sub. One of the earliest MQTT clients for Arduino is...

Exploring MQTT features

In this section, we will have a closer look at some important features of MQTT:

  • QoS: For message buffering (when offline).
  • Last will/testament: To let the network know when a device goes offline; for example, due to empty batteries.
  • Keep alive: To define how often each device needs to call back home to let the MQTT server know that it is still online.
  • Persistent sessions: To store various information on the MQTT server, while the client is offline.
  • Retained messages: To keep messages available for new subscribers.

Quality of Service

MQTT supports a feature called QoS, which makes it possible for clients to receive messages that were sent while the device was offline. You can see this feature being used in...

Analyzing the security of MQTT servers

There are four major ways to communicate with MQTT servers: MQTT, secure MQTT, MQTT over WebSockets, and MQTT over secure WebSockets. WebSockets are typically used when websites communicate with an MQTT server on the frontend.

Let's talk about the secure element in secure MQTT and secure WebSockets.

This book is all about prototyping: trying things out and failing (or succeeding) fast. For this, we don't care too much about security, as properly securing our prototypes would increase complexity and require more time. But, we should understand how to establish a basic level of security, and know when basic security is not enough.

SSL/TSL

When using secure MQTT, or secure MQTT over WebSockets...

MQTT servers and cloud providers

The center of each MQTT project is the MQTT server, sometimes referred to as the MQTT broker. It is the control center to which each device in the MQTT network needs to connect, where all of the MQTT messages are sent to, and where devices can subscribe to the topics that they are interested in. It handles the topic filtering, so it knows which devices are interested in which topics, and forwards the messages accordingly. It also keeps track of messages that could not be delivered yet; for example, because a subscribed device went offline. The server will try again, until the device goes back online and the message can be delivered.

Because MQTT is an open protocol, you can freely switch between MQTT servers. In contrast to proprietary solutions, where you cannot easily switch servers, MQTT makes this very easy. In most cases, you just...

Comparing MQTT iOS and Android apps

The beauty of MQTT is—apart from the things that you already know about—its landscape of servers, libraries, and apps with MQTT support, which you can mix and match in any way that you like.

It has never been this easy to control your Arduino project from your smartphone, without the need to write additional iOS or Android code. For this, you can utilize one of the many iOS and Android apps that connect to your MQTT server of choice, in the same way that Arduino does. It's just another MQTT client.

Apps with MQTT support serve two main purposes:

  • Logging/visualization/inspection of your MQTT devices and their data (passive).
  • Controlling your MQTT devices and sending commands (active).

While some apps exist that work purely using text (MQTT topic and message), and that are especially useful for debugging, there are a few that...

Exploring MQTT desktop apps

MQTT clients basically exist for every platform. To publish MQTT messages and subscribe to MQTT topics from your desktop computer, you can choose between many applications. While we will often be using the command-line MQTT client, Mosquitto, throughout this book, feel free to try out a desktop client in parallel. A good MQTT client will offer the same features that Mosquitto has, and can therefore be used interchangeably.

MQTT.fx (Windows, macOS, and Linux)

MQTT.fx (https://mqttfx.jensd.de/) is definitely one of the best desktop clients around right now. It is less fancy than the iOS and Android apps that allow you to create your own dashboards via user interface widgets, but it is more solid. The...

Understanding MQTT libraries

Due to its open nature, there are a vast amount of MQTT client libraries around. There is a good chance that your favorite programming language offers an MQTT library as well. In the following sections, we will have a short look at MQTT libraries for two programming languages that are relevant to us: Arduino and JavaScript.

Arduino libraries

Summary

In this chapter, we had a look at MQTT—the IoT protocol—which we will use to build the projects in the hands-on part of this book. It is a lightweight protocol, which is especially suitable for the communication of restricted devices, either because of processing power, network bandwidth, or battery power, and therefore, it is a perfect fit for the projects that we are about to build.

MQTT is based on the publish and subscribe pattern. Each message is published to a topic, which can be compared to the categories in a newspaper. A subscriber can subscribe to this information in order to receive updates about it, "I only want to get updates on foreign affairs and cultural news, but don't care about sports."

We also learned that a single entity, the MQTT server, manages all of the communication between the clients, and is essential for the communication...

Questions

  1. Who, or what, takes care of all of the MQTT messages that are published by a client?
  2. When publishing a message, what are valid topic names?
  3. What is the difference between the two wildcards, # and +, when subscribing to multiple topics at once?
  4. Can wildcards be used when publishing a message?
  5. What is last will (testament)?
  6. What are retained messages?

Further reading

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Hands-On Internet of Things with MQTT
Published in: Oct 2019Publisher: PacktISBN-13: 9781789341782
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Tim Pulver

Tim Pulver is a Berlin-based freelance interaction designer and developer. In his work, he combines his physical prototyping experience and knowledge of interface design with modern technologies such as 3D printing, laser cutting, web technologies, and machine learning to create unique interactive experiences. In recent years, he has worked on interactive data visualizations, web-based audiovisual experiences, musical interfaces, and cables an innovative browser-based visual programming language that enables the creation of interactive audiovisual prototypes without writing any code. He holds a Bachelor of Arts degree in interface design from the University of Applied Sciences in Potsdam, Germany.
Read more about Tim Pulver