MQTT Protocol Used With a Combustible Gas Detector Network

Introduction

Over the past few years, there has been an ever-increasing number of Internet of Things projects being launched around the world.  The large majority of these come in two camps.  Using microphones and speakers with machine learning to mimic audio or sound activity and the other camp uses motion sensors to monitor movement activity such as people monitoring or autonomous vehicles.

The next frontier so to speak could very well be an increasing number of projects based on what is in the air around us that are largely invisible.  For example, a single flammable gas detector might uncover an abnormal level of methane gas, a networked system of methane sensors could pinpoint the leak, compute the magnitude of the gas escaping, likely path over time, all together enabling responders to prioritize a plan of action.

This application demonstrates the use of Message Queueing Transport Telemetry, MQTT, a popular lightweight IoT protocol, with the NevadaNano MPS™ Flammable Gas Sensor. It was developed around their Flammable Gas Sensor Evaluation Unit and Raspberry Pi. The version of MQTT used for this application is 3.1.1, which while not the latest version is the most common version in use at this time.

There are three primary elements in the demonstration: A client application that runs on a Raspberry Pi board that interfaces with two MPS flammable sensors, an MQTT server, which was implemented on a second Raspberry Pi, and a command/monitor client that runs on a Linux workstation.

Both client applications are written in Python, using the Paho-MQTT library from the Eclipse Foundation. The MQTT server used for these applications is the Mosquitto server, also from Eclipse.

The two clients are configured in a command/response paradigm, where the workstation client issues commands and requests data from the sensor client. The sensor client application acts essentially as a command/data request server. The flammable sensor client application incorporates a NevadaNano UART utility directly with calls from within the Python code. 

MQTT Protocol Configuration

The configuration of the MQTT demonstration consists of two Raspberry Pi boards and a Linux workstation. One Raspberry Pi is used for an MQTT server and one is used for the MQTT client that manages the interface with two MPS flammable sensors. A Linux workstation application is used for the second client, which provides remote command/request monitoring of the sensors. The following diagram, Figure 1, shows the logical layout of the two clients and the server.

Figure 1

As an IoT protocol, MQTT allows clients and servers to be anywhere on the Internet. However in this case, all three elements shown in Figure 1, the two clients and one server, were run on a local network to test the applications. As shown in Figure 1, both clients subscribe to topics and both publish to topics.

MQTT Server

There is no particular significance to the fact that a Raspberry Pi board was chosen to host the MQTT server. Mosquitto, the MQTT server used for this application, can be installed on any system running Windows, macOS, or Linux. Small boards like Raspberry Pi or Beagle Bone are just handy for filling this type of role.

It should also be noted that there are alternative MQTT servers that can be installed locally or used from the Cloud. Mosquitto is a light-weight convenient option that is provided by the same organization that provides the Python MQTT library used by the client applications described here.

MPS Sensor Interface Client

The MPS Sensor Interface Client was deployed on another Raspberry Pi. First, the flammable gas sensors must be connected to the Raspberry Pi through the sensors’ UART interfaces. Then the sensor control code supplied as a download from Nevada Nanotech must be set up as it is an integral part of the MQTT client application. Finally the MQTT Python client application must be installed and configured.

UART Interface Configuration

The Nevada Nanotech MPS Flammable Gas Sensor Evaluation Unit comes with an MPS flammable gas sensor, an interface board, and USB cable. For the MPS Gas Sensor Interface Client application two sensors with an interface board for each are used. The interface board has a connector for a direct UART connection to the sensor as well as a USB connector, which gets translated into a UART connection to the sensor. For the MQTT demonstration two interface boards with sensors have been connected to a Raspberry Pi board. Figure 2 below shows two MPS sensors connected with USB to a Raspberry Pi 3B board. 

 

Figure 2

MQTT Python Configuration

Python virtual environments was employed for the development and testing of this MQTT application. While it is not required, it is a convenient way to isolate a Python environment and its packages from any other Python projects implemented on the same platform. Then from within the virtual environment, the Paho-MQTT library was installed. Version 1.3.1 was used for this MQTT application. 

Command/Monitor Client

An Ubuntu Linux workstation was used for the Command/Monitor Client. Again, a python virtual environment was setup in the same way that it was done for the MPS Sensor Interface Client on Raspberry Pi. And then the same version of Paho-MQTT used for the MPS Sensor Interface client, 1.3.1, was installed.

Applications

The MPS Sensor Interface Client Application and the Command/Monitor Client Application subscribe and publish to two categories of topics: Commands and Command Responses. The paradigm is command/response where the Command/Monitor Client publishes commands for which the MPS Sensor Interface Client subscribes. Conversely, the MPS Gas Sensor Interface Client publishes command responses for which the Command/Monitor Client subscribes.

MPS Sensor Interface Client Application

There are three files, config.py, MPS_commands.py, and MPS_mqtt_client.py. 

config.py

As the name suggests, the config.py file contains configuration information for the application. The information contained in it is specific for every configuration/installation of the application. The information is contained in a separate file because it is the most likely to be changed. 

MPS_commands.py

The MPS_commands.py contains symbolic references to command strings and command keys. The information is contained in a separate file because it is used by both applications.

MPS_mqtt_client.py

The MPS_mqtt_client.py file contains all of the executable python code for the application. It contains two class definitions and the main entry point of the application. The two classes are MPS and MPS_CommandProcessor. 

The MPS class deals with the interface to an MPS sensor and contains all the command functions. The MPS_CommandProcessor class deals with the MQTT protocol portion of the application. It connects to the server, subscribes to the commands topic and processes incoming commands in an endless loop. 

The application requires two command-line parameters, the first is the sensor name and the second is the ttyUSB port number. The sensor name will be MSPpi0 or MPSpi1 and the port number will be 0 or 1. The sensor name is used to fill in the middle level of the topic names.

For the full demonstration of the MQTT applications, two instantiations of the MPS Sensor Client are used. Recall that there are two MPS Flammable sensors connected to the MSP sensor interface Raspberry Pi. There needs to be separate independent processing for each sensor. While there are multiple ways to implement this, the simplest is to just let the Raspberry Pi Linux handle it for us. The application as written handles one sensor. We can simply start two instantiations of the application where each uses a different ttyUSBx port.

 

Command/Monitor Client Application

There are three files, config.py, MPS_commands.py, and MPS_mqtt_remote_monitor.py. 

config.py

The config.py file contains configuration information just as the one for the MPS Sensor Client. Again, the information contained in it is specific for every configuration/installation of the application. It’s a separate file because it is the most likely to be changed. Just as the config.py for the MPS Sensor Client, 

MPS_commands.py

The MPS_commands.py file is identical to the one for the MPS Sensor Client. In fact, it needs to be.

MPS_mqtt_remote_monitor.py

The MPS_mqtt_remote_monitor.py file contains all of the executable python code for the application. It subscribes to the “commandreturns” of both sensors and publishes on the “commands” topics of both sensors.

 

This is the overview of creating a network of flammable gas detectors based on NevadaNano’s LEL gas detector and the MQTT protocol.  Contact [email protected] to download the full application note to get started on your own industrial gas detector system.

 

 

Gary Marks, co-founder and CEO of iP Solutions Corp., has 25+ years’ experience in development of IoT, sensors, and embedded systems technology. He has applied his development expertise in markets that include gas detection, agriculture, public water systems, energy efficiency and smart grid applications.

Scroll to Top
WEBINAR: Reduce LDAR costs & cut emissions with MethaneTrack™ USA | UAE/Europe
This is default text for notification bar