Reader small image

You're reading from  Hands-On MQTT Programming with Python

Product typeBook
Published inMay 2018
Reading LevelExpert
PublisherPackt
ISBN-139781789138542
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Gaston C. Hillar
Gaston C. Hillar
author image
Gaston C. Hillar

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. Currently, Gaston is an independent IT consultant and a freelance author who is always looking for new adventures anywhere in the world. He was 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 has written many articles about Java for Oracle Java Magazine. Gaston was also a former Microsoft MVP in technical computing. He lives with his wife, Vanesa, and his two sons, Kevin and Brandon.
Read more about Gaston C. Hillar

Right arrow

Writing Code to Control a Vehicle with Python and MQTT Messages

In this chapter, we will write 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. We will gain take an in-depth look at the following:

  • Understanding the requirements to control a vehicle with MQTT
  • Defining the topics and commands
  • Learning the benefits of working with Python
  • Creating a virtual environment with Python 3.x and PEP 405
  • Understanding the directory structure for a virtual environment
  • Activating the virtual environment
  • Deactivating...

Understanding the requirements to control a vehicle with MQTT

In the previous three chapters, we learned how MQTT works in detail. We understood how to establish a connection between an MQTT client and an MQTT server. We learned what happened when we subscribed to topic filters and when a publisher sent messages to specific topics. We installed a Mosquitto server and then we secured it.

Now, we will use Python as our main programming language to generate MQTT clients that will act as publishers and subscribers. We will connect a Python MQTT client to the MQTT server and we will process commands to control a small vehicle with MQTT messages. The small vehicle replicates many capabilities found in real-life road vehicles.

We will use TLS encryption and TLS authentication because we don't want any MQTT client to be able to send commands to our vehicle. We want our Python 3.x...

Defining the topics and commands

We will use the following topic name to publish the commands for a vehicle: vehicles/vehiclename/commands, where vehiclename must be replaced with a unique name assigned to a vehicle. For example, if we assign vehiclepi01 as the name for a vehicle that is powered by a Raspberry Pi 3 Model B+ board, we will have to publish commands to the vehicles/vehiclepi01/commands topic. The Python code that runs on this board will subscribe to this topic to receive messages with commands and react to them.

We will use the following topic name to make the vehicles publish details about the successfully executed commands: vehicles/vehiclename/executedcommands, where vehiclename must be replaced with a unique name assigned to a vehicle. For example, if we assign vehiclebeagle03 as the name for a vehicle that is powered by a BeagleBone Black board, the client that...

Creating a virtual environment with Python 3.6.x and PEP 405

In the next sections and chapters, we will be writing different pieces of Python code that will subscribe to topics and will also publish messages to topics. As happens whenever we want to isolate an environment that requires additional packages, it is convenient to work with Python virtual environments. Python 3.3 introduced lightweight virtual environments, and they were improved in Python 3.4. We will work with these virtual environments, and therefore, you will need Python 3.4 or greater. You can read more about PEP 405 Python Virtual Environment, which introduced the venv module, here: https://www.python.org/dev/peps/pep-0405.

All the examples for this book were tested on Python 3.6.2 on macOS and Linux. The examples were also tested on the IoT boards mentioned throughout the book and their most popular operating...

Understanding the directory structure for a virtual environment

The specified target folder has a new directory tree that contains Python-executable files and other files that indicate it is a PEP405 virtual environment.

In the root directory for the virtual environment, the pyenv.cfg configuration file specifies different options for the virtual environment and its existence is an indicator that we are in the root folder for a virtual environment. In Linux and macOS, the folder will have the following main subfolders: bin, include, lib, lib/python3.6, and lib/python3.6/site-packages. Note that the folder names can be different based on the specific Python version. In Windows, the folder will have the following main subfolders: Include, Lib, Lib\site-packages, and Scripts. The directory trees for the virtual environment in each platform are the same as the layout of the Python...

Activating the virtual environment

Now that we have created a virtual environment, we will run a platform-specific script to activate it. After we activate the virtual environment, we will install packages that will only be available in this virtual environment. This way, we will work with an isolated environment in which all the packages we install won't affect our main Python environment.

Run the following command in Terminal in Linux or macOS. Note that the results of this command will be accurate if you don't start a different shell than the default shell in the Terminal session. If you have doubts, check your Terminal configuration and preferences:

