With the progress of 5G network technology, the world of Internet of Things is also developing rapidly. Up to now, countless iot devices are glowing and heating in every corner of the world.

However, different from traditional Internet, end-to-end communication has always been the difficulty of Internet of Things business. The different protocols used for iot communication create a huge gap in communication between these devices. Just like people with different language, can not communicate normally.

EMQ X Broker, as iot messaging middleware, is tasked with enabling these devices to communicate. To this end, we have developed a number of iot protocol plug-ins. Whether you prefer the warmth of a phone call (TCP long links like MQTT), the warmth of a letter (UDP connectionless links like CoAP), or you have your own “code words” (proprietary protocols), in the world of EMQ X, We can all help you find a Soulmate who can read you.

About the MQTT protocol: www.jianshu.com/p/ecde412d2…

About the CoAP protocol: www.jianshu.com/p/7fec0916a…

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

Step 1: Start the EMQ X Broker, open the CoAP plug-in, and ensure that the plug-in is running properly.

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

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

In the PUB/SUB model of MQTT, in order to realize end-to-end communication, topics are needed as Bridges between devices. We use coAP_TO_MQTT and MQTT_TO_COAP as message topics from CoAP to MQTT respectively. And MQTT to CoAP message topic.

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

PUT and GET: Publish and Subscribe.

URI: path-mapped topic, with the rule that the topic name topic_name is converted to the path/MQTT /topic_name, i.e. topic prefixed with/MQTT /.

URI Query: The path parameter carries terminal information, including terminal, user name, and password.

#The sampleput "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, IP for EMQ X Broker deployment, and port for CoAP plug-in (default 5683)

# /mqtt/$topic 
#Refer to MQTT topic, need to be converted, rule:
#Topic name topic_name. / MQTT /topic_name is required in CoAP

# URI Query
#C: terminal
#U: user name
#P: password
Copy the code

At this point, the preparatory work has been completed.

Invite the first attendee, the MQTT client

Connect MQTT X (EMQ’s open source MQTT desktop client) to your EMQ X Broker and subscribe to the topic coAP_TO_MQTT for it.

Invite the second participant, the CoAP client.

The CoAP client used in this article is libcoap.

Install libcoap first
# using the git 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
# unzip libcoap - 2. Zip

Go to the libcoap file directory
cd libcoap
The author is using V4.2.1
# If you use the download link in this article, there is no need to switch versions.Git checkout v4.2.1# Install configuration
./autogen
#./ Autogen may have some dependencies missing (such as Autoconf and Automake), just install them as prompted.
./configure --enable-documentation=no --enable-tests=no
# packaged
make
Copy the code

Step 4: Start the communication

When the installation is complete, the CoAP terminal puts messages to the COAP_TO_MQTT topic.

#The CoAP terminal sends the message Hello EMQ X world, I am CoAP, topic is COAP_TO_MQTT. / examples/coap - the 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
Copy the code

We can see MQTT X receiving greetings from CoAP.

Now subscribe to the MQTT_TO_COAP topic for the CoAP terminal.

#The CoAP terminal subscribes to the MQTT_TO_COAP topic, and -s 20 indicates that the subscription lasts for 20 seconds. / examples/coap - the client - m get -s 20 "coap: / / 127.0.0.1 MQTT/mqtt_to_coap? c=client1&u=tom&p=secret"Copy the code

MQTT X send hello coap, I am MQTT welcome to EMQ X Wrold! To the mqTT_TO_COAP topic.

CoAP also received a response from MQTT.

. / examples/coap - the 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 WorldCopy the code

At this point, we have completed an end-to-end communication process mediated by an EMQ X Broker, allowing MQTT and CoAP to successfully “date” in the EMQ X world.

At EMQ X World, there are not only MQTT, CoAP, LWM2M, JT808 and many more different iot protocol plug-ins that will be supported in the future, but we also provide you with development templates for plug-ins. We expect that all the devices of the Internet of things can meet here and spark dazzling collisions to illuminate the world of the Internet of things.

In this paper, the author

EMQ X R&d Engineer, EMQ X Broker & Enterprise

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

Copyright: EMQ

Original link: www.emqx.cn/blog/url-mq…