User Tools

Site Tools


iot:tutorial:mqtt

This is an old revision of the document!


IoT

Nowadays, the number of devices from which you can get various data is growing. The solution to the problem of their interaction is the concept of the Internet of Things (IoT). The concept is to unify devices into a network for some reason and then integrate them into larger networks.

MQTT Protocol

MQTT today has become an intermediary in IoT communication. It is supported by integration controller WebHMI and 7bit brand products such as Synapse, AirPoint and AirGate.

What is MQTT?

It is a lightweight, open and affordable protocol. It was designed for severe operating conditions, so it is ideal for devices with low power and limited autonomous. Such as smartphones, sensors and other devices, the number of which is constantly growing. MQTT has extremely low overhead publish/subscribe model, and bidirectional capabilities of MQTT are uniquely suited to meet the demands of industrial control systems. To understand, look at this diagram.

There is no need in direct connection which is quite fragile and entails overhead. So, the broker is the gamechanger. Any device can be both publisher and subscriber. Publishing of message is triggered only by some event, e.g. time condition. After that, the data packet is received by the broker and then sent to subscribers, which subscribed to that topic which data was published. In that way, the subscriber can get the data about events that happened on distant objects, collected from different publishers and collect it in a database to obtain further analytics. Therefore, it would be suitable to apply for electricity consumption monitoring, but also measuring non-electrical quantities.

MQTT using WebHMI

WebHMI is able to communicate using MQTT. Using this protocol WebHMI can act as a subscriber and a broker.

WebHMI as MQTT Broker

To start, firstly go to “Settings” to make sure that “MQTT Server” is turned on.

About how to use MQTT on WebHMI

WebHMI as MQTT Publisher (Client)

There is a possibility to create a WebHMI connection to subscribe or publish MQTT messages. This allows WebHMI register mapping with certain topics on any broker. So editing register value will trigger WebHMI to publish MQTT message and vice versa: if you have an active register, receiving the message from the broker will trigger changing the value.

Publish to WebHMI from (the same or another) WebHMI

Acting as a client, WebHMI can connect to any broker, i.e. its own MQTT broker.

Let’s post something to the WebHMI MQTT Broker from a WebHMI MQTT Client. It is convenient to stay subscribed to the topics in “MQTT.fx”, but this time publish with a WebHMI. To start publish something to broker it is necessary to become a client, so need to create a connection to it.

Click on “New connection” and configure it with:

  Device Model: MQTT
  Address: 192.168.1.1 (it is your IP of WebHMI with Broker )
  TCP Port: 1883

After that, create a new register into that connection which will represent MQTT topic related to MQTT broker. Click “New register” and fill in the following settings: When the connection is created and active it means that connection is established with the broker, let’s create a register with “Address” filled with the same topic name “iot-test”. Also it would be useful to add Lua script alias, e.g. “mqtt_publish”. It will be used in the script later.

  Connection: MQTT WebHMI Client (that one, that created before)
  Address: iot-test (any name, it is MQTT topic to subcribe and to publish)
  Script alias: mqtt_publish  

Go to “Value” tab to change its type to “String” And click “Add” button.

Now, it is time to change registers value manually and check the result response on another client such as Android app, PC clients such as “MQTT.fx” or another WebHMI.

Let's check the response in MQTT.fx PC client. How to configure it will be shown later.

In the MQTT.fx will be shown caught changes in the subscribed topics.

Connect to WebHMI MQTT Broker with external client

From PC client to WebHMI

Using any MQTT client it is possible to be connected to WebHMI MQTT Broker. For example, freeware MQTT client “MQTT.fx” will be used.


MQTT.fx


Official site download link


To do this, let’s create a new connection with the following settings: IMG Click on “+” in the lower left corner and set:

  Profile Type: "MQTT Broker
  Profile Name: e.g. “WebHMI”
  Broker Address: 192.168.1.21, this is the IP of the WebHMI.
  Broker Port: 1883

Press “Apply” and then “OK”.
Let’s connect to WebHMI Broker by pressing the “Connect” button.

IMG If everything is fine than connection will be established, so that indicator became green.

From Android client to WebHMI

In the Google Play Market, there is a lot of MQTT Client apps. For example, it would be shown the connection with MQTT Dashboard.

After first run it looks like this:

So, let’s add WebHMI Broker by click “Add first broker” button.

Fill in the following settings. In the address form your WebHMI IP address. In our case, it is 192.168.1.21.

Then, click the “Save” button in the bottom centre position.

Now, we have an empty connection. It is time to subscribe to a topic. It is also an option to publish to it. To do this, let’s click the “+” button in the bottom centre part of the screen.

To demonstrate how the function works, the “Standard” format will suffice.

Pick the “Text” type.

Fill in the “Subscribe topic” form the “iot-test” value and then press “Copy” symbol near “Publish topic” to copy the topics name to this form also. In the end, click the “Save” icon in the bottom centre position.

To test the connection, you can try to change the register’s value and check the smartphone's screen for the result.

The text message was sent through the MQTT protocol to WebHMI broker and the sent to the Android app.

It also works in another direction and can be published from the smartphone.

Useful Applications

Sharing links, strings, etc. There are a lot of mobile apps to connect with, etc.

iot/tutorial/mqtt.1591881145.txt.gz · Last modified: 2020/06/11 13:12 by atolstov

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki