Hands-On MQTT Programming with Python

Book description

Explore the features included in the latest versions of MQTT for IoT and M2M communications and use them with modern Python 3.

About This Book
  • Make your connected devices less prone to attackers by understanding security mechanisms
  • Take advantage of MQTT features for IoT and Machine-to-Machine communications
  • The only book that covers MQTT with a single language, Python
Who This Book Is For

This book is for developers who want to learn about the MQTT protocol for their IoT projects. Prior knowledge of working with IoT and Python will be helpful.

What You Will Learn
  • Learn how MQTT and its lightweight messaging system work
  • Understand the MQTT puzzle: clients, servers (formerly known as brokers), and connections
  • Explore the features included in the latest versions of MQTT for IoT and M2M communications
  • Publish and receive MQTT messages with Python
  • Learn the difference between blocking and threaded network loops
  • Take advantage of the last will and testament feature
  • Work with cloud-based MQTT interfaces in Python
In Detail

MQTT is a lightweight messaging protocol for small sensors and mobile devices. This book explores the features of the latest versions of MQTT for IoT and M2M communications, how to use them with Python 3, and allow you to interact with sensors and actuators using Python.

The book begins with the specific vocabulary of MQTT and its working modes, followed by installing a Mosquitto MQTT broker. You will use different utilities and diagrams to understand the most important concepts related to MQTT. You will learn to make all the necessary configuration to work with digital certificates for encrypting all data sent between the MQTT clients and the server. You will also work with the different Quality of Service levels and later analyze and compare their overheads.

You will write Python 3.x code to control a vehicle with MQTT messages delivered through encrypted connections (TLS 1.2), and learn how leverage your knowledge of the MQTT protocol to build a solution based on requirements. Towards the end, you will write Python code to use the PubNub cloud-based real-time MQTT provider to monitor a surfing competition.

In the end, you will have a solution that was built from scratch by analyzing the requirements and then write Python code that will run on water-proof IoT boards connected to multiple sensors in surfboards.

Style and approach

This book shows you what MQTT is, and how to install and secure an MQTT server. You will write Python 3 code to control a vehicle with MQTT messages, test and improve, then monitor a surfing competition with cloud-based real-time MQTT providers.

Table of contents

  1. Title Page
  2. Copyright and Credits
    1. Hands-On MQTT Programming with Python
  3. Packt Upsell
    1. Why subscribe?
    2. PacktPub.com
  4. About the Author
    1. About the reviewer
    2. Packt is searching for authors like you
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Download the color images
      3. Conventions used
    4. Get in touch
      1. Reviews
  6. Installing an MQTT 3.1.1 Mosquitto Server
    1. Understanding convenient scenarios for the MQTT protocol
    2. Working with the publish-subscribe pattern
    3. Working with message filtering
    4. Understanding the MQTT puzzle – clients, servers, and connections
    5. Installing a Mosquitto server on Linux
    6. Installing a Mosquitto server on macOS
    7. Installing a Mosquitto server on Windows
    8. Considerations for running a Mosquitto server in the cloud
    9. Test your knowledge
    10. Summary
  7. Using Command-Line and GUI Tools to Learn How MQTT Works
    1. Subscribing to topics with a command-line tool
    2. Subscribing to topics with a GUI tool
    3. Publishing messages with a command-line tool
    4. Publishing messages with a GUI tool
    5. Unsubscribing from topics with a GUI tool
    6. Learning best practices for topics
    7. Understanding MQTT wildcards
    8. Learning about the different QoS levels
    9. Working with at least once delivery (QoS level 1)
    10. Working with exactly once delivery (QoS level 2)
    11. Understanding overhead in the different Quality of Service levels
    12. Test your knowledge
    13. Summary
  8. Securing an MQTT 3.1.1 Mosquitto Server
    1. Understanding the importance of securing a Mosquitto server
    2. Generating a private certificate authority to use TLS with Mosquitto
    3. Creating a certificate for the Mosquitto server
    4. Configuring TLS transport security in Mosquitto
    5. Testing the MQTT TLS configuration with command-line tools
    6. Testing the MQTT TLS configuration with GUI tools
    7. Creating a certificate for each MQTT client
    8. Configuring TLS client certificate authentication in Mosquitto
    9. Testing the MQTT TLS client authentication with command-line tools
    10. Testing the MQTT TLS configuration with GUI tools
    11. Forcing the TLS protocol version to a specific number
    12. Test your knowledge
    13. Summary
  9. Writing Code to Control a Vehicle with Python and MQTT Messages
    1. Understanding the requirements to control a vehicle with MQTT
    2. Defining the topics and commands
    3. Creating a virtual environment with Python 3.6.x and PEP 405
    4. Understanding the directory structure for a virtual environment
    5. Activating the virtual environment
    6. Deactivating the virtual environment
    7. Installing paho-mqtt for Python
    8. Connecting a client to the secured MQTT server with paho-mqtt
    9. Understanding callbacks
    10. Subscribing to topics with Python
    11. Configuring certificates for IoT boards that will work as clients
    12. Creating a class to represent a vehicle
    13. Receiving messages in Python
    14. Working with multiple calls to the loop method
    15. Test your knowledge
    16. Summary
  10. Testing and Improving Our Vehicle Control Solution in Python
    1. Processing commands with Python
    2. Sending messages with Python
    3. Working with the network loop with Python
    4. Working with last will and testament with Python
    5. Working with retained last will messages
    6. Understanding blocking and non-blocking code
    7. Using the threaded client interface
    8. Test your knowledge
    9. Summary
  11. Monitoring a Surfing Competition with Cloud-Based Real-Time MQTT Providers and Python
    1. Understanding the requirements
    2. Defining the topics and payloads
    3. Coding a surfboard sensor emulator
    4. Configuring the PubNub MQTT interface
    5. Publishing data retrieved from sensors to the cloud-based MQTT server
    6. Working with multiple MQTT servers
    7. Running multiple clients
    8. Building a web-based dashboard with freeboard
    9. Test your knowledge
    10. Summary
  12. Solutions
    1. Chapter 1: Installing an MQTT 3.1.1 Mosquitto server
    2. Chapter 2: Using Command-Line and GUI Tools to Learn How MQTT Works
    3. Chapter 3: Securing an MQTT 3.1.1 Mosquitto Server
    4. Chapter 4: Writing Code to Control a Vehicle with Python and MQTT Messages
    5. Chapter 5: Testing and Improving our Vehicle Control Solution in Python
    6. Chapter 6: Monitoring a Surfing Competition with Cloud-Based Real-Time MQTT Providers and Python
  13. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: Hands-On MQTT Programming with Python
  • Author(s): Gaston C. Hillar
  • Release date: May 2018
  • Publisher(s): Packt Publishing
  • ISBN: 9781789138542