A "date" between MQTT and CoAP in EMQ X world

Posted by drunkenfury on Sat, 05 Mar 2022 03:42:41 +0100

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

However, unlike the traditional Internet, end-to-end communication has always been a difficulty in the Internet of things business. Different Internet of things communication protocols are used, which makes a huge gap in the communication between these devices. It's just like that people can't communicate normally due to different languages.

As the Internet of things message middleware, EMQ X Broker shoulders the mission of enabling these devices to provide communication. For example, you have developed a lot of TCP based protocols, such as a set of private Internet of things (or a set of EMQ based on your own protocol), whether it is based on MQTT, or a set of private Internet of things, We can all help you find and understand your "soulmate".

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

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

This article will show you an "appointment" between MQTT client and CoAP client at EMQ X World.

Step 1: start EMQ X Broker, open the CoAP plug-in, and ensure the running status of the plug-in.

About how to install and start EMQ X Broker, you can here Find help.

Open the Dashboard, click the plug-in on the left, search for CoAP on the right, click start, and run the CoAP plug-in (default port 5683).

In the PUB/SUB model of MQTT, in order to realize end-to-end communication, we need to use topic as a bridge between devices. We use coap_to_mqtt and MQTT_ to_ The two topics of CoAP are the message topic from CoAP to MQTT and the message topic from MQTT to CoAP.

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

PUT and GET: as Publish and Subscribe.

URI: path mapping topic. The rule is: topic name topic_ Convert name to path / mqtt/topic_name, i.e. topic, prefixed with / mqtt /.

URI Query: the path parameter carries the terminal information, including terminal, user name 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 deployed by EMQ X Broker, and the port of CoAP plug-in (5683 by default)

# /mqtt/$topic 
# Refers to the topic of mqtt, which needs to be converted. The rules are as follows:
# Topic name topic_name, you need to use / mqtt / topic in CoAP_ name

# URI Query
# c: terminal
# u: user name
# p: password

So far, the preparations have been completed.

Step 2: invite the first participant, MQTT client

Connect MQTT X (the open source MQTT desktop client under EMQ) to your EMQ X Broker and subscribe to the topic soap for it_ to_ MQTT.

Step 3: invite the second participant, the CoAP client.

The CoAP client used in this article is libcoap .

# Install libcoap first
# 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 to download, unzip it
# unzip libcoap-4.2.1.zip

# Enter the libcoap file directory
cd libcoap
# Switch to the stable version. The author uses v4 two point one
# If you use the download link in this article to download, you don't need to switch versions.
git checkout v4.2.1
# Installation configuration
./autogen
# Some dependencies may be missing during. / autogen (such as autoconf and automake). Follow the prompts to install the corresponding dependencies.
./configure --enable-documentation=no --enable-tests=no
# pack
make

Step 4: start communication

After the installation is completed, the PUT message of the CoAP terminal is sent to the coap_to_mqtt theme.

# The CoAP terminal sends the message Hello EMQ x world, I am CoAP, and the 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 your EMQ X Broker deployment address

We can see that MQTT X received greetings from CoAP.

Now subscribe to mqtt for CoAP terminals_ to_ The soap topic.

# CoAP terminal subscription mqtt_ to_ For the soap topic, - s 20 means that the 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 soap, I am mqtt welcome to EMQ x wrap! To mqtt_ to_ The soap topic.

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

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

In EMQ X World, there are not only MQTT, CoAP, LWM2M, JT808 and more different Internet of things protocol plug-ins that will be supported in the future, but also we provide you with Plug in development template . We hope that here, all Internet of things devices can meet and collide with dazzling sparks to illuminate the world of Internet of things.

Author

Huang Di, EMQ X R & D Engineer, EMQ X broker & Enterprise

Zhou Zibo, EMQ X open source project manager, is mainly responsible for project management and community maintenance. He has worked deeply in the field of IoT for many years and has very rich industry experience

Copyright notice: This article is EMQ Original, reprint, please indicate the source.

Original link: https://www.emqx.cn/blog/url-mqtt-and-coap

Topics: git github IoT