In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains how to connect MQTT and CoAP in EMQ X. interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to connect MQTT and CoAP in EMQ X.
Step 1: start EMQ X Broker, open the CoAP plug-in, and make sure the plug-in is running.
You can find help here on how to install and start EMQ X Broker.
Open 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 MQTT's PUB/SUB model, in order to achieve end-to-end communication, devices need to use topic as a bridge. We use coap_to_mqtt and mqtt_to_coap topic as CoAP-to-MQTT message topic and MQTT-to-CoAP message topic respectively.
Both MQTT and CoAP support publish / subscribe mechanism. MQTT relies on the Topic field in the message, while the 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: the topic name topic_name is converted into path / mqtt/topic_name, that is, topic is prefixed with / mqtt/.
URI Query: the path parameter carries 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, enter the IP deployed by EMQ X Broker, and the port of the CoAP plug-in (default 5683) # / mqtt/$topic # refers to the topic of mqtt, which needs to be converted Rule: # topic name topic_name, need to use / mqtt/topic_name# URI Query# c: terminal # u: user name # p: password in CoAP
At this point, the preparatory work has been completed.
Step 2: invite the first participant, MQTT client
Connect MQTT X (EMQ's open source MQTT desktop client) to your EMQ X Broker and subscribe to the theme coap_to_mqtt.
Step 3: invite the second participant, the CoAP client.
The CoAP client used in this article is libcoap.
# install libcoap# first and use git to download, or use the download link https://github.com/obgm/libcoap/archive/refs/tags/v4.2.1.zipgit clone https://github.com/obgm/libcoap.git# if you use the download link to download Unzip unzip # unzip libcoap-4.2.1.zip# enter the libcoap file directory cd libcoap# to switch to the stable version the author uses v4.2.download if you use the download link in this article, you do not need to switch the version. Git checkout v4.2.The process of installing configuration. / autogen#. / autogen may encounter some cases of missing dependencies (such as autoconf and automake). Follow the prompts to install the corresponding dependencies. . / configure-- enable-documentation=no-- enable-tests=no# package makeStep 4: start communication
After the installation is complete, the CoAP terminal PUT messages to the coap_to_mqtt topic.
# CoAP terminal sends message hello EMQ X world, I am coap,topic for 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" # command 127.0.0.1 replace with your EMQ X Broker deployment address
We can see that MQTT X received a greeting from CoAP.
Now subscribe to the mqtt_to_coap topic for the CoAP terminal.
# CoAP terminal subscribes to mqtt_to_coap topics.-s 20 means 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 coap, i am mqtt welcome to EMQ X Wrold! To the mqtt_to_coap 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. I believe you have a better understanding of "how to connect MQTT and CoAP in EMQ X". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.