Demo application: MQTT driver commnicating with Mosquitto MQTT Broker.

Introduction

In this article, we’ll create a demo environment for the basic configurations of an MQTT driver communicating with a Mosquitto MQTT Broker, with a client (MQTT Lens) connected for testing purposes in MQTT messages publication/subscription scenarios.

MQTT (Message Queue Telemetry Transport) protocol

The MQTT protocol, projected for light messages transportation, is one of the main network protocols for Internet of Things (IoT) devices. With MQTT, the device can “listen” and be notified only when one of the variables changes. Therefore, the connection remains open, but data will only be transmitted when necessary, which helps save battery and bandwidth, and improve real-time resources as a whole.

In this protocol, the communication hub is the MQTT Broker, which is responsible for managing all messages between senders and receivers. The MQTT protocol sets up two types of entities on the network: one message broker and several clients. The broker is a server that receivers all clients’ messages, and then routes these messages to clients of relevant destinatons. A client is any device that can interact with the broker and then receive messages; examples are IoT sensors in the field, or devices in a data center processing the IoT data.

The communication sequence is the following:

  1. Firstly, the client connects to the broker. It can subscribe to any message “topic” in the broker.
  2. Then, the client publiches all messages from a topic, sending both the message and the topic to the broker.
  3. After that, the broker routes the message to all clients subscribed to this topic.

 

Installing the Mosquitto MQTT Broker

1. First of all, download it from the Download section at Mosquitto.org. In this example, we’ll use the  mosquitto-1.6.2-install-windows-x86 installer, which is the native compilation  applicable to Windows Vista or higher.

2. Then, run the downloaded file, select the Service component, and click Next.

3.  After that, select the location for the installation and click Install.

4. The next step is to start the Mosquitto Broker service, via Control Panel–Administrative Tools–Services (services.msc).

5. To check whether the broker is working, use  netstat -an command from the prompt.

 

Testing with MQTTlens client

The MQTT Lens is a Chrome extension that is used for connecting to the MQTT Broker.

Note: Any other MQTT client can be used (e.g.: MQTT X, MQTT Explorer, etc.).

1. To add the extension, you must first access this link.

2. After installing it, click the installation or initialization app and run the add-on.

3. Then, add the connection to the broker.

4. By clicking Add a new connection, you will access a new screen; there, you’ll need to type the publication and subscription data in order to test the broker.

 

Subscribe: Plant-1/Sensor-001/Data/#
Publish: Plant-1/Sensor-001/Data/
Message: {"Temperature":25,"Humidity":44}

5. In the example above, notice that the  # symbol in the Subscribe box will subscribe all the contents available in Plant-1/Sensor-001/Data/ topic.

6.  For further information about the message, click Message Details, which is right next to the message.

 

Setting up the MQTT driver to communicate with Mosquitto Broker

1. First of all: in order to to receive the message from the broker, you’ll need to create the following JSON template for the driver to extract the message:

{"Temperature":"E3VAL",  "Humidity":"E3VAL"}

2. After that, access the Ethernet tab and type the IP address and the port being used by the broker.

3. Since the keyword used in the template for the extracted values is E3VAL, you’ll need to create a two-element block tag to receive one value per item.

4. Then, go to the tag’s Item parameter and and specify the topic and template (Plant-1/Sensor-001/Data/;Plant) desired.

The attached demo application was developed with Elipse E3 v4.8.300.

Attachments:

App.zip
Mosquitto-1.6.2-install-windows-x86.zip

Print Friendly, PDF & Email

Este artigo foi útil? Was this helpful?

Classificação média - Average rating 0 / 5. Count: 0

Leave a Reply

Your email address will not be published.Required fields are marked *