Book Image

MQTT Essentials - A Lightweight IoT Protocol

5 (1)
Book Image

MQTT Essentials - A Lightweight IoT Protocol

5 (1)

Overview of this book

This step-by-step guide will help you gain a deep understanding of the lightweight MQTT protocol. We’ll begin with the specific vocabulary of MQTT and its working modes, followed by installing a Mosquitto MQTT broker. Then, you will use best practices to secure the MQTT Mosquitto broker to ensure that only authorized clients are able to publish and receive messages. Once you have secured the broker with the appropriate configuration, you will develop a solution that controls a drone with Python. Further on, you will use Python on a Raspberry Pi 3 board to process commands and Python on Intel Boards (Joule, Edison and Galileo). You will then connect to the MQTT broker, subscribe to topics, send messages, and receive messages in Python. You will also develop a solution that interacts with sensors in Java by working with MQTT messages. Moving forward, you will work with an asynchronous API with callbacks to make the sensors interact with MQTT messages. Following the same process, you will develop an iOS app with Swift 3, build a website that uses WebSockets to connect to the MQTT broker, and control home automation devices with HTML5, JavaScript code, Node.js and MQTT messages
Table of Contents (16 chapters)
MQTT Essentials - A Lightweight IoT Protocol
Credits
About the Author
Acknowledgment
About the Reviewer
www.PacktPub.com
Customer Feedback
Dedication
Preface

Understanding wildcards


When we analyzed the subscription operation, we learn that an MQTT client can subscribe to one or more topic filters. In case we specify a topic name as a topic filter, we will only subscribe to a single topic. We can take advantage of the following two wildcards to create topic filters that subscribe to all the topics that match the filter:

  • Plus sign (+): It is a single level wildcard that matches any name for a specific topic level. We can use this wildcard instead of specifying a name for any topic level in the topic filter.

  • Hash (#): It is a multi level wildcard that we can use only at the end of the topic filter, as the last level and matches any topic whose first levels are the same as the topic levels specified at the left-hand side of the # symbol.

For example, in case we want to receive all the messages related to the altitudes for all the drones, we can use the + single level wildcard instead of a specific drone name. We can use the following topic filter: sensors/+/altitude.

If we publish messages to the following topics, the subscriber that used the sensors/+/altitude topic filter will receive all of them:

  • sensors/drone01/altitude

  • sensors/drone02/altitude

  • sensors/superdrone01/altitude

  • sensors/thegreatestdrone/altitude

The subscriber to the sensors/+/altitude topic filter won't receive messages sent to any of the following topics, because they won't match the topic filter.

  • sensors/drone01/speed/rotor/1

  • sensors/superdrone01/speed/rotor/2

  • sensors/superdrone01/remainingbattery

In case we want to receive all the messages related to all the sensors for the drone named drone01, we can use the # multi level wildcard after the drone name and the slash (/). We can use the following topic filter: sensors/drone01/#.

If we publish messages to the following topics, the subscriber that used the sensors/drone01/# topic filter will receive all of them:

  • sensors/drone01/altitude

  • sensors/drone01/speed/rotor/1

  • sensors/drone01/speed/rotor/2

  • sensors/drone01/speed/rotor/3

  • sensors/drone01/speed/rotor/4

  • sensors/drone01/remainingbattery

We used a multi level wildcard, and therefore, no matter the additional topic levels after sensors/drone01/, we will receive all of them.

The subscriber to the sensors/drone01/# topic filter won't receive messages sent to any of the following topics, because they won't match the topic filter. None of the following has sensors/drone01/ as a prefix, and therefore, they don't match the topic filter.

  • sensors/drone02/altitude

  • sensors/superdrone01/altitude

  • sensors/thegreatestdrone/altitude

  • sensors/drone02/speed/rotor/1

  • sensors/superdrone02/speed/rotor/2

  • sensors/superdrone02/remainingbattery

Obviously, we must be careful when we use any wildcard because we might be subscribing to a huge number of topics with a single topic filter. We have to avoid subscribing to topics aren't of interest to the client to avoid wasting unnecessary bandwidth and the broker server resources.

We will use these wildcards in subscriptions later to analyze how different QoS levels work with MQTT.