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.io/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.


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
# 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

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://"
# Replace in the command with the address of your EMQ X Broker deployment

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


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://"

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


CoAP has also received a response from MQTT.

./examples/coap-client -m get -s 20 "coap://"
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.

Zibo Zhou

Zibo Zhou

EMQ X R&D engineer, responsible for the development and maintenance of EMQ X open source projects, as well as community construction and support.

DDD Huang

DDD Huang

EMQ X Engineer, Broker & Enterprise

The streaming database built for IoT data storage and real-time processing.

Fully managed MQTT 5.0 IoT cloud, start a 180-day free trial.

Related Links

Retained message and message expiration interval of EMQ X MQTT 5.0 broker

The message retention function of [EMQ X MQTT Broker](https://emqx.io) is implemented by the `emqx_retainer` plugin, which is enabled by default. By modifying the configuration of the` emqx_retainer` plugin, you can adjust the EMQ X Broker's retention message Location, restrict the number of retained messages and maximum payload length, and adjust the expiration time of retained messages.

Remote control LED with ESP8266 and MQTT

In this project, we will implement remote control LED lights via NodeMCU(ESP8266) and MQTT broker, and use the Arduino IDE to program NodeMCU ESP8266.

Android MQTT TLS/SSL authentication

This article mainly introduces how to perform TLS/SSL one-way and two-way authentication via Android and MQTT.