Connect to MQTT server

A server provided by the UP master of a site

MQTT

​ After clicking on the link, you need to log in to the account first. The account can use the relevant information of a certain station to log in.
​ After logging in, click Tools -> IoT Platform, you can see the address, port number and supported protocols of the MQTT server, each account has a free device by default, first create a project -> add device (enter password) and the project to which it belongs), after entering the project, you can see information such as the ID and password of the device, but the subject prefix is ​​required for subscription and push information.Since the server of the UP master is private, a device ID and password are required to connect to the server, after testing, the server is not very stable and will be disconnected from time to time, but the impact is not large, you can set the reconnection in the program.
​  The mqtt.js file required by the applet is also provided at this address (the useful one is the mqtt.min.js file), which is available for download on the website title Development -> Internet of Things.

Two servers provided by EMQX official website

MQTT1 and MQTT2

​ broker-cn.emqx.io is the server address; broker.emqx.io is also the server address, the two addresses are different
; The address and port number of the free server on the official website, but the server is public, so the data has no privacy protection.Because it is public, the configuration does not require a username and password, but the server is stable and basically does not disconnect. This official website also provides an MQTT client test tool, in the title learning -> MQTT Websocket Toolkit, as shown below:
insert image description here

Server provided by Tencent Cloud

MQTT

​​  This server of Tencent Cloud is private, so a device ID and password are required to connect to the server, after testing the server is very stable.
​​ Go to Tencent Cloud official website, in the title above Product->Internet of Things->Internet of Things Communication->Use Now, then log in to the account, then Product List->Create New Product->Select the product type, fill in the product name, and select Authentication method (key authentication), select the data type (custom), and thus create a product, as shown in the following figure:
insert image description here
​​ After creating the product, click Manage to enter the product settings,There is a product ID in it that we need to save, as shown in the following figure:
insert image description here
​​ Next, click the device list->Add a new device->Enter the device name->Save, as shown below:
insert image description here
​​ Then, click the management operation behind the device, enter the page and slide to the bottomSave client id, MQTT username and password, as for the topic of the device, after entering the device, you can see a topic list, which contains the specified topic prefix and topic operation permissions, as shown in the following figure:
insert image description here
​​ Repeat the above operation to create a device again for a customer For data transmission from one client to another, finally, a rule engine needs to be set up to associate the topics of the two devices, so that one client A subscribes to topic 1, another client B sends topic 2, and client B topic The data of 2 is forwarded to topic 1 through the rule engine, so that client A receives the data on topic 1. A device on Tencent Cloud only allows one client to connect at the same time, so only two devices can be created. Then use the rule engine transfer to realize the data transmission.
​​In the rule engine, create a new rule, name the rule whatever you want, then click the management operation behind the rule, then edit the filtered data, enter * in the field bar (meaning no filtering), and in the subject bar, fill in the data source subject (that is, the topic that needs to be published), in the condition column, set it to empty (indicates that the rule engine is triggered under any condition). Finally, add a behavior operation, the behavior type is data forwarding to another topic, and the specific topic to be forwarded to is selected below. At this point, the rule engine has been set up, and the rule state can be turned on for data forwarding (Rules can only be written in the disabled state). Tencent Cloud's free server address and port number are divided into multiple servers with different protocols. The general client is an ordinary TCP connection, and the connected server address and port number are as follows
insert image description here
insert image description hereinsert image description here
:  It can be seen that the common tcp server address is: product ID+.iotcloud.tencentdevices.com, and the port number is 8883 or 1883.
​​ For the applet, it is based on the wss protocol, so the free server on Tencent Cloud has another server address corresponding to this protocol, as shown below
insert image description here
:  It can be seen that the address of the wss server is: product ID+.ap-guangzhou.iothub.tencentdevices.com, and the port number is 80.
​​ So, if the client is a normal tcp connection, then use the first server address and port number, if the client is a small program (wss), then use the second server address and port number, although the two The server address and port number connected by the client are different, but the final data will be handed over to the same server for processing, so the two clients can perform data transmission.

Server provided by Baidu Cloud

MQTT
​​  This server of Baidu Cloud is private, so connecting to the server requires a device ID and password, after testing the server is very stable.
​​ Go to Baidu Smart Cloud Official Website->Products->Internet of Things->Internet of Things Core Suite:
insert image description here
​​ Then you can use it immediately, and there is a help document for reference:
insert image description here
​​ After entering, you need to log in to your account->Instance List -> Create IoT Core (the name is optional)
insert image description here
​​ Then click the name of the created IoT Core to enter the specific device management,In the upper right corner of this page, the address of the server is displayed, which needs to be saved.

insert image description here
​​ Then add a template to the template in the device management (equivalent to a product, the name is optional), and then add a new device (the same name is optional, the authentication method is key authentication, and the template selects the template name created in the previous step)
insert image description here
​​ After adding a device, click the device name to enter the device information interface, where you can viewIoTCoreId, KEY and secret key of the device, this is what we need to save, because later we need to use an information generator to generate the corresponding device ID and password from these three data.
insert image description here
​​ Then repeat the above operation, and then create a new device, == because Baidu Cloud can only connect one client to one device, in order to realize the data transmission between the client and the client, you need to create two devices. ==After the two devices are created, we need to use the device KEY and key obtained above to generate the corresponding device ID and password. The operation method is as follows.
​ Open the help document indicated by the red arrow above, and do the following:
insert image description here
​​ There is an MQTT connection information generator, enter the previous IoT Core ID, device KEY and key, as follows:
insert image description here
​​ At this time, it will be below Generate server address, device ID and password, (client ID is basically useless). In this way, the two clients can connect to the server through these data. Next, you need to set the theme. After entering IoT Core, click the template name in the template to set the theme. It supports adding themes. The theme prefix is ​​as follows: $iot/ {deviceName}/user/xxx (the deviceName in it is the name of the new device we added earlier, and xxx is our custom theme). They are $iot/test1/user/xxx, $iot/test2/user/xxx, so how to associate these two topics so that one client publishes the topic and the other client subscribes to the topic, so that data can be transmitted? ? Then you need to use a rule engine. Also after entering IoT Core, there is a rule engine in it, then create a rule (name it casually), then edit the rule, and enter the data source (publish topic) and data destination (subscription) in it. theme), and finally save it.In this way, a client A publishes a topic A and forwards the data in the topic A to the client B subscribed to the topic B through the rule engine, so as to achieve the purpose of data transmission.
A MQTT test software download address is attached below: mqttbox test software

Guess you like

Origin blog.csdn.net/weixin_44069765/article/details/124108572