MQTT Essentials - A Lightweight IoT Protocol
()
About this ebook
- Make your connected devices less prone to attackers by understanding practical security mechanisms
- Dive deep into one of IoT’s extremely lightweight machines to enable connectivity protocol with some real-world examples
- Learn to take advantage of the features included in MQTT for IoT and Machine-to-Machine communications with complete real-life examples
This book is a great resource for developers who want to learn more about the MQTT protocol to apply it to their individual IoT projects. Prior knowledge of working with IoT devices is essential.
Gastón C. Hillar
Gaston C. Hillar has been working with computers since he was eight. He began programming with the legendary Texas TI-99/4A and Commodore 64 home computers in the early 80s. He has a Bachelor degree in Computer Science, graduated with honors, and an MBA (Master in Business Administration), graduated with an outstanding thesis. He worked as developer, architect, and project manager for many companies in Buenos Aires, Argentina. Now, he is an independent IT consultant and a freelance author always looking for new adventures around the world. He also works with electronics (he is an electronics technician). He is always researching about new technologies and writing about them. He owns an IT and electronics laboratory with many servers, monitors, and measuring instruments. Gaston write the C# 2008 and 2005 Threaded Programming: Beginner's Guide also published by Packt. He is also the author of more than 40 books in Spanish about computer science, modern hardware, programming, systems development, software architecture, business applications, balanced scorecard applications, IT project management, the Internet, and electronics. He contributes to Dr. Dobb's Go Parallel programming portal http://www.ddj.com/go-parallel/ and he is a guest blogger at Intel Software Network http://software.intel.com He usually writes articles for Spanish magazines Mundo Linux, Solo Programadores and Resistor.
Read more from Gastón C. Hillar
Building RESTful Python Web Services Rating: 5 out of 5 stars5/5Learning Object-Oriented Programming Rating: 0 out of 5 stars0 ratingsInternet of Things with Python Rating: 0 out of 5 stars0 ratingsJavaScript : Object-Oriented Programming Rating: 0 out of 5 stars0 ratingsSwift 3 Object-Oriented Programming - Second Edition Rating: 0 out of 5 stars0 ratingsProfessional Parallel Programming with C#: Master Parallel Extensions with .NET 4 Rating: 3 out of 5 stars3/5Microsoft Silverlight 4 and SharePoint 2010 Integration Rating: 0 out of 5 stars0 ratingsJava 9 with JShell Rating: 0 out of 5 stars0 ratings
Related to MQTT Essentials - A Lightweight IoT Protocol
Related ebooks
Arduino Networking Rating: 4 out of 5 stars4/5ARM® Cortex® M4 Cookbook Rating: 4 out of 5 stars4/5WebSocket Essentials – Building Apps with HTML5 WebSockets Rating: 0 out of 5 stars0 ratingsMastering Embedded Linux Programming - Second Edition Rating: 5 out of 5 stars5/5Instant Optimizing Embedded Systems Using BusyBox Rating: 0 out of 5 stars0 ratingsLearning Docker Rating: 5 out of 5 stars5/5Learning Docker - Second Edition Rating: 0 out of 5 stars0 ratingsGetting Started with WebRTC Rating: 0 out of 5 stars0 ratingsHands-On Parallel Programming with C# 8 and .NET Core 3: Build solid enterprise software using task parallelism and multithreading Rating: 0 out of 5 stars0 ratingsESP32 Programming for the Internet of Things: JavaScript, AJAX, MQTT and WebSockets Solutions Rating: 5 out of 5 stars5/5Learn IoT Programming Using Node-RED: Begin to Code Full Stack IoT Apps and Edge Devices with Raspberry Pi, NodeJS, and Grafana Rating: 0 out of 5 stars0 ratingsInternet of Things with ESP8266 Rating: 5 out of 5 stars5/5Mastering BeagleBone Robotics Rating: 5 out of 5 stars5/5Internet of Things & Wireless Sensor Network Rating: 0 out of 5 stars0 ratingsBuilding Wireless Sensor Networks Using Arduino Rating: 4 out of 5 stars4/5Smart Internet of Things Projects Rating: 4 out of 5 stars4/5Practical Internet of Things Security Rating: 0 out of 5 stars0 ratingsUsing Yocto Project with BeagleBone Black Rating: 0 out of 5 stars0 ratingsESP8266 Internet of Things Cookbook Rating: 5 out of 5 stars5/5Embedded Systems A Complete Guide - 2021 Edition Rating: 0 out of 5 stars0 ratingsA Practical Guide for IoT Solution Architects Rating: 5 out of 5 stars5/5Yocto for Raspberry Pi Rating: 0 out of 5 stars0 ratingsDesign Patterns for Embedded Systems in C: An Embedded Software Engineering Toolkit Rating: 5 out of 5 stars5/5Internet of Things: Principles and Paradigms Rating: 4 out of 5 stars4/5Python Programming for Arduino Rating: 5 out of 5 stars5/5Learning IoT with Particle Photon and Electron Rating: 0 out of 5 stars0 ratings
Hardware For You
CompTIA A+ Complete Review Guide: Core 1 Exam 220-1101 and Core 2 Exam 220-1102 Rating: 5 out of 5 stars5/5CompTIA A+ Complete Review Guide: Exam Core 1 220-1001 and Exam Core 2 220-1002 Rating: 5 out of 5 stars5/5Chip War: The Fight for the World's Most Critical Technology Rating: 4 out of 5 stars4/5Creative Selection: Inside Apple's Design Process During the Golden Age of Steve Jobs Rating: 5 out of 5 stars5/5Computer Science: A Concise Introduction Rating: 4 out of 5 stars4/5Programming Arduino: Getting Started with Sketches Rating: 4 out of 5 stars4/5Windows 11 For Seniors For Dummies Rating: 0 out of 5 stars0 ratingsMastering ChatGPT Rating: 0 out of 5 stars0 ratingsMacs For Dummies Rating: 5 out of 5 stars5/5Dancing with Qubits: How quantum computing works and how it can change the world Rating: 5 out of 5 stars5/5Raspberry Pi Electronics Projects for the Evil Genius Rating: 3 out of 5 stars3/5Computer Organization and Design: The Hardware / Software Interface Rating: 4 out of 5 stars4/5CompTIA A+ Complete Review Guide: Exams 220-901 and 220-902 Rating: 5 out of 5 stars5/5Raspberry Pi Cookbook for Python Programmers Rating: 0 out of 5 stars0 ratingsBuild Your Own PC Do-It-Yourself For Dummies Rating: 4 out of 5 stars4/5Amazon Web Services (AWS) Interview Questions and Answers Rating: 5 out of 5 stars5/5Debugging: The 9 Indispensable Rules for Finding Even the Most Elusive Software and Hardware Problems Rating: 4 out of 5 stars4/5Mastering Apple MacBook - MacBook Pro, MacBook Air, MacOS Ultimate User Guide Rating: 1 out of 5 stars1/5The Richest Man in Babylon: with The Magic Story Rating: 0 out of 5 stars0 ratingsTI-84 Plus CE Graphing Calculator For Dummies Rating: 0 out of 5 stars0 ratings50 Android Hacks Rating: 5 out of 5 stars5/5Upgrading and Fixing Computers Do-it-Yourself For Dummies Rating: 4 out of 5 stars4/53D Printing For Dummies Rating: 4 out of 5 stars4/5iPhone 14 Pro Max User Guide for Beginners and Seniors Rating: 0 out of 5 stars0 ratingsiPhone For Seniors For Dummies: Updated for iPhone 12 models and iOS 14 Rating: 4 out of 5 stars4/5Arduino: A Quick-Start Beginner's Guide Rating: 4 out of 5 stars4/5Beginning x64 Assembly Programming: From Novice to AVX Professional Rating: 0 out of 5 stars0 ratings
Reviews for MQTT Essentials - A Lightweight IoT Protocol
0 ratings0 reviews
Book preview
MQTT Essentials - A Lightweight IoT Protocol - Gastón C. Hillar
Table of Contents
MQTT Essentials - A Lightweight IoT Protocol
Credits
About the Author
Acknowledgment
About the Reviewer
www.PacktPub.com
Why subscribe?
Customer Feedback
Dedication
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Downloading the example code
Downloading the color images of this book
Errata
Piracy
Questions
1. Lightweight Messaging with MQTT 3.1.1 and Mosquitto
Understanding convenient scenarios for the MQTT protocol
Working with the publish-subscribe pattern
Working with message filtering
Understanding the MQTT puzzle -- clients, servers, and connections
Installing a Mosquitto broker on Ubuntu Linux
Installing a Mosquitto broker on macOS
Installing a Mosquitto broker on Windows
Subscribing to topics
Publishing messages
Unsubscribing from topics
Working with best practices when creating topics
Understanding wildcards
Learning about the different Quality of Service levels
Working with at least once delivery
Working with exactly once delivery
Test your knowledge
Summary
2. Securing an MQTT Mosquitto Server
Securing a Mosquitto server
Generating a private certificate authority to use TLS with Mosquitto
Creating a certificate for the Mosquitto server
Configuring TLS transport security in Mosquitto
Testing the MQTT TLS configuration with MQTT.fx
Testing the MQTT TLS configuration with MQTT-spy
Creating a certificate for each MQTT client
Configuring TLS client certificate authentication in Mosquitto
Testing the MQTT TLS client authentication with MQTT.fx
Testing the MQTT TLS configuration with MQTT-spy
Forcing the TLS protocol version
Test your knowledge
Summary
3. Controlling a Drone with Python
Understanding the requirements to control a drone with MQTT
Defining the topics and commands
Installing paho-mqtt for Python
Connecting the client to the MQTT server with Python
Understanding callbacks
Subscribing to topics with Python
Creating a class to represent a drone
Receiving messages with Python
Processing commands with Python
Sending messages with Python
Working with the network loop with Python
Test your knowledge
Summary
4. Interacting with Sensors in Java
Understanding the requirements
Defining the topics and commands
Installing the Eclipse Paho Java Client
Creating a security helper class to establish a TLS secured connection
Configuring a TLS secured connection in Java
Working with asynchronous notifications and callbacks in Java
Subscribing to topics and understanding notification events in Java
Processing commands that interact with sensors in Java
Working with last will and testament in Java
Publishing messages with commands to target sensors
Test your knowledge
Summary
5. Controlling Home Automation in JavaScript
Understanding the requirements
Defining the topics and commands to control home automation
Enabling WebSockets for a Mosquitto server
Testing WebSockets with the Eclipse Paho client JavaScript utility
Understanding the Eclipse Paho JavaScript asynchronous client
Creating an HTML 5 web page to control LEDs
Publishing messages to target remote devices in JavaScript
Processing the received messages in JavaScript
Working with WebSockets to connect to the MQTT server
Subscribing to topics in JavaScript
Processing commands that interact with LEDs in Node.js
Running the home automation web application
Debugging asynchronous code with Chrome Developer Tools (CDT) in JavaScript
Securing MQTT over WebSockets with TLS
Test your knowledge
Summary
6. Interacting with Actuators in Swift
Understanding the requirements
Defining topics and commands
Enabling connections without TLS for a Mosquitto server
Installing the CocoaMQTT client library to create a new iOS app
Building the UI for the iOS app that will control an actuator
Understanding the CocoaMQTT client in Swift 3
Connecting to the MQTT server and subscribing to topics in Swift
Publishing messages to target actuators in Swift
Processing commands that interact with actuators in Node.js
Running the iOS app that controls an actuator with MQTT
Debugging events in Swift
Test your knowledge
Summary
A. Exersise Answers
Chapter 1, Lightweight Messaging with MQTT 3.1.1 and Mosquitto
Chapter 2, Securing an MQTT Mosquitto Server
Chapter 3, Controlling a Drone with Python
Chapter 4, Interacting with Sensors in Java
Chapter 5, Controlling Home Automation in JavaScript
Chapter 6, Interacting with Actuators in Swift
MQTT Essentials - A Lightweight IoT Protocol
MQTT Essentials - A Lightweight IoT Protocol
Copyright © 2017 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: April 2017
Production reference: 1050417
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham
B3 2PB, UK.
ISBN 978-1-78728-781-5
www.packtpub.com
Credits
About the Author
Gastón C. Hillar is Italian and has been working with computers since he was eight. He began programming with the legendary Texas TI-99/4A and Commodore 64 home computers in the early 80s. He has a bachelor's degree in computer science (graduated with honors), and an MBA (graduated with an outstanding thesis). At present, Gastón is an independent IT consultant and freelance author who is always looking for new adventures around the world.
He has been a senior contributing editor at Dr. Dobb’s and has written more than a hundred articles on software development topics. Gastón was also a former Microsoft MVP in technical computing. He has received the prestigious Intel® Black Belt Software Developer award eight times.
He is a guest blogger at Intel® Software Network (http://software.intel.com). You can reach him at gastonhillar@hotmail.com and follow him on Twitter at http://twitter.com/gastonhillar. Gastón’s blog is http://csharpmulticore.blogspot.com.
He lives with his wife, Vanesa, and his two sons, Kevin and Brandon.
Acknowledgment
At the time of writing this book, I was fortunate to work with an excellent team at Packt Publishing, whose contributions vastly improved the presentation of this book. Reshma Raman allowed me to provide her ideas for a book dedicated to MQTT, and I jumped into the exciting project of teaching the features of one of the most important protocols for IoT projects with examples in many different programming languages. Vikas Tiwari helped me realize my vision for this book and provided many sensible suggestions regarding the text, the format, and the flow. The reader will notice his great work. It’s been great working with Reshma and Vikas in another project and I can’t wait to work with them again. I would like to thank my technical reviewers and proofreaders for their thorough reviews and insightful comments. I was able to incorporate some of the knowledge and wisdom they have gained in their many years in the software development industry. This book was possible because they gave valuable feedback.
The entire process of writing a book requires a huge number of lonely hours. I wouldn’t be able to write an entire book without dedicating some time to play soccer against my sons, Kevin and Brandon, and my nephew, Nicolas. Of course, I never won a match. However, I did score a few goals.
About the Reviewer
Ben Howes is the founder and lead consultant at Zoetrope Ltd, a specialist IoT consultancy and product development firm in the UK. Ben has been creating connected hardware for over 10 years and has worked across projects spanning from start-ups to multinational deployments.
I’d like to thank Richard Webb, my co-founder, for starting Zoetrope with me.
www.PacktPub.com
For support files and downloads related to your book, please visit www.PacktPub.com.
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at service@packtpub.com for more details.
At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.
https://www.packtpub.com/mapt
Get the most in-demand software skills with Mapt. Mapt gives you full access to all Packt books and video courses, as well as industry-leading tools to help you plan your personal development and advance your career.
Why subscribe?
Fully searchable across every book published by Packt
Copy and paste, print, and bookmark content
On demand and accessible via a web browser
Customer Feedback
Thanks for purchasing this Packt book. At Packt, quality is at the heart of our editorial process. To help us improve, please leave us an honest review on this book's Amazon page at https://www.amazon.com/dp/1787287815.
If you'd like to join our team of regular reviewers, you can e-mail us at customerreviews@packtpub.com. We award our regular reviewers with free eBooks and videos in exchange for their valuable feedback. Help us be relentless in improving our products!
Dedication
To my sons, Kevin and Brandon, and my wife, Vanesa
Preface
MQTT is the preferred IoT publish-subscribe lightweight messaging protocol. If you develop IoT, web applications, mobile apps, or a combination of these solutions, you must learn how MQTT and its lightweight messaging system work. Of course, it is extremely important to take security into account when working with this protocol.
Most of the times, when you work with IoT solutions, you won’t be using a single programming language. Instead, you will use a combination of different programming languages and code running on different IoT boards that might use diverse operating systems. MQTT has its own specific vocabulary and different working modes. Learning MQTT is challenging because it includes too many abstract concepts that require real-life examples to be easy to understand.
This book will allow you to dive deep into the latest version of the MQTT protocol: 3.1.1. You will learn to work with Mosquitto MQTT server, command-line tools, and GUI tools to allow you to understand how everything works with MQTT and the possibilities that this protocol provides for your projects. You will learn security best practices and you will use them for a Mosquitto MQTT server.
This book will provide you with examples in the following programming languages, which are extremely popular in IoT, web applications, and mobile apps: Python, Java, HTML 5, JavaScript, Node.js, and Swift 3. You will control a drone, interact with sensors, control home automation, and interact with actuators by exchanging MQTT messages with the different programming languages and client libraries.
You will be able to run the examples on a wide range of modern IoT boards, such as Raspberry Pi 3, Raspberry Pi 2 Model B, Raspberry Pi Zero, Intel Joule, Intel Edison, and Intel Galileo Gen 2. However, any other board that supports the programming languages explained in the book will be able to run the samples.
What this book covers
Chapter 1, Lightweight Messaging with MQTT 3.1.1 and Mosquitto, starts our journey towards understanding the MQTT protocol. You will learn convenient scenarios for this protocol, the details of the publish-subscribe pattern, and message filtering. You will learn basic concepts related to MQTT and understand the different components: clients, brokers or servers, and connections. You will learn to install a Mosquitto broker or server on Linux, macOS, and Windows. You will learn best practices related to topics and single-level and multilevel wildcards. We will study in detail the different Quality of Service levels supported by MQTT and when it is convenient to use each of them. We will analyze their advantages and disadvantages.
Chapter 2, Securing an MQTT Mosquitto Server, covers generating a private certificate authority, a server certificate, and client certificates to enable TLS transport security and TLS client authentication with Mosquitto. Communications between the MQTT clients and the MQTT server will be encrypted. We will work with OpenSSL to generate the digital certificates. We will test the MQTT TLS configuration with MQTT.fx, MQTT-spy, and the Mosquitto command-line utilities. We will force Mosquitto to use only a specific TLS version. This way, we will understand how to encrypt communications between the MQTT clients and the Mosquitto MQTT server.
Chapter 3, Controling a Drone with Python, analyzes the requirements for controlling a drone with MQTT messages. We will define the topics that we will use and the commands that will be part of the messages’ payloads to control a drone. Then, we will work with the Paho Python Client to write Python code that connects an MQTT client to the MQTT server. We will understand the methods we need to call for the Paho Python Client and their parameters. We will analyze how callbacks work and we will be able to publish messages to topics, subscribe to topic filters, and receive and process messages. We will write code that processes commands for a drone with Python. The code will be able to run on different IoT platforms, including Intel Joule boards and Raspberry Pi 3, and also on any computer that is capable of executing Python 2.7.x or Python 3.x. We will work with the network loop for the MQTT client in Python. We will work with a GUI utility, command-line-based utilities, and, finally, with Python code to control the drone and display the feedback provided by the drone.
Chapter 4, Interacting with Sensors in Java, analyzes the requirements for controlling sensors wired to different IoT boards with MQTT messages and Java code. We will define the topics that we will use and the commands that will be part of the messages’ payloads to control sensors. We will take advantage of the last will and testament feature of MQTT with Java. We will work with the Eclipse Paho Java Client to write Java code that will connect a non-blocking (asynchronous) MQTT client to the MQTT server. We will create a security helper class to establish a TLS secured connection, and we will work with asynchronous notifications and callbacks in Java. We will subscribe to topics, understand how notification events work, and we will write code that processes commands that interact with sensors. Finally, we will work with a GUI utility to interact with our Java application through MQTT messages. We will be able to turn on and turn off the sensors and retrieve data from them.
Chapter 5, Controlling Home Automation in JavaScript, analyzes the requirements to control LEDs wired to different IoT boards with MQTT messages over WebSockets. We will define the topics that we will use and the commands that will be part of the message’s payloads to set the colors for LEDs. Then, we will create an HTML 5 web page combined with JavaScript and the Eclipse Paho JavaScript asynchronous client to control LEDs. We will write code to publish messages to target remote devices in JavaScript and we will be able to process received messages to provide feedback to the user. We will work with MQTT.js to write a Node.js script to process commands that simulate the interaction with LEDs on different IoT boards. We will be able to run the home automation web application and understand how all the pieces work together by exchanging MQTT over WebSockets messages. Finally, we will understand the necessary steps to work with MQTT over WebSockets with TLS.
Chapter 6, Interacting with Actuators in Swift, analyzes the requirements for controlling actuators wired to different IoT boards with MQTT messages. We will define the topics that we will use and the commands that will be part of the message’s payloads to turn on and turn off motors. Then, we will create an iOS app with Xcode and Swift 3, combined with the CocoaMQTT client, to control a motor. We will write code to publish messages to target remote devices in Swift 3 and we will be able to process received messages to provide feedback to the user. We will work with MQTT.js to write a Node.js script to process commands that simulate the interaction with motors on the different IoT boards. We will be able to run the iOS app and the Node.js script and understand how all the pieces work together by exchanging MQTT messages.
What you need for this book
You will need any computer or device capable of running Python 3.x or greater, Chrome 56.x or greater, JDK 8 or greater, and Node.js 6.7.x or greater.
In order to work with Mosquitto 1.4.11 or greater, you will need a computer with any of the operating systems supported by this version of Mosquitto. The list is available at https://mosquitto.org/download/.
In order to work with Xcode 8.x, you will need a Mac computer capable of running OS X 10.11.5 or later, with 8 GB RAM.
Who this book is for
This book is a great resource for developers who want to learn more about the MQTT protocol to apply it to their individual IoT projects. Prior knowledge of working with IoT devices is essential.
Conventions
In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.
Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: If a client specifies and empty value as the ClientId.
A block of code is set as follows:
# Replace localhost with the IP for the Mosquitto
# or other MQTT server
mqtt_server_host = localhost
mqtt_server_port = 8883
mqtt_keepalive = 60
When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:
Select color for LED #2
Any command-line input or output is written as follows:
sudo apt-get update
New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: In order to download new modules, we will go to Files | Settings | Project Name | Project Interpreter.
Note
Warnings or important notes appear in a box like this.
Tip
Tips and tricks appear like this.
Reader feedback
Feedback from our readers is always welcome. Let us know what you think about this book-what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of. To send us general feedback, simply e-mail feedback@packtpub.com, and mention the book's title in the subject of your message. If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.
Customer support
Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.
Downloading the example code
You can download the example code files for this book from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
You can download the code files by following these steps:
Log in or register to our website using your e-mail address and password.
Hover the mouse pointer on the SUPPORT tab at the top.
Click on Code Downloads & Errata.
Enter the name of the book in the Search box.
Select the book for which you're looking to download the code files.
Choose from the drop-down menu where you purchased this book from.
Click on Code Download.
Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:
WinRAR / 7-Zip for Windows
Zipeg / iZip / UnRarX for Mac
7-Zip / PeaZip for Linux
The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/MQTT-Essentials-A-Lightweight-IoT-Protocol. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!