MQTT and CoAP Date in EMQ X World

With the advancement of 5G network technology, the Internet of Things (IoT) is rapidly developing. Today, countless IoT devices are shining in every corner of the world.

However, unlike traditional internet, end-to-end communication has always been a challenge for IoT businesses. The different IoT communication protocols used create a huge gap in communication between these devices, much like people speaking different languages and being unable to communicate effectively.

MQTT and CoAP Date in EMQ X World

As an IoT message middleware, EMQ X Broker is tasked with facilitating communication between these devices. To this end, we have developed many IoT protocol plugins. Whether you prefer the warmth of a long phone call (based on TCP long connections, such as MQTT), or the intimacy of a letter (based on UDP connectionless, such as CoAP), or you have your own “secret language” (private protocol), in the world of EMQ X, we can help you find your “soulmate” who can understand you.

About MQTT Protocol: https://www.jianshu.com/p/ecde412d2eeb

About CoAP Protocol: https://www.jianshu.com/p/7fec0916a0d3

This article will show you a “date” between the MQTT client and the CoAP client in EMQ X World.

Step 1:Start EMQ X Broker, enable the CoAP plugin, and ensure the plugin is running

You can find help on how to install and start EMQ X Broker at https://docs.emqx.cn/broker/v4.3/getting-started/install.html.
Open the Dashboard, click on the plugins on the left, search for CoAP on the right, click to start, and run the CoAP plugin (default port 5683).

MQTT and CoAP Date in EMQ X World

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

Both MQTT and CoAP support the publish/subscribe mechanism. MQTT relies on the Topic field in the message, while CoAP is based on REST design. In EMQ X Broker:

  • PUT and GET: serve as Publish and Subscribe.

  • URI: path maps to topic, with the rule: topic name topic_name is converted to path /mqtt/topic_name, that is, topic plus /mqtt/ prefix.

  • URI Query: path parameters carry terminal information, including terminal, 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 type  # coap://host:port # CoAP protocol path format, host and port, fill in the IP where EMQ X Broker is deployed, and the port of the CoAP plugin (default 5683) 
# /mqtt/$topic  # refers to mqtt's topic, needs conversion, rule: # topic name topic_name, in CoAP needs to use /mqtt/topic_name 
# URI Query # c : terminal # u : username # p : password
At this point, the preparations are complete.

Step 2:Invite the first participant, the MQTT client

Connect MQTT X (an open-source MQTT desktop client under EMQ) to your EMQ X Broker and subscribe to the topic coap_to_mqtt.

MQTT and CoAP Date in EMQ X World

► Step 3:Invite the second participant, the CoAP client

The CoAP client used in this article is libcoap (https://github.com/obgm/libcoap) .
# First install libcoap # Use git to download, 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 use the download link, unzip # unzip libcoap-4.2.1.zip # Enter the libcoap directory cd libcoap # Switch to stable version, the author used v4.2.1 # If you use the download link in this article, no need to switch versions. git checkout v4.2.1 # Configuration installation ./autogen # During the ./autogen process, you may encounter some missing dependencies (such as autoconf and automake), install the corresponding dependencies as prompted. ./configure --enable-documentation=no --enable-tests=no # Package make
► Step 4:Start communication

After installation, the CoAP terminal PUTs a message to the coap_to_mqtt topic.

# CoAP terminal sends message hello EMQ X world,  i am coap, 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 with your EMQ X Broker deployment address

We can see that MQTT X received greetings from CoAP.

MQTT and CoAP Date in EMQ X World

Now subscribe the CoAP terminal to the mqtt_to_coap topic.
# CoAP terminal subscribes to mqtt_to_coap topic, -s 20 indicates subscription lasts 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 World! to the mqtt_to_coap topic.

MQTT and CoAP Date in EMQ X World

CoAP 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 World

Thus, we have completed an end-to-end communication process using EMQ X Broker as a medium, successfully allowing MQTT and CoAP to “date” in the world of EMQ X.

In EMQ X World, there are not only MQTT, CoAP, LWM2M, JT808, and many more different IoT protocol plugins that will be supported in the future, but we also provide you with plugin development templates(https://github.com/emqx/emqx-plugin-template).

We look forward to a place where all IoT devices can meet and spark brilliant ideas, illuminating the world of IoT.

Article Author

Huang Di

EMQ X Development Engineer, EMQ X Broker & Enterprise

Zhou Zibo

EMQ X Open Source Project Manager, mainly responsible for project management and community maintenance, has many years of rich industry experience in the IoT field.

MQTT and CoAP Date in EMQ X World

Click “Read Original” to learn more.

↓↓↓

Leave a Comment