A「date」between MQTT and CoAP in the EMQ X world

With the progress of the 5G network technology, the IoT world is also developing rapidly. Today, countless IoT devices are shining in all corners of the world.

However, unlike the traditional Internet, end-to-end communication is always a difficult part of the IoT business. The differences in the IoT communication protocols used will cause a huge gap in communication between devices. It is just like people cannot communicate properly with each other if they speak different languages.

WechatIMG14.png As an IoT message broker, EMQ X Broker is on a mission to enable these devices to provide communication. Therefore, we have developed many IoT protocol plugins. Whether you prefer TCP-based long links such as MQTT, UDP-based connectionless such as CoAP, or private protocols, in the world of EMQ X we can help you find a 'soulmate' who can read you.

About the MQTT protocol: https://www.emqx.com/en/mqtt

This article will show you a「date」between MQTT client and CoAP client in the EMQ X world

Step 1: Enable EMQ X Broker, open the CoAP plugin and ensure that the plugin is running.

You can find help on how to install and run EMQ X Broker here.

Open Dashboard, click on Plugin on the left, search for CoAP on the right, and click Start to run the CoAP Plugin (default port 5683). WechatIMG13.png

In the PUB/SUB model of MQTT, a topic is used as a bridge between devices to achieve end-to-end communication. We use the two topics coap_to_mqtt and mqtt_to_coap, as the message topic from CoAP to MQTT, and the message topic from MQTT to CoAP, respectively.

Both MQTT and CoAP support a publish/subscribe mechanism, with MQTT relying on the Topic field in the message, and the CoAP protocol class is based on a REST design. In the EMQ X Broker:

PUT and GET: as Publish and Subscribe.

URI: path map topic, the rule is: topic_name is converted to the path /mqtt/topic_name, i.e. topic plus the /mqtt/ prefix.

URI Query: the path parameter carries information about the client, including the client id, username, and password.

# Example
put "coap://host:port/mqtt/$topic?c=$client&u=$username&p=$password"
get "coap://host:port/mqtt/$topic?c=$client&u=$username&p=$password"

# -m get | put | post ...
# method request method

# coap://host:port
# CoAP protocol path format, host and port, fill in the IP of the EMQ X Broker deployment, and the port of the CoAP plugin (default 5683)

# /mqtt/$topic 
# Refers to the topic of mqtt and needs to be converted, rule:
# in CoAP, topic_name needs to be /mqtt/topic_name

# URI Query
# c :client id
# u :username
# p :password

So far, the preparations have been completed.

Step 2:Invite the first participant, the MQTT client

Connect the MQTT client - MQTT X to your EMQ X Broker and subscribe to the topic coap_to_mqtt for it.

image20210410173501967.png

Step 3:Invite the second participant, the CoAP client

The CoAP client used in this article is libcoap.

# Install libcoap first
# Download using git, or use the download link https://github.com/obgm/libcoap/archive/refs/tags/v4.2.1.zip
git clone https://github.com/obgm/libcoap.git
# If you download using the download link, unzip
# unzip libcoap-4.2.1.zip

# Go to the libcoap file directory
cd libcoap
# Switching to a stable version, the author is using v4.2.1
# If you use the download link in this article to download, there is no need to switch versions.
git checkout v4.2.1
# Install configuration
./autogen
# You may encounter some missing dependencies during the ./autogen process (e.g. autoconf and automake), just follow the instructions to install the dependencies.
./configure --enable-documentation=no --enable-tests=no
# Packaging
make

Step 4:Start communication

After the installation is complete, the CoAP client PUT message to the coap_to_mqtt topic.

# CoAP client sends the message 'hello EMQ X world,  I am CoAP',and topic is 'coap_to_mqtt'
./examples/coap-client -m put -e "hello EMQ X world, I am CoAP" "coap://127.0.0.1/mqtt/coap_to_mqtt?c=coap20211&u=tom&p=secret"
# Replace 127.0.0.1 in the command with the address of your EMQ X Broker deployment

We can see that MQTT X has received a greeting from CoAP.

WechatIMG12.png

Now subscribe to the mqtt_to_coap theme for CoAP terminals.

# CoAP client subscribes to the topic 'mqtt_to_coap'. -s 20 means that the subscription is maintained for 20 seconds 
./examples/coap-client -m get -s 20 "coap://127.0.0.1/mqtt/mqtt_to_coap?c=client1&u=tom&p=secret"

MQTT X sends hello CoAP, I am MQTT, welcome to EMQ X Wrold! to the topic mqtt_to_coap.

WechatIMG11.png

CoAP has also received a response from MQTT.

./examples/coap-client -m get -s 20 "coap://127.0.0.1/mqtt/mqtt_to_coap?c=client1&u=tom&p=secret"
hello CoAP, I am MQTT, welcome to EMQ X Wrold!

So far, we have completed an end-to-end communication process using the EMQ X Broker as the medium, allowing MQTT and CoAP to successfully 'date' in the EMQ X world.

In EMQ X World, not only are MQTT, CoAP, LwM2M, JT808, and much more different IoT protocol plugins which will be supported in the future, but we also provide you with the development template for plugins. We expect that all IoT devices will meet here and collide to create dazzling sparks that will illuminate the world of IoT.

Did you like this blog post? Share it now

Related Posts

Use MQTT with Raspberry Pi

This article introduces how to use Python to write a simple MQTT client on Raspberry Pi to implement the connection, subscribe, messaging, etc.

Saiteng You 2020-08-12
How to use MQTT in Node.js

This article introduces how to use MQTT in the Node.js project, and implement the connection, subscription and messaging, etc of MQTT.

Shifan Yu 2021-09-08
MQTT.js tutorial

This article introduces the use of some common APIs of MQTT.js and some experience in the use process to help users quickly use MQTT in JavaScript projects.

Shifan Yu 2021-09-14