echo $SHELL

The command will display the name of the shell you are using in Terminal. In macOS, the default is /bin/bash and this means you are working with the bash shell. Depending on the shell, you must run a different...

Deactivating the virtual environment

It is extremely easy to deactivate a virtual environment generated with the previously explained process. The deactivation will remove all the changes made in the environment variables and will change the prompt back to its default message. Once you deactivate a virtual environment, you will go back to the default Python environment.

In macOS or Linux, just type deactivate and press Enter.

In Command Prompt, you have to run the deactivate.bat batch file included in the Scripts folder. In our example, the full path for this file is %USERPROFILE%\HillarMQTT\01\Scripts\deactivate.bat.

In Windows PowerShell, you have to run the Deactivate.ps1 script in the Scripts folder. In our example, the full path for this file is $env:userprofile\HillarMQTT\01\Scripts\Deactivate.ps1. Remember that you must have scripts execution enabled in Windows PowerShell...

Installing paho-mqtt for Python

The Eclipse Paho project provides an open source client implementation of MQTT. The project includes a Python client, also known as the Paho Python Client or Eclipse Paho MQTT Python client library. This Python client has been contributed from the Mosquitto project and was originally known as the Mosquitto Python client. The following is the web page for the Eclipse Paho project: http://www.eclipse.org/paho. The following is the web page for the Eclipse Paho MQTT Python client library version 1.3.1, that is, the paho-mqtt module version 1.3.1: https://pypi.python.org/pypi/paho-mqtt/1.3.1.

We can use paho-mqtt in many modern IoT boards that support Python 3.x or greater. We just need to make sure that pip is installed to make it easier to install paho-mqtt. You can use your development computer to run the examples or any of the afore mentioned boards...

Connecting a client to the secured MQTT server with paho-mqtt

First, we will use paho-mqtt to create an MQTT client that connects to the Mosquitto MQTT server. We will write a few lines of Python code to establish a secured connection and subscribe to a topic.

In Chapter 3, Securing an MQTT 3.1.1 Mosquitto Server, we secured our Mosquitto server, and therefore, we will use the digital certificates we created to authenticate the client. Most of the time, we will work with an MQTT server that uses TLS, and therefore, it is a good idea to learn how to establish a connection with TLS and TLS authentication. It is easier to establish an unsecured connection with an MQTT server, but it won't be the most common scenario we will face when developing applications that work with MQTT.

First, we need to copy the following files, which we created in Chapter 3, Securing an MQTT 3.1.1...

Understanding callbacks

The previous code uses the recently installed paho-mqtt version 1.3.1 module to establish an encrypted connection with the MQTT server, subscribe to the vehicles/vehiclepi01/tests topic filter, and run code when we receive messages in the topic. We will use this code to understand the basics of paho-mqtt. The code is a very simple version of an MQTT client that subscribes to a topic filter and we will definitely improve it in the next sections.

The first line imports the variables we have declared in the previously coded config.py file. The second line imports paho.mqtt.client as mqtt. This way, whenever we use the mqtt alias, we will be referencing paho.mqtt.client.

When we declare a function, we pass this function as an argument to another function or method, or we assign this function to an attribute and then some code calls this function at some time...

Subscribing to topics with Python

The code calls the client.subscribe method with "vehicles/vehiclepi01/tests" as an argument to subscribe to this specific single topic and the qos argument set to 2 to request a QoS level of 2.

In this case, we will only subscribe to one topic. However, it is very important to know that we are not limited to subscribing to a single topic filter; we might subscribe to many topic filters with a single call to the subscribe method.

After the MQTT server confirms the successful subscription to the specified topic filter with a SUBACK response, the specified callback in the client.on_subscribe attribute will be executed, that is, the on_subscribe function. This function receives a list of integers in the granted_qos argument that provides the QoS level that the MQTT server has granted for each of the topic filter subscription requests....

Configuring certificates for IoT boards that will work as clients

Now, we will write Python code that will be ready to work on the different IoT boards. Of course, you can work with a single development computer or development board. There is no need to run the code on different devices. We just want to make sure we can write code that will be capable of running on different devices.

Remember to copy the files we created in the previous chapter to a directory on the computer or device that will represent the board that controls a vehicle and that we will use to run a Python script. If you will be working with the same computer or device you have been using so far, you don't need to follow the next step.

