In the previous tutorial, it was learnt that how a smart phone and a PC can be set up as MQTT clients and their connection with an MQTT broker can be established. The smart phone was configured as an MQTT client using an android MQTT app – IOT MQTT Dashboard while the PC was configured as MQTT client using a chrome add-on – MQTTLens.
Now, in this tutorial, these MQTT clients – Mobile and PC will be made to communicate with each other using MQTT protocol. The communication between the MQTT clients is only possible via MQTT broker. An MQTT client can be set as publisher, subscriber or both. The publisher MQTT client sends data and information in the form of messages on defined topics to the MQTT broker. The MQTT subscriber can subscribe to defined topics and request data on those topics from the MQTT broker. When a client is configured as both publisher and subscriber, it receives messages on subscribed topics while can also publish messages on defined topics. In this tutorial, both clients (PC and Mobile) will be configured as both subscriber as well as publisher.
Creating Topics on Publish Side of Mobile Client –
After configuring Mobile as MQTT client, the Mobile client is connected to the broker. Now, for publishing a message with the mobile, it is necessary to create a topic and register that topic to the MQTT broker. After creating a topic, the mobile client can send messages on that topic any time whenever required.
After creating a connection (from the previous tutorial), the created connection will display on the MQTT dashboard. Tap on the connection to open connection specific activity.
Fig. 1: Mobile screenshot of Main Activity of IOT MQTT Dashboard
Fig. 2: Mobile Screenshot of Publish Subscribe Activity on IOT MQTT Dashboard
In the new activity, there are two tabs – Subscribe and Publish. Select the publish tab and tap the ‘Plus’ button on the top corner of the activity. A pop up will appear asking to select a component type.
Fig. 3: Mobile Screenshot of Publish Component Types on IOT MQTT Dashboard
Select text and a Publication activity will open. In this activity, write a friendly name for the client and write the topic name. The friendly name can be anything like Mobile-01. It is not mandatory that it be unique.
The topic name can be maximum 65535 bytes long. It should be an UTF-8 string. The topic can consist of one or more levels. In the topic name, each level is separated by the forward slash. The forward slash acts as topic level separator. Though, it is not necessary to create topics before publishing or subscribing, it is a good practice to do so in advance. An MQTT client can also create topics while publishing or subscribing messages. Any topic must have at least one character. It can be even a forward slash alone.
Fig. 4: Mobile Screenshot of Publication Activity on IOT MQTT Dashboard
So, create a topic like Mobile/Accelerometer/X/Axis. Next, to register the topic with the MQTT broker, it is important to set Quality of service level (Abbreviated as QoS). The QoS level defines the agreement between sender and receiver of a message regarding the guarantees of delivering a message. There are three QoS level (0, 1, and 2).
QoS-0 – The message on the topic will be delivered at most once. There can be possible of losing the message.
QoS-1 – The message will be delivered at least once. There can be chance of duplication of message.
QoS-2 – The message will be delivered exactly once.
Set the QoS level to 1 for publishing the messages. There is an option of retained flag. If it is checked, it is set to 1 and if left unchecked, it is set to 0. Set the retain flag to 0. If retain flag is set to 1 then the broker will hold on to the message after it has been delivered to the subscriber. So, if there will be now subscription on the topic, the last retained message will be delivered to the subscriber.
Fig. 5: Mobile Screenshot of Topic Creation on IOT MQTT Dashboard Publication Activity
Tap the create button on the top corner of the activity. Now, the mobile client is ready to publish messages on the defined topic.
Fig. 6: Mobile Screenshot of Publish Topics Listed on IOT MQTT Dashboard Publish Subscribe Activity
Creating Topics on Subscribe Side of Mobile Client –
Simultaneously, the topic on subscription side can also be set. The client subscribes the topic from MQTT broker. The Topic is the path to which other client is publishing the message. The subscriber client will register the topic on the broker. If the broker will have messages on that topic from the publisher client, the broker will transmits all those messages to the subscriber.
Tap on the connection to open connection specific activity and select the subscribe tab. Tap the ‘Plus’ button on the top corner of the activity. A Subscription activity will open.
In this activity, write a friendly name for the client and write the topic name. The friendly name can be anything like Mobile-01. It is not mandatory that it be unique.
Fig. 7: Mobile Screenshot of IOT MQTT Dashboard Subscribe Activity
Fig. 8: Mobile Screenshot of Topic Creation on IOT MQTT Dashboard Subscribe Activity
The MQTT protocol allows use of wildcards in topic names in subscription but not in publication. If a ‘+’ symbol is used as a topic level, it acts as single level wildcard. Suppose, if the topic name is Mobile/Accelerometer/+/Axis, then this will match to the following –
Mobile/Accelerometer/X/Axis
Mobile/Accelerometer/Y/Axis
Mobile/Accelerometer/Z/Axis
but not to these –
Mobile/Accelerometer/X/Change
Mobile/Accelerometer/Y/Change
Mobile/Brightness
If a ‘#’ symbol is used as a topic level, it acts as multiple level wildcard. Suppose, if the topic name is Mobile/Accelerometer/#, then this will match to the following –
Mobile/Accelerometer/X/Axis
Mobile/Accelerometer/Y/Axis
Mobile/Accelerometer/Z/Axis
Mobile/Accelerometer/X/Change
Mobile/Accelerometer/Y/Change
but not to these –
Mobile/Temperature/Celsius
Mobile/CPU/Speed
The topic names starting with ‘$’ symbol are reserved for internal statistics of the MQTT Broker. A client cannot publish messages to such topics.
Write a topic name like PC/BB/ISP and leave the unit filed. Set QoS level to 1 and keep is-numeric checkbox unchecked. Check the Notify Me checkbox so that whenever a message is received at broker for the subscribed topic, a notification will be generated by the app. Any JSON converter is not used so, keep the JSON converter option also unchecked. JSONPath expressions refer to JSON structures in context to XML documents. The topic name is prefixed with ‘$’ on checking this option making the topic reserved. Tap the create button. The subscribed topic will now display on the connection activity under subscribe tab.
Fig. 9: Mobile Screenshot of Subscribe Topics Listed on IOT MQTT Dashboard Publish Subscribe Activity
Creating Topics on Publish Side of PC Client –
After configuring PC as a client, PC client is connected to the MQTT broker. For publishing message, It is important to create a topic. Launch the MQTTLens add-on and create connection as described in the previous tutorial. Scroll down to Publish option.
Fig. 10: Screenshot of MQTTLens Publish Window
Write the topic name – ‘PC/BB/ISP’ and set the QoS level to 1. Keep the retained option unchecked.
Fig. 11: Screenshot of Topic Creation on MQTTLens Publish Window
Now, the PC client registers the topic to the MQTT broker and can send the message to the broker. Write a message like the topic PC/BB/ISP can have a message – Tikona and click on the Publish button.
Fig. 12: Screenshot of Publishing Message on a Topic on MQTTLens Publish Window
On clicking the publish button the message will be sent to the mobile client.
Fig. 13: Screenshot of MQTT message received on Mobile Client from PC Client via HiveMQ Broker
It can be observed that the MQTTLens assign a random Client ID to the PC. The messages received from the PC Client are visible on the Mobile Client with time stamp.
Creating Topics on Subscribe Side of PC Client –
The PC Client can subscribe to the topic published by the Mobile Client. Scroll down to subscribe option.
Fig. 14: Screenshot of MQTTLens Subscribe Window
Fig. 15: Screenshot of Topic Creation on MQTTLens Subscribe Window
Write the topic name – ‘Mobile/Accelerometer/X/Axis’, set the QoS Level to 1 and click subscribe button.
Fig. 16: Screenshot of Topic Creation on MQTTLens Subscribe Window
On the Mobile Client, send a message on the topic ‘Mobile/Accelerometer/X/Axis’ like ’14’ and click Publish button.
Fig. 17: Mobile Screenshot of Publishing Message on a Topic on IOT MQTT Dashboard
The PC client registers the topic to the MQTT broker and will receive the messages whenever the broker will have. Scroll down the MQTTLens window to check the received messages from the Mobile client.
Fig. 18: Screenshot of MQTT message received on PC Client from Mobile Client via HiveMQ Broker
Communication between mobile and PC clients –
The PC client and the Mobile Client have connected to the HiveMQ Broker with the same connection name. The mobile client is set as publisher for topic – ‘Mobile/Accelerometer/X/Axis’ and set as subscriber for topic – ‘PC/BB/ISP’. The PC client is set as publisher for topic – ‘PC/BB/ISP’ and set as subscriber for topic – ‘Mobile/Accelerometer/X/Axis’.
When either client publishes a message as publisher, the message is received on the other client as it has subscribed for that topic.
Mobile client is publishing the message “14” to the MQTT broker. The MQTT broker is passing the message to the PC client as PC client has subscribed the same topic. The PC client is publishing the message “Tikona” on the topic “PC/BB/ISP” to the MQTT broker. The Mobile client has also registered the same topic “PC/BB/ISP” from the MQTT broker. So, the mobile client receives the message automatically whenever PC client publishes the message.
The MQTT broker simply passes the messages to the subscribed clients. The two devices are communicating with each other using publish and subcription model. The communication between the multiple devices can be set up this way provided the connection name and MQTT broker remain the same.
PC to PC Commnication using HiveMQ Broker –
The communication between two PCs or two mobiles can also be setup using the HiveMQ broker. For connecting two PCs, the MQTTLens add-on must be installed on Chrome on both the PCs. They must connect to same MQTT broker which is HiveMQ in this case and should have the same connection name.
Let one of the PC be identified by name PCclient-01. It can register the topic PC01/PC02 with QoS set as 1 on the MQTT broker. The QoS defines the accuracy of data transfer. Let publish a message “Hi! This is client-1” to the MQTT broker.
Fig. 19: Screenshot of PC to PC Message Passing using HiveMQ Broker
Now let other PC be identified by name PCclient-02 and let it subsribe to the topic “PC01/PC02” with QoS level 2. It will receives the message from broker. Whenever the PCclient-01 publishes the message to the MQTT broker, the PCclient-02 receives the message from the MQTT broker automatically.
In the same way, PCclient-02 can register the topic “PC02/PC01” on MQTT broker with QoS level 1. Let it publish the message “Hi! This is PC client-2” on MQTT broker.
Fig. 20: Screenshot of PC to PC Message Passing using HiveMQ Broker
The PCclient-01 has subscribed the same topic with QoS level 1 from MQTT broker and receives a message whenever PCclient-02 publishes the message. In this way, two PC clients communicate with each other through MQTT protocol.
In the next tutorial, mobile to mobile communication using cloudMQTT broker will be demonstrated.
You may also like:
Filed Under: IoT tutorials, Tutorials
Questions related to this article?
👉Ask and discuss on Electro-Tech-Online.com and EDAboard.com forums.
Tell Us What You Think!!
You must be logged in to post a comment.