Page
1
(This page has no text content)
Page
2
Hands-On MQTT Programming with Python Work with the lightweight IoT protocol in Python Gaston C. Hillar
Page
3
BIRMINGHAM - MUMBAI
Page
4
Hands-On MQTT Programming with Python Copyright © 2018 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 or its dealers and distributors, will be held liable for any damages caused or alleged to have been 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. Commissioning Editor: Kunal Choudhari Acquisition Editor: Reshma Raman Content Development Editor: Aditi Gour Technical Editor: Sushmeeta Jena Copy Editor: Safis Editing Project Coordinator: Hardik Bhinde Proofreader: Safis Editing Indexer: Tejal Daruwale Soni Graphics: Jason Monteiro Production Coordinator: Shraddha Falebhai First published: May 2018 Production reference: 1180518 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78913-854-2 www.packtpub.com
Page
5
mapt.io Mapt is an online digital library that gives you full access to over 5,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website.
Page
6
Why subscribe? Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals Improve your learning with Skill Plans built especially for you Get a free eBook or video every month Mapt is fully searchable Copy and paste, print, and bookmark content
Page
7
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.PacktP ub.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.
Page
8
About the Author Gaston C. Hillar is Italian and has been working with computers since he was 8 years old. Gaston has a bachelor's degree in computer science (graduated with honors) and an MBA. He is the CTO of Mapgenix, a freelance author, and a speaker. He has been a senior contributing editor at Dr. Dobb's and has written more than a hundred articles on software development topics. He has received the prestigious Intel® Black Belt Software Developer award eight times. He lives with his wife, Vanesa, and his two sons, Kevin and Brandon. Acknowledgements: While writing this book, I was fortunate to work with an excellent team at Packt, whose contributions vastly improved the presentation of this book. Reshma Raman allowed me to provide her ideas to write a book dedicated to MQTT programming with Python, and I jumped into this exciting project. Aditi Gour 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 her great work. It′s been great working with Reshma on another project and I can't wait to work with Reshma and Aditi 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! Of course, I'm talking about real-life soccer, but I must also include virtual soccer when the weather didn't allow us to kick a real- life ball.
Page
9
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 cofounder, for starting Zoetrope with me.
Page
10
Packt is searching for authors like you If you're interested in becoming an author for Packt, please visit authors.packtpub.c om and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea.
Page
11
Table of Contents Title Page Copyright and Credits Hands-On MQTT Programming with Python Packt Upsell Why subscribe? PacktPub.com About the Author About the reviewer Packt is searching for authors like you Preface Who this book is for What this book covers To get the most out of this book Download the example code files Download the color images Conventions used Get in touch Reviews 1. Installing an MQTT 3.1.1 Mosquitto Server 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 server on Linux Installing a Mosquitto server on macOS Installing a Mosquitto server on Windows Considerations for running a Mosquitto server in the cloud Test your knowledge Summary 2. Using Command-Line and GUI Tools to Learn How MQTT Works Subscribing to topics with a command-line tool Subscribing to topics with a GUI tool Publishing messages with a command-line tool Publishing messages with a GUI tool Unsubscribing from topics with a GUI tool Learning best practices for topics Understanding MQTT wildcards
Page
12
Learning about the different QoS levels Working with at least once delivery (QoS level 1) Working with exactly once delivery (QoS level 2) Understanding overhead in the different Quality of Service levels Test your knowledge Summary 3. Securing an MQTT 3.1.1 Mosquitto Server Understanding the importance of 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 command-line tools Testing the MQTT TLS configuration with GUI tools Creating a certificate for each MQTT client Configuring TLS client certificate authentication in Mosquitto Testing the MQTT TLS client authentication with command-line tools Testing the MQTT TLS configuration with GUI tools Forcing the TLS protocol version to a specific number Test your knowledge Summary 4. Writing Code to Control a Vehicle with Python and MQTT Messages Understanding the requirements to control a vehicle with MQTT Defining the topics and commands Creating a virtual environment with Python 3.6.x and PEP 405 Understanding the directory structure for a virtual environment Activating the virtual environment Deactivating the virtual environment Installing paho-mqtt for Python Connecting a client to the secured MQTT server with paho-mqtt Understanding callbacks Subscribing to topics with Python Configuring certificates for IoT boards that will work as clients Creating a class to represent a vehicle Receiving messages in Python Working with multiple calls to the loop method Test your knowledge Summary 5. Testing and Improving Our Vehicle Control Solution in Python Processing commands with Python
Page
13
Sending messages with Python Working with the network loop with Python Working with last will and testament with Python Working with retained last will messages Understanding blocking and non-blocking code Using the threaded client interface Test your knowledge Summary 6. Monitoring a Surfing Competition with Cloud-Based Real-Time MQTT Providers and Pytho n Understanding the requirements Defining the topics and payloads Coding a surfboard sensor emulator Configuring the PubNub MQTT interface Publishing data retrieved from sensors to the cloud-based MQTT server Working with multiple MQTT servers Running multiple clients Building a web-based dashboard with freeboard Test your knowledge Summary Solutions Chapter 1: Installing an MQTT 3.1.1 Mosquitto server Chapter 2: Using Command-Line and GUI Tools to Learn How MQTT Works Chapter 3: Securing an MQTT 3.1.1 Mosquitto Server Chapter 4: Writing Code to Control a Vehicle with Python and MQTT Messages Chapter 5: Testing and Improving our Vehicle Control Solution in Python Chapter 6:  Monitoring a Surfing Competition with Cloud-Based Real-Ti me MQTT Providers and Python Other Books You May Enjoy Leave a review - let other readers know what you think
Page
14
Preface MQTT is the preferred IoT publish-subscribe lightweight messaging protocol. Python is definitely one of the most popular programming languages. It is open source, multiplatform, and you can use it to develop any kind of application. If you develop IoT, web applications, mobile apps, or a combination of these solutions, you must learn how MQTT and its lightweight messaging system works. The combination of Python and MQTT makes it possible to develop powerful applications that communicate with sensors, different devices, and other applications. Of course, it is extremely important to take security into account when working with this protocol. Most of the time, when you work with complex IoT solutions coded in modern Python 3.6, you will use 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 in to the latest version of the MQTT protocol: 3.1.1. You will learn to work with the most recent 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 use them for a Mosquitto MQTT server. Then, you will work with many real-life examples in Python 3.6. You will control a vehicle, process commands, interact with actuators, and monitor a surf competition by exchanging MQTT messages with the Eclipse Paho MQTT client library. You will also work with a cloud-based, real-time MQTT provider. You will be able to run the examples on a wide range of modern IoT boards, such as Raspberry Pi 3 Model B+, Qualcomm DragonBoard 410c, BeagleBone Black, MinnowBoard Turbot Quad-Core, LattePanda 2G, and UP Core 4GB. However, any other board that supports Python 3.6 will be able to run the samples.
Page
15
Who this book is for This book is aimed at Python developers who want to develop applications that can interact with other applications and devices, such as IoT boards, sensors, and actuators.
Page
16
What this book covers Chapter 1, Installing an MQTT 3.1.1 Mosquitto Server, starts our journey toward the usage of the preferred IoT publish-subscribe lightweight messaging protocol in diverse IoT solutions, combined with mobile apps and web applications. We will learn how MQTT and its lightweight messaging system work. We will understand the MQTT puzzle: clients, servers (formerly known as brokers), and connections. We will learn the procedures to install an MQTT 3.1.1 Mosquitto server in Linux, macOS, and Windows. We will learn special considerations for running a Mosquitto server on the Cloud (Azure, AWS, and other cloud providers). Chapter 2, Using Command-Line and GUI Tools to Learn How MQTT Works, teaches us to work with command-line and GUI tools to learn how MQTT works in detail. We will learn MQTT basics, the specific vocabulary for MQTT, and its working modes. We will use different utilities and diagrams to understand the most important concepts related to MQTT. We will understand everything we need to know before writing Python code to work with the MQTT protocol. We will work with the different Quality of Service levels, and we will analyze and compare their overheads. Chapter 3, Securing an MQTT 3.1.1 Mosquitto Server, focuses on how to secure an MQTT 3.1.1 Mosquitto server. We will make all the necessary configurations to work with digital certificates to encrypt all the data sent between the MQTT clients and the server. We will use TLS, and we will learn to work with client certificates for each MQTT client. We will also learn to force the desired TLS protocol version. Chapter 4, Writing Code to Control a Vehicle with Python and MQTT Messages, focuses on writing Python 3.x code to control a vehicle with MQTT messages delivered through encrypted connections (TLS 1.2). We will write code that will be able to run on different popular IoT platforms, such as a Raspberry Pi 3 board. We will understand how we can leverage our knowledge of the MQTT protocol to build a solution based on requirements. We will learn to work with the latest version of the Eclipse Paho MQTT Python client library.
Page
17
Chapter 5, Testing and Improving Our Vehicle Control Solution in Python, outlines using our vehicle control solution with MQTT messages and Python code. We will learn how to process commands received in MQTT messages with Python code. We will write Python code to compose and send MQTT messages with commands. We will work with the blocking and threaded network loops, and we will understand the difference between them. Finally, we will take advantage of the last will and testament feature. Chapter 6, Monitoring a Surfing Competition with Cloud-Based Real-Time MQTT Providers and Python, gets you started with writing Python code to use the PubNub cloud-based, real-time MQTT provider in combination with a Mosquitto MQTT server to monitor a surfing competition. We will build a solution from scratch by analyzing the requirements, and we will write Python code that will run on waterproof IoT boards connected to multiple sensors in surfboards. We will define the topics and commands, and we will work with a cloud-based MQTT server, in combination with the Mosquitto MQTT server used in the previous chapters. Appendix, Solutions, the right answers for the Test Your Knowledge sections of each chapter are included in the appendix.
Page
18
To get the most out of this book You need a basic knowledge of Python 3.6.x and IoT boards.
Page
19
Download the example code files You can download the example code files for this book from your account at www. packtpub.com. If you purchased this book elsewhere, you can visit www.packtpub.com/su pport and register to have the files emailed directly to you. You can download the code files by following these steps: 1. Log in or register at www.packtpub.com. 2. Select the SUPPORT tab. 3. Click on Code Downloads & Errata. 4. Enter the name of the book in the Search box and follow the onscreen instructions. 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/PacktPu blishing/Hands-On-MQTT-Programming-with-Python. In case there's an update to the code, it will be updated on the existing GitHub repository. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!
Page
20
Download the color images We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it here: http://www.packtpub.com/sites/default/fi les/downloads/HandsOnMQTTProgrammingwithPython_ColorImages.pdf.