O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

MQTT Essentials - A Lightweight IoT Protocol

Book Description

Send and receive messages with the MQTT protocol for your IoT solutions.

About This Book

  • 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

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.

What You Will Learn

  • Understand how MQTTv3.1 and v3.1.1 works in detail
  • Install and secure a Mosquitto MQTT broker by following best practices
  • Design and develop IoT solutions combined with mobile and web apps that use MQTT messages to communicate
  • Explore the features included in MQTT for IoT and Machine-to-Machine communications
  • Publish and receive MQTT messages with Python, Java, Swift, JavaScript, and Node.js
  • Implement the security best practices while setting up the MQTT Mosquitto broker

In Detail

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

Style and approach

This step-by-step guide describes the MQTT protocol for your IoT projects

Downloading the example code for this book. You can download the example code files for all Packt books you have purchased 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 code file.

Table of Contents

  1. MQTT Essentials - A Lightweight IoT Protocol
    1. MQTT Essentials - A Lightweight IoT Protocol
    2. Credits
    3. About the Author
    4. Acknowledgment
    5. About the Reviewer
    6. www.PacktPub.com
      1. Why subscribe?
    7. Customer Feedback
    8. Dedication
    9. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Downloading the example code
        2. Downloading the color images of this book
        3. Errata
        4. Piracy
        5. Questions
    10. 1. Lightweight Messaging with MQTT 3.1.1 and Mosquitto
      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 broker on Ubuntu Linux
      6. Installing a Mosquitto broker on macOS
      7. Installing a Mosquitto broker on Windows
      8. Subscribing to topics
      9. Publishing messages
      10. Unsubscribing from topics
      11. Working with best practices when creating topics
      12. Understanding wildcards
      13. Learning about the different Quality of Service levels
      14. Working with at least once delivery
      15. Working with exactly once delivery
      16. Test your knowledge
      17. Summary
    11. 2. Securing an MQTT Mosquitto Server
      1. 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 MQTT.fx
      6. Testing the MQTT TLS configuration with MQTT-spy
      7. Creating a certificate for each MQTT client
      8. Configuring TLS client certificate authentication in Mosquitto
      9. Testing the MQTT TLS client authentication with MQTT.fx
      10. Testing the MQTT TLS configuration with MQTT-spy
      11. Forcing the TLS protocol version
      12. Test your knowledge
      13. Summary
    12. 3. Controlling a Drone with Python
      1. Understanding the requirements to control a drone with MQTT
      2. Defining the topics and commands
      3. Installing paho-mqtt for Python
      4. Connecting the client to the MQTT server with Python
      5. Understanding callbacks
      6. Subscribing to topics with Python
      7. Creating a class to represent a drone
      8. Receiving messages with Python
      9. Processing commands with Python
      10. Sending messages with Python
      11. Working with the network loop with Python
      12. Test your knowledge
      13. Summary
    13. 4. Interacting with Sensors in Java
      1. Understanding the requirements
      2. Defining the topics and commands
      3. Installing the Eclipse Paho Java Client
      4. Creating a security helper class to establish a TLS secured connection
      5. Configuring a TLS secured connection in Java
      6. Working with asynchronous notifications and callbacks in Java
      7. Subscribing to topics and understanding notification events in Java
      8. Processing commands that interact with sensors in Java
      9. Working with last will and testament in Java
      10. Publishing messages with commands to target sensors
      11. Test your knowledge
      12. Summary
    14. 5. Controlling Home Automation in JavaScript
      1. Understanding the requirements
      2. Defining the topics and commands to control home automation
      3. Enabling WebSockets for a Mosquitto server
      4. Testing WebSockets with the Eclipse Paho client JavaScript utility
      5. Understanding the Eclipse Paho JavaScript asynchronous client
      6. Creating an HTML 5 web page to control LEDs
      7. Publishing messages to target remote devices in JavaScript
      8. Processing the received messages in JavaScript
      9. Working with WebSockets to connect to the MQTT server
      10. Subscribing to topics in JavaScript
      11. Processing commands that interact with LEDs in Node.js
      12. Running the home automation web application
      13. Debugging asynchronous code with Chrome Developer Tools (CDT) in JavaScript
      14. Securing MQTT over WebSockets with TLS
      15. Test your knowledge
      16. Summary
    15. 6. Interacting with Actuators in Swift
      1. Understanding the requirements
      2. Defining topics and commands
      3. Enabling connections without TLS for a Mosquitto server
      4. Installing the CocoaMQTT client library to create a new iOS app
      5. Building the UI for the iOS app that will control an actuator
      6. Understanding the CocoaMQTT client in Swift 3
      7. Connecting to the MQTT server and subscribing to topics in Swift
      8. Publishing messages to target actuators in Swift
      9. Processing commands that interact with actuators in Node.js
      10. Running the iOS app that controls an actuator with MQTT
      11. Debugging events in Swift
      12. Test your knowledge
      13. Summary
    16. A. Exersise Answers
      1. Chapter 1, Lightweight Messaging with MQTT 3.1.1 and Mosquitto
      2. Chapter 2, Securing an MQTT Mosquitto Server
      3. Chapter 3, Controlling a Drone with Python
      4. Chapter 4, Interacting with Sensors in Java
      5. Chapter 5, Controlling Home Automation in JavaScript
      6. Chapter 6, Interacting with Actuators in Swift