We saved the files in a directory called mqtt_certificates. Create a board_certificates directory on the computer or board you are going to use as the MQTT client for this...

Creating a class to represent a vehicle

We will create the following two classes:

  • Vehicle: This class will represent a vehicle and provide methods that will be called whenever a command has to be processed. In order to keep the example simple, our methods will just print the actions that the vehicle executes after each method is called to the console output. A real-life class that represents a vehicle would interact with the engine, the lights, the actuators, the sensors, and the other different components of the vehicle whenever each method is called.
  • VehicleCommandProcessor: This class will represent a command processor that will establish a connection with an MQTT server, subscribe to a topic in which the MQTT client will receive messages with commands, analyze the incoming messages, and delegate the execution of the commands to an associated instance of the Vehicle class...

Receiving messages in Python

We will use the recently installed paho-mqtt version 1.3.1 module to subscribe to a specific topic and run code when we receive messages in the topic. We will create a VehicleCommandProcessor class in the same Python file, named vehicle_mqtt_client.py, in the main virtual environment folder. This class will represent a command processor associated to an instance of the previously coded Vehicle class, configure the MQTT client and the subscription to the client, and declare the code for the callbacks that are going to be executed when certain events related to MQTT are fired.

We will split the code for the VehicleCommandProcessor class into many code snippets to make it easier to understand each code section. You have to add the next lines to the existing vehicle_mqtt_client.py Python file. The following lines declare the VehicleCommandProcessor class...

Working with multiple calls to the loop method

The following lines declare the process_incoming_commands method that is part of the VehicleCommandProcessor class. You have to add these lines to the existing vehicle_mqtt_client.py Python file. The code file for the sample is included in the mqtt_python_gaston_hillar_04_01 folder, in the vehicle_mqtt_client.py file:

    def process_incoming_commands(self):
self.client.loop()

The process_incoming_commands method calls the loop method for the MQTT client and ensures communication with the MQTT server is carried out. Think about the call to the loop method as synchronizing your mailbox. Any pending messages to be published in the outgoing box will be sent, any incoming messages will arrive to the inbox, and the events that we have previously analyzed will be fired. This way, the vehicle command processor will receive messages...

Test your knowledge

Let's see whether you can answer the following questions correctly:

  1. Which of the following Python modules is the Paho Python Client?
    1. paho-mqtt
    2. paho-client-pip
    3. paho-python-client
  2. To establish a connection with an MQTT server that uses TLS, which method do you have to call for the paho.mqtt.client.Client instance before calling connect?
    1. connect_with_tls
    2. tls_set
    3. configure_tls
  1. After the paho.mqtt.client.Client instance establishes a connection with the MQTT server, the callback assigned to which of the following attributes will be called?
    1. on_connection
    2. on_connect
    3. connect_callback
  2. After the paho.mqtt.client.Client instance receives a message from one of the topic filters to which it has subscribed, the callback assigned to which of the following attributes will be called?
    1. on_message_arrived
    2. on_message
    3. message_arrived_callback
  3. Which of the following...

Summary

In this chapter, we analyzed the requirements to control a vehicle with MQTT messages. We defined the topics that we would use and the commands that would be part of the messages’ payloads to control a vehicle. Then, we worked with the Paho Python Client to write Python code that connected an MQTT client to the MQTT server.

We understood the methods we needed to call for the Paho Python Client and their parameters. We analyzed how callbacks worked and we wrote code to subscribe to topic filters as well as to receive and process messages.

We wrote code that processed commands for a vehicle with Python. The code is able to run on different IoT platforms, including Raspberry Pi 3 family boards, Qualcomm DragonBoard, BeagleBone Black, MinnowBoard Turbot, LattePanda, UP squared, and also on any computer that is capable of executing Python 3.6.x code. We worked with the...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Hands-On MQTT Programming with Python
Published in: May 2018Publisher: PacktISBN-13: 9781789138542
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Gaston C. Hillar

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. Currently, Gaston is an independent IT consultant and a freelance author who is always looking for new adventures anywhere in the world. He was 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 has written many articles about Java for Oracle Java Magazine. Gaston was also a former Microsoft MVP in technical computing. He lives with his wife, Vanesa, and his two sons, Kevin and Brandon.
Read more about Gaston C. Hillar